http://codeforces.com/problemset/problem/375/D

树莫队就是把树用dfs序变成线性的数组。 (原数组要根据dfs的顺序来变化)

然后和莫队一样的区间询问。

这题和普通莫队有点区别,他需要的不单单是统计区间元素种类个数,是区间元素种类个数 >= k[i]的个数。

考虑最简单的用bit维护,复杂度多了个log

观察到每次只需要 + 1  或者 -1

用一个数组sum[k]表示种类数大于等于k的ans

在numc[val]++之后,sum[numc[val]]++,表明这个种类出现次数是numc[val]次的贡献递增1

在num[val]--之前,就需要把sum[numc[val]]--,表明贡献减1了

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
const int maxn = 1e5 + ;
struct Edge {
int u, v, tonext;
}e[maxn * ];
int first[maxn], num;
int c[maxn];
void addEdge(int u, int v) {
++num;
e[num].u = u, e[num].v = v, e[num].tonext = first[u];
first[u] = num;
}
int L[maxn], R[maxn], dfs_clock;
int color[maxn];
void dfs(int cur, int fa) {
L[cur] = ++dfs_clock;
color[dfs_clock] = c[cur];
for (int i = first[cur]; i; i = e[i].tonext) {
int v = e[i].v;
if (fa == v) continue;
dfs(v, cur);
}
R[cur] = dfs_clock;
}
int magic;
struct Node {
int L, R, k, id;
bool operator < (const struct Node & rhs) const {
if (L / magic != rhs.L / magic) return L / magic < rhs.L / magic;
else return R < rhs.R;
}
}query[maxn];
int ans[maxn], numc[maxn];
int bit[maxn];
int lowbit(int x) {
return x & (-x);
}
void add(int pos, int val) {
while (pos) {
bit[pos] += val;
pos -= lowbit(pos);
}
}
int ask(int pos) {
int ans = ;
while (pos <= maxn - ) {
ans += bit[pos];
pos += lowbit(pos);
}
return ans;
}
int suffix[maxn];
void work() {
int n, m;
cin >> n >> m;
for (int i = ; i <= n; ++i) cin >> c[i];
for (int i = ; i <= n - ; ++i) {
int u, v;
scanf("%d%d", &u, &v);
addEdge(u, v);
addEdge(v, u);
}
dfs(, );
magic = (int)sqrt(n + 0.5);
for (int i = ; i <= m; ++i) {
int which, k;
scanf("%d%d", &which, &k);
query[i].L = L[which], query[i].R = R[which], query[i].k = k;
query[i].id = i;
}
sort(query + , query + + m);
int L = , R = ;
int temp = ;
for (int i = ; i <= m; ++i) {
while (R < query[i].R) {
++R;
suffix[++numc[color[R]]]++;
}
while (R > query[i].R) {
suffix[numc[color[R]]--]--;
--R;
}
while (L < query[i].L) {
suffix[numc[color[L]]--]--;
L++;
}
while (L > query[i].L) {
L--;
suffix[++numc[color[L]]]++;
}
ans[query[i].id] = suffix[query[i].k];
}
for (int i = ; i <= m; ++i) {
printf("%d\n", ans[i]);
}
}
int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

Tree and Queries CodeForces - 375D 树上莫队的更多相关文章

  1. SPOJ COT2 - Count on a tree II(LCA+离散化+树上莫队)

    COT2 - Count on a tree II #tree You are given a tree with N nodes. The tree nodes are numbered from  ...

  2. Codeforces 375D Tree and Queries(DFS序+莫队+树状数组)

    题目链接  Tree and Queries 题目大意  给出一棵树和每个节点的颜色.每次询问$vj, kj$ 你需要回答在以$vj$为根的子树中满足条件的的颜色数目, 条件:具有该颜色的节点数量至少 ...

  3. spoj COT2 - Count on a tree II 树上莫队

    题目链接 http://codeforces.com/blog/entry/43230树上莫队从这里学的,  受益匪浅.. #include <iostream> #include < ...

  4. 【SPOJ】Count On A Tree II(树上莫队)

    [SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...

  5. 「日常训练&知识学习」莫队算法(二):树上莫队(Count on a tree II,SPOJ COT2)

    题意与分析 题意是这样的,给定一颗节点有权值的树,然后给若干个询问,每次询问让你找出一条链上有多少个不同权值. 写这题之前要参看我的三个blog:Codeforces Round #326 Div. ...

  6. COT2 - Count on a tree II(树上莫队)

    COT2 - Count on a tree II You are given a tree with N nodes. The tree nodes are numbered from 1 to N ...

  7. Codeforces 852I Dating 树上莫队

    Dating 随便树上莫队搞一搞就好啦. #include<bits/stdc++.h> #define LL long long #define LD long double #defi ...

  8. SP10707 COT2 - Count on a tree II (树上莫队)

    大概学了下树上莫队, 其实就是在欧拉序上跑莫队, 特判lca即可. #include <iostream> #include <algorithm> #include < ...

  9. SPOJ COT2 Count on a tree II (树上莫队,倍增算法求LCA)

    题意:给一个树图,每个点的点权(比如颜色编号),m个询问,每个询问是一个区间[a,b],图中两点之间唯一路径上有多少个不同点权(即多少种颜色).n<40000,m<100000. 思路:无 ...

随机推荐

  1. category extension

    1 category 分类,只能添加方法,添加的方法成为原来类的一部分,达到扩展类的目的,可以被子类继承 主要给没有源代码的类 添加方法,团队合作项目互不影响,不该动原有类的基础上添加方法 self ...

  2. HDOJ1238(string)

    Substrings Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  3. Webpack 基础使用

    使用webstorm编译: 1.新建一个工程(最基本的工程) 2.在webstorm的控制台,使用命令行     cnpm install webpack  -g  全局安装 3.安装完后,可以使用 ...

  4. 使用py 和flask 实现的服务器系统目录浏览,日志文件实时显示到网页的功能

    看日志希望带有彩色,希望从浏览器上看到,不用连到机器上看. 浏览系统的文件夹,scan + 系统文件夹的层级名字当做url路由,可以深层次看到机器上任何层级的文件夹,实现系统文件夹浏览下载. 如果是点 ...

  5. Android开发者学习必备:10个优质的源码供大家学习

    最近看了一些开发的东西,汇总了一些源码.希望可以给大家有些帮助! 1.Android 源码解析—PagerSlidingTabStrippagerSlidingTabStrip 实现联动效果的原理是, ...

  6. “在注释中遇到意外的文件结束”--记一个令人崩溃的bug

    下午写程序,写的好好的,突然报错"在注释中遇到意外的文件结束". 下面是官方给出的错误原因是缺少注释终结器 (* /). // C1071.cpp int main() { } / ...

  7. [原创]SQL表值函数:返回自定义时间段的日期数据

    跟以往类似,我依旧介绍一个我日常开发遇到的知识点,谨此记录一下,也希望能帮助到一些朋友. 这次我要介绍的是通过SQL函数返回你输入的两个时间点内的日期数据. 效果图如下: 执行函数:SELECT * ...

  8. CentOS 6.5 and Ubuntu 14.04 使用外部邮箱发送邮件

    我们可以使用外部邮箱(163,126,gmail,139等等)为我们发邮件 for CentOS 6.5 yum -y install mailx vi /etc/mail.rc 在文件的末行添加以下 ...

  9. python 列表中 [[], [], []] 和 3*[[]]差异

    问: What's the difference between "[[], [], []]" and "3*[[]]" ? 答: [[], [], []] m ...

  10. 手机连接fiddler后,浏览器无法打开网页或者fiddler抓取不到手机应用相关数据的情况

    关于手机如何连接fiddler,网上有很多教程,我暂时就不写了 今天在使用fiddler的过程中,发现fiddler突然无法抓取移动端应用的数据包,再三确认连接无误.因此就开始了解决之旅 起因是安卓手 ...