线段树练习:【SPOJ1716】 GSS3
2015-02-03 19:25
288 查看
原题:http://www.spoj.com/problems/GSS3/
You are given a sequence A of N (N <= 50000) integers between -10000 and 10000. On this sequence you have to apply M (M <= 50000) operations:
modify the i-th element in the sequence or for given x y print max{Ai + Ai+1 + .. + Aj | x<=i<=j<=y }.
The first line of input contains an integer N. The following line contains N integers, representing the sequence A1..AN.
The third line contains an integer M. The next M lines contain the operations in following form:
0 x y: modify Ax into y (|y|<=10000).
1 x y: print max{Ai + Ai+1 + .. + Aj | x<=i<=j<=y }.
For each query, print an integer as the problem required.
与GSS1类似,单点更新直到叶子,然后一层层PushUp
SPOJ Problem Set (classical)1716. Can you answer these queries IIIProblem code: GSS3 |
modify the i-th element in the sequence or for given x y print max{Ai + Ai+1 + .. + Aj | x<=i<=j<=y }.
Input
The first line of input contains an integer N. The following line contains N integers, representing the sequence A1..AN.The third line contains an integer M. The next M lines contain the operations in following form:
0 x y: modify Ax into y (|y|<=10000).
1 x y: print max{Ai + Ai+1 + .. + Aj | x<=i<=j<=y }.
Output
For each query, print an integer as the problem required.
Example
Input: 4 1 2 3 4 4 1 1 3 0 3 -3 1 2 4 1 3 3 Output: 6 4 -3
Added by: | Bin Jin |
Date: | 2007-08-03 |
Time limit: | 0.330s |
Source limit: | 5000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel Pentium G860 3GHz) |
Languages: | All except: C++ 4.9 SCM chicken |
Resource: | own problem |
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXD = 10010, INF = 0x3f3f3f3f; struct Seg { int mid, l, r; }; Seg seg[4 * MAXD]; int A[MAXD], a[MAXD]; void update(int cur, int x, int y) { int mid = (x + y) >> 1, ls = cur << 1, rs = cur << 1 | 1; seg[cur].mid = max(seg[ls].mid, seg[rs].mid); seg[cur].mid = max(seg[cur].mid, seg[ls].r + seg[rs].l); seg[cur].l = max(seg[ls].l, A[mid] - A[x - 1] + seg[rs].l); seg[cur].r = max(seg[rs].r, A[y] - A[mid] + seg[ls].r); } void build(int cur, int x, int y) { int mid = (x + y) >> 1, ls = cur << 1, rs = cur << 1 | 1; if (x == y) { seg[cur].mid = seg[cur].l = seg[cur].r = a[x]; return ; } build(ls, x, mid); build(rs, mid + 1, y); update(cur, x, y); } int query(int cur, int x, int y, int s, int t, int flag, int &ans) { int mid = (x + y) >> 1, ls = cur << 1, rs = cur << 1 | 1; if (x >= s && y <= t) { ans = max(ans, seg[cur].mid); if (flag == 0) return seg[cur].l; else return seg[cur].r; } if (mid >= t) return query(ls, x, mid, s, t, 0, ans); else if (mid + 1 <= s) return query(rs, mid + 1, y, s, t, 1, ans); int ln, rn; ln = query(ls, x, mid, s, t, 1, ans); rn = query(rs, mid + 1, y, s, t, 0, ans); ans = max(ans, ln + rn); if (flag == 0) return max(seg[ls].l, A[mid] - A[x - 1] + rn); else return max(seg[rs].r, A[y] - A[mid] + ln); } void Search(int cur, int x, int y, int s, int t, int flag, int &ans) { int mid = (x + y) >> 1, ls = cur << 1, rs = cur << 1 | 1; if (x >= s && y <= t) { if (flag == 0) ans = max(ans, A[x - 1] - A[s - 1] + seg[cur].l); else ans = max(ans, A[t] - A[y] + seg[cur].r); return; } if (mid >= s) Search(ls, x, mid, s, t, flag, ans); if (mid + 1 <= t) Search(rs, mid + 1, y, s, t, flag, ans); } int main(void) { int N, q, ans, t1, t2, x1, y1, x2, y2; scanf("%d", &N); A[0] = 0; for (int i = 1; i <= N; i ++) { scanf("%d", &a[i]); A[i] = A[i - 1] + a[i]; } build(1, 1, N); scanf("%d", &q); for (int i = 0; i < q; i ++) { scanf("%d%d%d%d", &x1, &y1, &x2, &y2); if (y1 < x2) { t1 = t2 = -INF; Search(1, 1, N, x1, y1, 1, t1); Search(1, 1, N, x2, y2, 0, t2); printf("%d\n", t1 + t2 + A[x2 - 1] - A[y1]); } else { ans = -INF; query(1, 1, N, x2, y1, 0, ans); t1 = t2 = -INF; Search(1, 1, N, x1, y1, 1, t1); Search(1, 1, N, y1, y2, 0, t2); ans = max(ans, t1 + t2 - a[y1]); t1 = t2 = -INF; Search(1, 1, N, x1, x2, 1, t1); Search(1, 1, N, x2, y2, 0, t2); ans = max(ans, t1 + t2 - a[x2]); printf("%d\n", ans); } } return 0; }
与GSS1类似,单点更新直到叶子,然后一层层PushUp
相关文章推荐
- spoj(GSS3) 1716 Can you answer these queries III(线段树求最大子段和)
- SPOJ 1716 Can you answer these queries III(GSS3 线段树)
- SPOJ 1716 GSS3 zkw线段树
- [SPOJ1043,1557,1716,2713,2916,4487,6779,19543]GSS八题系列
- spoj 1716 Can you answer these queries III(线段树)
- SPOJ 2713. Can you answer these queries IV(GSS4 线段树)
- Spoj 1716 Can you answer these queries III 线段树 单点修改 区间求最大子段和
- SPOJ GSS系列 最大子段和 线段树+树链剖分+splay 1043 1557 1716 2713 2916 4487 6779
- [BZOJ3211][SPOJ2713][线段树]GSS 4(花神游历各国)[一般题]
- spoj 1716. Can you answer these queries III(线段树)
- SPOJ GSS2 离线线段树
- 【SPOJ - GSS2】Can you answer these queries II(线段树)
- SPOJ GSS1_Can you answer these queries I(线段树区间合并)
- SPOJ GSS3 线段树
- SPOJ 1557 GSS2 线段树
- SPOJ 1043 GSS 1 线段树
- SPOJ GSS1 Can you answer these queries I[线段树]
- SPOJ GSS3 线段树系列1
- BZOJ 2482 || SPOJ GSS2 Can you answer these queries II(线段树 离线 后缀和)
- [SPOJ GSS1] Can you answer these queries I [线段树]