题解:P15397 浙江旅行团 / hangzhou
educational,但是非常好的题目!
问题:
定义矩阵乘法为
给定序列
考虑对于每个点维护出所有的操作序列。
则一个标记
变成可持久化则考虑建树,则用可持久化数组维护出所有版本每个颜色的最新对应节点,每次统计即向上跳链统计,倍增即可。
注意每次查询的时候带上最终颜色的贡献。
#include "bits/stdc++.h"
using namespace std;
typedef long long ll1;
#define pii pair<int,int>
#define fir first
#define sec second
#define mkp make_pair
const int N=1e6+5,inf=1e9;
const int M=1e7+5;
ll1 va,vb,vc,vd;
struct mat{
ll1 c[2][2];
void init(){c[0][0]=c[1][1]=0,c[0][1]=c[1][0]=inf;}
ll1 getval(){return (c[0][0]^va)+(c[0][1]^vb)+(c[1][0]^vc)+(c[1][1]^vd);}
friend mat operator *(const mat &lf,const mat &rf){
mat res;
for(int i=0;i<2;i++)for(int j=0;j<2;j++)
res.c[i][j]=min(lf.c[i][0]+rf.c[0][j],lf.c[i][1]+rf.c[1][j]);
return res;
}
};
int fat[N],a[N],n,m,q;
int rtCity[N],rtBot[N];
namespace Tree{//颜色版本树
int d[N],tm[N],tot;
int fa[N],lb[N];
mat ffa[N],flb[N];
void init(){ffa[0].init(),flb[0].init();}
int addp(int Fa,int T,mat upf){
int u=++tot;d[u]=d[Fa]+1,tm[u]=T;
fa[u]=Fa,ffa[u]=upf;
int x=lb[Fa],y=lb[x];
if(d[x]-d[y]==d[Fa]-d[x]){
lb[u]=y,flb[u]=flb[x]*flb[Fa]*upf;
}else lb[u]=Fa,flb[u]=upf;
return u;
}
ll1 qry(int u,int lim){
mat res;res.init();
while(u&&tm[lb[u]]>=lim){
res=flb[u]*res,u=lb[u];
}
while(u&&tm[u]>=lim){
res=ffa[u]*res,u=fa[u];
}
return res.getval();
}
}
struct Node{int col,tl,tr;};
#define ls son[o][0]
#define rs son[o][1]
#define mid ((l+r)>>1)
namespace sgtcity{//每个版本,每个颜色最新点
int dat[M],son[M][2],tot=0;
int newnode(int o=0){
++tot,dat[tot]=dat[o];
son[tot][0]=ls,son[tot][1]=rs;
return tot;
}
int mdf(int o,int l,int r,int p,int x){
o=newnode(o);if(l==r)return dat[o]=x,o;
p<=mid?ls=mdf(ls,l,mid,p,x):rs=mdf(rs,mid+1,r,p,x);
return o;
}
int qry(int o,int l,int r,int p){
if(!o)return 0;if(l==r)return dat[o];
return p<=mid?qry(ls,l,mid,p):qry(rs,mid+1,r,p);
}
}
namespace sgtbot{
int son[M][2],tot=0,lsttot=0;
Node tgc[M];ll1 tga[M];
void init(){tgc[0]=(Node){0,0,0},tga[0]=0;}
int newnode(int o=0){
if(o>lsttot)return o;
++tot,tga[tot]=tga[o],tgc[tot]=tgc[o];
son[tot][0]=ls,son[tot][1]=rs;
return tot;
}
int build(int l,int r){
int o=newnode();
if(l==r){
tgc[o]=(Node){a[l],0,0};
return o;
}
ls=build(l,mid),rs=build(mid+1,r);
return o;
}
int Cover(int o,Node x,ll1 y){
o=newnode(o),tga[o]^=y;
if(x.col){
if(tgc[o].col){
int u=sgtcity::qry(rtCity[fat[x.tl]],1,n,tgc[o].col);
tga[o]^=Tree::qry(u,tgc[o].tr);
tgc[o].col=x.col,tgc[o].tr=x.tr;
}else tgc[o]=x;
}
return o;
}
int psd(int o){
o=newnode(o);
ls=Cover(ls,tgc[o],tga[o]);
rs=Cover(rs,tgc[o],tga[o]);
tgc[o]=(Node){0,0,0},tga[o]=0;
return o;
}
int cover(int o,int l,int r,int lt,int rt,Node x){
o=newnode(o);
if(l>=lt&&r<=rt)return Cover(o,x,0);
o=psd(o);
if(lt<=mid)ls=cover(ls,l,mid,lt,rt,x);
if(rt> mid)rs=cover(rs,mid+1,r,lt,rt,x);
return o;
}
ll1 qry(int o,int l,int r,int p,int T){
if(l==r){
ll1 res=tga[o];
int u=sgtcity::qry(rtCity[T],1,n,tgc[o].col);
res^=Tree::qry(u,tgc[o].tr);
return res;
}
o=psd(o);
return p<=mid?qry(ls,l,mid,p,T):qry(rs,mid+1,r,p,T);
}
void fix(){
lsttot=tot;
}
#undef ls
#undef rs
#undef mid
}
int TYPE;
ll1 Ans=0;
signed main(){
scanf("%d%d%d%d",&n,&m,&q,&TYPE);
scanf("%lld%lld%lld%lld",&va,&vb,&vc,&vd);
for(int i=1;i<=m;i++)scanf("%d",&a[i]);
Tree::init(),sgtbot::init();
rtCity[0]=0;
rtBot[0]=sgtbot::build(1,m);
sgtbot::fix();
char opt;int lst;
ll1 VAL,l,r,c,x;
for(int T=1;T<=q;T++){
scanf("%d %c",&lst,&opt);
fat[T]=lst;
rtCity[T]=rtCity[lst];
rtBot[T]=rtBot[lst];
if(opt=='t'){
scanf("%lld%lld%lld",&l,&r,&c);
if(TYPE)l^=Ans,r^=Ans,c^=Ans;
rtBot[T]=sgtbot::cover(rtBot[T],1,m,l,r,(Node){int(c),T,T});
}
if(opt=='e'){
scanf("%lld%lld",&c,&VAL);
if(TYPE)c^=Ans,VAL^=Ans;
mat dat;
dat.c[0][0]=((VAL>>24)&255);
dat.c[0][1]=((VAL>>16)&255);
dat.c[1][0]=((VAL>>8)&255);
dat.c[1][1]=(VAL&255);
int fap=sgtcity::qry(rtCity[T],1,n,c);
int u=Tree::addp(fap,T,dat);
rtCity[T]=sgtcity::mdf(rtCity[T],1,n,c,u);
}
if(opt=='q'){
scanf("%lld",&x);
if(TYPE)x^=Ans;
Ans=sgtbot::qry(rtBot[T],1,m,x,T);
printf("%lld\n",Ans);
}
sgtbot::fix();
}
return 0;
}