CodeForces 375D Tree and Queries
传送门:https://codeforces.com/problemset/problem/375/D
题意:
给你一颗有根树,树上每个节点都有其对应的颜色,有m次询问,每次问你以点v为父节点的子树内满足某种颜色的数量大于k的颜色一共有多少种
题解:
冷静分析,胡乱分析,询问次数这么多,但是并没有修改操作,倘若把询问离线下来就好了
可是询问问的是每个点的树
这个时候我们的dfs序就可以派上用场了
dfs序:"所谓DFS序, 就是DFS整棵树依次访问到的结点组成的序列"
"DFS序有一个很强的性质: 一颗子树的所有节点在DFS序内是连续的一段, 利用这个性质我们可以解决很多问题"
通过对树进行一次dfs序操作,我们可以得到每个点他的子树所在的一段区间,这样我们就可以得到每个询问在dfs序下询问的区间了,那么我们怎么统计区间内颜色数量大于k的种类数量呢?
由于我们得到了离线下来的询问区间,用莫队算法可以在O(nsqrt(n))的时间内得到所有询问的答案
由于每次询问是查询区间内颜色种类的数量之和,我们需要用一个区间求和,单点修改的数据结构来维护莫队时统计答案和修改操作,因此我们可以用树状数组或者线段树来维护
代码:
/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
// warm heart, wagging tail,and a smile just for you!
//
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// .' \| |// `.
// / \||| : |||// \
// / _||||| -:- |||||- \
// | | \\ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// 佛祖保佑 永无BUG
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3f;
struct EDGE {
int v, nxt;
} edge[maxn * 2];
int head[maxn];
int tot;
void add_edge(int u, int v) {
edge[tot].v = v;
edge[tot].nxt = head[u];
head[u] = tot++;
}
int n, m;
int col[maxn];
int dfn[maxn], st[maxn], ed[maxn], cnt;
int vis[maxn];
int num[maxn];
int bit[maxn];
int ans[maxn];
void dfs(int u, int fa, int depth) {
st[u] = ++cnt;
num[cnt] = col[u];
for(int i = head[u]; i != -1; i = edge[i].nxt) {
int v = edge[i].v;
if(v != fa) {
dfs(v, u, depth + 1);
}
}
ed[u] = cnt;
}
vector<int> vec[maxn];
int v[maxn];
int k[maxn];
struct node {
int l, r, id, k;
} q[maxn];
int pos[maxn];
bool cmp(node a, node b) {
if(pos[a.l] == pos[b.l]) return a.r < b.r;
return pos[a.l] < pos[b.l];
}
int lowbit(int x) {
return x & -x;
}
void update(int pos, int x) {
if(pos <= 0) return;
while(pos < maxn) {
bit[pos] += x;
pos += lowbit(pos);
}
}
int query(int pos) {
int ans = 0;
while(pos) {
ans += bit[pos];
pos -= lowbit(pos);
}
return ans;
}
void add(int x) {
update(vis[num[x]], -1);
vis[num[x]]++;
update(vis[num[x]], 1);
}
void del(int x) {
update(vis[num[x]], -1);
vis[num[x]]--;
update(vis[num[x]], 1);
}
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) {
scanf("%d", &col[i]);
}
memset(head, -1, sizeof(head));
tot = cnt = 0;
for(int i = 1, u, v; i <= n - 1; i++) {
scanf("%d%d", &u, &v);
add_edge(u, v);
add_edge(v, u);
}
dfs(1, 0, 1);
int sz = sqrt(cnt);
for(int i = 1; i <= cnt; i++) {
pos[i] = i / sz;
}
for(int i = 1; i <= m; i++) {
scanf("%d%d", &v[i], &k[i]);
q[i].l = st[v[i]];
q[i].r = ed[v[i]];
q[i].id = i;
q[i].k = k[i];
}
sort(q + 1, q + m + 1, cmp);
int L = 1, R = 0;
for(int i = 1; i <= m; i++) {
while(L < q[i].l) {
L++;
del(L - 1);
}
while(L > q[i].l) {
add(L - 1);
L--;
}
while(R < q[i].r) {
R++;
add(R);
}
while(R > q[i].r) {
del(R);
R--;
}
ans[q[i].id] = query(maxn - 1) - query(q[i].k - 1);
}
for(int i = 1; i <= m; i++) {
printf("%d\n", ans[i]);
}
return 0;
}
CodeForces 375D Tree and Queries的更多相关文章
- Codeforces 375D Tree and Queries(DFS序+莫队+树状数组)
题目链接 Tree and Queries 题目大意 给出一棵树和每个节点的颜色.每次询问$vj, kj$ 你需要回答在以$vj$为根的子树中满足条件的的颜色数目, 条件:具有该颜色的节点数量至少 ...
- CodeForces 375D Tree and Queries 莫队||DFS序
Tree and Queries 题意:有一颗以1号节点为根的树,每一个节点有一个自己的颜色,求出节点v的子数上颜色出现次数>=k的颜色种类. 题解:使用莫队处理这个问题,将树转变成DFS序区间 ...
- CodeForces - 375D Tree and Queries (莫队+dfs序+树状数组)
You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. We will ass ...
- Codeforces 375D - Tree and Queries(dfs序+莫队)
题目链接:http://codeforces.com/contest/351/problem/D 题目大意:n个数,col[i]对应第i个数的颜色,并给你他们之间的树形关系(以1为根),有m次询问,每 ...
- codeforces 375D . Tree and Queries 启发式合并 || dfs序+莫队
题目链接 一个n个节点的树, 每一个节点有一个颜色, 1是根节点. m个询问, 每个询问给出u, k. 输出u的子树中出现次数大于等于k的颜色的数量. 启发式合并, 先将输入读进来, 然后dfs完一个 ...
- CodeForces 376F Tree and Queries(假·树上莫队)
You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. We will ass ...
- CF 375D. Tree and Queries【莫队 | dsu on tree】
题意: 一棵树,询问一个子树内出现次数$≥k$的颜色有几种 强制在线见上一道 用莫队不知道比分块高到哪里去了,超好写不用调7倍速度!!! 可以用分块维护出现次数这个权值,实现$O(1)-O(\sqrt ...
- CF 375D. Tree and Queries加强版!!!【dfs序分块 大小分类讨论】
传送门 题意: 一棵树,询问一个子树内出现次数$\ge k$的颜色有几种,Candy?这个沙茶自带强制在线 吐槽: 本来一道可以离散的莫队我非要强制在线用分块做:上午就开始写了然后发现思路错了...: ...
- [Codeforces Round #221 (Div. 1)][D. Tree and Queries]
题目链接:375D - Tree and Queries 题目大意:给你一个有n个点的树,每个点都有其对应的颜色,给出m次询问(v,k),问v的子树中有多少种颜色至少出现k次 题解:先对所有的询问进行 ...
随机推荐
- MyEclipse2014,java文件无法编译,run as上是none applicable,不是文件本身的问题
1.配置一下JDK目录 2.window -> Preferences -> java -> Installed JREs -> Add
- spider csdn blog part II
继续上次的笔记, 继续完善csdn博文的提取. 发现了非常好的模块. html2docx 结果展示: 运行之后, 直接生成docx文档. 截个图如下: 结果已经基本满意了!!! 在编写过程中的一些感想 ...
- 2019-8-31-C#-将-Begin-和-End-异步方法转-task-异步
title author date CreateTime categories C# 将 Begin 和 End 异步方法转 task 异步 lindexi 2019-08-31 16:55:58 + ...
- 洛谷P3366 【模板】最小生成树(kuskal)
#include<bits/stdc++.h> using namespace std; ; ; struct node{ int cnt,fa; }f[maxn]; inline voi ...
- iOS开发那些事-响应内存警告
好的应用应该在系统内存警告情况下释放一些可以重新创建的资源.在iOS中我们可以在应用程序委托对象.视图控制器以及其它类中获得系统内存警告消息. 1.应用程序委托对象 在应用程序委托对象中接收内存警告消 ...
- H3C 常用信息查看命令
- PHPstorm相关设置以及快捷键
转自:http://blog.csdn.net/fenglailea/article/details/12166617 1.界面中文方框问题 Settings->Appearance中Theme ...
- oracle避免在索引列上使用计算
WHERE子句中,如果索引列是函数的一部分.优化器将不使用索引而使用全表扫描. 举例: 低效: SELECT … FROM DEPT WHERE SAL * 12 > 25000; 高效: SE ...
- Ant design在vue,react的引入
文章地址: https://www.cnblogs.com/sandraryan/ 最近由于 一些不可描述的原因 要研究一下Ant design这个前端框架. 祭上官网: https://ant.de ...
- Python 3.7.0 For Mac版软件安装教程附下载地址
https://www.jianshu.com/p/f02d6f01eba7