2243: [SDOI2011]染色
Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 5848 Solved: 2140
[ 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,表示x和y之间有一条无向边。
下面 行每行描述一个操作:
“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
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
1
2
HINT
数N<=10^5,操作数M<=10^5,所有的颜色C为整数且在[0, 10^9]之间。
Source
树剖树剖树剖
树剖就是对于每个节点,选择其size最大的一个儿子作为关键节点(或者是重点)
然后所有重点连起来,一条由重点连成的链称作重路径(通过线段树连接)
某个点往上走的时候,碰到轻点直接走,重点就通过线段树往上
这样每次往上走当前子树大小至少乘以2
因此向上次数不超过log
至于统计ans,线段树再多维护两端点的颜色,反正总数一直加,碰到端点相同就--ans
嗯,自己想了一个询问的解决办法,把一个询问拆成两条路径,p ->lca1,q ->lca2
lca1与lca2自然是,,父子,,不多说了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<bitset>
using namespace std;
const int maxn = 1E5 + 10;
int n,m,cnt,root,lca1,lca2,ans,Color[maxn],c[maxn*20],Root[maxn],
L[maxn],fa[maxn][20],lc[maxn*20],rc[maxn*20],Lef[maxn*20],
Rig[maxn*20],pos[maxn],siz[maxn],Len[maxn],Mark[maxn*20];
bool Huge[maxn];
vector <int> v[maxn];
void dfs1(int x,int from)
{
for (int i = 1; i < 20; i++)
fa[x][i] = fa[fa[x][i-1]][i-1];
siz[x] = 1;
int Max = 0,po;
for (int i = 0; i < v[x].size(); i++) {
int to = v[x][i];
if (to == from) continue;
L[to] = L[x] + 1;
fa[to][0] = x;
dfs1(to,x);
siz[x] += siz[to];
if (siz[to] > Max) Max = siz[to],po = to;
}
if (siz[x] > 1) Huge[po] = 1;
}
void pushdown(int o)
{
if (Mark[o] != -1) {
c[lc[o]] = c[rc[o]] = 1;
Mark[lc[o]] = Mark[rc[o]] = Mark[o];
Lef[lc[o]] = Lef[rc[o]] = Mark[o];
Rig[lc[o]] = Rig[rc[o]] = Mark[o];
Mark[o] = -1;
}
}
void Modi(int o,int l,int r,int ml,int mr,int col)
{
pushdown(o);
if (ml <= l && r <= mr) {
c[o] = 1; Mark[o] = col;
Lef[o] = Rig[o] = col;
return;
}
int mid = (l+r) >> 1;
if (ml <= mid) {
if (!lc[o]) lc[o] = ++cnt;
Modi(lc[o],l,mid,ml,mr,col);
}
if (mr > mid) {
if (!rc[o]) rc[o] = ++cnt;
Modi(rc[o],mid+1,r,ml,mr,col);
}
c[o] = c[lc[o]] + c[rc[o]];
Lef[o] = Lef[lc[o]];
Rig[o] = Rig[rc[o]];
if (Rig[lc[o]] == Lef[rc[o]]) --c[o];
}
void dfs2(int x,int from)
{
if (siz[x] == 1 && Huge[x]) {
Root[x] = ++cnt;
Len[x] = pos[x];
Modi(Root[x],1,Len[x],pos[x],pos[x],Color[x]);
return;
}
int po;
for (int i = 0; i < v[x].size(); i++) {
int to = v[x][i];
if (to == from) continue;
if (Huge[to]) pos[to] = pos[x] + 1,po = to;
dfs2(to,x);
}
if (Huge[x]) {
Root[x] = Root[po];
Len[x] = Len[po];
Modi(Root[x],1,Len[x],pos[x],pos[x],Color[x]);
}
}
int Getcom()
{
char ch = getchar();
while (ch != 'C' && ch != 'Q') ch = getchar();
return ch == 'C'?1:2;
}
void LCA(int p,int q)
{
int Log; for (Log = 0; L[p] - (1<<Log) >= 1; Log++); Log--;
for (int j = Log; j >= 0; j--)
if (L[p] - (1<<j) >= L[q])
p = fa[p][j];
if (p == q) {
lca1 = lca2 = p;
return;
}
for (int j = Log; j >= 0; j--)
if (fa[p][j] != fa[q][j])
p = fa[p][j],q = fa[q][j];
lca1 = p; lca2 = fa[q][0];
}
int Quickfa(int x,int t)
{
int pos = 0;
for (; t; t >>= 1) {
if (t & 1) x = fa[x][pos];
++pos;
}
return x;
}
void Modify(int x,int goal,int C)
{
for (; x != fa[goal][0];) {
if (!Huge[x]) {
Color[x] = C;
x = fa[x][0];
}
else {
int l,t;
if (L[x] - pos[x] + 1 >= L[goal]) l = 1,t = pos[x] - 1;
else l = pos[goal],t = pos[x] - pos[goal];
Modi(Root[x],1,Len[x],l,pos[x],C);
x = Quickfa(x,t);
x = fa[x][0];
}
}
}
int Q_color(int o,int l,int r,int po)
{
pushdown(o);
if (l == r) return Lef[o];
int mid = (l + r) >> 1;
if (po <= mid) return Q_color(lc[o],l,mid,po);
else return Q_color(rc[o],mid+1,r,po);
}
int Q_num(int o,int l,int r,int ql,int qr)
{
pushdown(o);
if (ql <= l && r <= qr) return c[o];
int mid = (l + r) >> 1;
int ret = 0;
if (ql <= mid && qr > mid) {
ret = Q_num(lc[o],l,mid,ql,qr) + Q_num(rc[o],mid+1,r,ql,qr);
if (Rig[lc[o]] == Lef[rc[o]]) --ret;
}
else if (ql <= mid) ret = Q_num(lc[o],l,mid,ql,qr);
else ret = Q_num(rc[o],mid+1,r,ql,qr);
return ret;
}
int Query(int x,int goal)
{
int C = -1;
for (;;) {
if (!Huge[x]) {
++ans;
if (C == Color[x]) --ans;
C = Color[x];
x = fa[x][0];
if (x == fa[goal][0]) return C;
}
else {
int Co = Q_color(Root[x],1,Len[x],pos[x]);
if (Co == C) --ans;
int l,t;
if (L[x] - pos[x] + 1 >= L[goal]) l = 1,t = pos[x] - 1;
else l = pos[goal],t = pos[x] - pos[goal];
ans += Q_num(Root[x],1,Len[x],l,pos[x]);
x = Quickfa(x,t);
C = Q_color(Root[x],1,Len[x],pos[x]);
x = fa[x][0];
if (x == fa[goal][0]) return C;
}
}
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
cin >> n >> m;
memset(Mark,-1,sizeof(Mark));
for (int i = 1; i <= n; i++) scanf("%d",&Color[i]);
for (int i = 1; i < n; i++) {
int x,y; scanf("%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
root = n/2;
L[root] = 1; dfs1(root,0);
dfs2(root,0);
while (m--) {
int typ = Getcom(),p,q;
scanf("%d%d",&p,&q);
if (L[p] < L[q]) swap(p,q); LCA(p,q);
if (typ == 1) {
int C; scanf("%d",&C);
if (lca1 == lca2) Modify(p,lca1,C);
else Modify(p,lca1,C),Modify(q,lca2,C);
}
else {
ans = 0;
if (lca1 == lca2) Query(p,lca1),printf("%d\n",ans);
else {
int l = Query(p,lca1);
int r = Query(q,lca2);
if (l == r) --ans;
printf("%d\n",ans);
}
}
}
return 0;
}