【BZOJ】1500: [NOI2005]维修数列
【算法】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]维修数列的更多相关文章
- bzoj 1500: [NOI2005]维修数列 splay
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 6556 Solved: 1963[Submit][Status ...
- BZOJ 1500: [NOI2005]维修数列 (splay tree)
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 4229 Solved: 1283[Submit][Status ...
- [BZOJ 1500] [NOI2005] 维修数列
题目链接:BZOJ - 1500 题目分析 我要先说一下,这道题我写了一晚上,然后Debug了一整个白天..........再一次被自己的蒟蒻程度震惊= = 这道题是传说中的Splay维护数列的Bos ...
- BZOJ 1500 [NOI2005]维修数列 FHQ Treap
终于A了这题...这题还是很好...但是我太菜...重构了三遍qwq FHQ Treap大法好!qwq...~~ Ins:直接拿输入造一棵树,把原来的树split成[1,pos],[pos+1,n], ...
- 【BZOJ】1500: [NOI2005]维修数列(splay+变态题)
http://www.lydsy.com/JudgeOnline/problem.php?id=1500 模板不打熟你确定考场上调试得出来? 首先有非常多的坑点...我遇到的第一个就是,如何pushu ...
- 1500: [NOI2005]维修数列
Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目.第2行包含N个数字,描述初始时的数列.以下M行,每行一 ...
- bzoj千题计划221:bzoj1500: [NOI2005]维修数列(fhq treap)
http://www.lydsy.com/JudgeOnline/problem.php?id=1500 1.覆盖标记用INF表示无覆盖标记,要求可能用0覆盖 2.代表空节点的0号节点和首尾的两个虚拟 ...
- [NOI2005] 维修数列
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 8397 Solved: 2530 Description In ...
- [BZOJ1500][NOI2005]维修数列---解题报告
Portal Gun:[BZOJ1500][NOI2005]维修数列 有一段时间没写博客了,最近在刚数据结构......各种板子背得简直要起飞,题目也是一大堆做不完,这里就挑一道平衡树的题来写写好了 ...
随机推荐
- jspSmartUpload上传下载使用例子
--------------------------------------------------------------------- ServletUpload.java 上传 package ...
- iOS-根据两个经纬度计算相距距离
CLLocation *orig=[[[CLLocation alloc] initWithLatitude:[mainDelegate.latitude_self doubleValue] long ...
- <Android>tab选项卡
1.继承TabActivity实现 a) 在布局文件中使用FrameLayout列出Tab组件及Tab中的内容组件 b) Activity要继承TabActivity c ...
- 《学习OpenCV》课后习题解答1
题目:(P104) 下面这个练习是帮助掌握矩阵类型.创造一个三通道二维矩阵,字节类型,大小为100*100,并设置所有数值为0. a.在矩阵中使用cvCircle( CvArr* img, CvPoi ...
- 使用协程(gevent)实现请求
协程,又称微线程.英文名Coroutine. 协程最大的优势就是协程极高的执行效率.因为子程序切换不是线程切换,而是由程序自身控制,因此,没有线程切换的开销,和多线程比,线程数量越多,协程的性能优势就 ...
- PAT L1-039 古风排版
https://pintia.cn/problem-sets/994805046380707840/problems/994805091888906240 中国的古人写文字,是从右向左竖向排版的.本题 ...
- [CLR via C#]异常和状态管理
当CLR检测到某个正在运行的.NET应用程序处于一种特殊的正常执行顺序被打断的状态时,会生成一个异常对象来表示这个错误,并将此对象在方法调用堆栈中向上传送.如果一个程序引发了一个异常却没有处理,CLR ...
- 刚装的vs无法运行正确的程序
- Jdk1.7 与 jdk1.8的区别,最新的特征有哪些(美团,360,京东面试题目)
在jdk7的新特性方面主要有下面几方面的增强: 1.1二进制变量的表示,支持将整数类型用二进制来表示,用0b开头. 所有整数int.short.long.byte都可以用二进制表示: byte aBy ...
- 请问:在delphi中怎样判断DBgrid中数据是否被修改,以便在退出窗口时加以提示
若DBGrid.DataSource.DateSet为ADOQuery1,这样试一下:if ADOQuery1.Modified then ... procedure TForm1.FormClose ...