Bzoj 2243: [SDOI2011]染色 树链剖分,LCT,动态树

时间:2024-10-23 23:35:50

2243: [SDOI2011]染色

Time Limit: 20 Sec  Memory Limit: 512 MB
Submit: 5020  Solved: 1872
[Submit][Status][Discuss]

Description

给定一棵有n个节点的无根树和m个操作,操作有2类:

1、将节点a到节点b路径上所有点都染成颜色c;

2、询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),如“112221”由3段组成:“11”、“222”和“1”。

请你写一个程序依次完成这m个操作。

Input

第一行包含2个整数n和m,分别表示节点数和操作数;

第二行包含n个正整数表示n个节点的初始颜色

下面 行每行包含两个整数x和y,表示xy之间有一条无向边。

下面 行每行描述一个操作:

“C a b c”表示这是一个染色操作,把节点a到节点b路径上所有点(包括a和b)都染成颜色c;

“Q a b”表示这是一个询问操作,询问节点a到节点b(包括a和b)路径上的颜色段数量。

Output

对于每个询问操作,输出一行答案。

Sample Input

6 5

2 2 1 2 1 1

1 2

1 3

2 4

2 5

2 6

Q 3 5

C 2 1 1

Q 3 5

C 5 1 2

Q 3 5

Sample Output

3

1

2

HINT

数N<=10^5,操作数M<=10^5,所有的颜色C为整数且在[0, 10^9]之间。

Source

第一轮day1

题解:

树链剖分处理一下连接点之间的颜色是否相同即可。。。

线段树中记录 左右颜色,分成段数即可。。。

其实LCT做这道题也是不错的呦!!!(两个程序都挂上吧。。。)

对比:

LCT:

9088 kb

17756 ms

树链剖分:

29944 kb 8652 ms

树链剖分:

 #include<bits/stdc++.h>
using namespace std;
#define MAXN 100010
struct node
{
int begin,end,next;
}edge[MAXN*];
struct NODE
{
int left,right,lc,rc,color,tag,sum;
}tree[MAXN*];
int cnt,Head[MAXN],size[MAXN],deep[MAXN],P[MAXN][],pos[MAXN],belong[MAXN],c[MAXN],SIZE,n;
bool vis[MAXN];
void addedge(int bb,int ee)
{
edge[++cnt].begin=bb;edge[cnt].end=ee;edge[cnt].next=Head[bb];Head[bb]=cnt;
}
void addedge1(int bb,int ee)
{
addedge(bb,ee);addedge(ee,bb);
}
int read()
{
int s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
void dfs1(int u)
{
int i,v;
size[u]=;vis[u]=true;
for(i=Head[u];i!=-;i=edge[i].next)
{
v=edge[i].end;
if(vis[v]==false)
{
deep[v]=deep[u]+;
P[v][]=u;
dfs1(v);
size[u]+=size[v];
}
}
}
void Ycl()
{
int i,j;
for(j=;(<<j)<=n;j++)
{
for(i=;i<=n;i++)
{
if(P[i][j-]!=-)P[i][j]=P[P[i][j-]][j-];
}
}
}
void dfs2(int u,int chain)
{
int k=,i,v;
pos[u]=++SIZE;belong[u]=chain;
for(i=Head[u];i!=-;i=edge[i].next)
{
v=edge[i].end;
if(deep[v]>deep[u]&&size[v]>size[k])k=v;
}
if(k==)return;
dfs2(k,chain);
for(i=Head[u];i!=-;i=edge[i].next)
{
v=edge[i].end;
if(deep[v]>deep[u]&&v!=k)dfs2(v,v);
}
}
int LCA(int x,int y)
{
int i,j;
if(deep[x]<deep[y])swap(x,y);
for(i=;(<<i)<=deep[x];i++);i--;
for(j=i;j>=;j--)if(deep[x]-(<<j)>=deep[y])x=P[x][j];
if(x==y)return x;
for(j=i;j>=;j--)
{
if(P[x][j]!=-&&P[x][j]!=P[y][j])
{
x=P[x][j];
y=P[y][j];
}
}
return P[x][];
}
void Pushup(int k)
{
int l=k*,r=k*+;
tree[k].lc=tree[l].lc;
tree[k].rc=tree[r].rc;
if(tree[l].rc==tree[r].lc)tree[k].sum=tree[l].sum+tree[r].sum-;
else tree[k].sum=tree[l].sum+tree[r].sum;
}
void Pushdown(int k)
{
int l=k*,r=k*+;
if(tree[k].tag!=-)
{
tree[l].tag=tree[k].tag;
tree[r].tag=tree[k].tag;
tree[l].lc=tree[l].rc=tree[l].color=tree[k].tag;
tree[r].lc=tree[r].rc=tree[r].color=tree[k].tag;
tree[l].sum=tree[r].sum=;
tree[k].tag=-;
}
}
void Build(int k,int l,int r)
{
tree[k].left=l;tree[k].right=r;tree[k].tag=-;
if(l==r)return;
int mid=(l+r)/;
Build(k*,l,mid);Build(k*+,mid+,r);
}
void Change(int k,int l,int r,int C)
{
if(l<=tree[k].left&&tree[k].right<=r){tree[k].lc=tree[k].rc=tree[k].color=tree[k].tag=C;tree[k].sum=;return;}
Pushdown(k);
int mid=(tree[k].left+tree[k].right)/;
if(r<=mid)Change(k*,l,r,C);
else if(l>mid)Change(k*+,l,r,C);
else {Change(k*,l,mid,C);Change(k*+,mid+,r,C);}
Pushup(k);
}
void Solve_change(int x,int f,int C)
{
while(belong[x]!=belong[f])
{
Change(,pos[belong[x]],pos[x],C);
x=P[belong[x]][];
}
Change(,pos[f],pos[x],C);
}
int Query_sum(int k,int l,int r)
{
if(l<=tree[k].left&&tree[k].right<=r)return tree[k].sum;
Pushdown(k);
int mid=(tree[k].left+tree[k].right)/;
if(r<=mid)return Query_sum(k*,l,r);
else if(l>mid)return Query_sum(k*+,l,r);
//else return Query_sum(k*2,l,mid)+Query_sum(k*2+1,mid+1,r);
else
{
if(tree[k*].rc==tree[k*+].lc)return Query_sum(k*,l,mid)+Query_sum(k*+,mid+,r)-;
else return Query_sum(k*,l,mid)+Query_sum(k*+,mid+,r);
}
//Pushup(k);
}
int get_color(int k,int lr)
{
if(tree[k].left==tree[k].right)return tree[k].color;
Pushdown(k);
int mid=(tree[k].left+tree[k].right)/;
if(lr<=mid)return get_color(k*,lr);
else if(lr>mid)return get_color(k*+,lr);
}
int Solve_sum(int x,int f)
{
int sum=;
while(belong[x]!=belong[f])
{
sum+=Query_sum(,pos[belong[x]],pos[x]);
if(get_color(,pos[belong[x]])==get_color(,pos[P[belong[x]][]]))sum--;
x=P[belong[x]][];
}
sum+=Query_sum(,pos[f],pos[x]);
return sum;
}
int main()
{
int m,i,bb,ee,A,B,C,lca;
char zs[];
n=read();m=read();
for(i=;i<=n;i++)c[i]=read();
memset(Head,-,sizeof(Head));cnt=;
for(i=;i<n;i++){bb=read();ee=read();addedge1(bb,ee);}
memset(P,-,sizeof(P));SIZE=;
dfs1();Ycl();
dfs2(,);
Build(,,n);
for(i=;i<=n;i++)Change(,pos[i],pos[i],c[i]);
for(i=;i<=m;i++)
{
scanf("%s",zs);
if(zs[]=='C')
{
A=read();B=read();C=read();
lca=LCA(A,B);
Solve_change(A,lca,C);Solve_change(B,lca,C);
}
else
{
A=read();B=read();
lca=LCA(A,B);
printf("%d\n",Solve_sum(A,lca)+Solve_sum(B,lca)-);
}
}
fclose(stdin);
fclose(stdout);
return ;
}

LCT(代码略丑):

 #include<bits/stdc++.h>
using namespace std;
#define LL long long
#define MAXN 100010
#define INF 1e9
struct node
{
LL left,right,color,tag1,lc,rc,sum;
}tree[MAXN];
LL rev[MAXN],father[MAXN],Stack[MAXN];
LL read()
{
LL s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
LL isroot(LL x)
{
return tree[father[x]].left!=x&&tree[father[x]].right!=x;
}
void pushdown(LL x)
{
LL l=tree[x].left,r=tree[x].right;
if(rev[x]!=)
{
rev[x]^=;rev[l]^=;rev[r]^=;
swap(tree[x].left,tree[x].right);
swap(tree[x].lc,tree[x].rc);
//tree[x].lc=tree[r].lc;tree[x].rc=tree[l].rc;
//swap(tree[l].color,tree[r].color);
//swap(tree[l].sum,tree[r].sum);
}
if(tree[x].tag1!=-)
{
//tree[l].lc=tree[l].rc=tree[x].tag1;
//tree[r].lc=tree[r].rc=tree[x].tag1;
/*tree[l].color=*/tree[x].color=tree[x].lc=tree[x].rc=tree[x].tag1;
tree[l].tag1=tree[r].tag1=tree[x].tag1;
tree[x].sum=;
tree[x].tag1=-;
}
}
void pushup(LL x)
{
LL l=tree[x].left,r=tree[x].right;
//if(tree[l].rc==tree[r].lc)
//{
if(l!=)pushdown(l);
if(r!=)pushdown(r);
//if(l!=0)lr+=tree[l].sum;
//if(r!=0)lr+=tree[r].sum;
tree[x].sum=tree[l].sum+tree[r].sum+;
if(l!=&&tree[l].rc==tree[x].color)tree[x].sum--;
if(r!=&&tree[r].lc==tree[x].color)tree[x].sum--;
//tree[x].sum=lr;
tree[x].lc=tree[x].rc=tree[x].color;
if(l!=)tree[x].lc=tree[l].lc;
if(r!=)tree[x].rc=tree[r].rc;
//tree[x].sum=tree[l].sum+tree[r].sum-1;
//tree[x].lc=tree[l].lc;
//tree[x].rc=tree[r].rc;
//}
//else
//{
//tree[x].sum=tree[l].sum+tree[r].sum;
//tree[x].lc=tree[l].lc;
//tree[x].rc=tree[r].rc;
//if(l!=0)lr+=tree[l].sum,tree[x].lc=tree[l].lc;
//if(r!=0)lr+=tree[r].sum,tree[x].rc=tree[r].rc;
//if(lr!=0)tree[x].sum=lr+1;
//}
}
void rotate(LL x)
{
LL y=father[x],z=father[y];
if(!isroot(y))
{
if(tree[z].left==y)tree[z].left=x;
else tree[z].right=x;
}
if(tree[y].left==x)
{
father[x]=z;father[y]=x;tree[y].left=tree[x].right;tree[x].right=y;father[tree[y].left]=y;
}
else
{
father[x]=z;father[y]=x;tree[y].right=tree[x].left;tree[x].left=y;father[tree[y].right]=y;
}
pushup(y);pushup(x);
}
void splay(LL x)
{
LL top=,i,y,z;Stack[++top]=x;
for(i=x;!isroot(i);i=father[i])Stack[++top]=father[i];
for(i=top;i>=;i--)pushdown(Stack[i]);
while(!isroot(x))
{
y=father[x];z=father[y];
if(!isroot(y))
{
if((tree[y].left==x)^(tree[z].left==y))rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(LL x)
{
LL last=;
while(x!=)
{
splay(x);
tree[x].right=last;pushup(x);
last=x;x=father[x];
}
}
void makeroot(LL x)
{
access(x);splay(x);rev[x]^=;
}
void link(LL u,LL v)
{
makeroot(u);father[u]=v;splay(u);
}
void cut(LL u,LL v)
{
makeroot(u);access(v);splay(v);father[u]=tree[v].left=;
}
LL findroot(LL x)
{
access(x);splay(x);
while(tree[x].left!=)x=tree[x].left;
return x;
}
int main()
{
LL i,x,y,n,m,a,b,c;
char fh[];
n=read();m=read();
tree[].lc=tree[].rc=-INF;
for(i=;i<=n;i++)tree[i].color=tree[i].lc=tree[i].rc=read(),tree[i].sum=,tree[i].tag1=-;
for(i=;i<n;i++)
{
x=read();y=read();
link(x,y);
}
for(i=;i<=m;i++)
{
scanf("\n%s",fh);
if(fh[]=='C')
{
a=read();b=read();c=read();
makeroot(a);access(b);splay(b);
tree[b].color=tree[b].lc=tree[b].rc=c;tree[b].tag1=c;
tree[b].sum=;
}
else
{
a=read();b=read();
makeroot(a);access(b);splay(b);
printf("%d\n",tree[b].sum);
}
}
return ;
}