GSS5 - Can you answer these queries V
You are given a sequence A[1], A[2], ..., A[N] . ( |A[i]| <= 10000 , 1 <= N <= 10000 ). A query is defined as follows: Query(x1,y1,x2,y2) = Max { A[i]+A[i+1]+...+A[j] ; x1 <= i <= y1 , x2 <= j <= y2 and x1 <= x2 , y1 <= y2 }. Given M queries (1 <= M <= 10000), your program must output the results of these queries.
Input
The first line of the input consist of the number of tests cases <= 5. Each case consist of the integer N and the sequence A. Then the integer M. M lines follow, contains 4 numbers x1, y1, x2 y2.
Output
Your program should output the results of the M queries for each test case, one query per line.
Example
Input: 2 6 3 -2 1 -4 5 2 2 1 1 2 3 1 3 2 5 1 1 1 1 1 1 1 Output: 2 3 1
【题解】
维护前缀/后缀最大,区间最大,区间总和
区间[x1, y1] [x2, y2]分情况查询
两个区间没有交(y1 < x2), 则答案为[x1, y1].right + [x2, y2].left + (y1 +1 <= x2 - 1 ? [y1 + 1, x2 - 1].sum : 0)
两个区间有交(y1 >= x2), 则分种情况讨论:
左端点 右端点
1、左区间非公共部分 公共部分
2、 公共部分 公共部分
3、左区间非公共部分 右区间非公共部分
4、 公共部分 右区间非公共部分
其实这四种情况可以并为三种:
公共部分最大连续区间
左区间右边最大连续后缀, 右区间非公共部分最大连续前缀
左区间非公共部分最大连续后缀, 右区间最大连续前缀
考虑一下边界,看好怎么+1 -1 合适即可
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #define max(a, b) ((a) > (b) ? (a) : (b)) 6 #define min(a, b) ((a) < (b) ? (a) : (b)) 7 //改longlong 8 inline void read(long long &x) 9 { 10 x = 0;char ch = getchar(), c = ch; 11 while(ch < '0' || ch > '9')c = ch, ch = getchar(); 12 while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar(); 13 if(c == '-')x = -x; 14 } 15 16 inline void swap(long long &a, long long &b) 17 { 18 long long tmp = a;a = b;b = tmp; 19 } 20 21 const long long MAXN = 100000 + 10; 22 const long long INF = 0x3f3f3f3f3f3f3f3f; 23 24 long long n,m,num[MAXN],left[MAXN],right[MAXN],ma[MAXN],sum[MAXN]; 25 26 void build(long long o = 1, long long l = 1, long long r = n) 27 { 28 if(l == r) 29 { 30 left[o] = right[o] = ma[o] = sum[o] = num[l]; 31 return; 32 } 33 long long mid = (l + r) >> 1; 34 build(o << 1, l, mid); 35 build(o << 1 | 1, mid + 1, r); 36 37 left[o] = max(left[o << 1], sum[o << 1] + left[o << 1 | 1]); 38 right[o] = max(right[o << 1 | 1], sum[o << 1 | 1] + right[o <<1]); 39 ma[o] = max(left[o], max(right[o], left[o << 1 | 1] + right[o << 1])); 40 ma[o] = max(ma[o], max(ma[o << 1], ma[o << 1 | 1])); 41 sum[o] = sum[o << 1] + sum[o << 1 | 1]; 42 } 43 44 struct Node 45 { 46 long long left, right, ma, sum; 47 Node(){left = right = ma = -INF;sum = 0;} 48 Node(long long _left, long long _right, long long _ma, long long _sum){left = _left, right = _right, ma = _ma, sum = _sum;} 49 }; 50 51 Node ask(long long ll, long long rr, long long o = 1, long long l = 1, long long r = n) 52 { 53 if(ll <= l && rr >= r)return Node(left[o], right[o], ma[o], sum[o]); 54 int mid = (l + r) >> 1; 55 int flag1 = 0, flag2 = 0; 56 Node re, ans1, ans2; 57 if(mid >= ll) ans1 = ask(ll, rr, o << 1, l, mid), flag1 = 1; 58 if(mid < rr) ans2 = ask(ll, rr, o <<1 | 1, mid + 1, r), flag2 = 1; 59 60 re.sum = ans1.sum + ans2.sum; 61 if(flag1)re.left = max(ans1.left, ans1.sum + ans2.left); 62 else re.left = ans2.left; 63 if(flag2)re.right = max(ans2.right, ans2.sum + ans1.right); 64 else re.right = ans1.right; 65 re.ma = max(re.left, max(re.right, max(ans1.right + ans2.left, max(ans1.ma, ans2.ma)))); 66 67 return re; 68 } 69 70 long long solution(long long x1, long long y1, long long x2, long long y2) 71 { 72 register Node tmp1, tmp2, tmp3; 73 long long ans = -INF; 74 if(y1 < x2) 75 { 76 tmp1 = ask(x1, y1); 77 tmp2 = ask(x2, y2); 78 if(y1 + 1 <= x2 - 1)tmp3 = ask(y1 + 1, x2 - 1); 79 return tmp1.right + tmp2.left + tmp3.sum; 80 } 81 else 82 { 83 tmp1 = ask(x2, y1); 84 ans = tmp1.ma; 85 86 tmp2 = ask(x2, y2); 87 if(x1 <= x2 - 1)tmp3 = ask(x1, x2 - 1); 88 else tmp3.right = 0; 89 ans = max(ans, tmp2.left + tmp3.right); 90 91 tmp2 = ask(x1, y1); 92 if(y1 + 1 <= y2)tmp3 = ask(y1 + 1, y2); 93 else tmp3.left = 0; 94 ans = max(ans, tmp2.right + tmp3.left); 95 96 return ans; 97 } 98 } 99 100 int main() 101 { 102 long long t;read(t); 103 for(;t;--t) 104 { 105 read(n); 106 for(register long long i = 1;i <= n;++ i)read(num[i]); 107 memset(ma, -0x3f, sizeof(ma)); 108 memset(left, -0x3f, sizeof(left)); 109 memset(right, -0x3f, sizeof(right)); 110 memset(sum, 0, sizeof(sum)); 111 build(); 112 read(m); 113 for(register long long i = 1;i <= m;++ i) 114 { 115 long long x1, x2, y1, y2; 116 read(x1), read(y1), read(x2), read(y2); 117 printf("%lld ", solution(x1, y1, x2, y2)); 118 } 119 } 120 return 0; 121 }
注册不了SPJ, 跟标称大数据/小数据(测边界情况)对拍,拍了近半个小时,无错