1 经历无数次RE 最后来了一个TLE 先放着以后在做 o(︶︿︶)o 唉 2 #include<stdio.h> 3 #include<algorithm> 4 using namespace std; 5 typedef long long ll; 6 const int maxn = 500100; 7 struct node1 { 8 int max_sub_x, max_sub_y; 9 ll max_sub, max_suffix, max_prefix; 10 int max_suffix_x; 11 int max_prefix_x; 12 }N[4 * maxn]; 13 int val[maxn], sum[maxn]; 14 15 void build_prefix(int o, int l, int r) { 16 int m = (l + r) / 2; 17 if(l == r) { 18 N[o].max_prefix = val[l]; 19 N[o].max_prefix_x = l; 20 } 21 else { 22 build_prefix(o * 2, l, m); 23 build_prefix(o * 2 + 1, m + 1, r); 24 int max1 = sum[m] - sum[l - 1] + N[o * 2 + 1].max_prefix; 25 if(N[o * 2].max_prefix >= max1) { 26 N[o].max_prefix = N[o * 2].max_prefix; 27 N[o].max_prefix_x = N[o * 2].max_prefix_x; 28 } 29 else { 30 N[o].max_prefix = max1; 31 N[o].max_prefix_x = N[o * 2 + 1].max_prefix_x; 32 } 33 } 34 } 35 36 void build_suffix(int o, int l, int r) { 37 int m = (l + r) / 2; 38 if(l == r) { 39 N[o].max_suffix = val[l]; 40 N[o].max_suffix_x = l; 41 } 42 else { 43 build_suffix(o * 2, l, m); 44 build_suffix(o * 2 + 1, m + 1, r); 45 int max1 = sum[r] - sum[m] + N[o * 2].max_suffix; 46 if(max1 >= N[o * 2 + 1].max_suffix) { 47 N[o].max_suffix = max1; 48 N[o].max_suffix_x = N[o * 2].max_suffix_x; 49 } 50 else { 51 N[o].max_suffix = N[o * 2 + 1].max_suffix; 52 N[o].max_suffix_x = N[o * 2 + 1].max_suffix_x; 53 } 54 } 55 } 56 57 struct node2 { 58 ll val; 59 int x, y; 60 }c[10]; 61 62 bool cmp(node2 A, node2 B) { 63 if(A.val != B.val) return A.val > B.val; 64 else if(A.x != B.x) return A.x < B.x; 65 else return A.y < B.y; 66 } 67 68 void build_max_sub(int o, int l, int r) { 69 int m = (l + r) / 2; 70 if(l == r) { 71 N[o].max_sub = val[l]; 72 N[o].max_sub_x = N[o].max_sub_y = l; 73 } 74 else { 75 build_max_sub(o * 2, l, m); 76 build_max_sub(o * 2 + 1, m + 1, r); 77 c[0].val = N[o * 2].max_sub; 78 c[0].x = N[o * 2].max_sub_x; 79 c[0].y = N[o * 2].max_sub_y; 80 c[1].val = N[o * 2 + 1].max_sub; 81 c[1].x = N[o * 2 + 1].max_sub_x; 82 c[1].y = N[o * 2 + 1].max_sub_y; 83 c[2].val = N[o * 2].max_suffix + N[o * 2 + 1].max_prefix; 84 c[2].x = N[o * 2].max_suffix_x; 85 c[2].y = N[o * 2 + 1].max_prefix_x; 86 sort(c, c + 3, cmp); 87 N[o].max_sub = c[0].val; 88 N[o].max_sub_x = c[0].x; 89 N[o].max_sub_y = c[0].y; 90 } 91 } 92 93 node2 query_prefix(int o, int l, int r, int ql, int qr) { 94 int m = (l + r) / 2; 95 if(N[o].max_prefix_x <= ql) { 96 c[0].x = l; 97 c[0].y = N[o].max_prefix_x; 98 c[0].val = N[o].max_prefix; 99 } 100 else if(qr <= m) { 101 c[0] = query_prefix(o * 2, l, m, ql, qr); 102 } 103 else { 104 c[0] = query_prefix(o * 2, l, m, ql, qr); 105 c[2] = query_prefix(o * 2 + 1, m + 1, r, m + 1, qr); 106 c[1].x = l; 107 c[1].val = c[2].val + sum[m] - sum[l - 1]; 108 c[1].y = c[2].y; 109 sort(c, c + 2, cmp); 110 } 111 return c[0]; 112 } 113 114 node2 query_suffix(int o, int l, int r, int ql, int qr) { 115 int m = (l + r) / 2; 116 if(N[o].max_suffix_x >= ql) { 117 c[0].x = N[o].max_suffix_x; 118 c[0].y = r; 119 c[0].val = N[o].max_suffix; 120 } 121 else if(ql > m) { 122 c[0] = query_suffix(o * 2 + 1, m + 1, r, ql, qr); 123 } 124 else { 125 c[0] = query_suffix(o * 2 + 1, m + 1, r, ql, qr); 126 c[2] = query_suffix(o * 2, l, m, ql, m); 127 c[1].x = c[2].x; 128 c[1].val = c[2].val + sum[r] - sum[m]; 129 c[1].y = r; 130 sort(c, c + 2, cmp); 131 } 132 return c[0]; 133 } 134 135 node2 query_sub(int o, int l, int r, int ql, int qr) { 136 int m = (l + r) / 2; 137 node2 a, b; 138 if(ql <= l && qr >= r) { 139 c[0].val = N[o].max_sub; 140 c[0].x = N[o].max_sub_x; 141 c[0].y = N[o].max_sub_y; 142 return c[0]; 143 } 144 if(qr <= m) return query_sub(o * 2, l, m, ql, qr); 145 else if(ql > m) return query_sub(o * 2 + 1, m + 1, r, ql, qr); 146 else { 147 c[0] = query_sub(o * 2, l, m, ql, m); 148 c[1] = query_sub(o * 2 + 1, m + 1, r, m + 1, qr); 149 a = query_suffix(o * 2, l, m, ql, m); 150 b = query_prefix(o * 2 + 1, m + 1, r, m + 1, qr); 151 c[2].val = a.val + b.val; 152 c[2].x = a.x; 153 c[2].y = b.y; 154 sort(c, c + 3, cmp); 155 return c[0]; 156 } 157 } 158 159 int main() { 160 int n, m, kase = 0, ql, qr, num = 0; 161 while(scanf("%d%d", &n, &m) != EOF) { 162 sum[0] = 1; 163 for(int i = 1; i <= n; ++i) { 164 scanf("%d", &val[i]); 165 sum[i] = sum[i - 1] + val[i]; 166 } 167 build_prefix(1, 1, n); 168 build_suffix(1, 1 ,n); 169 build_max_sub(1, 1, n); 170 printf("Case %d:\n", ++kase); 171 while(m--) { 172 scanf("%d%d", &ql, &qr); 173 node2 ans = query_sub(1, 1, n, ql, qr); 174 printf("%lld %lld\n", ans.x, ans.y); 175 } 176 } 177 return 0; 178 }