【算法】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。

最新文章

  1. MYSQL远程登录权限设置
  2. Programming Assignment 3: Collinear Points
  3. 推荐一个国外的vps
  4. AVAudioPlayer 播放音频
  5. m元素集合的n个元素子集
  6. [原创] 小而美 | Mac上鲜为人知,但极大提升效率的小工具
  7. Ansible入门篇:playbook的使用
  8. [Swift]LeetCode81. 搜索旋转排序数组 II | Search in Rotated Sorted Array II
  9. ESP8266清理flash学习记录
  10. Visual Studio 2012 与此版本的 Windows 不兼容。有关详细信息,请联系 Microsoft
  11. Vue——服务器上部署vue.js
  12. 关于一个常用的CheckBox样式
  13. HDFS体系结构(NameNode、DataNode详解)
  14. ambari 2.6.2 安装 hdp 2.6.5.0 遇到的问题
  15. c++刷题(12/100)无序数组中和为定值的最长子数组
  16. element-ui : &lt;el-table&gt; 按钮点击操作阻止@row-click
  17. UI/GUI/UE/UX/ID/UED/UCD的区别
  18. 百度图片API
  19. DataType--类型基础
  20. SrpingCloud 之SrpingCloud config分布式配置中心实时刷新

热门文章

  1. 2019寒假训练营第三次作业part2 - 实验题
  2. Java 二维数组
  3. ACM 第三天
  4. 3ds max启动慢怎么办?
  5. 201621123033 《Java程序设计》第5周学习总结
  6. SpringData——HelloWorld
  7. Java多线程同步机制之同步块(方法)——synchronized
  8. js 关键字 in 判断 一个属性或方法是否属于一个对象
  9. 【.Net】C# 反编译工具之dnSpy
  10. 【bzoj3297】[USACO2011 Open]forgot STL+dp