-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGSS1.cpp
77 lines (68 loc) · 1.77 KB
/
GSS1.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
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
struct node {
long sum;
long best;
long left;
long right;
};
int n;
long q[50003];
node tree[200003];
node makeNode(long sum, long best, long left, long right) {
node tmp;
tmp.sum = sum;
tmp.best = best;
tmp.left = left;
tmp.right = right;
return tmp;
}
node combine (node l, node r) {
long left = l.left;
if (l.sum + r.left>left) left =l.sum + r.left;
long right = r.right;
if (r.sum + l.right > right) right = r.sum + l.right;
long best = max(l.right + r.left, max(l.best, r.best));
return makeNode(l.sum+r.sum,best, left, right);
}
node build(int from, int to, int index) {
if (from == to) {
tree[index] = makeNode(q[from], q[from], q[from], q[from]);
return tree[index];
}
int mid = (from+to)/2;
node l = build(from,mid, (index<<1));
node r = build(mid+1,to, (index<<1)+1);
tree[index] = combine(l,r);
return tree[index];
}
node ans(int index, int from, int to, int a, int b) {
if (from == a && to == b) {
return tree[index];
}
int mid = (from+to)/2;
if (b <= mid) {
return ans((index<<1), from, mid, a, b);
}
if (a > mid) {
return ans((index<<1) + 1, mid+1,to,a,b);
}
node l = ans((index<<1), from, mid, a, mid);
node r = ans((index<<1) + 1, mid+1,to,mid+1,b);
return combine(l,r);
}
int main() {
scanf("%d",&n);
for (int i = 1; i <= n; ++i) scanf("%d",&q[i]);
build(1,n,1);
int t;
scanf("%d",&t);
int a,b;
for (int i = 0; i < t; ++i) {
scanf("%d%d",&a,&b);
printf("%ldn", ans(1,1,n,a,b).best);
}
return 0;
}