SPOJ GSS6 Can you answer these queries VI ——Splay
【题目分析】
增加了插入和删除。
直接用Splay维护就好辣!
写了一个晚上,(码力不精),最后发现更新写挂了
【代码】
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- #include <cstdlib>
- #include <map>
- #include <set>
- #include <queue>
- #include <string>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- #define maxn 500005
- #define eps 1e-8
- #define db double
- #define L ch[x][0]
- #define R ch[x][1]
- #define ll long long
- #define inf 0x3f3f3f3f
- #define F(i,j,k) for (int i=j;i<=k;++i)
- #define D(i,j,k) for (int i=j;i>=k;--i)
- void Finout()
- {
- #ifndef ONLINE_JUDGE
- freopen("in.txt","r",stdin);
- freopen("wa.txt","w",stdout);
- #endif
- }
- int Getint()
- {
- int x=0,f=1; char ch=getchar();
- while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}
- while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
- return x*f;
- }
- int ch[maxn][2],sum[maxn],lx[maxn],rx[maxn],mx[maxn],fa[maxn],siz[maxn],v[maxn];
- int a[maxn],n,rt,q,tot;
- void update(int x)
- {
- siz[x]=siz[L]+siz[R]+1;
- sum[x]=sum[L]+sum[R]+v[x];
- mx[x]=lx[x]=rx[x]=v[x];
- if (x==1)
- {
- mx[x]=mx[R];
- lx[x]=lx[R];
- rx[x]=rx[R];
- return ;
- }
- if (x==n+2)
- {
- mx[x]=mx[L];
- lx[x]=lx[L];
- rx[x]=rx[L];
- return ;
- }
- if (((!L))&&((!R))) lx[x]=rx[x]=mx[x]=sum[x]=v[x];
- else if ((!L))
- {
- lx[x]=max(v[x],v[x]+lx[R]);
- rx[x]=max(rx[R],sum[R]+v[x]);
- mx[x]=max(v[x],max(mx[R],v[x]+lx[R]));
- mx[x]=max(mx[x],max(lx[x],rx[x]));
- }
- else if ((!R))
- {
- lx[x]=max(lx[L],sum[L]+v[x]);
- rx[x]=max(v[x],v[x]+rx[L]);
- mx[x]=max(v[x],max(mx[L],rx[L]+v[x]));
- mx[x]=max(mx[x],max(lx[x],rx[x]));
- }
- else
- {
- lx[x]=max(lx[L],max(sum[L]+v[x],sum[L]+v[x]+lx[R]));
- rx[x]=max(rx[R],max(sum[R]+v[x],sum[R]+v[x]+rx[L]));
- mx[x]=max(mx[L],max(mx[R],max(rx[L],0)+v[x]+max(lx[R],0)));
- mx[x]=max(mx[x],max(lx[x],rx[x]));
- }
- }
- void rot(int x,int &k)
- {
- int y=fa[x],z=fa[y],l,r;
- if (ch[y][0]==x) l=0; else l=1; r=l^1;
- if (y==k) k=x;
- else
- {
- if (ch[z][0]==y) ch[z][0]=x;
- else ch[z][1]=x;
- }
- fa[x]=z; fa[y]=x; fa[ch[x][r]]=y;
- ch[y][l]=ch[x][r];ch[x][r]=y;
- update(y); update(x);
- }
- void splay(int x,int &k)
- {
- while (x!=k)
- {
- int y=fa[x],z=fa[y];
- if (y!=k)
- {
- if ((ch[z][0]==y)^(ch[y][0]==x)) rot(x,k);
- else rot(y,k);
- }
- rot(x,k);
- }
- }
- int find(int x,int f)
- {
- if (f<=siz[L]) return find(L,f);
- else if (f==(siz[L]+1)) return x;
- else return find(R,f-siz[L]-1);
- }
- int build(int l,int r,int lst)
- {
- int mid=l+r>>1;
- v[mid]=a[mid]; fa[mid]=lst;
- ch[mid][0]=l<mid?build(l,mid-1,mid):0;
- ch[mid][1]=r>mid?build(mid+1,r,mid):0;
- update(mid);
- return mid;
- }
- void debug()
- {
- printf("The Tree's root is %d\n",rt);
- 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]);
- }
- int fx,fy;
- int main()
- {
- Finout();
- n=Getint();
- F(i,1,n) a[i+1]=Getint();
- rt=build(1,n+2,0); tot=n+2;
- // debug();
- q=Getint(); char opt[11];int x,y;
- F(i,1,q)
- {
- scanf("%s",opt);
- switch(opt[0])
- {
- case 'Q':
- x=Getint();y=Getint();
- // printf("%d %d\n",x,y);
- fx=find(rt,x),fy=find(rt,y+2);
- // printf("%d %d\n",fx,fy);
- splay(fx,rt); splay(fy,ch[fx][1]);
- printf("%d\n",mx[ch[ch[fx][1]][0]]);
- update(fy); update(fx);
- break;
- case 'I':
- ++tot; x=Getint();y=Getint();
- v[tot]=y;
- fx=find(rt,x),fy=find(rt,x+1);
- splay(fx,rt);splay(fy,ch[fx][1]);
- fa[tot]=fy; ch[fy][0]=tot;
- update(tot); update(fy); update(fx);
- break;
- case 'R':
- x=Getint();y=Getint();
- fx=find(rt,x+1); splay(fx,rt);
- v[fx]=y;update(fx);
- break;
- case 'D':
- x=Getint();
- fx=find(rt,x); fy=find(rt,x+2);
- splay(fx,rt); splay(fy,ch[fx][1]);
- ch[fy][0]=0; update(fy); update(fx);
- break;
- }
- // debug();
- }
- }
SPOJ GSS6 Can you answer these queries VI ——Splay的更多相关文章
- 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 ...
- SPOJ 4487. Can you answer these queries VI splay
题目链接:点击打开链接 题意比較明显,不赘述. 删除时能够把i-1转到根,把i+1转到根下 则i点就在 根右子树 的左子树,且仅仅有i这一个 点 #include<stdio.h> #in ...
- 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取替 ...
- 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 ...
- 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 查询一 ...
- 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 ...
- GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树
GSS7Can you answer these queries VII 给出一棵树,树的节点有权值,有两种操作: 1.询问节点x,y的路径上最大子段和,可以为空 2.把节点x,y的路径上所有节点的权 ...
- [题解] SPOJ GSS1 - Can you answer these queries I
[题解] SPOJ GSS1 - Can you answer these queries I · 题目大意 要求维护一段长度为 \(n\) 的静态序列的区间最大子段和. 有 \(m\) 次询问,每次 ...
- 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 ...
随机推荐
- HDU 4274 Spy's Work (树形DP,模拟)
题意: 给定一棵树,每个节点代表一个员工,节点编号小的级别就小,那么点1就是boss了.接下来给出对m个点的限制,有3种符号分别是op=“大于/小于/等于”,表示以第i个点为根的子树所有人的工资之和 ...
- 洛谷 P1438 无聊的数列
题目背景 无聊的YYB总喜欢搞出一些正常人无法搞出的东西.有一天,无聊的YYB想出了一道无聊的题:无聊的数列...(K峰:这题不是傻X题吗) 题目描述 维护一个数列{a[i]},支持两种操作: 1.1 ...
- DRBD+NFS+Keepalived高可用环境
1.前提条件 准备两台配置相同的服务器 2.安装DRBD [root@server139 ~]# yum -y update kernel kernel-devel [root@server139 ~ ...
- winhex与磁盘格式与 数据恢复
第一阶段: 熟悉WinHex的使用. n 熟悉磁盘工具的使用. n 利用WinHex查看物理磁盘和逻辑磁盘. n 了解WinHex中相关工具的用法. 以管理员身份运行winhex(以便之后修改) 上方 ...
- 二、pandas入门
import numpy as np import pandas as pd Series: #创建Series方法1 s1=pd.Series([1,2,3,4]) s1 # 0 1 # 1 2 # ...
- linux环境nginx的安装与使用
因为公司需要需要安装一系列环境,新手上路第一次配的时候什么也不懂在网上找了半天,觉得这篇不错,我在这里顺便记录一下.(原文:https://www.cnblogs.com/wyd168/p/66365 ...
- linux设置http/https proxy及忽略proxy的方法
msys2设置网络代理 在文件 .bashrc 中添加 export http_proxy="proxy IP:port" 如 export http_proxy="19 ...
- ulimit 值超出允许范围导致无法登陆操作系统
在linux中,使用ulimit可以设置一些资源的使用限制. [root@root ~]# ulimit -a core file size (blocks, -c) unlimit ...
- 628. Maximum Product of Three Numbers@python
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- jquery html5 实现placeholder 兼容password ie6
<style type="text/css"> /* 设置提示文字颜色 */ ::-webkit-input-placeholder { color: #838383; ...