ETT“裸题”
支持子树加,移动子树,求一个点到根的路径和。
维护欧拉序,第一次出现为正值,第二次出现为负值。求根到x的路径改为求的权值和。移动子树改为序列移动即可,剩下的都是Splay的“基操”。。。。。?
我只调了亿会就调完了!
说说每个操作:
1.求区间和:注意我们给的是,这个不是交换子树后当前欧拉序列中的下标,而是原序列的编号,这也即平衡树的节点编号。
显然需要把转到根,把转到根的右儿子?直接转哨兵和即可??错!移动子树后的后继已经不一定是了!
所以必须这么写:
long long get_sum(int l,int r){
l++,r++;//哨兵
splay(l,0);int L=pre_();
splay(r,0);int R=nxt_();
splay(L,0);splay(R,L);//哦!不应该直接用l-1,r+1!应该找前驱后继!
return sum[son[R][0]];
}2.子树修改:
变成区间修改,但是左括号是加,右括号是减。对应到Splay上一个节点的子树和,为了维护它,你不得不记录每个节点的子树内有几个左括号,几个右括号。
我一开始没注意到初始值有0,又调了好久/ll
void modify(int l,int r,int k){//读题不仔细,爆零两行泪
l++,r++;
splay(l,0);int L=pre_();
splay(r,0);int R=nxt_();
splay(L,0);splay(R,L);//哦!不应该直接用l-1,r+1!应该找前驱后继!
color(son[R][0],k);
push_up(R);push_up(L);
} void push_up(int x){
sum[x]=sum[Ls]+sum[Rs]+a[x];
nm[x]=nm[Ls]+nm[Rs]+F[x];
}//Splay中编号即原序列编号
void color(int rt,long long w){
a[rt]+=F[rt]*w;//自己的值也应该要更新吧! //这每加!
sum[rt]+=nm[rt]*w;
cols[rt]+=w;
}
void push_down(int x){
if(cols[x]){
if(Ls)color(Ls,cols[x]);//没判断有没有左右儿子!!
if(Rs)color(Rs,cols[x]);
cols[x]=0;
}
}
3.区间移动操作
这个好麻烦啊!!
假设要把[l,r]移动到pos后,流程是这样的://注意这里的l,r是原splay上的节点编号(而非当前序列的下标)
先把l转到根,这时[1,l-1]变成了根的左儿子,把它断掉;(双向断,同时push_up)(l-1不太严谨,应是l的前驱)。记录它的根为p1
再把r转到根,这时[r+1,n]变成了根的右儿子,把它断掉;(双向断,同时push_up)(r+1不太严谨,应是r的后继)。记录它的根为p2
定义merge(p1,p2)为把以p1和p2为根两棵树合并起来的操作;要求p1子树所有点排名都小于p2所有点。
把p1的最大点转到根,再把p2接上去即可:
void merge(int p1,int p2){//合并两个树:一个把最大值变成根,然后把另一个变成右儿子
int nw=get_mx(p1);
splay(nw,0);//splay意味着整个树的根都是它了!
setson(nw,p2,1);
up(nw);
}那我们就merge(p1,p2)
再在合并后的树中找到pos(本题中pos给的也非排名而是平衡树中编号)。
把它转到根,然后把它的右子树断掉(记被这个被断掉的子树的根为p3)。把一开始剩下的[l,r]这棵树接上去(它的根此时为r)。(使用merge(root,r))
然后再把这棵树和p3合并(merge(root,p3)),就完成了。
细节:
-
每次断边后都要双向断,而且需要push_up;如果不是根节点,那需要一路push_up到根。
-
注意每次splay(x,0)都会让root=x;而我们断边的时候,会把一棵splay劈成俩,但root还是原来的根;而每次splay(x,0)都会让你的root指向x(即使root原来都不是这棵树的根)。需要特别注意,比如每次merge(p1,p2)之后,因为splay了,root都会指向合并后树的新根。
void move(int l,int r,int pos){//[l,r]平移到pos后
l++,r++,pos++;
splay(l,0);int p1=son[l][0];fa[p1]=son[l][0]=0;push_up(l);//如果fa[p1]不清空感觉会在splay时出现问题?
splay(r,0);int p2=son[r][1];fa[p2]=son[r][1]=0;push_up(r); //怎么感觉这样复杂度很假啊?? 哦好像不假
merge(p1,p2);//必须合并到当前的根?要不着mx找不对。
splay(pos,0);int p3=son[pos][1];fa[p3]=son[pos][1]=0;push_up(pos);
merge(root,r);//因为r是原来的根
merge(root,p3);
}其他细节:
-
push_down要判断是否有左右儿子
-
color要把a数组(即这一个点的值)也改了(要不push_up就不对了)
啊zbl
(删注释版本)
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
//#define int long long
//using std::cout;
//using std::endl;
using std::sort;
using std::min;
using std::max;
const int MAXN=200100;
int read(){//int 无负数
int h=0;char c=getchar();
while(c<'0'||c>'9')c=getchar();
while(c>='0'&&c<='9')h=(h<<1)+(h<<3)+c-'0',c=getchar();
return h;
}
struct Edge{
int to;
int nxt;
}edge[MAXN<<1];
int frm[MAXN];
int cnt_e;
void insert_e(int u,int v){
cnt_e++;
edge[cnt_e].to=v;
edge[cnt_e].nxt=frm[u];
frm[u]=cnt_e;
}
int n,q;
long long a[MAXN];//有可能爆int!
int v[MAXN];
int dfn[MAXN],cntd;//欧拉序
int st[MAXN],ed[MAXN];
int F[MAXN];//左括号or右括号,a可以是0!!所以会出错!!!
void pre_dfs(int x,int ff){
dfn[++cntd]=x,st[x]=cntd;
for(int i=frm[x];i;i=edge[i].nxt){
int v=edge[i].to;if(v==ff)continue;
pre_dfs(v,x);
}
dfn[++cntd]=x,ed[x]=cntd;
}
struct Splay{
#define Ls son[x][0]
#define Rs son[x][1]
int fa[MAXN],son[MAXN][2];
long long sum[MAXN];
int nm[MAXN];//维护左括号数+右括号数
long long cols[MAXN];//加的和 //应该longlong!!
int sta[MAXN],tots;
int root;
bool which_son(int x){return son[fa[x]][1]==x;}
void push_up(int x){
sum[x]=sum[Ls]+sum[Rs]+a[x];
nm[x]=nm[Ls]+nm[Rs]+F[x];
}//Splay中编号即原序列编号
void color(int rt,long long w){
a[rt]+=F[rt]*w;//自己的值也应该要更新吧! //这每加!
sum[rt]+=nm[rt]*w;
cols[rt]+=w;
}
void push_down(int x){
if(cols[x]){
if(Ls)color(Ls,cols[x]);//没判断有没有左右儿子!!
if(Rs)color(Rs,cols[x]);
cols[x]=0;
}
}
void setson(int f,int x,int w){//Splay森林应该可以这么写吧? //是不是平移区间,根本不要存在一个同时两棵树的时刻??
if(f)son[f][w]=x;
if(x)fa[x]=f;
}
void rotate(int x){
int f=fa[x],ff=fa[f];
int w=which_son(x),wf=which_son(f);
int p=son[x][!w];
setson(f,p,w);
setson(x,f,!w);
setson(ff,x,wf);
push_up(f),push_up(x);
}
void splay(int x,int am){
int xx=x;tots=0;
for(;xx;xx=fa[xx]){sta[++tots]=xx;}
while(tots){push_down(sta[tots--]);}
for(;fa[x]!=am;rotate(x)){
if(fa[fa[x]]!=am&&which_son(x)==which_son(fa[x]))rotate(fa[x]);
}
if(am==0)root=x;
}
int build(int l,int r,int ff){
if(l>r)return 0;
int mid=(l+r)>>1;
int nw=mid;
fa[nw]=ff;
son[nw][0]=build(l,mid-1,nw);
son[nw][1]=build(mid+1,r,nw);
push_up(nw);
return nw;
}
long long get_sum(int l,int r){
l++,r++;//哨兵
splay(l,0);int L=pre_();
splay(r,0);int R=nxt_();
splay(L,0);splay(R,L);//哦!不应该直接用l-1,r+1!应该找前驱后继!
return sum[son[R][0]];
}
int nxt_(){
int x=root;x=son[x][1];
while(son[x][0])push_down(x),x=son[x][0];//也应该push_down!
return x;
}
int pre_(){
int x=root;x=son[x][0];
while(son[x][1])push_down(x),x=son[x][1];
return x;
}
int get_mx(int x){
while(son[x][1])x=son[x][1];
return x;
}
void merge(int p1,int p2){//合并两个树:一个把最大值变成根,然后把另一个变成右儿子
int nw=get_mx(p1);
splay(nw,0);//splay意味着整个树的根都是它了!
setson(nw,p2,1);
push_up(nw);
}
void move(int l,int r,int pos){//[l,r]平移到pos后
l++,r++,pos++;
splay(l,0);int p1=son[l][0];fa[p1]=son[l][0]=0;push_up(l);//如果fa[p1]不清空感觉会在splay时出现问题?
splay(r,0);int p2=son[r][1];fa[p2]=son[r][1]=0;push_up(r); //怎么感觉这样复杂度很假啊?? 哦好像不假
merge(p1,p2);//必须合并到当前的根?要不着mx找不对。
splay(pos,0);int p3=son[pos][1];fa[p3]=son[pos][1]=0;push_up(pos);
merge(root,r);//因为r是原来的根
merge(root,p3);
}
void modify(int l,int r,int k){//读题不仔细,爆零两行泪
l++,r++;
splay(l,0);int L=pre_();
splay(r,0);int R=nxt_();
splay(L,0);splay(R,L);//哦!不应该直接用l-1,r+1!应该找前驱后继!
color(son[R][0],k);
push_up(R);push_up(L);
}
}t;
signed main(){
n=read();
for(int i=2;i<=n;i++){
int u=read(),v=i;
insert_e(u,v);insert_e(v,u);
}
pre_dfs(1,1);
for(int i=1;i<=n;i++){
int h=read();
a[st[i]+1]=h,a[ed[i]+1]=-h;//整体平移,给哨兵腾地方
F[st[i]+1]=1,F[ed[i]+1]=-1;
}
t.root=t.build(1,cntd+2,0);//哨兵!
//是不是移动的时候不需要find_k,因为已知是l和r在Splay中的编号了??
// for(int i=1;i<=cntd;i++)cout<<dfn[i]<<" ";
q=read();for(int i=1;i<=q;i++){
char ch[9];scanf("%s",ch);
if(ch[0]=='Q'){
int x=read();
printf("%lld\n",t.get_sum(1,st[x]));
}
if(ch[0]=='C'){
int x=read(),nf=read();
t.move(st[x],ed[x],st[nf]);
}
if(ch[0]=='F'){
int x=read(),k=read();
t.modify(st[x],ed[x],k);
}
// t.Print();
}
return 0;
}(不删调试(恶趣味
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
//#define int long long
//using std::cout;
//using std::endl;
using std::sort;
using std::min;
using std::max;
const int MAXN=200100;
int read(){//int 无负数
int h=0;char c=getchar();
while(c<'0'||c>'9')c=getchar();
while(c>='0'&&c<='9')h=(h<<1)+(h<<3)+c-'0',c=getchar();
return h;
}
struct Edge{
int to;
int nxt;
}edge[MAXN<<1];
int frm[MAXN];
int cnt_e;
void insert_e(int u,int v){
cnt_e++;
edge[cnt_e].to=v;
edge[cnt_e].nxt=frm[u];
frm[u]=cnt_e;
}
int n,q;
long long a[MAXN];//有可能爆int!
int v[MAXN];
int dfn[MAXN],cntd;//欧拉序
int st[MAXN],ed[MAXN];
int F[MAXN];//左括号or右括号,a可以是0!!所以会出错!!!
void pre_dfs(int x,int ff){
dfn[++cntd]=x,st[x]=cntd;
for(int i=frm[x];i;i=edge[i].nxt){
int v=edge[i].to;if(v==ff)continue;
pre_dfs(v,x);
}
dfn[++cntd]=x,ed[x]=cntd;
}
struct Splay{
#define Ls son[x][0]
#define Rs son[x][1]
int fa[MAXN],son[MAXN][2];
// int siz[MAXN];//是不是压根不用siz啊?
long long sum[MAXN];
int nm[MAXN];//维护左括号数+右括号数
long long cols[MAXN];//加的和 //应该longlong!!
int sta[MAXN],tots;
int root;
bool which_son(int x){return son[fa[x]][1]==x;}
void push_up(int x){
sum[x]=sum[Ls]+sum[Rs]+a[x];
nm[x]=nm[Ls]+nm[Rs]+F[x];
}//Splay中编号即原序列编号
void color(int rt,long long w){
a[rt]+=F[rt]*w;//自己的值也应该要更新吧! //这每加!
sum[rt]+=nm[rt]*w;
cols[rt]+=w;
}
void push_down(int x){
if(cols[x]){
if(Ls)color(Ls,cols[x]);//没判断有没有左右儿子!!
if(Rs)color(Rs,cols[x]);
cols[x]=0;
}
}
void setson(int f,int x,int w){//Splay森林应该可以这么写吧? //是不是平移区间,根本不要存在一个同时两棵树的时刻??
if(f)son[f][w]=x;
if(x)fa[x]=f;
}
void rotate(int x){
int f=fa[x],ff=fa[f];
int w=which_son(x),wf=which_son(f);
int p=son[x][!w];
setson(f,p,w);
setson(x,f,!w);
setson(ff,x,wf);
push_up(f),push_up(x);
}
void splay(int x,int am){
int xx=x;tots=0;
for(;xx;xx=fa[xx]){sta[++tots]=xx;}
while(tots){push_down(sta[tots--]);}
for(;fa[x]!=am;rotate(x)){
if(fa[fa[x]]!=am&&which_son(x)==which_son(fa[x]))rotate(fa[x]);
}
if(am==0)root=x;
}
// void print(int x){
// push_down(x);
// if(Ls)print(Ls);
// cout<<fa[x]<<' '<<x<<' '<<sum[x]<<endl;
// if(Rs)print(Rs);
// }
// void Print(int x=-1){
// puts("-----------------");
// print(x!=-1?x:root);
// puts("\n---------------");
// }
int build(int l,int r,int ff){
if(l>r)return 0;
int mid=(l+r)>>1;
int nw=mid;
fa[nw]=ff;
son[nw][0]=build(l,mid-1,nw);
son[nw][1]=build(mid+1,r,nw);
push_up(nw);
return nw;
}
long long get_sum(int l,int r){
l++,r++;//哨兵
splay(l,0);int L=pre_();
splay(r,0);int R=nxt_();
splay(L,0);splay(R,L);//哦!不应该直接用l-1,r+1!应该找前驱后继!
// Print();
return sum[son[R][0]];
}
int nxt_(){
int x=root;x=son[x][1];
while(son[x][0])push_down(x),x=son[x][0];//也应该push_down!
return x;
}
int pre_(){
int x=root;x=son[x][0];
while(son[x][1])push_down(x),x=son[x][1];
return x;
}
int get_mx(int x){
while(son[x][1])x=son[x][1];
return x;
}
// void up(int x){
// while(x){push_up(x),x=fa[x];}
// }
void merge(int p1,int p2){//合并两个树:一个把最大值变成根,然后把另一个变成右儿子
int nw=get_mx(p1);
splay(nw,0);//splay意味着整个树的根都是它了!
setson(nw,p2,1);
push_up(nw);
}
void move(int l,int r,int pos){//[l,r]平移到pos后
l++,r++,pos++;
// splay(l);int pre=
// splay(pre_(l-1),0);splay(r+1,l-1);//这里有问题!l-1可能不再是l-1的前驱了!
// Print();
// int nw=son[r+1][0];//记下这个根
// son[r+1][0]=0;push_up(r+1),push_up(root);//俩都要push_up!
// splay(pos,0);int nxt=nxt_();splay(nxt,pos);
// Print();
// son[nxt][0]=nw;fa[nw]=nxt;
// push_up(nxt);push_up(pos);
// Print();
splay(l,0);int p1=son[l][0];fa[p1]=son[l][0]=0;push_up(l);//如果fa[p1]不清空感觉会在splay时出现问题?
splay(r,0);int p2=son[r][1];fa[p2]=son[r][1]=0;push_up(r); //怎么感觉这样复杂度很假啊?? 哦好像不假
// Print(p1);Print(p2);
//感觉这些充满了问题
merge(p1,p2);//必须合并到当前的根?要不着mx找不对。
// Print(root);
splay(pos,0);int p3=son[pos][1];fa[p3]=son[pos][1]=0;push_up(pos);
// Print(root);
merge(root,r);//因为r是原来的根
// Print(root);
merge(root,p3);
// Print(root);
}
void modify(int l,int r,int k){//读题不仔细,爆零两行泪
l++,r++;
splay(l,0);int L=pre_();
splay(r,0);int R=nxt_();
splay(L,0);splay(R,L);//哦!不应该直接用l-1,r+1!应该找前驱后继!
color(son[R][0],k);
push_up(R);push_up(L);
// Print();
}
}t;
signed main(){
n=read();
for(int i=2;i<=n;i++){
int u=read(),v=i;
insert_e(u,v);insert_e(v,u);
}
pre_dfs(1,1);
for(int i=1;i<=n;i++){
int h=read();
// if(h==0)puts("???");
a[st[i]+1]=h,a[ed[i]+1]=-h;//整体平移,给哨兵腾地方
F[st[i]+1]=1,F[ed[i]+1]=-1;
}
// for(int i=1;i<=cntd+1;i++)cout<<a[i]<<' ';puts("");
t.root=t.build(1,cntd+2,0);//哨兵!
// t.Print();
//是不是移动的时候不需要find_k,因为已知是l和r在Splay中的编号了??
// for(int i=1;i<=cntd;i++)cout<<dfn[i]<<" ";
q=read();for(int i=1;i<=q;i++){
char ch[9];scanf("%s",ch);
if(ch[0]=='Q'){
int x=read();
printf("%lld\n",t.get_sum(1,st[x]));
}
if(ch[0]=='C'){
int x=read(),nf=read();
t.move(st[x],ed[x],st[nf]);
}
if(ch[0]=='F'){
int x=read(),k=read();
t.modify(st[x],ed[x],k);
}
// t.Print();
}
return 0;
}