区间更新求和

主要用来练习splay树区间更新问题

//splay树的题解

  1. // File Name: 3468-splay.cpp
  2. // Author: Zlbing
  3. // Created Time: 2013年08月09日 星期五 16时30分32秒
  4.  
  5. #include<iostream>
  6. #include<string>
  7. #include<algorithm>
  8. #include<cstdlib>
  9. #include<cstdio>
  10. #include<set>
  11. #include<map>
  12. #include<vector>
  13. #include<cstring>
  14. #include<stack>
  15. #include<cmath>
  16. #include<queue>
  17. using namespace std;
  18. #define CL(x,v); memset(x,v,sizeof(x));
  19. #define INF 0x3f3f3f3f
  20. #define LL long long
  21. #define REP(i,r,n) for(int i=r;i<=n;i++)
  22. #define RREP(i,n,r) for(int i=n;i>=r;i--)
  23.  
  24. #define L ch[x][0]
  25. #define R ch[x][1]
  26. #define KT (ch[ch[rt][1]][0])
  27. const int MAXN=2e5+;
  28. struct SplayTree{
  29. int ch[MAXN][];
  30. int pre[MAXN],sz[MAXN],val[MAXN];
  31. int rt,top;
  32. void Rotate(int x,int f)
  33. {
  34. int y=pre[x];
  35. down(y);down(x);
  36. ch[y][!f]=ch[x][f];
  37. pre[ch[x][f]]=y;
  38. pre[x]=pre[y];
  39. if(pre[x])
  40. ch[pre[y]][ch[pre[y]][]==y]=x;
  41. ch[x][f]=y;
  42. pre[y]=x;
  43. up(y);
  44. }
  45. void Splay(int x,int goal)
  46. {
  47. down(x);
  48. while(pre[x]!=goal)
  49. {
  50. down(pre[pre[x]]);
  51. down(pre[x]);
  52. down(x);
  53. if(pre[pre[x]]==goal)
  54. Rotate(x,ch[pre[x]][]==x);
  55. else
  56. {
  57. int y=pre[x],z=pre[y];
  58. int f=(ch[z][]==y);
  59. if(ch[y][f]==x)
  60. Rotate(x,!f),Rotate(x,f);
  61. else Rotate(y,f),Rotate(x,f);
  62. }
  63. }
  64. up(x);
  65. if(goal==)rt=x;
  66. }
  67. void RTO(int k,int goal)
  68. {
  69. int x=rt;
  70. down(x);
  71. while(sz[L]+!=k)
  72. {
  73. if(k<sz[L]+)x=L;
  74. else
  75. {
  76. k-=(sz[L]+);
  77. x=R;
  78. }
  79. down(x);
  80. }
  81. Splay(x,goal);
  82. }
  83. void vist(int x)
  84. {
  85. if(x)
  86. {
  87. printf("结点%2d : 左儿子 %2d 右儿子 %2d val: %2d sum=%lld\n",x,ch[x][],ch[x][],val[x],sum[x]);
  88. vist(L);
  89. vist(R);
  90. }
  91. }
  92. void debug()
  93. {
  94. puts(""); vist(rt); puts("");
  95. }
  96. void up(int x)
  97. {
  98. sz[x]=+sz[L]+sz[R];
  99. sum[x]=val[x]+sum[L]+sum[R];
  100. }
  101. void down(int x)
  102. {
  103. if(add[x])
  104. {
  105. val[L]+=add[x];
  106. val[R]+=add[x];
  107. add[L]+=add[x];
  108. add[R]+=add[x];
  109. sum[L]+=(LL)add[x]*sz[L];
  110. sum[R]+=(LL)add[x]*sz[R];
  111. add[x]=;
  112. }
  113. }
  114. void Newnode(int &x,int c,int f)
  115. {
  116. x=++top;
  117. L=R=; sz[x]=; pre[x]=f;
  118. val[x]=sum[x]=c;
  119. add[x]=;
  120. }
  121. void build(int &x,int l,int r,int f)
  122. {
  123. if(l>r)return;
  124. int m=(l+r)>>;
  125. Newnode(x,num[m],f);
  126. build(L,l,m-,x);
  127. build(R,m+,r,x);
  128. up(x);
  129. }
  130. void init(int n)
  131. {
  132. ch[][]=ch[][]=pre[]=;
  133. sz[]=rt=top=;
  134.  
  135. add[]=sum[]=;
  136. Newnode(rt,-,);
  137. Newnode(ch[rt][],-,rt);
  138. sz[rt]=;
  139. for(int i=;i<=n;i++)
  140. scanf("%d",&num[i]);
  141.  
  142. build(KT,,n,ch[rt][]);
  143. up(ch[rt][]);up(rt);
  144. }
  145. void update()
  146. {
  147. int l,r,c;
  148. scanf("%d%d%d",&l,&r,&c);
  149. RTO(l,);
  150. RTO(r+,rt);
  151. add[KT]+=c;
  152. val[KT]+=c;
  153. sum[KT]+=(LL)c*sz[KT];
  154. }
  155. void query()
  156. {
  157. int l,r;
  158. scanf("%d%d",&l,&r);
  159. RTO(l,);
  160. RTO(r+,rt);
  161. printf("%lld\n",sum[KT]);
  162. }
  163. LL sum[MAXN];
  164. int add[MAXN];
  165. int num[MAXN];
  166. }spt;
  167. int main()
  168. {
  169. int m,n;
  170. char op[];
  171. while(~scanf("%d%d",&n,&m))
  172. {
  173. spt.init(n);
  174. while(m--)
  175. {
  176. scanf("%s",op);
  177. if(op[]=='Q')spt.query();
  178. else spt.update();
  179. }
  180. }
  181. return ;
  182. }

线段树的题解

  1. // File Name: /home/neuacm/ACM/POJ/3468.cpp
  2. // Author: Zlbing
  3. // Created Time: 2013年08月09日 星期五 14时35分11秒
  4.  
  5. #include<iostream>
  6. #include<string>
  7. #include<algorithm>
  8. #include<cstdlib>
  9. #include<cstdio>
  10. #include<set>
  11. #include<map>
  12. #include<vector>
  13. #include<cstring>
  14. #include<stack>
  15. #include<cmath>
  16. #include<queue>
  17. using namespace std;
  18. #define CL(x,v); memset(x,v,sizeof(x));
  19. #define INF 0x3f3f3f3f
  20. #define LL long long
  21. #define REP(i,r,n) for(int i=r;i<=n;i++)
  22. #define RREP(i,n,r) for(int i=n;i>=r;i--)
  23. #define lson l,m,rt<<1
  24. #define rson m+1,r,rt<<1|1
  25. const int MAXN=1e5+;
  26. LL col[MAXN<<];
  27. LL sum[MAXN<<];
  28. void pushup(int rt)
  29. {
  30. sum[rt]=sum[rt<<]+sum[rt<<|];
  31. }
  32. void pushdown(int rt,int l,int r)
  33. {
  34. if(col[rt])
  35. {
  36. col[rt<<]+=col[rt];
  37. col[rt<<|]+=col[rt];
  38. int m=(l+r)>>;
  39. sum[rt<<]+=(m-l+)*col[rt];
  40. sum[rt<<|]+=(r-m)*col[rt];
  41. col[rt]=;
  42. }
  43. }
  44. void build(int l,int r,int rt)
  45. {
  46. col[rt]=;
  47. if(l==r)
  48. {
  49. //scanf("%I64d",&sum[rt]);
  50. cin>>sum[rt];
  51. return ;
  52. }
  53. int m=(l+r)>>;
  54. build(lson);
  55. build(rson);
  56. pushup(rt);
  57. }
  58. LL query(int L,int R,int l,int r,int rt)
  59. {
  60. if(L<=l&&R>=r)
  61. {
  62. return sum[rt];
  63. }
  64. pushdown(rt,l,r);
  65. int m=(l+r)>>;
  66. LL ret=;
  67. if(L<=m)ret+=query(L,R,lson);
  68. if(R>m)ret+=query(L,R,rson);
  69. return ret;
  70. }
  71. void update(int L,int R,int p,int l,int r,int rt)
  72. {
  73. if(L<=l&&R>=r)
  74. {
  75. col[rt]+=p;
  76. sum[rt]+=(r-l+)*p;
  77. return;
  78. }
  79. pushdown(rt,l,r);
  80. int m=(l+r)>>;
  81. if(L<=m)update(L,R,p,lson);
  82. if(R>m)update(L,R,p,rson);
  83. pushup(rt);
  84. }
  85. void debug(int l,int r,int rt)
  86. {
  87. if(l==r)
  88. {
  89. printf("l==%d r==%d rt=%d sum[rt]=%lld col[rt]=%lld\n",l,r,rt,sum[rt],col[rt]);
  90. return ;
  91. }
  92. printf("l==%d r==%d rt=%d sum[rt]=%lld col[rt]=%lld\n",l,r,rt,sum[rt],col[rt]);
  93. int m=(l+r)>>;
  94. debug(lson);
  95. debug(rson);
  96. }
  97. int main()
  98. {
  99. int n,m;
  100. //std::ios::sync_with_stdio(false);
  101. while(~scanf("%d%d",&n,&m))
  102. {
  103. build(,n,);
  104. char ch[];
  105. int a,b,c;
  106. REP(i,,m)
  107. {
  108. scanf("%s",ch);
  109. if(ch[]=='Q')
  110. {
  111. scanf("%d%d",&a,&b);
  112. LL ans=query(a,b,,n,);
  113. //debug(1,n,1);
  114. cout<<ans<<endl;
  115. }
  116. else if(ch[]=='C')
  117. {
  118. scanf("%d%d%d",&a,&b,&c);
  119. update(a,b,c,,n,);
  120. //debug(1,n,1);
  121. }
  122. }
  123. }
  124. return ;
  125. }

POJ-3468-A Simple Problem with Integers(区间更新,求和)-splay或线段树的更多相关文章

  1. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  2. poj 3468 A Simple Problem with Integers 【线段树-成段更新】

    题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...

  3. 线段树(成段更新) POJ 3468 A Simple Problem with Integers

    题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...

  4. POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)

    题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  5. poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

    题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...

  6. POJ 3468 A Simple Problem with Integers(线段树&区间更新)题解

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  7. POJ 3468 A Simple Problem with Integers(分块入门)

    题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  8. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  9. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  10. poj 3468:A Simple Problem with Integers(线段树,区间修改求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 58269   ...

随机推荐

  1. 【Android】android镜像翻转

    Android镜像翻转指的是将屏幕进行水平的翻转,达到所有内容显示都会反向的效果,就像是在镜子中看到的界面一样.这种应用的使用场景相对比较受限,主要用在一些需要使用Android手机界面进行镜面投影的 ...

  2. install erlang environment on centos

    #(erlide in linux can't detect the runtime if build from source, but erlang shell works correctly)su ...

  3. &amp与&

       

  4. java中怎么进行字符串替换?

    String str = "test.doc"; String newStr = str.replaceAll("doc","html");

  5. 常用的dos命令之简略总结

    Dos常用命令  一.基础命令  1 dir  无参数:查看当前所在目录的文件和文件夹.  /s:查看当前目录已经其所有子目录的文件和文件夹.  /a:查看包括隐含文件的所有文件.  /ah:只显示出 ...

  6. 关于 ASP.NET 验证码

    Session["CheckCode"] 这个..不懂神马意思.. .创建一个用户控件 用户名:TextBox 密码: TextBox 验证码:TextBox 验证码图片 < ...

  7. hdoj 2602(背包)

    Problem D Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

  8. Qt Creator编译问题

    有时候需要自己编译Qt Creator,需要注意的就是qmake版本的问题,比如我用4.8.1和4.8.6同样编译出来的Qt Creator在同样的qtconfig-qt4下所呈现的效果是不一样的. ...

  9. WARN [main] conf.HiveConf (HiveConf.java:initialize(1488)) - DEPRECATED

    问题描述:hive 关于告警问题的解决:WARN  [main] conf.HiveConf (HiveConf.java:initialize(1488)) - DEPRECATED: Config ...

  10. chrome调试状态下动态加载的js

    在js文件中加入 //@ sourceURL=文件名.js