0
点赞
收藏
分享

微信扫一扫

codeforces 375d Tree and Queries


​​http://www.elijahqi.win/2018/03/01/codeforces-375d/​​​
题意翻译

给出一棵 n 个结点的树,每个结点有一个颜色 c i 。 询问 q 次,每次询问以 v 结点为根的子树中,出现次数 ≥k 的颜色有多少种。

感谢@elijahqi 提供的翻译

题目描述

You have a rooted tree consisting of

n
n vertices. Each vertex of the tree has some color. We will assume that the tree vertices are numbered by integers from 1 to

n
n . Then we represent the color of vertex

v
v as

c_{v}
cv​ . The tree root is a vertex with number 1.

In this problem you need to answer to

m
m queries. Each query is described by two integers

v_{j},k_{j}
vj​,kj​ . The answer to query

v_{j},k_{j}
vj​,kj​ is the number of such colors of vertices

x
x , that the subtree of vertex

v_{j}
vj​ contains at least

k_{j}
kj​ vertices of color

x
x .

You can find the definition of a rooted tree by the following link: ​​http://en.wikipedia.org/wiki/Tree_(graph_theory)​​.

输入输出格式

输入格式:
The first line contains two integers

n
n and

m
m (2<=n<=105;1<=m<=105) ( 2 <= n <= 10 5 ; 1 <= m <= 10 5 )

c_{1},c_{2},…,c_{n}
c1​,c2​,…,cn​

(1<=c_{i}<=10^{5})
(1<=ci​<=105) . The next

n-1
n−1 lines contain the edges of the tree. The

i
i -th line contains the numbers

a_{i},b_{i}
ai​,bi​ (1<=ai,bi<=n;ai≠bi) ( 1 <= a i , b i <= n ; a i ≠ b i )

Next

m
m lines contain the queries. The

j
j -th line contains two integers

v_{j},k_{j}
vj​,kj​ (1<=vj<=n;1<=kj<=105) ( 1 <= v j <= n ; 1 <= k j <= 10 5 )

输出格式:
Print

m
m integers — the answers to the queries in the order the queries appear in the input.

输入输出样例

输入样例#1: 复制

8 5
1 2 2 3 3 2 3 3
1 2
1 5
2 3
2 4
5 6
5 7
5 8
1 2
1 3
1 4
2 3
5 3
输出样例#1: 复制

2
2
1
0
1
输入样例#2: 复制

4 1
1 2 3 4
1 2
2 3
3 4
1 1
输出样例#2: 复制

4
说明

A subtree of vertex

v
v in a rooted tree with root

r
r is a set of vertices u:dist(r,v)+dist(v,u)=dist(r,u) u : d i s t ( r , v ) + d i s t ( v , u ) = d i s t ( r , u )

dist(x,y)
dist(x,y) is the length (in edges) of the shortest path between vertices

x
x and

y
y .

考虑子树一定是dfs序列上一个连续的区间 那么每次对子树操作总相当于针对dfs序上的一段连续区间操作

就可以莫队了 设ans表示出现次数超过i次的颜色数 f[i]表示i号颜色出现的次数 Ans相对应的记录每个询问的答案 那么考虑每个颜色出现一定是从0~xx 那么我每出现一次 我即对ans进行修改即可 并不需要区间修改 然后相应的减去的时候 也是这时候的出现次数-1 然后对应的ans数组再减1即可

#include<cmath>
#include<cstdio>
#include<algorithm>
#define N 110000
using namespace std;
inline char gc(){
static char now[1<<16],*S,*T;
if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=gc();}
while(ch<='9'&&ch>='0') x=x*10+ch-'0',ch=gc();
return x*f;
}
struct node1{
int y,next;
}data[N<<1];
struct node{
int l,r,id,bl,k;
}qr[N];bool visit[N];
int num,h[N],in[N],out[N],w[N],c[N],ans[N],Ans[N],f[N],nn,n,m;
inline bool cmp(const node &a,const node &b){
return a.bl==b.bl?a.r<b.r:a.bl<b.bl;
}
inline void dfs(int x,int fa){
in[x]=++num;w[num]=c[x];
for (int i=h[x];i;i=data[i].next){
int y=data[i].y;if (y==fa) continue;dfs(y,x);
}out[x]=num;
}
inline void update(int x){
if (visit[x]) --ans[f[w[x]]--];
else ++ans[++f[w[x]]];
visit[x]^=1;
}
int main(){
// freopen("cf375d.in","r",stdin);
n=read();m=read();for (int i=1;i<=n;++i) c[i]=read();
for (int i=1;i<n;++i){
int x=read(),y=read();
data[++num].y=y;data[num].next=h[x];h[x]=num;
data[++num].y=x;data[num].next=h[y];h[y]=num;
}num=0;dfs(1,1);nn=sqrt(n);
for (int i=1;i<=m;++i) {
qr[i].id=i;int x=read();qr[i].l=in[x];qr[i].r=out[x];qr[i].k=read();
qr[i].bl=(qr[i].l-1)/nn+1;
}sort(qr+1,qr+m+1,cmp);
int cl=1,cr=0;
for (int i=1;i<=m;++i){
int l=qr[i].l,r=qr[i].r;
while(cl<l) update(cl++);
while(cl>l) update(--cl);
while(cr>r) update(cr--);
while(cr<r) update(++cr);
Ans[qr[i].id]=ans[qr[i].k];
}
for (int i=1;i<=m;++i) printf("%d\n",Ans[i]);
return 0;
}


举报

相关推荐

0 条评论