Tree and Queries CodeForces - 375D 树上莫队
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 树上莫队的更多相关文章
- 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 ...
- Codeforces 375D Tree and Queries(DFS序+莫队+树状数组)
题目链接 Tree and Queries 题目大意 给出一棵树和每个节点的颜色.每次询问$vj, kj$ 你需要回答在以$vj$为根的子树中满足条件的的颜色数目, 条件:具有该颜色的节点数量至少 ...
- spoj COT2 - Count on a tree II 树上莫队
题目链接 http://codeforces.com/blog/entry/43230树上莫队从这里学的, 受益匪浅.. #include <iostream> #include < ...
- 【SPOJ】Count On A Tree II(树上莫队)
[SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...
- 「日常训练&知识学习」莫队算法(二):树上莫队(Count on a tree II,SPOJ COT2)
题意与分析 题意是这样的,给定一颗节点有权值的树,然后给若干个询问,每次询问让你找出一条链上有多少个不同权值. 写这题之前要参看我的三个blog:Codeforces Round #326 Div. ...
- 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 ...
- Codeforces 852I Dating 树上莫队
Dating 随便树上莫队搞一搞就好啦. #include<bits/stdc++.h> #define LL long long #define LD long double #defi ...
- SP10707 COT2 - Count on a tree II (树上莫队)
大概学了下树上莫队, 其实就是在欧拉序上跑莫队, 特判lca即可. #include <iostream> #include <algorithm> #include < ...
- SPOJ COT2 Count on a tree II (树上莫队,倍增算法求LCA)
题意:给一个树图,每个点的点权(比如颜色编号),m个询问,每个询问是一个区间[a,b],图中两点之间唯一路径上有多少个不同点权(即多少种颜色).n<40000,m<100000. 思路:无 ...
随机推荐
- Python:内置函数zip()
zip函数接受任意多个可迭代对象作为参数,将对象中对应的元素打包成一个tuple,然后返回一个可迭代的zip对象. 这个可迭代对象可以使用循环的方式列出其元素 若多个可迭代对象的长度不一致,则所返回的 ...
- CSS 关于文本 背景 边框整理
文本与字体 1)阴影:text-shadow 格式:text-shadow:5px 5px 3px #FFFFFF分别对应 水平方向 垂直方向 模糊程度 颜色值 代码: <!DOCTYPE ht ...
- 问题:C# params类型参数;结果:C#的参数类型:params、out和ref
C#的参数类型:params.out和ref PS:由于水平有限,难免会有错误和遗漏,欢迎各位看官批评和指正,谢谢~ 首先回顾一下C#声明一个方法的语法和各项元素,[]代表可选 [访问修饰符] 返回值 ...
- iOS开发者福利之精品源码汇总!免费下载
汇总一些看着不错的源码,有需要的朋友过来下载吧!{:4_102:} 1.用swift制作的色彩炫丽的进度条-KDCircularProgressKDCircularProgress是使用swift制作 ...
- .Net下RabbitMQ发布订阅模式实践
一.概念AMQP,即Advanced Message Queuing Protocol,高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计.消息中间件主要用于组件之间的解耦,消息的发 ...
- 21 、GPD-PSL-VCF
https://genome.ucsc.edu/FAQ/FAQformat.html#format9 1.Variant Call Format(VCF) Example ##fileformat=V ...
- Entity Framework Code-First(2):What is Code-First?
What is Code-First?: Entity Framework introduced Code-First approach from Entity Framework 4.1. Code ...
- Spring入门第三课
属性注入 属性注入就是通过setter方法注入Bean的属性值或依赖的对象. 属性植入使用<property>元素,使用name属性指定Bean的属性名称,value属性或者<val ...
- VS2010和VS2015的Dll项目
最近在使用公司VS2010开发的老的项目时,发现一些问题 公司用VS2010开发了一个项目,生成 GUS_TestIdentity.dll, 放在 C:\Windows\assembly 中 当在另一 ...
- 理解setTimeout和setInterval
setTimeout和setInterval,这两个js函数都是用来定时执行.setTimeout执行一次,setInterval执行多次. 问题出现在今天,使用setInterval是,设置执行速度 ...