对于一个询问(x,y)对y出现次数分类,若<=lim,在儿子处统计答案,若>lim则y的种类肯定<lim,在祖先处统计(仿佛要去重?但是没去重也过了,那个时限仿佛怎么做都能过)

#include<cstdio>
#include<algorithm>
#include<vector>
#define pr pair<int,int>
#define mp make_pair
#define fr first
#define sc second
using namespace std;
int cnt,last[1000005],Sum[1000005],c[1000005],sz[1000005];
long long ANS[1000005];
vector<pr> vec1[1000005],vec2[1000005];
struct node{
int to,next;
}e[1000005];
void add(int a,int b){
e[++cnt].to=b;
e[cnt].next=last[a];
last[a]=cnt;
}
void solve1(int x,int fa){
for (int i=0; i<(int)vec1[c[x]].size(); i++){
pr q=vec1[c[x]][i];
ANS[q.fr]+=Sum[q.sc];
}
Sum[c[x]]++;
for (int i=last[x]; i; i=e[i].next){
int V=e[i].to;
if (V==fa) continue;
solve1(V,x);
}
Sum[c[x]]--;
}
void solve2(int x,int fa){
Sum[c[x]]++;
for (int i=0; i<(int)vec2[c[x]].size(); i++){
pr q=vec2[c[x]][i];
ANS[q.fr]-=Sum[q.sc];
}
for (int i=last[x]; i; i=e[i].next){
int V=e[i].to;
if (V==fa) continue;
solve2(V,x);
}
for (int i=0; i<(int)vec2[c[x]].size(); i++){
pr q=vec2[c[x]][i];
ANS[q.fr]+=Sum[q.sc];
}
}
int main(){
int n,R,q;
scanf("%d%d%d",&n,&R,&q);
scanf("%d",&c[1]);
sz[c[1]]++;
int lim=500;
for (int i=2; i<=n; i++){
int x;
scanf("%d%d",&x,&c[i]);
sz[c[i]]++;
add(x,i);
add(i,x);
}
for (int i=1; i<=q; i++){
int X,Y;
scanf("%d%d",&X,&Y);
if (sz[Y]<=lim) vec1[Y].push_back(mp(i,X));
else vec2[X].push_back(mp(i,Y));
}
solve1(1,0);
solve2(1,0);
for (int i=1; i<=q; i++) printf("%lld\n",ANS[i]);
return 0;
}

  

最新文章

  1. python 操作execl文件
  2. Prism简介
  3. C/C++基本数据类型
  4. 安装了VS2010 sp1 后再安装ASP.NET MVC 3.0的问题(Final Result: Installation failed with error code: (0x80070643), &quot;安装时发生严重错误 &quot; (Ela)
  5. C/C++综合測试题(三)
  6. C++程序中应增加STL、运算和字符串的头文件
  7. 9、JavaScript常用函数
  8. Dokan简介[转]
  9. Lucene查询结果高亮
  10. 实现select联动效果,数据从后台获取
  11. Python OpenCV 图像相识度对比
  12. oracle:TNS:监听程序无法分发客户机连接
  13. 软件可维护性的影响因素&amp;如何提升
  14. 剖析ElasticSearch核心概念,NRT,索引,分片,副本等
  15. java中变量关系
  16. JS的Ajax和同源策略
  17. parted 分区命令
  18. python flask demo
  19. Python处理HTML转义字符
  20. 为什么要用MQ

热门文章

  1. 一键部署WordPress开源内容管理系统
  2. C#语言基础 Main 函数中的输出输入
  3. codeforce Gym 100685E Epic Fail of a Genie(MaximumProduction 贪心)
  4. Typescript的优势
  5. (六)VMware Harbor简单使用
  6. tpcc-mysql的安装和使用
  7. ubuntu开放端口
  8. x86,x64,i386,i686
  9. 解决升级mac os X EI Capitan后遇到LibclangError: dlopen(libclang.dylib, 6): image not found.的问题
  10. cocos2dx 3.x c++代码打包给lua调用过程(mac)