poj 3468 A Simple Problem with Integers(splay tree模板题)
2012-10-03 15:13
316 查看
A Simple Problem with Integers
Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is
to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
Sample Output
Hint
The sums may exceed the range of 32-bit integers.
这俩篇论文很好:http://www.docin.com/p-62465596.html
http://www.docin.com/p-63165342.html
模板留着用:出处http://www.notonlysuccess.com/index.php/splay-tree/
建树时是按照数列序号从小到大排好的,每个节点左子树的序号小于右子树的序号及这个节点本身,以后每次旋转也是在这个基础上..
注意:因为询问时要伸展l-1和r+1两个节点,所以我们就有必要在原数列的头尾都再加上一个任意数,这样就能保证一定能找到l-1和r+1。因为数列中第一个数前面没有数字了,并且最后一个数后面也没有数字了,这样提取区间时就会出一些问题。为了不进行过多的特殊判断, 我们在原数列最前面和最后面分别加上一个数, 在伸展树中就体现为结点,这样提取区间的时候原来的第 k 个数就是现在的第k+1 个数。并且我们还要注意,这两个结点维护的信息不能影响到正确的结果。
在这题里另加的这俩个节点的val值可以是任意数都不影响正确的求和结果,因为当提取区间[a,b]时,将a前面一个数对应的结点转到树根后, a及a后面的数就在根的右子树上,然后又将b 后面一个结点对应的结点转到树根的右边,那么[a,b]这个区间就是下图中*所示的子树。在*子树中没有这俩个节点存在,所以不会影响到正确结果。。。。。
如果有什么不正确的地方欢迎各位大牛指正。。。。。欢迎讨论哦!!
![](http://img.my.csdn.net/uploads/201210/03/1349248610_5285.png)
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 36775 | Accepted: 10613 | |
Case Time Limit: 2000MS |
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is
to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
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
Sample Output
4 55 9 15
Hint
The sums may exceed the range of 32-bit integers.
这俩篇论文很好:http://www.docin.com/p-62465596.html
http://www.docin.com/p-63165342.html
模板留着用:出处http://www.notonlysuccess.com/index.php/splay-tree/
建树时是按照数列序号从小到大排好的,每个节点左子树的序号小于右子树的序号及这个节点本身,以后每次旋转也是在这个基础上..
注意:因为询问时要伸展l-1和r+1两个节点,所以我们就有必要在原数列的头尾都再加上一个任意数,这样就能保证一定能找到l-1和r+1。因为数列中第一个数前面没有数字了,并且最后一个数后面也没有数字了,这样提取区间时就会出一些问题。为了不进行过多的特殊判断, 我们在原数列最前面和最后面分别加上一个数, 在伸展树中就体现为结点,这样提取区间的时候原来的第 k 个数就是现在的第k+1 个数。并且我们还要注意,这两个结点维护的信息不能影响到正确的结果。
在这题里另加的这俩个节点的val值可以是任意数都不影响正确的求和结果,因为当提取区间[a,b]时,将a前面一个数对应的结点转到树根后, a及a后面的数就在根的右子树上,然后又将b 后面一个结点对应的结点转到树根的右边,那么[a,b]这个区间就是下图中*所示的子树。在*子树中没有这俩个节点存在,所以不会影响到正确结果。。。。。
如果有什么不正确的地方欢迎各位大牛指正。。。。。欢迎讨论哦!!
![](http://img.my.csdn.net/uploads/201210/03/1349248610_5285.png)
#include <cstdio> #define keyTree (ch[ ch[root][1] ][0]) const int maxn = 222222; struct SplayTree{ int sz[maxn]; int ch[maxn][2]; int pre[maxn]; int root , top1; /*这是题目特定变量*/ int num[maxn]; int val[maxn]; int add[maxn]; long long sum[maxn]; inline void Rotate(int x,int f) { int y = pre[x]; push_down(y); push_down(x); ch[y][!f] = ch[x][f]; pre[ ch[x][f] ] = y; pre[x] = pre[y]; if(pre[x]) ch[ pre[y] ][ ch[pre[y]][1] == y ] = x; ch[x][f] = y; pre[y] = x; push_up(y); } inline void Splay(int x,int goal) { push_down(x); while(pre[x] != goal) { if(pre[pre[x]] == goal) { Rotate(x , ch[pre[x]][0] == x); } else { int y = pre[x] , z = pre[y]; int f = (ch[z][0] == y); if(ch[y][f] == x) { Rotate(x , !f) , Rotate(x , f); } else { Rotate(y , f) , Rotate(x , f); } } } push_up(x); if(goal == 0) root = x; } inline void RotateTo(int k,int goal) {//把第k位的数转到goal下边 int x = root; push_down(x); while(sz[ ch[x][0] ] != k) { if(k < sz[ ch[x][0] ]) { x = ch[x][0]; } else { k -= (sz[ ch[x][0] ] + 1);//多减去1,所以k+1位 x = ch[x][1]; } push_down(x); } Splay(x,goal); } //以上一般不修改////////////////////////////////////////////////////////////////////////////// void debug() {printf("%d\n",root);Treaval(root);} void Treaval(int x) { if(x) { Treaval(ch[x][0]); printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d ,val = %2d\n",x,ch[x][0],ch[x][1],pre[x],sz[x],val[x]); Treaval(ch[x][1]); } } //以上Debug //以下是题目的特定函数: inline void NewNode(int &x,int c) { x = ++top1; ch[x][0] = ch[x][1] = pre[x] = 0; sz[x] = 1; val[x] = sum[x] = c;/*这是题目特定函数*/ add[x] = 0; } //把延迟标记推到孩子 inline void push_down(int x) {/*这是题目特定函数*/ if(add[x]) { val[x] += add[x]; add[ ch[x][0] ] += add[x]; add[ ch[x][1] ] += add[x]; sum[ ch[x][0] ] += (long long)sz[ ch[x][0] ] * add[x]; sum[ ch[x][1] ] += (long long)sz[ ch[x][1] ] * add[x]; add[x] = 0; } } //把孩子状态更新上来 inline void push_up(int x) { sz[x] = 1 + sz[ ch[x][0] ] + sz[ ch[x][1] ]; /*这是题目特定函数*/ sum[x] = add[x] + val[x] + sum[ ch[x][0] ] + sum[ ch[x][1] ]; } /*初始化*/ inline void makeTree(int &x,int l,int r,int f) { if(l > r) return ; int m = (l + r)>>1; NewNode(x , num[m]); /*num[m]权值改成题目所需的*/ makeTree(ch[x][0] , l , m - 1 , x); makeTree(ch[x][1] , m + 1 , r , x); pre[x] = f; push_up(x); } inline void init(int n) {/*这是题目特定函数*/ ch[0][0] = ch[0][1] = pre[0] = sz[0] = 0; add[0] = sum[0] = 0; root = top1 = 0; //为了方便处理边界,加两个边界顶点 NewNode(root , 0); NewNode(ch[root][1] , 0); pre[top1] = root; sz[root] = 2; for (int i = 0 ; i < n ; i ++) scanf("%d",&num[i]); makeTree(keyTree , 0 , n-1 , ch[root][1]); push_up(ch[root][1]); push_up(root); } /*更新*/ inline void update( ) {/*这是题目特定函数*/ int l , r , c; scanf("%d%d%d",&l,&r,&c); RotateTo(l-1,0); RotateTo(r+1,root); add[ keyTree ] += c; sum[ keyTree ] += (long long)c * sz[ keyTree ]; } /*询问*/ inline void query() {/*这是题目特定函数*/ int l , r; scanf("%d%d",&l,&r); RotateTo(l-1 , 0); RotateTo(r+1 , root); printf("%lld\n",sum[keyTree]); } }spt; int main() { int n , m; scanf("%d%d",&n,&m); spt.init(n); //spt.debug(); while(m --) { char op[2]; scanf("%s",op); if(op[0] == 'Q') { spt.query(); } else { spt.update(); //spt.debug(); } } return 0; }
相关文章推荐
- POJ 3468 A Simple Problem with Integers(线段树区间更新模板题)
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
- POJ 3468 A Simple Problem with Integers(线段树)
- POJ_3468 A Simple Problem with Integers(线段树区间修改+附线段树模板)
- A Simple Problem with Integers POJ - 3468 (线段树区间增减,区间查询模板)
- 暑期训练狂刷系列——poj 3468 A Simple Problem with Integers (线段树+区间更新)
- poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)
- A Simple Problem with Integers(树状数组,更新区间查询区间模板)
- A Simple Problem with Integers POJ - 3468 线段树模板
- POJ - 3468 B - A Simple Problem with Integers 线段树区间更新模板
- 线段树模板(二)——成段更新 + POJ 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers (伸展树区间更新求和操作 , 模板)
- POJ 3468 A simple problem with integers Splay 区间维护模板题
- POJ A Simple Problem with Integers (线段树区间更新区间查询模板)
- poj 3468 A Simple Problem with Integers(splay tree解法)
- poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)
- poj3468 A Simple Problem with Integers(zkw区间修改模板)
- poj 3468 A Simple Problem with Integers 线段树
- POJ 3468 A Simple Problem with Integers(线段树区间更新模板题)
- POJ 3468 A Simple Problem with Integers (线段树区间更新模板)