GSS6 Can you answer these queries VI

给出一个数列,有以下四种操作:

I x y: 在位置x插入y。
D x  : 删除位置x上的元素。
R x y: 把位置x用y取替。
Q x y: 输出区间[x,y]的最大字段和。

分析:

其实这题是BZOJ 1500 [NOI2005]维修数列这题的简化版。

使用splay来做非常简单。

我的做法增加一个虚拟节点在数列的最开始处,增加两个虚拟节点在最后,这是为了方便在数列最后插入的操作。

splay网上的资料比较多,其实splay比sbt、avl都简单。这里有我的一份总结,并且有一些资料: splay总结

囧,我写的splay太慢了,最后上网搜了一个IO外挂才过了。

  1. #include <set>
  2. #include <map>
  3. #include <list>
  4. #include <cmath>
  5. #include <queue>
  6. #include <stack>
  7. #include <string>
  8. #include <vector>
  9. #include <cstdio>
  10. #include <cstring>
  11. #include <iostream>
  12. #include <algorithm>
  13.  
  14. using namespace std;
  15.  
  16. typedef long long ll;
  17. typedef unsigned long long ull;
  18.  
  19. #define debug puts("here")
  20. #define rep(i,n) for(int i=0;i<n;i++)
  21. #define rep1(i,n) for(int i=1;i<=n;i++)
  22. #define REP(i,a,b) for(int i=a;i<=b;i++)
  23. #define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
  24. #define pb push_back
  25. #define RD(n) scanf("%d",&n)
  26. #define RD2(x,y) scanf("%d%d",&x,&y)
  27. #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
  28. #define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
  29. #define All(vec) vec.begin(),vec.end()
  30. #define MP make_pair
  31. #define PII pair<int,int>
  32. #define PQ priority_queue
  33. #define cmax(x,y) x = max(x,y)
  34. #define cmin(x,y) x = min(x,y)
  35. #define Clear(x) memset(x,0,sizeof(x))
  36. /*
  37.  
  38. #pragma comment(linker, "/STACK:1024000000,1024000000")
  39.  
  40. int size = 256 << 20; // 256MB
  41. char *p = (char*)malloc(size) + size;
  42. __asm__("movl %0, %%esp\n" :: "r"(p) );
  43.  
  44. */
  45.  
  46. /******** program ********************/
  47.  
  48. const int MAXN = 210005;
  49. const int INF = 1e9;
  50.  
  51. int ch[MAXN][2],fa[MAXN],sz[MAXN],root,tot;
  52. int sum[MAXN],val[MAXN];
  53. int lmax[MAXN],rmax[MAXN],mmax[MAXN];
  54. int a[MAXN];
  55.  
  56. #define lx ch[x][0]
  57. #define rx ch[x][1]
  58. #define px fa[x]
  59.  
  60. #define ly ch[y][0]
  61. #define ry ch[y][1]
  62. #define py fa[y]
  63.  
  64. #define lz ch[z][0]
  65. #define rz ch[z][1]
  66. #define pz fa[z]
  67.  
  68. #define rt ch[root][1]
  69. #define lrt ch[rt][0]
  70. #define rrt ch[rt][1]
  71.  
  72. // 调试
  73. void dfs(int x){
  74. if(x){
  75. int a = ch[x][0];
  76. int b = ch[x][1];
  77. dfs(a);
  78. printf("x: %3d %3d %3d ",x,a,b);
  79. printf("val: %3d\n",val[x]);
  80. dfs(b);
  81. }
  82. }
  83.  
  84. void gdb(){
  85. puts("\n---------------------");
  86. dfs(root);
  87. puts("---------------------\n");
  88. }
  89.  
  90. inline void update(int x){
  91. if(!x)return;
  92. sz[x] = sz[lx]+sz[rx]+1;
  93. sum[x] = sum[lx]+sum[rx]+val[x];
  94. lmax[x] = max( lmax[lx] , sum[lx]+val[x]+max(0,lmax[rx]) );
  95. rmax[x] = max( rmax[rx] , sum[rx]+val[x]+max(0,rmax[lx]) );
  96. mmax[x] = max( mmax[lx] , mmax[rx] );
  97. mmax[x] = max( mmax[x] , max(0,lmax[rx])+val[x]+max(0,rmax[lx]) );
  98. }
  99.  
  100. inline int sgn(int x){
  101. return ch[px][1]==x;
  102. }
  103.  
  104. inline void setc(int y,int d,int x){
  105. ch[y][d] = x;
  106. px = y;
  107. }
  108.  
  109. inline void rot(int x,int d){
  110. int y = px;
  111. int z = py;
  112. setc(y,!d,ch[x][d]);
  113. if(z) setc(z,sgn(y),x);
  114. fa[x] = z;
  115. setc(x,d,y);
  116. update(y);
  117. }
  118.  
  119. inline void splay(int x,int goal=0){
  120. if(!x)return;
  121. while(px!=goal){
  122. int y = px;
  123. int z = py;
  124. if(z==goal){
  125. rot(x,!sgn(x));
  126. break;
  127. }
  128. if(lz==y){
  129. if(ly==x)
  130. rot(y,1),rot(x,1);
  131. else
  132. rot(x,0),rot(x,1);
  133. }
  134. else{
  135. if(ry==x)
  136. rot(y,0),rot(x,0);
  137. else
  138. rot(x,1),rot(x,0);
  139. }
  140. }
  141. update(x);
  142. if(goal==0)
  143. root = x;
  144. }
  145.  
  146. inline void newNode(int &x,int y,int v){
  147. x = ++ tot;
  148. sz[x] = 1;
  149. ch[x][0] = ch[x][1] = 0;
  150. val[x] = sum[x] = lmax[x] = rmax[x] = mmax[x] = v;
  151. fa[x] = y;
  152. }
  153.  
  154. inline void build(int &x,int y,int l,int r){
  155. if(l>r) return;
  156. int mid = (l+r)>>1;
  157. newNode(x,y,a[mid]);
  158. build(lx,x,l,mid-1);
  159. build(rx,x,mid+1,r);
  160. update(x);
  161. }
  162.  
  163. inline int getKth(int x,int k){
  164. while( sz[lx]+1 != k ){
  165. if(k<=sz[lx])x = lx;
  166. else{
  167. k -= sz[lx]+1;
  168. x = rx;
  169. }
  170. }
  171. return x;
  172. }
  173.  
  174. inline void splay(int x,int y,int ok){
  175. splay( getKth(root,x) );
  176. splay( getKth(root,y) , root );
  177. }
  178.  
  179. inline void Int(int &num){
  180. char in;
  181. bool neg=false;
  182. while(((in=getchar()) > '9' || in<'0') && in!='-') ;
  183. if(in=='-'){
  184. neg=true;
  185. while((in=getchar()) >'9' || in<'0');
  186. }
  187. num=in-'0';
  188. while(in=getchar(),in>='0'&&in<='9')
  189. num*=10,num+=in-'0';
  190. if(neg)
  191. num=0-num;
  192. }
  193.  
  194. char Char() {
  195. char res;
  196. while (res = getchar(), !isalpha(res));
  197. return res;
  198. }
  199.  
  200. char s[MAXN<<2];
  201. int cur;
  202.  
  203. inline int Int(){
  204. int ans = 0;
  205. bool ok = 0 , f = 0;
  206. for(;s[cur];cur++){
  207. if(s[cur]==' '){
  208. if(f)break;
  209. continue;
  210. }f = true;
  211. if(s[cur]=='-')ok = true;
  212. else ans = ans*10+s[cur]-'0';
  213. }
  214. if(ok)ans = -ans;
  215. return ans;
  216. }
  217.  
  218. int main(){
  219.  
  220. #ifndef ONLINE_JUDGE
  221. freopen("sum.in","r",stdin);
  222. //freopen("sum.out","w",stdout);
  223. #endif
  224.  
  225. int n,m,x,y;
  226. char op;
  227.  
  228. RD(n);
  229. rep1(i,n)
  230. Int(a[i]);
  231. lmax[0] = mmax[0] = rmax[0] = -INF;
  232.  
  233. a[++n] = INF; // 补一个虚拟节点,方便在最后插入
  234.  
  235. newNode(root,0,-INF);
  236. newNode(ch[root][1],root,INF);
  237.  
  238. update(rt);
  239. update(root);
  240.  
  241. build(lrt,rt,1,n);
  242. update(rt);
  243. update(root);
  244.  
  245. RD(m);
  246. while(m--){
  247. op = Char();
  248. Int(x);
  249. if(op=='I'){
  250. Int(y);
  251. splay(x,x+1,0);
  252. newNode(lrt,rt,y);
  253. update(rt);
  254. update(root);
  255. }else if(op=='D'){
  256. splay(x,x+2,0);
  257. fa[lrt] = 0;
  258. lrt = 0;
  259. }else if(op=='R'){
  260. Int(y);
  261. splay(x,x+2,0);
  262. fa[lrt] = 0;
  263. lrt = 0;
  264. newNode(lrt,rt,y);
  265. }else{
  266. Int(y);
  267. splay(x,y+2,0);
  268. printf("%d\n",mmax[lrt]);
  269. }
  270. }
  271.  
  272. return 0;
  273. }

  

GSS6 4487. Can you answer these queries VI splay的更多相关文章

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

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

  2. 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 ...

  3. SPOJ GSS6 Can you answer these queries VI ——Splay

    [题目分析] 增加了插入和删除. 直接用Splay维护就好辣! 写了一个晚上,(码力不精),最后发现更新写挂了 [代码] #include <cstdio> #include <cs ...

  4. 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 ...

  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. kuangbin专题七 HDU4027 Can you answer these queries? (线段树)

    A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use ...

  7. 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 ...

  8. hdu 4027 Can you answer these queries?

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4027 Can you answer these queries? Description Proble ...

  9. GSS4 2713. Can you answer these queries IV 线段树

    GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...

随机推荐

  1. [转][IIS]发布网站,提示用户 'IIS APPPOOL\***' 登录失败。

    链接:http://www.cnblogs.com/tianguook/p/3881075.html 用户 'IIS APPPOOL\DefaultAppPool' 登录失败. 我在windows8中 ...

  2. <创建和销毁对象>经验法则——考虑用静态工厂方法代替公有构造方法

    一.引出静态工厂方法 对于java类而言,为了让使用者获取它自身的一个实例化对象,会有以下方法: 1.该类提供一个公有的构造方法.在这种情况下,程序可以通过多个“new 构造方法”语句来创建类的任意多 ...

  3. Chrome的JS调试工具

    你是怎么调试 JavaScript 程序的?最原始的方法是用 alert() 在页面上打印内容,稍微改进一点的方法是用 console.log() 在 JavaScript 控制台上输出内容.嗯~,用 ...

  4. 译 - EF 6秘诀(第二版) - 目录

    本博文系Entity Framework 6 Recipes, 2nd Edition的目录译文.保留原文,方便参考. 第一章  EF入门Chapter 1. Getting Started with ...

  5. Python抓取页面中超链接(URL)的三中方法比较(HTMLParser、pyquery、正则表达式) <转>

    Python抓取页面中超链接(URL)的3中方法比较(HTMLParser.pyquery.正则表达式) HTMLParser版: #!/usr/bin/python # -*- coding: UT ...

  6. linux 硬链接和软链接(转)

    1.Linux链接概念Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link)。默认情况下,ln命令产生硬链接。 【硬连接】硬连接指通过索引节点 ...

  7. Codeforces Gym 100531D Digits 暴力

    Problem D. Digits 题目连接: http://codeforces.com/gym/100531/attachments Description Little Petya likes ...

  8. 【JavaScript】关于javascript原型的深入理解

    http://mozilla.com.cn/post/21667/ http://liuzhijun.iteye.com/blog/1157453 http://liuzhijun.iteye.com ...

  9. apache2.2 虚拟主机配置

    一.改动httpd.conf 打开appserv的安装文件夹,找到httpd.conf文件,分别去掉以下两行文字前面的#号. #LoadModule vhost_alias_module module ...

  10. AutoCompleteTextView输入汉字拼音首字母实现过滤提示(支持多音字,Filterable的使用)

    AutoCompleteTextView具有输入提示的功能,但是它的这种提示不适合对股票列表的过滤,如果你玩过股票软件,就会知道只要输入股票名称的首字母或股票代码就会出现符合匹配的股票,这种过滤怎么实 ...