| 比赛 |
期末考试1 |
评测结果 |
AAAAAAAAAA |
| 题目名称 |
Output Only |
最终得分 |
100 |
| 用户昵称 |
dream |
运行时间 |
1.988 s |
| 代码语言 |
C++ |
内存使用 |
43.26 MiB |
| 提交时间 |
2026-02-08 09:59:56 |
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
const int N=200005,M=N*2,P=N*3*20;//n个根,n个初始颜色,n个修改的颜色
int n,k,q;
int hd[N],to[M],nxt[M],tot;
int root[N],cnt,f[N],ans,siz[N];
struct node{
int sum,ls,rs;
}tr[P];
void add(int x,int y){
tot++;
to[tot]=y;
nxt[tot]=hd[x];
hd[x]=tot;
}
int a[N];
void update(int &p,int l,int r,int v,int x){
if(!p) p=++cnt,tr[p]={0,0,0};
if(l==r){
tr[p].sum+=x;
return;
}
int mid=(l+r)/2;
if(v<=mid) update(tr[p].ls,l,mid,v,x);
else update(tr[p].rs,mid+1,r,v,x);
}
void build(int u,int fa){
f[u]=fa;
for(int i=hd[u];i;i=nxt[i]){
int v=to[i];
if(v==fa) continue;
siz[u]++;
if(a[u]!=a[v]) ans++;
update(root[u],1,k,a[v],1);
build(v,u);
}
}
int query(int p,int l,int r,int v){
if(l==r){
return tr[p].sum;
}
int mid=(l+r)/2;
if(v<=mid) return query(tr[p].ls,l,mid,v);
else return query(tr[p].rs,mid+1,r,v);
}
int main(){
freopen("tioj_outplay.in","r",stdin);
freopen("tioj_outplay.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n>>k>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
a[i]++;
}
for(int i=1;i<n;i++){
int x,y;
cin>>x>>y;
add(x,y);
add(y,x);
}
build(1,0);
for(int i=1;i<=q;i++){
int w,x;
cin>>w>>x;
x++;
update(root[f[w]],1,k,a[w],-1);
update(root[f[w]],1,k,x,1);
if(a[w]!=a[f[w]]) ans--;
ans-=siz[w]-query(root[w],1,k,a[w]);
a[w]=x;
if(a[w]!=a[f[w]]) ans++;
ans+=siz[w]-query(root[w],1,k,a[w]);
if(a[1]!=1){
cout<<ans+1<<"\n";
}
else cout<<ans<<"\n";
}
//特判全0情况
return 0;
}