【题目分析】

增加了插入和删除。

直接用Splay维护就好辣!

写了一个晚上,(码力不精),最后发现更新写挂了

【代码】

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <cstdlib>
  5.  
  6. #include <map>
  7. #include <set>
  8. #include <queue>
  9. #include <string>
  10. #include <iostream>
  11. #include <algorithm>
  12.  
  13. using namespace std;
  14.  
  15. #define maxn 500005
  16. #define eps 1e-8
  17. #define db double
  18. #define L ch[x][0]
  19. #define R ch[x][1]
  20. #define ll long long
  21. #define inf 0x3f3f3f3f
  22. #define F(i,j,k) for (int i=j;i<=k;++i)
  23. #define D(i,j,k) for (int i=j;i>=k;--i)
  24.  
  25. void Finout()
  26. {
  27. #ifndef ONLINE_JUDGE
  28. freopen("in.txt","r",stdin);
  29. freopen("wa.txt","w",stdout);
  30. #endif
  31. }
  32.  
  33. int Getint()
  34. {
  35. int x=0,f=1; char ch=getchar();
  36. while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}
  37. while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
  38. return x*f;
  39. }
  40.  
  41. int ch[maxn][2],sum[maxn],lx[maxn],rx[maxn],mx[maxn],fa[maxn],siz[maxn],v[maxn];
  42. int a[maxn],n,rt,q,tot;
  43.  
  44. void update(int x)
  45. {
  46. siz[x]=siz[L]+siz[R]+1;
  47. sum[x]=sum[L]+sum[R]+v[x];
  48. mx[x]=lx[x]=rx[x]=v[x];
  49. if (x==1)
  50. {
  51. mx[x]=mx[R];
  52. lx[x]=lx[R];
  53. rx[x]=rx[R];
  54. return ;
  55. }
  56. if (x==n+2)
  57. {
  58. mx[x]=mx[L];
  59. lx[x]=lx[L];
  60. rx[x]=rx[L];
  61. return ;
  62. }
  63. if (((!L))&&((!R))) lx[x]=rx[x]=mx[x]=sum[x]=v[x];
  64. else if ((!L))
  65. {
  66. lx[x]=max(v[x],v[x]+lx[R]);
  67. rx[x]=max(rx[R],sum[R]+v[x]);
  68. mx[x]=max(v[x],max(mx[R],v[x]+lx[R]));
  69. mx[x]=max(mx[x],max(lx[x],rx[x]));
  70. }
  71. else if ((!R))
  72. {
  73. lx[x]=max(lx[L],sum[L]+v[x]);
  74. rx[x]=max(v[x],v[x]+rx[L]);
  75. mx[x]=max(v[x],max(mx[L],rx[L]+v[x]));
  76. mx[x]=max(mx[x],max(lx[x],rx[x]));
  77. }
  78. else
  79. {
  80. lx[x]=max(lx[L],max(sum[L]+v[x],sum[L]+v[x]+lx[R]));
  81. rx[x]=max(rx[R],max(sum[R]+v[x],sum[R]+v[x]+rx[L]));
  82. mx[x]=max(mx[L],max(mx[R],max(rx[L],0)+v[x]+max(lx[R],0)));
  83. mx[x]=max(mx[x],max(lx[x],rx[x]));
  84. }
  85. }
  86.  
  87. void rot(int x,int &k)
  88. {
  89. int y=fa[x],z=fa[y],l,r;
  90. if (ch[y][0]==x) l=0; else l=1; r=l^1;
  91. if (y==k) k=x;
  92. else
  93. {
  94. if (ch[z][0]==y) ch[z][0]=x;
  95. else ch[z][1]=x;
  96. }
  97. fa[x]=z; fa[y]=x; fa[ch[x][r]]=y;
  98. ch[y][l]=ch[x][r];ch[x][r]=y;
  99. update(y); update(x);
  100. }
  101.  
  102. void splay(int x,int &k)
  103. {
  104. while (x!=k)
  105. {
  106. int y=fa[x],z=fa[y];
  107. if (y!=k)
  108. {
  109. if ((ch[z][0]==y)^(ch[y][0]==x)) rot(x,k);
  110. else rot(y,k);
  111. }
  112. rot(x,k);
  113. }
  114. }
  115.  
  116. int find(int x,int f)
  117. {
  118. if (f<=siz[L]) return find(L,f);
  119. else if (f==(siz[L]+1)) return x;
  120. else return find(R,f-siz[L]-1);
  121. }
  122.  
  123. int build(int l,int r,int lst)
  124. {
  125. int mid=l+r>>1;
  126. v[mid]=a[mid]; fa[mid]=lst;
  127. ch[mid][0]=l<mid?build(l,mid-1,mid):0;
  128. ch[mid][1]=r>mid?build(mid+1,r,mid):0;
  129. update(mid);
  130. return mid;
  131. }
  132.  
  133. void debug()
  134. {
  135. printf("The Tree's root is %d\n",rt);
  136. F(i,1,15) printf("Node %d : fa %d L %d R %d lx %d rx %d mx %d sum %d siz %d\n",i,fa[i],ch[i][0],ch[i][1],lx[i],rx[i],mx[i],sum[i],siz[i]);
  137. }
  138.  
  139. int fx,fy;
  140.  
  141. int main()
  142. {
  143. Finout();
  144. n=Getint();
  145. F(i,1,n) a[i+1]=Getint();
  146. rt=build(1,n+2,0); tot=n+2;
  147. // debug();
  148. q=Getint(); char opt[11];int x,y;
  149. F(i,1,q)
  150. {
  151. scanf("%s",opt);
  152. switch(opt[0])
  153. {
  154. case 'Q':
  155. x=Getint();y=Getint();
  156. // printf("%d %d\n",x,y);
  157. fx=find(rt,x),fy=find(rt,y+2);
  158. // printf("%d %d\n",fx,fy);
  159. splay(fx,rt); splay(fy,ch[fx][1]);
  160. printf("%d\n",mx[ch[ch[fx][1]][0]]);
  161. update(fy); update(fx);
  162. break;
  163. case 'I':
  164. ++tot; x=Getint();y=Getint();
  165. v[tot]=y;
  166. fx=find(rt,x),fy=find(rt,x+1);
  167. splay(fx,rt);splay(fy,ch[fx][1]);
  168. fa[tot]=fy; ch[fy][0]=tot;
  169. update(tot); update(fy); update(fx);
  170. break;
  171. case 'R':
  172. x=Getint();y=Getint();
  173. fx=find(rt,x+1); splay(fx,rt);
  174. v[fx]=y;update(fx);
  175. break;
  176. case 'D':
  177. x=Getint();
  178. fx=find(rt,x); fy=find(rt,x+2);
  179. splay(fx,rt); splay(fy,ch[fx][1]);
  180. ch[fy][0]=0; update(fy); update(fx);
  181. break;
  182. }
  183. // debug();
  184. }
  185. }

  

SPOJ GSS6 Can you answer these queries VI ——Splay的更多相关文章

  1. SPOJ GSS6 Can you answer these queries VI

    Can you answer these queries VI Time Limit: 2000ms Memory Limit: 262144KB This problem will be judge ...

  2. SPOJ 4487. Can you answer these queries VI splay

    题目链接:点击打开链接 题意比較明显,不赘述. 删除时能够把i-1转到根,把i+1转到根下 则i点就在 根右子树 的左子树,且仅仅有i这一个 点 #include<stdio.h> #in ...

  3. GSS6 4487. Can you answer these queries VI splay

    GSS6 Can you answer these queries VI 给出一个数列,有以下四种操作: I x y: 在位置x插入y.D x  : 删除位置x上的元素.R x y: 把位置x用y取替 ...

  4. spoj 4487. Can you answer these queries VI (gss6) splay 常数优化

    4487. Can you answer these queries VI Problem code: GSS6 Given a sequence A of N (N <= 100000) in ...

  5. SP4487 GSS6 - Can you answer these queries VI

    题目大意 给出一个由N个整数组成的序列A,你需要应用M个操作: I p x 在 p  处插入插入一个元素 x D p 删除 p 处的一个元素 R p x 修改 p 处元素的值为 x Q l r 查询一 ...

  6. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  7. GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树

    GSS7Can you answer these queries VII 给出一棵树,树的节点有权值,有两种操作: 1.询问节点x,y的路径上最大子段和,可以为空 2.把节点x,y的路径上所有节点的权 ...

  8. [题解] SPOJ GSS1 - Can you answer these queries I

    [题解] SPOJ GSS1 - Can you answer these queries I · 题目大意 要求维护一段长度为 \(n\) 的静态序列的区间最大子段和. 有 \(m\) 次询问,每次 ...

  9. SPOJ 1557. Can you answer these queries II 线段树

    Can you answer these queries II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://www.spoj.com/pr ...

随机推荐

  1. HDU 4274 Spy's Work (树形DP,模拟)

    题意: 给定一棵树,每个节点代表一个员工,节点编号小的级别就小,那么点1就是boss了.接下来给出对m个点的限制,有3种符号分别是op=“大于/小于/等于”,表示以第i个点为根的子树所有人的工资之和  ...

  2. 洛谷 P1438 无聊的数列

    题目背景 无聊的YYB总喜欢搞出一些正常人无法搞出的东西.有一天,无聊的YYB想出了一道无聊的题:无聊的数列...(K峰:这题不是傻X题吗) 题目描述 维护一个数列{a[i]},支持两种操作: 1.1 ...

  3. DRBD+NFS+Keepalived高可用环境

    1.前提条件 准备两台配置相同的服务器 2.安装DRBD [root@server139 ~]# yum -y update kernel kernel-devel [root@server139 ~ ...

  4. winhex与磁盘格式与 数据恢复

    第一阶段: 熟悉WinHex的使用. n 熟悉磁盘工具的使用. n 利用WinHex查看物理磁盘和逻辑磁盘. n 了解WinHex中相关工具的用法. 以管理员身份运行winhex(以便之后修改) 上方 ...

  5. 二、pandas入门

    import numpy as np import pandas as pd Series: #创建Series方法1 s1=pd.Series([1,2,3,4]) s1 # 0 1 # 1 2 # ...

  6. linux环境nginx的安装与使用

    因为公司需要需要安装一系列环境,新手上路第一次配的时候什么也不懂在网上找了半天,觉得这篇不错,我在这里顺便记录一下.(原文:https://www.cnblogs.com/wyd168/p/66365 ...

  7. linux设置http/https proxy及忽略proxy的方法

    msys2设置网络代理 在文件 .bashrc 中添加 export http_proxy="proxy IP:port" 如 export http_proxy="19 ...

  8. ulimit 值超出允许范围导致无法登陆操作系统

    在linux中,使用ulimit可以设置一些资源的使用限制. [root@root ~]# ulimit -a core file size          (blocks, -c) unlimit ...

  9. 628. Maximum Product of Three Numbers@python

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  10. jquery html5 实现placeholder 兼容password ie6

    <style type="text/css"> /* 设置提示文字颜色 */ ::-webkit-input-placeholder { color: #838383; ...