【线段树】spoj GSS2 Can you answer these queries II
来源:互联网 发布:沈阳seo技术 编辑:程序博客网 时间:2024/05/18 01:51
写了三个小时的线段树也是醉了 = =
PushDown手贱了一下于是一直23333
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#include <cmath>using namespace std;const int maxN = 100005;#define lc (u << 1)#define rc (u << 1 | 1)namespace SegmentTree { long long sum[maxN << 2], maxx[maxN << 2], lazy_sum[maxN << 2], lazy_max[maxN << 2]; inline void init() { memset(sum, 0, sizeof(sum)); memset(maxx, 0, sizeof(maxx)); memset(lazy_sum, 0, sizeof(lazy_sum)); memset(lazy_max, 0, sizeof(lazy_max)); } inline void update(long long &a, long long b) { if(b > a) a = b; } inline void PushDown(int u) { if (!lazy_max[u] && !lazy_sum[u]) return; update(lazy_max[lc], lazy_sum[lc] + lazy_max[u]); update(maxx[lc], sum[lc] + lazy_max[u]); sum[lc] += lazy_sum[u]; lazy_sum[lc] += lazy_sum[u]; update(lazy_max[rc], lazy_sum[rc] + lazy_max[u]); update(maxx[rc], sum[rc] + lazy_max[u]); sum[rc] += lazy_sum[u]; lazy_sum[rc] += lazy_sum[u]; lazy_max[u] = lazy_sum[u] = 0; } inline void PushUp(int u) { sum[u] = max(sum[lc], sum[rc]); maxx[u] = max(maxx[lc], maxx[rc]); } inline void Add(int u, int l, int r, int L, int R, const int &c) { if (l == L && r == R) { sum[u] += c; lazy_sum[u] += c; update(maxx[u], sum[u]); update(lazy_max[u], lazy_sum[u]); return; } PushDown(u); int mid = (l + r) >> 1; if (R <= mid) Add(lc, l, mid, L, R, c); else if (L > mid) Add(rc, mid + 1, r, L, R, c); else { Add(lc, l, mid, L, mid, c); Add(rc, mid + 1, r, mid + 1, R, c); } PushUp(u); } long long Query(int u, int l, int r, int L, int R) { if (l == L && r == R) return maxx[u]; PushDown(u); int mid = (l + r) >> 1; if (R <= mid) return Query(lc, l, mid, L, R); if (L > mid) return Query(rc, mid + 1, r, L, R); long long temp = Query(lc, l, mid, L, mid); update(temp, Query(rc, mid + 1, r, mid + 1, R)); return max(temp, 0ll); }}int N, M;int a[maxN], last[maxN << 1];long long ans[maxN];struct question { int pos, l, r;}q[maxN];bool cmp(const question &a, const question &b) { return a.r < b.r; }int main(){ ios :: sync_with_stdio(false); while (cin >> N) { for (int i = 1; i <= N; ++ i) cin >> a[i]; cin >> M; for (int i = 1; i <= M; ++ i) { cin >> q[i].l >> q[i].r; q[i].pos = i; } sort(q + 1, q + M + 1, cmp); memset(last, 0, sizeof(last)); int cnt = 1; using namespace SegmentTree; init(); for (int i = 1; i <= N; ++ i) { Add(1, 1, N, last[a[i] + maxN] + 1, i, a[i]); for ( ; q[cnt].r == i && cnt <= M; ++ cnt) ans[q[cnt].pos] = Query(1, 1, N, q[cnt].l, q[cnt].r); last[a[i] + maxN] = i; } for (int i = 1; i <= M; ++ i) cout << ans[i] << endl; } return 0;}
0 0
- [SPOJ GSS2] Can you answer these queries II [线段树]
- 【线段树】spoj GSS2 Can you answer these queries II
- GSS2 - Can you answer these queries II(线段树)
- SPOJ 1557 Can you answer these queries II(GSS2 线段树)
- spoj 1557 Can you answer these queries II (gss2)线段树
- BZOJ 2482 || SPOJ GSS2 Can you answer these queries II(线段树 离线 后缀和)
- SPOJ GSS2 Can you answer these queries II
- spoj 1557. Can you answer these queries II(线段树)
- 【SPOJ】1557 Can you answer these queries II 线段树
- [SPOJ1557][GSS2][线段树]Can you answer these queries II[好题]
- SPOJ/GSS3:Can you answer these queries III(线段树)
- spoj 1043. Can you answer these queries I (线段树)
- spoj 1716. Can you answer these queries III(线段树)
- spoj 2713. Can you answer these queries IV(线段树)
- spoj 2916. Can you answer these queries V(线段树)
- SPOJ GSS3 Can you answer these queries III (线段树)
- SPOJ GSS4 Can you answer these queries IV (线段树)
- 【SPOJ】Can you answer these queries I【线段树】
- ASP小程序 处理动作,如计算数字值,回复聊天内容,基础框架 可扩展简单
- 【问底】徐汉彬:Web系统大规模并发——电商秒杀与抢购
- 接入机房产生冷凝水
- _CrtIsValidHeapPointer 跨dll接口参数数据传递问题
- POJ 3087 Shuffle'm Up - 模拟
- 【线段树】spoj GSS2 Can you answer these queries II
- HDU 1106 - Split String
- Linux进程类型
- HDU5312.Sequence
- [leetcode-15]3Sum(java)
- Spring声明式事务配置管理方法
- 第五篇 学习OpenCV之视频处理
- docker固定IP容器构造
- Hadoop集群(第8期)_HDFS初探之旅