hdu-4348-To the moon-主席树在线区间更新
2015-08-04 00:15
381 查看
To the moon
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2937 Accepted Submission(s): 576
[align=left]Problem Description[/align]
Background
To The Moon is a independent game released in November 2011, it is a role-playing adventure game powered by RPG Maker.
The premise of To The Moon is based around a technology that allows us to permanently reconstruct the memory on dying man. In this problem, we'll give you a chance, to implement the logic behind the scene.
You‘ve been given N integers A[1], A[2],..., A
. On these integers, you need to implement the following operations:
1. C l r d: Adding a constant d for every {Ai | l <= i <= r}, and increase the time stamp by 1, this is the only operation that will cause the time stamp increase.
2. Q l r: Querying the current sum of {Ai | l <= i <= r}.
3. H l r t: Querying a history sum of {Ai | l <= i <= r} in time t.
4. B t: Back to time t. And once you decide return to a past, you can never be access to a forward edition anymore.
.. N, M ≤ 105, |A[i]| ≤ 109, 1 ≤ l ≤ r ≤ N, |d| ≤ 104 .. the system start from time 0, and the first modification is in time 1, t ≥ 0, and won't introduce you to a future state.
[align=left]Input[/align]
n m
A1 A2 ... An
... (here following the m operations. )
[align=left]Output[/align]
... (for each query, simply print the result. )
[align=left]Sample Input[/align]
10 5 1 2 3 4 5 6 7 8 9 10 Q 4 4 Q 1 10 Q 2 4 C 3 6 3 Q 2 4 2 4 0 0 C 1 1 1 C 2 2 -1 Q 1 2 H 1 2 1
[align=left]Sample Output[/align]
4 55 9 15 0 1
[align=left]Author[/align]
HIT
[align=left]Source[/align]
2012 Multi-University Training Contest 5
题目大意:给你一个长度为n的数组,总共四种操作,其中还有一个时间T,每次更新区间的时候时间T+1,更新操作就是把lr区间全部加上d查询操作是一个查询当前时间lr的和,另一个是在第t秒的时候,查询lr区间的和,最后一种操作是把时间退回到第t秒,并且不能再询问t秒以后的时间。刚开始的时候T等于0.
思路:一开始的时候初始化一颗线段树,就普通线段树一样,现在主要的问题是怎么进行区间更新,因为主席树的思想就是要利用前面的树,并且不能修改,只能引用,所以按普通的区间更新的话,每次更新就相当于要建一颗r-l+1长的线段树,空间肯定不够,考虑普通的线段树的down操作是把懒惰标记往下传,并更新子树,那么可不可以不更新子树了,当然是可以的,直接把当前区间的懒惰标记用一个参数传下去,然后找到要求和的区间时,直接把从上到下的懒惰标记累加和乘以区间长度再加上这段区间原本的和就可以了。然后这时候主席树里面每颗线段树的sum记录的就是这段区间跟新过得和,因为在新建的一颗主席树上更新了l-r区间,所以所有有覆盖l-r的区间的sum都是可以跟新的因为没有用到前面一颗树的sum,是另开的空间,所以保证了查询的时候只查到这一段就可以直接返回sum了。至于add当然就是等于前面一颗树的add加上C。另外当时间回到t的时候可以回收空间,就是t到T的这一段内存已经用不到了,这时可以直接把tot
= T[t+1],不写这句话内存用了5w+,写了只用了1w+。
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <iostream> #include <cstdlib> #include <queue> typedef __int64 LL; using namespace std; const int maxn = 100005; int n,m; int T[maxn]; int lson[maxn*30],rson[maxn*30]; LL sum[maxn*30],add[maxn*30]; int tot = 0; int build(int l,int r) { int root = tot++; add[root] = 0; if(l < r) { int mid = (l + r) >> 1; lson[root] = build(l,mid); rson[root] = build(mid + 1,r); } else { scanf("%I64d",&sum[root]); return root; } sum[root] = sum[lson[root]] + sum[rson[root]]; return root; } int A,B; LL C; void up(int o,int len) { sum[o] = sum[lson[o]] + sum[rson[o]] + add[lson[o]] * (len - len/2) + add[rson[o]] * (len/2); } int update(int root,int l,int r) { int newroot = tot++; add[newroot] = add[root]; if(A <= l && r <= B) { sum[newroot] = sum[root]; add[newroot] = add[root] + C; lson[newroot] = lson[root]; rson[newroot] = rson[root]; return newroot; } int mid = (l + r) >> 1; if(A <= mid) lson[newroot] = update(lson[root],l,mid); else lson[newroot] = lson[root]; if(B > mid) rson[newroot] = update(rson[root],mid+1,r); else rson[newroot] = rson[root]; up(newroot,r - l + 1); return newroot; } LL qurry(int root,int l,int r,LL add1) { if(A <= l && r <= B) { return sum[root] + (add1 + add[root]) * (r - l + 1); } LL ans = 0; int mid = (l + r) >>1; if(A <= mid) ans += qurry(lson[root],l,mid,add[root] + add1); if(B > mid) ans += qurry(rson[root],mid + 1,r,add[root] + add1); return ans; } int main() { int ca = 0; while(scanf("%d %d",&n,&m) != EOF) { if(ca) printf("%d\n"); ca++; int i; tot = 0; int t = 0; T[0] = build(1,n); char op[10]; while(m--) { scanf("%s",op); if(op[0] == 'Q') { scanf("%d %d",&A,&B); printf("%I64d\n",qurry(T[t],1,n,0)); } else if(op[0] == 'C') { scanf("%d %d %I64d",&A,&B,&C); T[t+1] = update(T[t],1,n); t++; } else if(op[0] == 'H') { scanf("%d %d %I64d",&A,&B,&C); printf("%I64d\n",qurry(T[C],1,n,0)); } else { scanf("%d",&A); t = A; tot = T[t+1]; } } } return 0; }
相关文章推荐
- 设置单个项目的jdk版本
- 封装类中实现另外两个Activity之间的跳转
- JDOM解析XML
- HDU - 2883 kebab (最大流)
- Lubuntu 12.04开机启动应用程序脚本
- volley学习笔记(六):volley框架下发送和读取cookie
- Leetcode 162 Find Peak Element 查找峰值元素(极大值)
- SPOJ QTREE 系列
- 面试备用
- 同步方式比较
- Google C++ Coding Style:右值引用(Rvalue Reference)
- 第13章 Swing程序设计----标签组件与图标
- 基于Tinyhttp改写的http服务器(memcached,mysql)
- 并查集实例详解
- epoll使用详解(精髓)
- Linux小知识
- CodeForces - 200DProgramming Language纯模拟
- 计数排序C语言实现
- 如何初始化二维数组
- Android应用架构