codeforces-375DTree and Queries
2018-01-27 20:48
232 查看
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 ≤ n; ai ≠ 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
output
input
output
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;
}
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 ≤ n; ai ≠ 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;
}
相关文章推荐
- CodeForces 551 D.GukiZ and Binary Operations(dp+矩阵快速幂)
- CodeForces 547A--找循环节
- codeforces contest 358
- codeforces 333B - Chips
- codeforces 544D Destroying Roads (最短路)
- codeforces 617E. XOR and Favorite Number (莫队)
- CSU-ACM2017暑期训练12-KMP G - Next[]树 CodeForces - 432D
- Codeforces 161D Distance in Tree
- CodeForces - 357C C - Knight Tournament 并查集or set模拟
- codeforces 13A. Numbers
- codeforces 550B Preparing Olympiad(枚举)
- Codeforces 766D-Mahmoud and a Dictionary
- codeforces [Gym-100814K]
- CodeForces 688A-Opponents
- CodeForces 735D - Taxes
- codeforces 328A. IQ Test
- Codeforces 556B Case of Fake Numbers 数字转盘
- 【Codeforces 711 D D. Directed Roads】 + DFS
- CodeForces 828D High Road
- CodeForces 166A Rank List