Codeforces 375D Tree and Queries(DFS序+莫队+树状数组)
题目链接 Tree and Queries
题目大意 给出一棵树和每个节点的颜色。每次询问$vj, kj$
你需要回答在以$vj$为根的子树中满足条件的的颜色数目,
条件:具有该颜色的节点数量至少为$kj$。
(莫队居然可以过)
首先转$DFS$序,这样就变成了区间查询。
然后直接套用莫队,求出每次询问状态下的$t[],t[k]$表示当前区间内拥有$k$个节点的颜色数量。
然后统计$t[k] + t[k + 1], ..., t[MAX]$即可,这个过程用树状数组维护。
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) const int N = 1e5 + 10;
const int d = N + 10; int b[N], c[N * 3], f[N];
int in[N], out[N];
int a[N], belong[N];
int n, m, ti = 0, bs = 0;
int l, r;
int ans[N];
vector <int> v[N]; struct node{
int v, k, id;
int l, r;
friend bool operator < (const node &a, const node &b){
return belong[a.l] == belong[b.l] ? a.r < b.r : belong[a.l] < belong[b.l];
}
} q[N]; inline void update(int x, int val){
for (; x <= (N * 3 - 10); x += x & -x) c[x] += val; } inline int query(int x){
if (x == 0) return c[x];
int ret = 0;
for (; x; x -= x & -x) ret += c[x];
return ret;
} void dfs(int x, int fa){
in[x] = ++ti;
for (auto u : v[x]){
if (u == fa) continue;
dfs(u, x);
}
out[x] = ti;
} int main(){ scanf("%d%d", &n, &m);
rep(i, 1, n) scanf("%d", a + i);
rep(i, 2, n){
int x, y;
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
} dfs(1, 0); rep(i, 1, n) b[in[i]] = i; rep(i, 1, m){
scanf("%d%d", &q[i].v, &q[i].k);
q[i].id = i;
q[i].l = in[q[i].v];
q[i].r = out[q[i].v];
} bs = sqrt(n);
rep(i, 1, n) belong[i] = (i - 1) / bs + 1;
sort(q + 1, q + m + 1); update(d, n); l = 1, r = 0; rep(i, 1, m){
while (l > q[i].l){
--l;
update(f[a[b[l]]] + d, -1);
++f[a[b[l]]];
update(f[a[b[l]]] + d, 1);
} while (l < q[i].l){
update(f[a[b[l]]] + d, -1);
--f[a[b[l]]];
update(f[a[b[l]]] + d, 1);
++l;
} while (r < q[i].r){
++r;
update(f[a[b[r]]] + d, -1);
++f[a[b[r]]];
update(f[a[b[r]]] + d, 1);
} while (r > q[i].r){
update(f[a[b[r]]] + d, -1);
--f[a[b[r]]];
update(f[a[b[r]]] + d, 1);
--r;
}
ans[q[i].id] = max(0, query(N * 3 - 10) - query(q[i].k - 1 + d));
} rep(i, 1, m) printf("%d\n", ans[i]);
return 0;
}
Codeforces 375D Tree and Queries(DFS序+莫队+树状数组)的更多相关文章
- Codeforces 375D - Tree and Queries(dfs序+莫队)
题目链接:http://codeforces.com/contest/351/problem/D 题目大意:n个数,col[i]对应第i个数的颜色,并给你他们之间的树形关系(以1为根),有m次询问,每 ...
- bzoj 3289: Mato的文件管理 莫队+树状数组
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description Mato同学 ...
- 【BZOJ3460】Jc的宿舍(树上莫队+树状数组)
点此看题面 大致题意: 一棵树,每个节点有一个人,他打水需要\(T_i\)的时间,每次询问两点之间所有人去打水的最小等待时间. 伪·强制在线 这题看似强制在线,但实际上,\(pre\ mod\ 2\) ...
- bzoj3236 作业 莫队+树状数组
莫队+树状数组 #include<cstdio> #include<cstring> #include<iostream> #include<algorith ...
- BZOJ_3289_Mato的文件管理_莫队+树状数组
BZOJ_3289_Mato的文件管理_莫队+树状数组 Description Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号 .为了防止他人 ...
- BZOJ3236[Ahoi2013]作业——莫队+树状数组/莫队+分块
题目描述 输入 输出 样例输入 3 4 1 2 2 1 2 1 3 1 2 1 1 1 3 1 3 2 3 2 3 样例输出 2 2 1 1 3 2 2 1 提示 N=100000,M=1000000 ...
- COGS.1822.[AHOI2013]作业(莫队 树状数组/分块)
题目链接: COGS.BZOJ3236 Upd: 树状数组实现的是单点加 区间求和,采用值域分块可以\(O(1)\)修改\(O(sqrt(n))\)查询.同BZOJ3809. 莫队为\(O(n^{1. ...
- 51nod 1290 Counting Diff Pairs | 莫队 树状数组
51nod 1290 Counting Diff Pairs | 莫队 树状数组 题面 一个长度为N的正整数数组A,给出一个数K以及Q个查询,每个查询包含2个数l和r,对于每个查询输出从A[i]到A[ ...
- HihoCoder 1488 : 排队接水(莫队+树状数组)
描述 有n个小朋友需要接水,其中第i个小朋友接水需要ai分钟. 由于水龙头有限,小Hi需要知道如果为第l个到第r个小朋友分配一个水龙头,如何安排他们的接水顺序才能使得他们等待加接水的时间总和最小. 小 ...
随机推荐
- linux文件属性描述(inode,block)
1.ls -lhi 文字解释: 第一列:inode索引节点编号(人的身份证,全国唯一) 系统读取文件时首先通过文件名找到inode,然后才能读取到文件内容. 第二列:文件类型及权限(共10个字符, ...
- linux中怎样关闭ICMP回应功能
引用自:http://blog.csdn.net/qq844352155/article/details/49700121 linux中怎样关闭ICMP回应功能 输入: echo 1 > ...
- 最长公共子序列(LCS)问题
最长公共子串(Longest Common Substirng)和最长公共子序列(Longest Common Subsequence,LCS)的区别为:子串是串的一个连续的部分,子序列则是从不改变序 ...
- LayoutInflater的用法
Instantiates a layout XML file into its corresponding View objects. It is never used directly. Inste ...
- NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config的问题
故障问题: 使用springMVC3.05 ,tomcat服务器进行开发, Spring配置正确,console有输出,但是url打开时不能出现页面,提示错误信息为:NoClassDefFoundEr ...
- dev c++ 提示没有iostream.h文件
dev c++ 提示没有iostream.h文件 解决办法路径没有打通最好是这样写:#include <iostream>using namespace std;int main(int ...
- aiomysql inserting operation failed !
emotions: those days,i am using aiomysql(python3.5) to acess my database .But a 'strange' problem ma ...
- Balanced Lineup(ST)
描述 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. On ...
- 面向对象编程(二)封装--构造方法,this关键字,static关键字,方法重载
面向对象三大特点:封装.继承.多态 封装概念 ① 将东西包装在一起,然后以新的完整形式呈现出来: 将方法和字段一起包装到一个单元中,单元以类的形式实现; ② 信息隐藏,隐藏对象的实现细节,不让 ...
- php preg_replace去除html xml 注释
php preg_replace去除html xml 注释 //不确定是否最优 $content = preg_replace('/<!--((?!-->).)*-->/s', '' ...