Can you answer these queries V 【SPOJ - GSS5】【线段树 最大子段和】
2019-08-10 11:42
1316 查看
题目链接
WA了好多发,最后的时候把两个其中两个分开的判断「y1 == y2 与 else」放到一块去之后尽然直接A了。
有一个整数序列a
。有m次询问,每次询问Max { A[i]+A[i+1]+...+A[j] ; x1 <= i <= y1 , x2 <= j <= y2 , x1 <= x2 , y1 <= y2 }。
然后,就是去考虑怎样处理这个关系,直接分类讨论吧讨论区间是分开的y1 < y2这样的类型,还是else。
给两组样例:
[code]1 6 12 -6 -5 -8 -5 -4 1 1 2 2 3
[code]1 6 12 -6 -5 -8 -5 -4 1 1 3 2 5
[code]#include <iostream> #include <cstdio> #include <cmath> #include <string> #include <cstring> #include <algorithm> #include <limits> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #define lowbit(x) ( x&(-x) ) #define pi 3.141592653589793 #define e 2.718281828459045 #define INF 0x3f3f3f3f #define efs 1e-6 #define HalF (l + r)>>1 #define lsn rt<<1 #define rsn rt<<1|1 #define Lson lsn, l, mid #define Rson rsn, mid+1, r #define QL Lson, ql, qr #define QR Rson, ql, qr #define myself rt, l, r #define max3(a, b, c) max(a, max(b, c)) #define max4(a, b, c, d) max(max(a, b), max(c, d)) #define max5(a, b, c, d, f) max4(a, b, c, max(d, f)) #define max6(a, b, c, d, f, g) max(max3(a, b, c), max3(d, f, g)) using namespace std; typedef unsigned long long ull; typedef long long ll; const int maxN = 1e4 + 7; int N, M; struct node { int lm, rm, mx, sum; node(int a=0, int b=0, int c=0, int d=0):lm(a), rm(b), mx(c), sum(d) {} void give(int x) { lm = rm = mx = sum = x; } }tree[maxN<<2]; inline void pushup(node &root, node lc, node rc) { root.lm = max(lc.lm, lc.sum + rc.lm); root.rm = max(rc.rm, rc.sum + lc.rm); root.sum = lc.sum + rc.sum; root.mx = max5(root.lm, root.rm, lc.mx, rc.mx, lc.rm + rc.lm); } void buildTree(int rt, int l, int r) { if(l == r) { int val; scanf("%d", &val); tree[rt].give(val); return; } int mid = HalF; buildTree(Lson); buildTree(Rson); pushup(tree[rt], tree[lsn], tree[rsn]); } node query(int rt, int l, int r, int ql, int qr) { if(ql <= l && qr >= r) return tree[rt]; int mid = HalF; if(qr <= mid) return query(QL); else if(ql > mid) return query(QR); else { node TL = query(QL), TR = query(QR); node ans; pushup(ans, TL, TR); return ans; } } int main() { int Cas; scanf("%d", &Cas); while(Cas--) { scanf("%d", &N); buildTree(1, 1, N); scanf("%d", &M); int range_left = 0, range_right = 0, range_sum = 0; for(int i=1, x1, y1, x2, y2; i<=M; i++) { scanf("%d%d%d%d", &x1, &y1, &x2, &y2); if(y1 < x2) //x1 <= y1 < x2 <= y2 { range_left = query(1, 1, N, x1, y1).rm; range_right = query(1, 1, N, x2, y2).lm; range_sum = 0; if(x2 - y1 > 1) range_sum = query(1, 1, N, y1 + 1, x2 - 1).sum; printf("%d\n", range_left + range_right + range_sum); } else { int ans; range_left = query(1, 1, N, x1, x2).rm; range_right = 0; if(x2 < y2) range_right = query(1, 1, N, x2 + 1, y2).lm; range_right = max(0, range_right); ans = range_left + range_right; range_left = query(1, 1, N, x1, y1).rm; range_right = 0; if(y1 < y2) range_right = query(1, 1, N, y1 + 1, y2).lm; ans = max(ans, range_left + range_right); ans = max(ans, query(1, 1, N, x2, y1).mx); printf("%d\n", ans); } } } return 0; }
相关文章推荐
- Spoj 1557 Can you answer these queries II 线段树 任意区间最大子段和 不重复数字
- 【SPOJ-GSS1】Can you answer these queries I【线段树】【最大子段和】
- Spoj 1716 Can you answer these queries III 线段树 单点修改 区间求最大子段和
- Spoj 1557 Can you answer these queries II 线段树 随意区间最大子段和 不反复数字
- spoj(GSS3) 1716 Can you answer these queries III(线段树求最大子段和)
- Spoj 2916 Can you answer these queries V 线段树 求任意重叠区间的最大子段和
- [SPOJ GSS5] Can you answer these queries V [线段树]
- SPOJ GSS5 Can you answer these queries V ——线段树
- GSS1 spoj 1043 Can you answer these queries I 最大子段和
- 【SPOJ】6779 Can you answer these queries VII 树链剖分+线段树 求树上的最大子段和
- SPOJ 1043 Can you answer these queries I 求任意区间最大连续子段和 线段树
- Can you answer these queries VII 【SPOJ - GSS7】【树链剖分 最大子段和】
- Spoj 6779 Can you answer these queries VII 树链剖分 在树上任意路径的最大子段和 区间修改点权
- SPOJ 1557 Can you answer these queries II(离线处理+线段树求历史最大)
- SPOJ GSS1 Can you answer these queries I (线段树求区间最大连续和)
- 数据结构(线段树):SPOJ GSS3 - Can you answer these queries III
- spoj 1557. Can you answer these queries II(线段树)
- SPOJ 2713. Can you answer these queries IV(GSS4 线段树)
- 【SPOJ】2916 Can you answer these queries V 线段树
- spoj 1557 Can you answer these queries II (gss2)线段树