用splay做了一遍。

建树时是按照数列序号从小到大排好的,每个节点左子树的序号小于右子树的序号及这个节点本身。
由于查询[l,r]要伸展l-1,r+1所以我们要多加2个结点,保证边界处理时不出问题。由于这样每次查找l-1时,
要找的应该是l(r+1也是找r+2)。

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <algorithm>
  5. #define ll __int64
  6. #define key_value ch[ch[root][1]][0]
  7. using namespace std;
  8. const int MAXN=;
  9. int pre[MAXN],ch[MAXN][],key[MAXN],tot1,root;
  10. int siz[MAXN],tot2,a[MAXN],n,s[MAXN];
  11. ll lazy[MAXN],sum[MAXN];
  12.  
  13. void Treavel(int x)
  14. {
  15. if(x)
  16. {
  17. Treavel(ch[x][]);
  18. printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size=%2d,key=%2d add=%2d sum=%I64d\n",x,ch[x][],ch[x][],pre[x],siz[x],key[x],lazy[x],sum[x]);
  19. Treavel(ch[x][]);
  20. }
  21. }
  22. void debug()
  23. {
  24. printf("root:%d\n",root);
  25. Treavel(root);
  26. }
  27.  
  28. void Newnode(int &rt,int father,int k)
  29. {
  30. if(tot2)
  31. rt = s[--tot2];
  32. else
  33. rt = ++tot1;
  34. pre[rt] = father;
  35. key[rt] = k;
  36. siz[rt] = ;
  37. lazy[rt] = ;
  38. ch[rt][] = ch[rt][] = ;
  39. }
  40. void pushup(int rt)
  41. {
  42. siz[rt] = siz[ch[rt][]] + siz[ch[rt][]] + ;
  43. sum[rt] = sum[ch[rt][]] + sum[ch[rt][]] + key[rt];
  44. }
  45. void pushdown(int rt)
  46. {
  47. if(lazy[rt]){
  48. lazy[ch[rt][]] += lazy[rt];
  49. lazy[ch[rt][]] += lazy[rt];
  50. key[ch[rt][]] += lazy[rt];
  51. key[ch[rt][]] += lazy[rt];
  52. sum[ch[rt][]] += (ll)siz[ch[rt][]]*lazy[rt];
  53. sum[ch[rt][]] += (ll)siz[ch[rt][]]*lazy[rt];
  54. lazy[rt] = ;
  55. }
  56. }
  57. void build(int &rt,int l,int r,int father)
  58. {
  59. if(l > r)
  60. return ;
  61. int m = (l+r)/;
  62. Newnode(rt,father,a[m]);
  63. build(ch[rt][],l,m-,rt);
  64. build(ch[rt][],m+,r,rt);
  65. pushup(rt);
  66. }
  67. void Init()
  68. {
  69. int i,j;
  70. for(i=; i<=n; i++){
  71. scanf("%d",&a[i]);
  72. }
  73. root = tot1 = tot2 = ;
  74. ch[root][] = ch[root][] = key[root] = siz[root] = lazy[root] = pre[root] = sum[root] = ;
  75. Newnode(root,,-);
  76. Newnode(ch[root][],root,-);//头尾各加入一个点
  77. build(key_value,,n,ch[root][]);//让所有数据夹在这两个点之间 由于树的结构 所以在ch[ch[root][1]][0]
  78. pushup(ch[root][]);
  79. pushup(root);
  80. }
  81. void Rotate(int rt,int kind)
  82. {
  83. int y = pre[rt];
  84. pushdown(y);
  85. pushdown(rt);
  86. ch[y][!kind] = ch[rt][kind];
  87. pre[ch[rt][kind]] = y;
  88. if(pre[y]){
  89. ch[pre[y]][ch[pre[y]][]==y] = rt;
  90. }
  91. pre[rt] = pre[y];
  92. ch[rt][kind] = y;
  93. pre[y] = rt;
  94. pushup(y);
  95. }
  96. void splay(int rt,int goal)
  97. {
  98. pushdown(rt);
  99. while(pre[rt] != goal)
  100. {
  101. if(pre[pre[rt]] == goal){
  102. Rotate(rt,ch[pre[rt]][]==rt);
  103. }
  104. else {
  105. int y = pre[rt];
  106. int kind = ch[pre[y]][]==y;
  107. if(ch[y][kind] == rt){
  108. Rotate(rt,!kind);
  109. Rotate(rt,kind);
  110. }
  111. else {
  112. Rotate(y,kind);
  113. Rotate(rt,kind);
  114. }
  115. }
  116. }
  117. pushup(rt);
  118. if(goal == )
  119. root = rt;
  120.  
  121. }
  122. int Get_kth(int rt,int k)
  123. {
  124. pushdown(rt);
  125. int t = siz[ch[rt][]] + ;
  126. if(t == k){
  127. return rt;
  128. }
  129. else if(t > k){
  130. return Get_kth(ch[rt][],k);
  131. }
  132. else
  133. return Get_kth(ch[rt][],k-t);
  134. }
  135. void updata(int l,int r,int v)
  136. {
  137. splay(Get_kth(root,l),);
  138. splay(Get_kth(root,r+),root);
  139. key[key_value] += v;
  140. lazy[key_value] += v;
  141. sum[key_value] += (ll)v*siz[key_value];
  142. }
  143. ll query(int l,int r)
  144. {
  145. splay(Get_kth(root,l),);//由于开始的时候多添加了2个结点,所以编号都是在这2个结点之间的 所以查询的时候都要大1
  146. splay(Get_kth(root,r+),root);
  147. return sum[key_value];
  148. }
  149. int main()
  150. {
  151. int i,j,q;
  152. while(~scanf("%d%d",&n,&q))
  153. {
  154. Init();
  155. //debug();
  156. char s[];
  157. while(q--)
  158. {
  159. scanf("%s",s);
  160. if(s[] == 'C'){
  161. int x,y,z;
  162. scanf("%d%d%d",&x,&y,&z);
  163. updata(x,y,z);
  164. }
  165. else {
  166. int x,y;
  167. scanf("%d%d",&x,&y);
  168. printf("%I64d\n",query(x,y));
  169. }
  170. }
  171. }
  172. }

poj3468 splay(成段跟新 区间求和)的更多相关文章

  1. UESTC-1057 秋实大哥与花(线段树+成段加减+区间求和)

    秋实大哥与花 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit St ...

  2. 线段树(成段更新,区间求和lazy操作 )

    hdu1556 Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  3. POJ-3468 A Simple Problem with Integers (区间求和,成段加减)

    You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...

  4. POJ 3468.A Simple Problem with Integers-线段树(成段增减、区间查询求和)

    POJ 3468.A Simple Problem with Integers 这个题就是成段的增减以及区间查询求和操作. 代码: #include<iostream> #include& ...

  5. poj 3468 A Simple Problem with Integers (线段树 成段更新 加值 求和)

    题目链接 题意: 只有这两种操作 C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.&quo ...

  6. POJ 3225.Help with Intervals-线段树(成段替换、区间异或、简单hash)

    POJ3225.Help with Intervals 这个题就是对区间的各种操作,感觉这道题写的一点意思都没有,写到后面都不想写了,而且更神奇的是,自己的编译器连结果都输不出来,但是交上就过了,也是 ...

  7. poj_3468线段树成段更新求区间和

    #include<iostream> #include<string.h> #include<cstdio> long long num[100010]; usin ...

  8. 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和

    poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...

  9. poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)

    转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...

随机推荐

  1. Java向上转型与向下转型

    一.向上转型 例如:Parent p=new Son(); 这样引用p只能调用子类中重载父类的方法:但属性是父类的:如果想调用子类属性的话,可以用getter()方法. 二.向下转型 子类对象的父类引 ...

  2. 边工作边刷题:70天一遍leetcode: day 81

    Encode and Decode Strings 要点:题的特点:不是压缩,而是encode为字节流.所以需要找delimiter来分割每个word,但是delimiter可能是字符本身,所以可以用 ...

  3. python刷题专用函数。。

    无它,非bin()莫属. bin(x) Convert an integer number to a binary string. The result is a valid Python expre ...

  4. java7-4 继承的练习

    1.Override和Overload的区别,Overload能改变返回值类型吗? 答: Override就是方法重写:在子类中,出现和父类中一模一样的方法声明的现象 Overload就是方法重载:在 ...

  5. 注解与反射 ---Spring与Mybatis等框架的实现原理

    Java中各大框架,无论是AOP 还是 IoC 其基本实现思路都是源自Java 运行时支撑的反射功能, 而反射最基本的一点就是 任何一个类 其在JVM环境中,都有一个对象的Class对象,这个对象提供 ...

  6. 两种方式判断类的存在→className getAttribute

    通过className获取 var p = document.getElementsByTagName('p'); for(var i = 0;i <p.length;i++){ if(p[i] ...

  7. AFN框架基本使用

    0.AFN框架基本使用 0.1 AFN内部结构 AFN结构体 - NSURLConnection + AFURLConnectionOperation(已经被废弃) + AFHTTPRequestOp ...

  8. [转]iOS 应用内付费(IAP)开发步骤

    FROM : http://blog.csdn.net/xiaoxiangzhu660810/article/details/17434907 参考文章链接: (1)http://mobile.51c ...

  9. string与stringBuilder的效率与内存占用实测

    using UnityEngine; using System.Diagnostics; using System.Text; using UnityEngine.UI; public class s ...

  10. php基础31:正则匹配-元字符

    <?php //2.正则表达式:元字符 $model = "/php/"; $string = "php"; // 1.元字符 [a-z] 匹配任何a-z ...