-
Notifications
You must be signed in to change notification settings - Fork 9
/
GSS5 - Can you answer these queries V.cpp
99 lines (99 loc) · 2.56 KB
/
GSS5 - Can you answer these queries V.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include<bits/stdc++.h>
using namespace std;
#define mx 10004
#define inf 1000000000
int max3(int a, int b, int c)
{
return max(a,max(b,c));
}
int max4(int a, int b, int c, int d)
{
return max(a,max3(b,c,d));
}
struct Node
{
int tot;
int pre;
int suf;
int best;
}tree[mx*4],neg;
int arr[mx];
Node merge(Node a, Node b)
{
if(a.tot==-inf) return b;
if(b.tot ==-inf) return a;
Node ret;
ret.tot = a.tot + b.tot;
ret.pre = max(a.pre, a.tot+b.pre);
ret.suf = max(b.suf, b.tot+a.suf);
ret.best = max3(a.best,b.best,a.suf+b.pre);
return ret;
}
void build(int pos, int b, int e)
{
if(b>e) return;
int m = (b+e)/2;
int l = pos*2;
int r = l+1;
if(b==e){
tree[pos].tot = tree[pos].pre = tree[pos].suf = tree[pos].best = arr[b];
return;
}
build(l,b,m);
build(r,m+1,e);
tree[pos] = merge(tree[l],tree[r]);
}
Node query(int pos, int b, int e, int i, int j)
{
if(b>e || b>j || e<i) return neg;
int m = (b+e)/2;
int l = pos*2;
int r = l+1;
if(b>=i && e<=j){
return tree[pos];
}
return merge(query(l,b,m,i,j), query(r,m+1,e,i,j));
}
int main()
{
int tc,n,q,a,b,c,d;
neg.tot = -inf;
scanf("%d", &tc);
while(tc--){
scanf("%d", &n);
for(int i=1; i<=4*n; i++){
tree[i].tot = -inf;
}
for(int i=1; i<=n; i++){
scanf("%d", &arr[i]);
}
build(1,1,n);
scanf("%d", &q);
while(q--){
scanf("%d%d%d%d", &a, &b,&c, &d);
int add = 0,ans=-inf;
if((b+1)<c){/// gape
Node rm = query(1,1,n,b+1,c-1);
add = rm.tot;
Node rl = query(1,1,n,a,b);
Node rr = query(1,1,n,c,d);
ans = max4(add+rl.suf+rr.pre, add+rl.tot+rr.pre,add+rl.suf+rr.tot, add+rl.tot+rr.tot);
}
else if((b+1)==c){/// no gape
Node rl = query(1,1,n,a,b);
Node rr = query(1,1,n,c,d);
ans = max4(add+rl.suf+rr.pre, add+rl.tot+rr.pre,add+rl.suf+rr.tot, add+rl.tot+rr.tot);
}
else{/// overlap
int x = c-1;
int y = b+1;
Node ro = query(1,1,n,c,b);
Node rl = query(1,1,n,a,x);
Node rr = query(1,1,n,y,d);
ans = max4(ro.best,rl.suf+ro.pre,ro.suf+rr.pre,ro.tot+rl.suf+rr.pre);
}
printf("%d\n",ans);
}
}
return 0;
}