codeforces 375D:Tree and Queries

Description

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

 

 

正解:莫队算法
解题报告:
  今天心血来潮要刷莫队算法的题。
  明明是一棵树,居然可以用莫队?!好吧是我大惊小怪了,dfs一遍,搞出dfs序,然后直接转成序列问题,询问、颜色全部转成序列的模式。
  一样的维护就可以了。不过需要注意,我们记录每种颜色的出现次数,和至少出现某种次数的颜色个数。开始有一点想不通怎么O(1)更新至少某种次数,感觉要gi,后来发现,反正莫队一定是每次修改一位,那么最大贡献一定是1,那么我们一路更改下去就可以了,不虚。
  好神,%%%。
  1 //It is made by jump~
  2 #include <iostream>
  3 #include <cstdlib>
  4 #include <cstring>
  5 #include <cstdio>
  6 #include <cmath>
  7 #include <algorithm>
  8 #include <ctime>
  9 #include <vector>
 10 #include <queue>
 11 #include <map>
 12 #include <set>
 13 #ifdef WIN32   
 14 #define OT "%I64d"
 15 #else
 16 #define OT "%lld"
 17 #endif
 18 using namespace std;
 19 typedef long long LL;
 20 const int MAXN = 200011;
 21 const int MAXM = 100011;
 22 int n,m;
 23 int ans[MAXM];
 24 int yan[MAXN],col[MAXN];
 25 int first[MAXN],to[MAXN*2],next[MAXN*2];
 26 int L[MAXN],R[MAXN];
 27 int ecnt;
 28 int cnt[MAXM];//记录每种颜色的个数
 29 int jilu[MAXM];//至少k个颜色的个数
 30 int nowl,nowr;
 31 
 32 struct wen{
 33     int l,r,id,k,belong;
 34 }q[MAXM];
 35 
 36 inline int getint()
 37 {
 38        int w=0,q=0;
 39        char c=getchar();
 40        while((c<'0' || c>'9') && c!='-') c=getchar();
 41        if (c=='-')  q=1, c=getchar();
 42        while (c>='0' && c<='9') w=w*10+c-'0', c=getchar();
 43        return q ? -w : w;
 44 }
 45 
 46 inline void dfs(int x,int fa){
 47     L[x]=++ecnt; col[ecnt]=yan[x];
 48     for(int i=first[x];i;i=next[i]) {
 49     int v=to[i];
 50     if(v!=fa) dfs(v,x);    
 51     }
 52     R[x]=ecnt;
 53 }
 54 
 55 inline bool cmp(wen p,wen pp){ if(p.belong==pp.belong) return p.r<pp.r; return p.belong<pp.belong; }
 56 
 57 inline void add(int x,int type){
 58     if(type==1) {
 59     cnt[col[x]]++;
 60     jilu[cnt[col[x]]]++;
 61     }else{
 62     jilu[cnt[col[x]]]--;
 63     cnt[col[x]]--;
 64     }
 65 }
 66 
 67 inline void work(){
 68     n=getint(); m=getint();
 69     for(int i=1;i<=n;i++) yan[i]=getint();
 70     int x,y;
 71     for(int i=1;i<n;i++) {
 72     x=getint(); y=getint();
 73     next[++ecnt]=first[x]; to[ecnt]=y; first[x]=ecnt;
 74     next[++ecnt]=first[y]; to[ecnt]=x; first[y]=ecnt;
 75     }
 76     ecnt=0;  dfs(1,0);
 77     int size=sqrt(n);
 78     for(int i=1;i<=m;i++) {
 79     x=getint(); q[i].l=L[x]; q[i].r=R[x]; q[i].id=i; q[i].k=getint();
 80     q[i].belong=(q[i].l-1)/size+1;
 81     }
 82     sort(q+1,q+m+1,cmp);
 83     for(int i=q[1].l;i<=q[1].r;i++) {
 84     cnt[col[i]]++;
 85     jilu[cnt[col[i]]]++;//只需一路添加就可以了,想想就知道并没有问题
 86     }
 87     ans[q[1].id]=jilu[q[1].k];
 88     nowl=q[1].l; nowr=q[1].r;
 89     for(int i=2;i<=m;i++) {
 90     while(nowl<q[i].l) add(nowl,-1),nowl++;
 91     while(nowl>q[i].l) add(nowl-1,1),nowl--;
 92     while(nowr<q[i].r) add(nowr+1,1),nowr++;
 93     while(nowr>q[i].r) add(nowr,-1),nowr--;
 94     ans[q[i].id]=jilu[q[i].k];
 95     }
 96     for(int i=1;i<=m;i++) printf("%d\n",ans[i]);
 97 }
 98 
 99 int main()
100 {
101   work();
102   return 0;
103 }

 

 

转载于:https://www.cnblogs.com/ljh2000-jump/p/5681849.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值