After doing Ray a great favor to collect sticks for Ray, Poor Neal becomes very hungry. In return for Neal‘s help, Ray makes a great dinner for Neal. When it is time for dinner, Ray arranges all the dishes he makes in a single line (actually this line is very long ... <tex2html_verbatim_mark>, the dishes are represented by 1, 2, 3 ... <tex2html_verbatim_mark>). ``You make me work hard and don‘t pay me! You refuse to teach me Latin Dance! Now it is time for you to serve me", Neal says to himself.
Every dish has its own value represented by an integer whose absolute value is less than 1,000,000,000. Before having dinner, Neal is wondering about the total value of the dishes he will eat. So he raises many questions about the values of dishes he would have.
For each question Neal asks, he will first write down an interval [a, b] <tex2html_verbatim_mark>(inclusive) to represent all the dishes a, a + 1,..., b <tex2html_verbatim_mark>, where a <tex2html_verbatim_mark>and b <tex2html_verbatim_mark>are positive integers, and then asks Ray which sequence of consecutive dishes in the interval has the most total value. Now Ray needs your help.
The input file contains multiple test cases. For each test case, there are two integers n <tex2html_verbatim_mark>and m <tex2html_verbatim_mark>in the first line (n, m < 500000) <tex2html_verbatim_mark>. n <tex2html_verbatim_mark>is the number of dishes and m <tex2html_verbatim_mark>is the number of questions Neal asks.
Then n <tex2html_verbatim_mark>numbers come in the second line, which are the values of the dishes from left to right. Next m <tex2html_verbatim_mark>lines are the questions and each line contains two numbers a <tex2html_verbatim_mark>, b <tex2html_verbatim_mark>as described above. Proceed to the end of the input file.
For each test case, output m <tex2html_verbatim_mark>lines. Each line contains two numbers, indicating the beginning position and end position of the sequence. If there are multiple solutions, output the one with the smallest beginning position. If there are still multiple solutions then, just output the one with the smallest end position. Please output the result as in the Sample Output.
3 1 1 2 3 1 1
Case 1: 1 1
线段树单点更新,虽然思路很简单,但我写的丑爆了!!
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <assert.h> 7 #include <queue> 8 9 using namespace std; 10 11 #define read() freopen("sw.in", "r", stdin) 12 #define ls l, m, rt << 1 13 #define rs m + 1, r, rt << 1 | 1 14 15 const int MAX = 500007; 16 const int INF = 1e9 + 7; 17 typedef long long ll; 18 int N, M; 19 ll max_sub[4 * MAX], max_suffix[4 * MAX], max_prefix[4 * MAX]; 20 int id_suffix[4 * MAX], id_prefix[4 * MAX]; 21 int s[4 * MAX], e[4 * MAX]; 22 ll sum[4 * MAX]; 23 struct node { 24 ll maxsub, maxprefix, maxsuffix , sum; 25 int idprefix, idsuffix, ids, ide; 26 }; 27 queue <node> q; 28 29 void push_up(int rt) { 30 sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; 31 32 ll t[3], id = 0; 33 t[0] = max_sub[rt << 1]; 34 t[1] = max_suffix[rt << 1] + max_prefix[rt << 1 | 1]; 35 t[2] = max_sub[rt << 1 | 1]; 36 for (int i = 0; i <= 2; ++i) { 37 //printf("%d\n", t[i]); 38 if (t[id] < t[i]) { 39 id = i; 40 41 } 42 } 43 44 //printf("id = %d\n", id); 45 max_sub[rt] = t[id]; 46 if (id == 0) { 47 s[rt] = s[rt << 1]; 48 e[rt] = e[rt << 1]; 49 } else if (id == 1) { 50 s[rt] = id_suffix[rt << 1]; 51 e[rt] = id_prefix[rt << 1 | 1]; 52 } else { 53 s[rt] = s[rt << 1 | 1]; 54 e[rt] = e[rt << 1 | 1]; 55 } 56 57 if (max_prefix[rt << 1] < sum[rt << 1] + max_prefix[rt << 1 | 1]) { 58 max_prefix[rt] = sum[rt << 1] + max_prefix[rt << 1 | 1]; 59 id_prefix[rt] = id_prefix[rt << 1 | 1]; 60 } else { 61 max_prefix[rt] = max_prefix[rt << 1] ; 62 id_prefix[rt] = id_prefix[rt << 1]; 63 } 64 65 if (max_suffix[rt << 1 | 1] <= sum[rt << 1 | 1] + max_suffix[rt << 1]) { 66 max_suffix[rt] = sum[rt << 1 | 1] + max_suffix[rt << 1]; 67 id_suffix[rt] = id_suffix[rt << 1]; 68 } else { 69 max_suffix[rt] = max_suffix[rt << 1 | 1]; 70 id_suffix[rt] = id_suffix[rt << 1 | 1]; 71 } 72 73 74 75 } 76 77 void build(int l, int r, int rt) { 78 if (l == r) { 79 cin >> sum[rt]; 80 max_sub[rt] = max_suffix[rt] = max_prefix[rt] = sum[rt]; 81 id_suffix[rt] = id_prefix[rt] = s[rt] = e[rt] = l; 82 return; 83 } 84 85 int m = (l + r) >> 1; 86 build(ls); 87 build(rs); 88 89 push_up(rt); 90 91 } 92 93 void query(int ql, int qr, int l, int r, int rt) { 94 if (ql <= l && r <= qr) { 95 if (q.size() == 1) { 96 node x = q.front(), a; 97 ll t[3]; 98 t[0] = x.maxsub; 99 t[1] = x.maxsuffix + max_prefix[rt]; 100 t[2] = max_sub[rt]; 101 int id = 0; 102 for (int i = 0; i <= 2; ++i) { 103 if (t[id] < t[i]) { 104 id = i; 105 } 106 } 107 108 a.maxsub = t[id]; 109 if (id == 0) { 110 a.ids = x.ids; 111 a.ide = x.ide; 112 } else if (id == 1) { 113 a.ids = x.idsuffix; 114 a.ide = id_prefix[rt]; 115 } else { 116 a.ids = s[rt]; 117 a.ide = e[rt]; 118 } 119 120 121 if (max_prefix[rt] <= x.sum + max_prefix[rt]) { 122 a.maxprefix = x.sum + max_prefix[rt]; 123 a.idprefix = id_prefix[rt]; 124 } else { 125 a.maxprefix = x.maxprefix; 126 a.idprefix = x.idprefix; 127 } 128 129 if (max_suffix[rt] <= sum[rt] + x.maxsuffix) { 130 a.maxsuffix = sum[rt] + x.maxsuffix; 131 a.idsuffix = x.idsuffix; 132 } else { 133 a.maxsuffix = max_suffix[rt]; 134 a.idsuffix = id_suffix[rt]; 135 } 136 137 q.pop(); 138 q.push(a); 139 } else { 140 node a; 141 a.ids = s[rt]; a.ide = e[rt]; 142 a.idprefix = id_prefix[rt]; a.idsuffix = id_suffix[rt]; 143 a.maxprefix = max_prefix[rt]; a.maxsub = max_sub[rt]; 144 a.maxsuffix = max_suffix[rt]; a.sum = sum[rt]; 145 q.push(a); 146 } 147 148 149 } else { 150 int m = (l + r) >> 1; 151 if (ql <= m) query(ql ,qr, ls); 152 if (qr > m) query(ql, qr, rs); 153 } 154 } 155 156 int main() 157 { 158 //read(); 159 int ca = 1; 160 while (~scanf("%d%d", &N, &M)) { 161 memset(sum, 0, sizeof(sum)); 162 build(1, N, 1); 163 printf("Case %d:\n", ca++); 164 for (int i = 1; i <= M; ++i) { 165 int l, r; 166 scanf("%d%d", &l, &r); 167 query(l, r, 1, N, 1); 168 node x = q.front(); q.pop(); 169 printf("%d %d\n", x.ids, x.ide); 170 171 172 } 173 } 174 175 //cout << "Hello world!" << endl; 176 return 0; 177 }
原文:http://www.cnblogs.com/hyxsolitude/p/3856521.html