codeforces-375DTree and Queries

本文介绍了一种使用树状数组与莫队算法解决特定树形结构查询问题的方法。通过对树进行DFS序转化,将树上的问题转换为区间查询问题,并利用树状数组和莫队算法高效求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Tree and Queries
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You have a rooted tree consisting of 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. Then we represent the color of vertex v as cv. The tree root is a vertex with number 1.

In this problem you need to answer to m queries. Each query is described by two integers vj, kj. The answer to query vj, kj is the number of such colors of vertices x, that the subtree of vertex vj contains at least kj vertices of color x.

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

Input

The first line contains two integers n and m (2 ≤ n ≤ 105; 1 ≤ m ≤ 105). The next line contains a sequence of integers c1, c2, ..., cn(1 ≤ ci ≤ 105). The next n - 1 lines contain the edges of the tree. The i-th line contains the numbers ai, bi (1 ≤ ai, bi ≤ nai ≠ bi) — the vertices connected by an edge of the tree.

Next m lines contain the queries. The j-th line contains two integers vj, kj (1 ≤ vj ≤ n; 1 ≤ kj ≤ 105).

Output

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

Examples
input
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
output
2
2
1
0
1
input
4 1
1 2 3 4
1 2
2 3
3 4
1 1
output
4
Note

A subtree of vertex v in a rooted tree with root r is a set of vertices {u : dist(r, v) + dist(v, u) = dist(r, u)}. Where dist(x, y) is the length (in edges) of the shortest path between vertices x and y.

题目大意:给你一棵树,然后树上每个结点都有一个颜色,现在会给出m次询问,每次询问有值u,v,问对于结点u及其子树,总数量大于v的颜色有多少种。

直接搞肯定GG,对于树结点不好操作,我们考虑用dfs序把树上的层次关系转化为区间关系。预处理后,对于任意结点x,(in[x],out[x])所包含的区间恰好就是该结点与子树。问题转化为区间查询,发现没有修改操作结合数据范围考虑用莫队解决。然后直接上莫队开cnt数组记录颜色数量,发现部分操作无法实现,如对于连续的1,3和1,4询问,此时莫队无法转移区间,也就无法给出正确答案。后面考虑分块,发现没有给出颜色无法直接套HDU4391,同样打出GG。后面思索无果后与QT交流,发现可以用树状数组维护答案,其实本质上与普通莫队的效果相同,只不过add与del函数中不再简单维护单一颜色的数目,而是维护树状数组中一个区域内的颜色(不好解释..)。

结合树状数组后算法复杂度应该是O(M*sqrt(N)*LOG(N))??

#include<bits/stdc++.h>
using namespace std;
const int maxn =1e5+100;
int ax[maxn],in[maxn],out[maxn],tot=0,n,m;
int block,num,l[maxn],r[maxn],belong[maxn],L,R,ans=0;
vector<int> ac[maxn];
int res[maxn],vis[maxn],cnt[maxn],Rank[maxn],C[maxn];
struct node
{
	int l,r,x,k,id;
}query[maxn];
bool comp(node A,node B){
     if(belong[A.l]==belong[B.l]){
         return A.r<B.r;
     }
	 return belong[A.l]<belong[B.l];
}
void build(){
     block = sqrt(n);
     num = n/block;if(n%block)num++;
     for(int i=1;i<=num;i++)
		l[i] = (i-1)*block+1,r[i]=i*block;
	 r[num]=n;
	 for(int i=1;i<=n;i++)
		belong[i]=(i-1)/block+1;
}
void dfs(int u,int fa){
     in[u] = ++tot;
     Rank[tot]=u;
     for(int i=0;i<ac[u].size();i++){
		int v =ac[u][i];
		if(v!=fa)
			dfs(v,u);
     }
     out[u] = tot;
}
int lowbit(int x){
   return x&(-x);
}
int sum(int x){
    int ret=0;
    while(x<=n){
        ret +=C[x],x+=lowbit(x);
    }
    return ret;
}
void add1(int x,int d){
    while(x>0)
        C[x]+=d,x-=lowbit(x);
}
void add(int i,int u){
	 int j =Rank[i];
     cnt[ax[j]]++;
     if(cnt[ax[j]]-1!=0)
     add1(cnt[ax[j]]-1,-1);
     add1(cnt[ax[j]],1);
}
void del(int i,int u){
	 int j =Rank[i];
     add1(cnt[ax[j]],-1);
     if(cnt[ax[j]]-1!=0)
     add1(cnt[ax[j]]-1,1);
     cnt[ax[j]]--;
}
void update(int x){
     int l = query[x].l,r = query[x].r;
     //cout<<l<<" "<<r<<endl;
     while(R<r){R++,add(R,x);}
     while(R>r){del(R,x),R--;}
     while(L>l){L--,add(L,x);}
     while(L<l){del(L,x),L++;}
     res[query[x].id]=sum(query[x].k);
}
int main(){
   scanf("%d%d",&n,&m);
   build();
   for(int i=1;i<=n;i++)
	scanf("%d",&ax[i]);
   for(int i=0;i<n-1;i++){
    int l,r;
    scanf("%d%d",&l,&r);
    ac[l].push_back(r);
    ac[r].push_back(l);
   }
   dfs(1,0);
   for(int i=0;i<m;i++)
	scanf("%d%d",&query[i].x,&query[i].k),query[i].id=i,query[i].l=in[query[i].x],query[i].r=out[query[i].x];
   sort(query,query+m,comp);
   L = query[0].l,R = query[0].r;
   for(int i=L;i<=R;i++){
       int j =Rank[i];
	   cnt[ax[j]]++;
	   if(cnt[ax[j]]-1!=0)
	    add1(cnt[ax[j]]-1,-1);
       add1(cnt[ax[j]],1);
   }
   ans = sum(query[0].k);
   //cout<<ans<<endl;
   res[query[0].id]=ans;
   for(int i=1;i<m;i++){
       update(i);
   }
   for(int i=0;i<m;i++)
      printf("%d\n",res[i]);


   return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值