题目大意:

两个操作

1 id op  把id的位置+op

2 id op  查询在【id。op】之间的全部的数的差

思路:

关键是pushup函数。

自己退一下会发现。跟区间的总和,区间的节点个数有关。

比方假设左区间是 1 2 的话

右区间来一个 9

那么

就要加上

9-1+9-2

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <set>
  6. #define inf 0x3f3f3f3f
  7. #define maxn 222222
  8. #define keyTree (ch[ch[root][1]][0])
  9. //当把l-1放在根节点 r+1放在根节点的右子树
  10. //那么根节点的右子树的左子树就是[l,r] 这个区间的全部值
  11. using namespace std;
  12. typedef long long LL;
  13. int S[maxn],que[maxn],ch[maxn][2],pre[maxn],siz[maxn];
  14. int root,top1,top2;
  15.  
  16. LL ans[maxn],val[maxn],a[maxn],b[maxn];
  17. LL sum[maxn];
  18. set <LL> tab;
  19. void Treaval(int x)
  20. {
  21. if(x)
  22. {
  23. Treaval(ch[x][0]);
  24. printf("%I64d ",val[x]);
  25. Treaval(ch[x][1]);
  26. }
  27. }
  28. void debug()
  29. {
  30. // printf("root=%d\n",root);
  31. Treaval(root);
  32. puts("");
  33. }
  34. void New(int &x,int PRE,LL v)
  35. {
  36. if(top2)x=S[--top2];
  37. else x=++top1;
  38.  
  39. ch[x][0]=ch[x][1]=0;
  40. siz[x]=1;
  41. pre[x]=PRE;
  42. /*special*/
  43. sum[x]=v;
  44. ans[x]=0;
  45. val[x]=v;
  46. }
  47. void pushup(int x)/*special*/
  48. {
  49. siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;
  50.  
  51. sum[x]=sum[ch[x][0]]+sum[ch[x][1]]+val[x];
  52. ans[x]=ans[ch[x][0]]+ans[ch[x][1]]+siz[ch[x][0]]*val[x]-sum[ch[x][0]]+sum[ch[x][1]]-val[x]*siz[ch[x][1]]+siz[ch[x][0]]*sum[ch[x][1]]-siz[ch[x][1]]*sum[ch[x][0]];
  53. }
  54. void pushdown(int x)/*special*/
  55. {
  56.  
  57. }
  58. void build(int &x,int s,int e,int f)
  59. {
  60. if(s>e)return;
  61. int mid=(s+e)>>1;
  62.  
  63. New(x,f,a[mid]);
  64.  
  65. if(s<mid)build(ch[x][0],s,mid-1,x);
  66. if(e>mid)build(ch[x][1],mid+1,e,x);
  67. pushup(x);
  68. }
  69.  
  70. void Rotate(int x,int kind)
  71. {
  72. int y=pre[x];
  73. pushdown(x);
  74. pushdown(y);
  75. ch[y][!kind]=ch[x][kind];
  76. pre[ch[x][kind]]=y;
  77. if(pre[y])ch[pre[y]][ch[pre[y]][1]==y]=x;
  78. pre[x]=pre[y];
  79. ch[x][kind]=y;
  80. pre[y]=x;
  81. pushup(y);
  82. }
  83.  
  84. void Splay(int x,int goal)
  85. {
  86. pushdown(x);
  87. while(pre[x]!=goal)
  88. {
  89. if(pre[pre[x]]==goal)
  90. Rotate(x,ch[pre[x]][0]==x);
  91. else
  92. {
  93. int y=pre[x];
  94. int kind=ch[pre[y]][0]==y;
  95. if(ch[y][kind]==x){
  96. Rotate(x,!kind);
  97. Rotate(x,kind);
  98. }
  99. else {
  100. Rotate(y,kind);
  101. Rotate(x,kind);
  102. }
  103. }
  104. }
  105. pushup(x);
  106. if(goal==0)root=x;
  107. }
  108.  
  109. void RotateTo(int k,int goal)
  110. {
  111. int r=root;
  112. pushdown(r);
  113. while(siz[ch[r][0]]!=k)
  114. {
  115. if(k<siz[ch[r][0]])
  116. {
  117. r=ch[r][0];
  118. }
  119. else
  120. {
  121. k-=siz[ch[r][0]]+1;
  122. r=ch[r][1];
  123. }
  124. pushdown(r);
  125. }
  126. Splay(r,goal);
  127. }
  128.  
  129. void erase(int x)
  130. {
  131. int y=pre[x];
  132. int head=0,tail=0;
  133. for(que[tail++]=x;head<tail;head++)
  134. {
  135. S[top2++]=que[head];
  136. if(ch[que[head]][0])que[tail++]=ch[que[head]][0];
  137. if(ch[que[head]][1])que[tail++]=ch[que[head]][1];
  138. }
  139. ch[y][ch[y][1]==x]=0;
  140. pushup(y);
  141. }
  142.  
  143. void init(int n)
  144. {
  145. root=top1=top2=0;
  146. ch[0][0]=ch[0][1]=siz[0]=pre[0]=0;
  147. ans[0]=sum[0]=val[0]=0;
  148.  
  149. New(root,0,-inf);
  150. New(ch[root][1],root,inf);
  151.  
  152. siz[root]=2;
  153.  
  154. for(int i=0;i<n;i++)
  155. {
  156. scanf("%I64d",&a[i]);
  157. tab.insert(a[i]);
  158. b[i]=a[i];
  159. }
  160. tab.insert(-inf);tab.insert(inf);
  161. sort(a,a+n);
  162. build(keyTree,0,n-1,ch[root][1]);
  163. pushup(ch[root][1]);
  164. pushup(root);
  165. }
  166.  
  167. int find(LL x,int t)
  168. {
  169. if(x==val[t])return t;
  170. else if(x>val[t])return find(x,ch[t][1]);
  171. else return find(x,ch[t][0]);
  172. }
  173.  
  174. set<LL>::iterator it;
  175.  
  176. int add(int x,LL Num,int pos)
  177. {
  178. if(Num<=val[x])
  179. {
  180. if(ch[x][0]==0)
  181. {
  182. Splay(x,0);
  183. int S=siz[ch[root][0]];
  184. RotateTo(S-1,0);
  185. RotateTo(S,root);
  186. New(keyTree,ch[root][1],Num);
  187. pushup(ch[root][1]);
  188. pushup(root);
  189. }
  190. else add(ch[x][0],Num,pos);
  191. }
  192. else
  193. {
  194. if(ch[x][1]==0)
  195. {
  196. Splay(x,0);
  197. int S=siz[ch[root][0]];
  198. RotateTo(S,0);
  199. RotateTo(S+1,root);
  200. New(keyTree,ch[root][1],Num);
  201. pushup(ch[root][1]);
  202. pushup(root);
  203. }
  204. else add(ch[x][1],Num,siz[ch[x][0]]+1+pos);
  205. }
  206. }
  207. int main()
  208. {
  209. tab.clear();
  210. int n;
  211. scanf("%d",&n);
  212. init(n);
  213. //debug();
  214. int m;
  215. scanf("%d",&m);
  216.  
  217. while(m--)
  218. {
  219. int op;
  220. int l,r;
  221. scanf("%d%d%d",&op,&l,&r);
  222. if(op==1)
  223. {
  224. it=tab.lower_bound(b[l-1]);
  225.  
  226. //printf("*it = %I64d\n",*it);
  227.  
  228. int pos=find(*it,root);
  229. //printf("---%I64d\n",val[pos]);
  230. Splay(pos,0);
  231.  
  232. int Spos=siz[ch[root][0]];
  233. RotateTo(Spos-1,0);
  234. RotateTo(Spos+1,root);
  235.  
  236. //erase(keyTree);
  237. erase(keyTree);
  238. pushup(ch[root][1]);
  239. pushup(root);
  240.  
  241. tab.erase(it);
  242.  
  243. b[l-1]+=r;
  244.  
  245. tab.insert(b[l-1]);
  246.  
  247. add(root,b[l-1],0);
  248. }
  249. else
  250. {
  251. it=tab.lower_bound(l);
  252. it--;
  253. //printf("%I64d\n",*it);
  254. //printf("%I64d\n",val[]);
  255. Splay(find(*it,root),0);
  256. it=tab.upper_bound(r);
  257. Splay(find(*it,root),root);
  258. printf("%I64d\n",ans[keyTree]);
  259. }
  260. //debug();
  261. }
  262. return 0;
  263. }

CF 295E Yaroslav and Points(Splay)的更多相关文章

  1. 【BZOJ3506】排序机械臂(Splay)

    [BZOJ3506]排序机械臂(Splay) 题面 神TMBZOJ没有题面,感谢SYC的题面 洛谷的题面也不错 题解 对于每次旋转的物体 显然可以预处理出来 现在只要模拟旋转操作就行了 至于在哪里放标 ...

  2. 【BZOJ1500】【NOI2005】维修数列(Splay)

    [BZOJ1500][NOI2005]维修数列(Splay) 题面 不想再看见这种毒瘤题,自己去BZOJ看 题解 Splay良心模板题 真的很简单 我一言不发 #include<iostream ...

  3. 【BZOJ1862】[ZJOI2006]游戏排名系统 (Splay)

    [BZOJ1862][ZJOI2006]游戏排名系统 (Splay) 题面 BZOJ 洛谷 题解 双倍经验题

  4. 【BZOJ1056】[HAOI2008]排名系统(Splay)

    [BZOJ1056][HAOI2008]排名系统(Splay) 题面 BZOJ 洛谷 题解 \(Splay\)随便维护一下就好了,至于名字什么的,我懒得手写哈希表了,直接哈希之后拿\(map\)压. ...

  5. 【BZOJ2329】括号修复(Splay)

    [BZOJ2329]括号修复(Splay) 题面 BZOJ 洛谷 题解 本来想着用线段树来写 但是有一个区间翻转 所以不能用线段树了,就只能用平衡树 然后直接\(Splay\)就好了 注意一下几个标记 ...

  6. CF 984C Finite or not? (数论)

    CF 984C Finite or not? (数论) 给定T(T<=1e5)组数据,每组数据给出十进制表示下的整数p,q,b,求问p/q在b进制意义下是否是有限小数. 首先我们先把p/q约分一 ...

  7. P3391 【模板】文艺平衡树(Splay)新板子

    P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转 ...

  8. fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)

    题面: [模板]文艺平衡树(Splay) 题解:无 代码: #include<cstdio> #include<cstring> #include<iostream> ...

  9. BZOJ 1251 序列终结者(Splay)

    题目大意 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术 ...

随机推荐

  1. 总结Widows 7 Start->Run 命令

    Widows + R基本上成为很常用的方式,那么通过Windows + R我们可以在运行中做什么手脚呢. 下面从最基本的系统命令说起 notepad--------打开记事本    services. ...

  2. Entity Framework 级联删除

    为一对主从表增加级联删除功能 protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.E ...

  3. jquery 自定义tab

    <!DOCTYPE html> <html> <head> <script src="/jquery/jquery-1.11.1.min.js&qu ...

  4. Linux系统管理技术手册——第6章 添加新用户

    6.1/etc/passwd文件 用户登录时Linux识别用户的文件/etc/passwd /etc/passwd包括7个字段: 登录名(不超过32位,使用NIS系统后不超过8位) 经过加密的口令或口 ...

  5. WebForm

    ASP开发方式 格式 <%  %> C#代码可以写在里面 <%= %>  往外输出一个值,可以放一个变量,一个方法(这个方法是有返回值的直接打印到界面上去) <%@ %& ...

  6. PHP调试工具Xdebug安装配置教程

    说道PHP代码调试,对于有经验的PHPer,通过echo.print_r.var_dump函数,或PHP开发工具zend studio.editplus可解决大部分问题,但是对于PHP入门学习的童鞋来 ...

  7. 通过预编译头文件来提高C++ Builder的编译速度

    C++ Builder是最快的C++编译器之一,从编译速度来说也可以说是最快的win32C++编译器了.除了速度之外,C++builder的性能也在其它C++编译器的之上,但许多Delphi程序员仍受 ...

  8. AOP举例子

    切面类TestAspect package com.spring.aop; /** * 切面 * */ public class TestAspect { public void doAfter(Jo ...

  9. bzoj3295

    没什么好说的,树套树应该随便搞我在128MB空间下大胆的写了主席树当然要把原树和修改树分开来建没有然后了 type node=record l,r,s:longint; end; ..] of nod ...

  10. Qt入门(12)——Qt国际化

    应用的国际化就是使应用成为能被非本国的人使用的过程.有的情况下,国际化很简单,例如,使一个US应用可被Australian或者British用户理解,工作可能少于几个拼写修正.但是使一个US应用可以被 ...