[BJOI2017]喷式水战改
XXXVI.[BJOI2017]喷式水战改
这题类似于毒瘤数据结构题,想起来非常简单,但是写起来……
平衡树是必须写的——这种毒瘤的维护肯定要写平衡树。
然后说一下怎么DP吧。在每个节点上维护\(f[i][j]\),表示在以该节点为根的子树上,阶段\(i\)到阶段\(j\)的最大收益。
直接在pushup时维护即可。
主要是这个插入难,要分裂某个节点。
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define lson t[x].ch[0]
#define rson t[x].ch[1]
#define ROOT t[0].ch[1]
int n,cnt;
struct SPLAY{
int num[4],tot,sz,fa,ch[2],f[4][4];
SPLAY(int a=0,int b=0,int c=0,int d=0){
num[0]=a,num[1]=b,num[2]=c,num[3]=a,tot=sz=d;
fa=ch[0]=ch[1]=0;
memset(f,0,sizeof(f));
for(int i=0;i<4;i++)for(int j=i;j<4;j++)for(int k=i;k<=j;k++)f[i][j]=max(f[i][j],num[k]*tot);
}
}t[200100];
void pushup(int x){
if(!x)return;
int g[4][4];
memset(t[x].f,0,sizeof(t[x].f));
t[x].sz=t[x].tot;
for(int i=0;i<4;i++)for(int j=i;j<4;j++)for(int k=i;k<=j;k++)t[x].f[i][j]=max(t[x].f[i][j],t[x].num[k]*t[x].tot);
if(lson){
memset(g,0,sizeof(g));
for(int i=0;i<4;i++)for(int j=i;j<4;j++)for(int k=i;k<=j;k++)g[i][j]=max(g[i][j],t[lson].f[i][k]+t[x].f[k][j]);
for(int i=0;i<4;i++)for(int j=i;j<4;j++)t[x].f[i][j]=max(t[x].f[i][j],g[i][j]);
t[x].sz+=t[lson].sz;
}
if(rson){
memset(g,0,sizeof(g));
for(int i=0;i<4;i++)for(int j=i;j<4;j++)for(int k=i;k<=j;k++)g[i][j]=max(g[i][j],t[x].f[i][k]+t[rson].f[k][j]);
for(int i=0;i<4;i++)for(int j=i;j<4;j++)t[x].f[i][j]=max(t[x].f[i][j],g[i][j]);
t[x].sz+=t[rson].sz;
}
}
void connect(int x,int y,int dir){
if(x)t[x].fa=y;
t[y].ch[dir]=x;
}
int identify(int x){
return t[t[x].fa].ch[1]==x;
}
void rotate(int x){
int y=t[x].fa;
int z=t[y].fa;
int dirx=identify(x);
int diry=identify(y);
int b=t[x].ch[!dirx];
connect(b,y,dirx);
connect(y,x,!dirx);
connect(x,z,diry);
pushup(y),pushup(x);
}
void splay(int x,int y){
y=t[y].fa;
while(t[x].fa!=y){
int fa=t[x].fa;
if(t[fa].fa==y)rotate(x);
else if(identify(fa)==identify(x))rotate(fa),rotate(x);
else rotate(x),rotate(x);
}
}
int findkth(int k){
if(!k)return 0;
int x=ROOT;
while(true){
if(t[lson].sz>=k)x=lson;
else if(t[x].tot+t[lson].sz<k)k-=(t[x].tot+t[lson].sz),x=rson;
else{splay(x,ROOT);return x;}
}
}
void ins(SPLAY q,int pos){
if(!ROOT){ROOT=++cnt,t[ROOT]=q;return;}
if(!pos){
int x=ROOT;
while(lson)x=lson;
splay(x,ROOT);
++cnt;
t[cnt]=q;
connect(cnt,x,0);
pushup(x);
}else{
int x=findkth(pos);
int left=t[x].tot;
t[x].tot=pos-t[lson].sz;
left-=t[x].tot;
++cnt;
t[cnt]=q;
connect(rson,cnt,1);
connect(cnt,x,1);
int y=cnt;
if(left){
++cnt;
t[cnt]=SPLAY(t[x].num[0],t[x].num[1],t[x].num[2],left);
connect(t[y].ch[1],cnt,1);
connect(cnt,y,1);
pushup(cnt);
}
pushup(y),pushup(x),splay(y,ROOT);
}
}
void iterate(int x){
if(lson)iterate(lson);
for(int i=0;i<t[x].tot;i++)printf("(%lld,%lld,%lld)",t[x].num[0],t[x].num[1],t[x].num[2]);
if(rson)iterate(rson);
}
signed main(){
scanf("%lld",&n);
for(int i=1,lans=0,a,b,c,d,e;i<=n;i++){
scanf("%lld%lld%lld%lld%lld",&a,&b,&c,&d,&e);
ins(SPLAY(b,c,d,e),a);
printf("%lld\n",t[ROOT].f[0][3]-lans);
lans=t[ROOT].f[0][3];
// iterate(ROOT);puts("");
}
return 0;
}

浙公网安备 33010602011771号