| ||||||||||
Boring countingTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others)Total Submission(s): 1972 Accepted Submission(s): 561
Problem Description
In this problem we consider a rooted tree with N vertices. The vertices are numbered from 1 to N, and vertex 1 represents the root. There are integer weights on each vectice. Your task is to answer a list of queries, for each query, please tell us among all the vertices in the subtree rooted at vertice u, how many different kinds of weights appear exactly K times?
Input
The first line of the input contains an integer T( T<= 5 ), indicating the number of test cases.
For each test case, the first line contains two integers N and K, as described above. ( 1<= N <= 10 5, 1 <= K <= N ) Then come N integers in the second line, they are the weights of vertice 1 to N. ( 0 <= weight <= 10 9 ) For next N-1 lines, each line contains two vertices u and v, which is connected in the tree. Next line is a integer Q, representing the number of queries. (1 <= Q <= 10 5) For next Q lines, each with an integer u, as the root of the subtree described above.
Output
For each test case, output "Case #X:" first, X is the test number. Then output Q lines, each with a number -- the answer to each query.
Seperate each test case with an empty line.
Sample Input
Sample Output
|
树上的查询。
思路:首先dfs对节点进行编号,转化成线性的,然后离线处理。更新的时候注意,下面举个例子:
0 1 2 3 4
2 2 2 2 2
当处理到位置4的时候,要对2位置-2,1位置+1,3位置+1,因为2位置先前是1,-2的目的是,当查询(2,4)是结果是零,同理,原来1位置是-1,+1后变成0,这样同样保证查询的正确性。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100100;
int N,K,Q,idx;
int data1[maxn],data2[maxn];
int low[maxn],high[maxn],ans[maxn];
map<int,int> m;
vector<int> g[maxn];
vector<int> pos[maxn];
struct QU
{
int s,e,id;
bool operator < (const QU & a)const
{
return e<a.e;
}
}qu[maxn];
struct IntervalTree
{
int sum[maxn<<3];
void build(){memset(sum,0,sizeof(sum));}
void update(int o,int l,int r,int pos,int val)
{
sum[o]+=val;
if(l==r)return ;
int mid=(l+r)>>1;
if(pos<=mid)update(o<<1,l,mid,pos,val);
else update(o<<1|1,mid+1,r,pos,val);
}
int query(int o,int l,int r,int q1,int q2)
{
if(q1<=l&&r<=q2)return sum[o];
int mid=(l+r)>>1;
int ans=0;
if(q1<=mid)ans+=query(o<<1,l,mid,q1,q2);
if(q2>mid)ans+=query(o<<1|1,mid+1,r,q1,q2);
return ans;
}
}tree;
void dfs(int u,int fa)
{
low[u]=++idx;
data2[idx]=data1[u];
int len=g[u].size();
for(int i=0;i<len;i++)
{
int v=g[u][i];
if(fa==v)continue;
dfs(v,u);
}
high[u]=idx;
}
void init()
{
m.clear();
for(int i=0;i<=N;i++)
{
g[i].clear();
pos[i].clear();
}
idx=0;
}
int main()
{
int T,cas=1,first=1;
scanf("%d",&T);
while(T--)
{
if(first)first=0;
else printf("\n");
scanf("%d%d",&N,&K);
int dataid=0;
init();
for(int i=1;i<=N;i++)
{
scanf("%d",&data1[i]);
if(m.find(data1[i])==m.end())
m[data1[i]]=dataid++;
}
for(int i=1;i<N;i++)
{
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
}
dfs(1,-1);
scanf("%d",&Q);
for(int i=1;i<=Q;i++)
{
int s;
scanf("%d",&s);
qu[i].s=low[s];
qu[i].e=high[s];
qu[i].id=i;
}
sort(qu+1,qu+1+Q);
tree.build();
int num=1;
for(int i=1;i<=N;i++)
{
int tmp=m[data2[i]];
pos[tmp].push_back(i);
int cnt=pos[tmp].size();
if(cnt>=K)
{
if(cnt>K)tree.update(1,1,idx,pos[tmp][cnt-K-1],-2);
if(cnt>K+1)tree.update(1,1,idx,pos[tmp][cnt-K-2],1);
tree.update(1,1,idx,pos[tmp][cnt-K],1);
}
while(num<=Q&&qu[num].e==i)
{
ans[qu[num].id]=tree.query(1,1,idx,qu[num].s,qu[num].e);
num++;
}
}
printf("Case #%d:\n",cas++);
for(int i=1;i<Q;i++)printf("%d\n",ans[i]);
printf("%d\n",ans[Q]);
}
return 0;
}