【算法】splay

【题解】数据结构

感谢Occult的模板>_<:HYSBZ 1500 维修数列

#include<cstdio>
#include<cctype>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=5e5+,inf=0x3f3f3f3f;
int n,m,root,a[maxn],t[maxn][],f[maxn],A[maxn],L[maxn],R[maxn],M[maxn],sum[maxn],en[maxn],g[maxn],s[maxn];
//关系类:f父亲 t儿子 | 数值类:num值 L左起最大子段和 R右起最大子段和 M最大子段和 sum和 s结点数 | 标记类:en覆盖标记 g翻转标记
queue<int>q;
int read()
{
char c;int s=,t=;
while(!isdigit(c=getchar()))if(c=='-')t=-;
do{s=s*+c-'';}while(isdigit(c=getchar()));
return s*t;
}
int Node(int fa,int num)
{
int sz=q.front();q.pop();
t[sz][]=t[sz][]=en[sz]=g[sz]=;
s[sz]=;f[sz]=fa;A[sz]=L[sz]=R[sz]=M[sz]=sum[sz]=num;
return sz;
}
void count(int x)//与线段树更新不同,记得加自身
{
L[x]=max(L[t[x][]],sum[t[x][]]+A[x]+max(,L[t[x][]]));
R[x]=max(R[t[x][]],sum[t[x][]]+A[x]+max(,R[t[x][]]));
M[x]=max(,R[t[x][]])+A[x]+max(,L[t[x][]]);
M[x]=max(M[x],max(M[t[x][]],M[t[x][]]));
sum[x]=sum[t[x][]]+A[x]+sum[t[x][]];
s[x]=s[t[x][]]++s[t[x][]];
}
void pushdown(int x)
{
if(g[x])
{
g[t[x][]]^=;g[t[x][]]^=;
swap(L[t[x][]],R[t[x][]]);
swap(L[t[x][]],R[t[x][]]);
swap(t[x][],t[x][]);
g[x]=;
}
if(en[x])
{
if(t[x][])
{
en[t[x][]]=;
A[t[x][]]=A[x];
sum[t[x][]]=A[x]*s[t[x][]];
L[t[x][]]=R[t[x][]]=M[t[x][]]=A[x]>?sum[t[x][]]:A[x];
}
if(t[x][])
{
en[t[x][]]=;
A[t[x][]]=A[x];
sum[t[x][]]=A[x]*s[t[x][]];
L[t[x][]]=R[t[x][]]=M[t[x][]]=A[x]>?sum[t[x][]]:A[x];
}
en[x]=;
}
}
void rotate(int x)
{
int k=x==t[f[x]][];
int y=f[x];
t[y][k]=t[x][!k];f[t[x][!k]]=y;
if(f[y])t[f[y]][y==t[f[y]][]]=x;f[x]=f[y];f[y]=x;
t[x][!k]=y;
L[x]=L[y];R[x]=R[y];M[x]=M[y];sum[x]=sum[y];s[x]=s[y];
count(y);
}
void splay(int x,int r)
{
for(int fa=f[r];f[x]!=fa;)//因为y被旋走了,所以要判断f[y]
{
if(f[f[x]]==fa){rotate(x);break;}
int X=x==t[f[x]][],Y=f[x]==t[f[f[x]]][];//等号别打少……
if(X^Y)rotate(x),rotate(x);
else rotate(f[x]),rotate(x);
}
}
void find(int &x,int k)
{
for(int i=x;i;)
{
pushdown(i);
if(k<=s[t[i][]]){i=t[i][];continue;}
if(k==s[t[i][]]+){splay(i,x);x=i;break;}
k-=s[t[i][]]+;i=t[i][];//else不安全。。。
}
}
void build(int fa,int &x,int l,int r)
{
if(l>r)return;
int mid=(l+r)>>;
x=Node(fa,a[mid]);
build(x,t[x][],l,mid-);
build(x,t[x][],mid+,r);
count(x);//
}
void insert()
{
int l=read(),k=read();
for(int i=;i<=k;i++)a[i]=read();
int r;build(,r,,k);
find(root,l+);find(t[root][],);
t[t[root][]][]=r;f[r]=t[root][];
count(t[root][]);count(root);//记得count
}
void erase(int x)
{
if(!x)return;
q.push(x);
erase(t[x][]);
erase(t[x][]);
}
void del()
{
int l=read(),k=read();
find(root,l);find(t[root][],k+);
erase(t[t[root][]][]);
t[t[root][]][]=;
count(t[root][]);count(root);//
}
void cover()
{
int l=read(),k=read(),num=read();
find(root,l);find(t[root][],k+);
int y=t[t[root][]][];
en[y]=;A[y]=num;
sum[y]=s[y]*A[y];
L[y]=R[y]=M[y]=A[y]>?sum[y]:A[y];
count(t[root][]);count(root);//
}
void round()
{
int l=read(),k=read();
find(root,l);find(t[root][],k+);
int y=t[t[root][]][];
g[y]^=;
swap(L[y],R[y]);
count(t[root][]);count(root);//
}
void getsum()
{
int l=read(),k=read();
find(root,l);find(t[root][],k+);
printf("%d\n",sum[t[t[root][]][]]);
}
void getM()
{
int sz=s[root];
find(root,);
find(t[root][],sz-);
printf("%d\n",M[t[t[root][]][]]);
}
char str[];
int main()
{
n=read();m=read();
a[]=a[n+]=root=;
for(int i=;i<=maxn;i++)q.push(i);
L[]=R[]=M[]=-inf;//使空结点干扰判断,过程中需保证任何时刻都不应修改到该点的值。
for(int i=;i<=n;i++)a[i]=read();
build(,root,,n+);
for(int i=;i<=m;i++)
{
scanf("%s",str);
if(str[]=='S')insert();
if(str[]=='L')del();
if(str[]=='K')cover();
if(str[]=='V')round();
if(str[]=='T')getsum();
if(str[]=='X')getM();
}
return ;
}

update:现在已改用fhq-treap代替splay。

【BZOJ】1500: [NOI2005]维修数列的更多相关文章

  1. bzoj 1500: [NOI2005]维修数列 splay

    1500: [NOI2005]维修数列 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 6556  Solved: 1963[Submit][Status ...

  2. BZOJ 1500: [NOI2005]维修数列 (splay tree)

    1500: [NOI2005]维修数列 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 4229  Solved: 1283[Submit][Status ...

  3. [BZOJ 1500] [NOI2005] 维修数列

    题目链接:BZOJ - 1500 题目分析 我要先说一下,这道题我写了一晚上,然后Debug了一整个白天..........再一次被自己的蒟蒻程度震惊= = 这道题是传说中的Splay维护数列的Bos ...

  4. BZOJ 1500 [NOI2005]维修数列 FHQ Treap

    终于A了这题...这题还是很好...但是我太菜...重构了三遍qwq FHQ Treap大法好!qwq...~~ Ins:直接拿输入造一棵树,把原来的树split成[1,pos],[pos+1,n], ...

  5. 【BZOJ】1500: [NOI2005]维修数列(splay+变态题)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1500 模板不打熟你确定考场上调试得出来? 首先有非常多的坑点...我遇到的第一个就是,如何pushu ...

  6. 1500: [NOI2005]维修数列

    Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目.第2行包含N个数字,描述初始时的数列.以下M行,每行一 ...

  7. bzoj千题计划221:bzoj1500: [NOI2005]维修数列(fhq treap)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1500 1.覆盖标记用INF表示无覆盖标记,要求可能用0覆盖 2.代表空节点的0号节点和首尾的两个虚拟 ...

  8. [NOI2005] 维修数列

    1500: [NOI2005]维修数列 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 8397  Solved: 2530 Description In ...

  9. [BZOJ1500][NOI2005]维修数列---解题报告

    Portal Gun:[BZOJ1500][NOI2005]维修数列 有一段时间没写博客了,最近在刚数据结构......各种板子背得简直要起飞,题目也是一大堆做不完,这里就挑一道平衡树的题来写写好了 ...

随机推荐

  1. 理解BitSet

    先来看几道面试题: 1.统计40亿个数据中没有出现的数据,将40亿个不同数据进行排序. 2.现在有1千万个随机数,随机数的范围在1到1亿之间,要求写出一种算法,将1到1亿之间没有在随机数中的数求出来. ...

  2. IIS部署网部常用问题汇总

    1.unrecognized attribute 'targetframework' A: 需要注册.net framework到iis.步骤如下: (1)'Start' -> 'CMD' (2 ...

  3. How To Disable MacBook ProTrackpad

    How To Disable MacBook Pro Trackpad how to close macbook pro touchpad? https://www.wikihow.com/Chang ...

  4. TCP/IP协议与OSI协议

    OSI协议是一个理想化的协议,它把网络传输过程分为七层模型,以达到形象化的理解的效果,在实际应用中没有被使用.TCP/IP协议可以看作是它的简化版,是目前应用最广泛的网络协议,许多协议都是以它为基础而 ...

  5. input 元素 相对父元素错位

    <div class="recommend"> <i class="iconfont icon-user"></i> < ...

  6. C#操作access练习

    ORM框架使用dapper,dapper不仅能操作sqlserver,也能操作access,下面为基本代码: OleDbConnection strConnection = new OleDbConn ...

  7. 关于__name__=='__main__

    if __name__=='__main__' :  为了区分你是主动执行这个脚本,还是从别的地方把它当做一个模块去调用. 如果是主动执行,则执行.如果是调用的,则不执行主体. 里面存放的可能是一些测 ...

  8. AtCoder Regular Contest 103 题解

    C-/\/\/\ #include<algorithm> #include<iostream> #include<cstdlib> #include<ioma ...

  9. [洛谷P5205]【模板】多项式开根

    题目大意:给你$n$项多项式$A(x)$,求出$B(x)$满足$B^2(x)\equiv A(x)\pmod{x^n}$ 题解:考虑已经求出$B_0(x)$满足$B_0^2(x)\equiv A(x) ...

  10. BZOJ5319 & 洛谷4559 & LOJ2551:[JSOI2018]军训列队——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=5319 https://www.luogu.org/problemnew/show/P4559 ht ...