【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]维修数列 有一段时间没写博客了,最近在刚数据结构......各种板子背得简直要起飞,题目也是一大堆做不完,这里就挑一道平衡树的题来写写好了 ...
随机推荐
- Alpha冲刺——第一天
Alpha第一天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...
- DAY4敏捷冲刺
站立式会议 工作安排 (1)服务器配置 已完成对微信小程序登录凭证储存至云端数据库,计划使用微信接口返回的session_id进行转化返回本地,以保持登录态. (2)数据库配置 单词学习记录+用户信息 ...
- java.lang.ClassNotFoundException: com.google.gson.Gson 问题解决
我是这么解决:把gson.jar放到WEB-INF/lib目录下. 放在其他目录就会报错.
- DAY1敏捷冲刺
站立式会议 工作安排 (1)服务器配置 (2)数据库建表 (3)页面初步样式设计 (4)主要页面之间的交互 燃尽图 代码提交记录 感想 林一心:后端云服务器的配置确实是一个挑战,目前还在摸索中 赵意: ...
- 解决chrome css本地映射不成功&&附带映射方法
解决办法:把本地文件夹名改成英文的(不要有中文) 顺便写一下怎么把在chrome调试的本地项目中的css映射到本地: 1.F12(option+command+i)启动chrome调试工具 2.打开s ...
- 三次握手 四次握手 与socket函数的关系
“一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览 ...
- js 事件阻止传播方法,准确定位事件源
1事件冒泡 在目标元素获得机会处理事件后,事件模型检查目标元素的父元素,看是否为同类型事件建立了处理程序.如果是,则也调用父元素的处理程序.在这之后,再检查其父元素,然后父元素,然后父元素...持续不 ...
- 【ADO.NET】ADO.NET知识点
ADO.NET 是一组向 .NET 程序员公开数据访问服务的类.提供了对各种关系数据.XML 和应用程序数据的访问. 所有的数据访问类位于System.Data.dll中.System.Data包含了 ...
- springBoot @Enable*注解的工作原理
使用注解实现异步 RunnableDemo类 package com.boot.enable.bootenable; import org.springframework.scheduling.ann ...
- Python十六进制转码问题
使用Python的decode函数转码十六进制的字符串时,会出现UnicodeDecodeError: 'utf8' codec can't decode byte 0xba in position ...