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. hdu 4372 Count the Buildings —— 思路+第一类斯特林数

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4372 首先,最高的会被看见: 然后考虑剩下 \( x+y-2 \) 个被看见的,每个带了一群被它挡住的楼, ...

  2. RPG游戏地牢设计的29个要点

    转自:http://www.gameres.com/491660.html Troy 是一名 RPG 开发者,以整理了一些自己开发地下城 RPG 的经验,开发者不妨参考一下: 1.地下城应该有个地方无 ...

  3. linux使用JMETER进行分布式压力测试

    1. 下载jmeter tgz文件   http://jmeter.apache.org/download_jmeter.cgi 2. 登录linux服务器,创建jmeter目录,rz上传jmeter ...

  4. java基础知识(7)---多态

    多 态:(面向对象特征之一):函数本身就具备多态性,某一种事物有不同的具体的体现.体现:父类引用或者接口的引用指向了自己的子类对象.//Animal a = new Cat();多态的好处:提高了程序 ...

  5. 关于导入excel报错的处理(xls,xlsx)

    关于导入excel报错的处理(xls,xlsx) 最近在做一个将excel导入到dataGriview中的小功能在做的过程中遇到以下问题: 链接excel的链接串是这样写的 string strCon ...

  6. CSS是什么?W3C是什么?W3C盒子与IE盒子的区别?

    (1)层叠样式(Cascading Style Sheets, CSS)是用来表现HTML或XML文本样式的语言.   (2)W3C推荐规范(World Wide Web Consortium,W3C ...

  7. foregroundservice的用处和用法

    由于android的系统资源回收机制,当内存不足的时候,会自动关闭一些后台服务,如果这时候我们的服务正在播放歌曲,由于被关闭,歌曲会被中断,这样会造成很差的用户体验. 这时候我们可以通过在servic ...

  8. 使用Cors后台设置WebAPI接口跨域访问

    昨天根据项目组前端开发工程师反映,在浏览器端无法直接使用ajax访问后台接口获取数据,根据他的反映,我查阅了相关跨域的解决方案: 一:使用jsonP,但是jsonP只能使用GET请求,完全不符合我项目 ...

  9. Python中使用json.loads解码字符串时出错:ValueError: Expecting property name: line 1 column 1 (char 1)

    解决办法,json数据只能用双引号,而不能用单引号

  10. SDUT2140图结构练习——判断给定图是否存在合法拓扑序列

    拓扑序列的判断方法为不存在有向环,代码实现的话有两种,一种是直接去判断是否存在环,较为难理解一些,另一种的话去判断结点入度,如果存在的入度为0的点大于一个,则该有向图肯定不存在一个确定的拓扑序列 #i ...