Codeforces375D Tree and Queries
题目链接
题目大意
给定一棵 \(n\) 个节点的树,根节点为 \(1\)。每个节点上有一个颜色 \(c_i\)
\(m\) 次询问。
每次询问给出 \(u\) \(k\):询问在以 \(u\) 为根的子树中,出现次数 \(≥k\) 的颜色有多少种。
解题思路
可以开棵权值线段树
如果当前颜色出现的次数 \(cnt[i] = x\), 就把树的第 \(x\) 个位置的值 \(+ 1\)
那么对于每个询问的 \(k\) 输出树的第 \(k\) 个位置的值即可
AC_Code
#include<bits/stdc++.h>
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,n,a) for (int i=n;i>=a;i--)
#define int long long
#define pb push_back
#define fi first
#define se second
using namespace std;
const int N = 3e5 + 10;
struct Tree{
int l , r , lazy , sum;
}tree[N << 2];
void push_up(int rt)
{
tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
}
void push_down(int rt)
{
int x = tree[rt].lazy;
tree[rt].lazy = 0;
tree[rt << 1].lazy = tree[rt << 1 | 1].lazy = x;
tree[rt << 1].sum += (tree[rt << 1].r - tree[rt << 1].l + 1) * x;
tree[rt << 1 | 1].sum += (tree[rt << 1 | 1].r - tree[rt << 1 | 1].l + 1) * x;
}
void build(int l , int r , int rt)
{
tree[rt].l = l , tree[rt].r = r , tree[rt].lazy = 0;
if(l == r)
{
tree[rt].sum = 0;
return ;
}
int mid = l + r >> 1;
build(l , mid , rt << 1);
build(mid + 1 , r , rt << 1 | 1);
push_up(rt);
}
void update_range(int L , int R , int rt , int val)
{
int l = tree[rt].l , r = tree[rt].r;
if(L <= l && r <= R)
{
tree[rt].lazy += val;
tree[rt].sum += (r - l + 1) * val;
return ;
}
push_down(rt);
int mid = l + r >> 1;
if(L <= mid) update_range(L , R , rt << 1 , val);
if(R > mid) update_range(L , R , rt << 1 | 1 , val);
push_up(rt);
}
int query_range(int L , int R , int rt)
{
int l = tree[rt].l , r = tree[rt].r;
if(L <= l && r <= R) return tree[rt].sum;
push_down(rt);
int mid = l + r >> 1 , ans = 0;
if(L <= mid) ans += query_range(L , R , rt << 1);
if(R > mid) ans += query_range(L , R , rt << 1 | 1);
return ans;
}
struct Edge{
int nex , to;
}edge[N << 1];
int head[N] , TOT;
void add_edge(int u , int v)
{
edge[++ TOT].nex = head[u] ;
edge[TOT].to = v;
head[u] = TOT;
}
int dep[N] , sz[N] , hson[N] , HH;
int col[N] , n , m , up;
int cnt[N] , sum[N];
vector<pair<int , int>>Q[N] , ans;
void dfs(int u , int far)
{
dep[u] = dep[far] + 1;
sz[u] = 1;
for(int i = head[u] ; i ; i = edge[i].nex)
{
int v = edge[i].to;
if(v == far) continue ;
dfs(v , u);
sz[u] += sz[v];
if(sz[v] > sz[hson[u]]) hson[u] = v;
}
}
void calc(int u , int far, int val)
{
cnt[col[u]] += val;
if(val == 1)
{
int k = cnt[col[u]];
update_range(k , k , 1 , 1);
}
if(val == -1)
{
int k = cnt[col[u]] + 1;
update_range(k , k , 1 , -1);
}
for(int i = head[u] ; i ; i = edge[i].nex)
{
int v = edge[i].to;
if(v == far || v == HH) continue ;
calc(v , u , val);
}
}
void dsu(int u , int far , int op)
{
for(int i = head[u] ; i ; i = edge[i].nex)
{
int v = edge[i].to;
if(v == far || v == hson[u]) continue ;
dsu(v , u , 0);
}
if(hson[u]) dsu(hson[u] , u , 1) , HH = hson[u];
calc(u , far , 1);
for(auto i : Q[u])
{
int id = i.fi , k = i.se;
int res = query_range(k , k , 1);
ans.pb(make_pair(id , res));
}
HH = 0;
if(!op) calc(u , far , -1);
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0) , cout.tie(0);
cin >> n >> m;
rep(i , 1 , n) cin >> col[i];
rep(i , 1 , n - 1)
{
int u , v;
cin >> u >> v;
add_edge(u , v) , add_edge(v , u);
}
rep(i , 1 , m)
{
int u , k;
cin >> u >> k;
Q[u].pb(make_pair(i , k));
}
build(1 , 100000 , 1);
dfs(1 , 0);
dsu(1 , 0 , 0);
sort(ans.begin() , ans.end());
for(auto i : ans) cout << i.se << '\n';
return 0;
}
Codeforces375D Tree and Queries的更多相关文章
- [Codeforces375D]Tree and Queries(莫队算法)
题意:给定一棵树,每个节点有颜色,对于每个询问(u,k)询问以u为根节点的子树下有多少种颜色出现次数>=k 因为是子树,跟dfs序有关,转化为一段区间,可以用莫队算法求解 直接用一个数组统计出现 ...
- [Codeforces Round #221 (Div. 1)][D. Tree and Queries]
题目链接:375D - Tree and Queries 题目大意:给你一个有n个点的树,每个点都有其对应的颜色,给出m次询问(v,k),问v的子树中有多少种颜色至少出现k次 题解:先对所有的询问进行 ...
- 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
Description You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...
- CF375D Tree and Queries
题意翻译 给出一棵 n 个结点的树,每个结点有一个颜色 c i . 询问 q 次,每次询问以 v 结点为根的子树中,出现次数 ≥k 的颜色有多少种.树的根节点是1. 感谢@elijahqi 提供的翻译 ...
- 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 ...
- Codeforces 375 D Tree and Queries
Discription You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...
- 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 ...
随机推荐
- dubbo配置的覆盖关系
dubbo推荐在Provider上尽量多配置Consumer端属性: 由服务提供方设置超时,因为一个方法需要执行多长时间,服务提供方更清楚,如果一个消费方同时引用多个服务,就不需要关心每个服务的参数设 ...
- 普通人如何站在时代风口学好AI?这是我看过最好的答案
摘要:当前,数据.算法.算力的发展突破正推动AI应用的逐步落地. AI是什么? 根据维基百科的定义,人工智能是一种新的通用目的技术(GPT, General Purpose Technology),它 ...
- Graph-GCN
前言 在大型图中,节点的低维向量embedding被证明了作为各种各样的预测和图分析任务的特征输入是非常有用的.顶点embedding最基本的基本思想是使用降维技术从高维信息中提炼一个顶点的邻居信息, ...
- D. Tavas and Malekas 解析(字串匹配)
Codeforce 535 D. Tavas and Malekas 解析(字串匹配) 今天我們來看看CF535D 題目連結 題目 給你一個字串$p$和一些$index$代表字串$p$在哪些位置會和長 ...
- MVC IIS 403.14
描述:HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this direc ...
- 21个写SQL的好习惯,你值得拥有
前言 每一个好习惯都是一笔财富,本文分SQL后悔药, SQL性能优化,SQL规范优雅三个方向,分享写SQL的21个好习惯,谢谢阅读,加油哈~ 公众号:捡田螺的小男孩 1. 写完SQL先explain查 ...
- 4-Kotlin数据类型与变量
一. 变量概念 在Kotlin中变量其实对应着内存中特定区域,变量就像是一个装"数据"的容器 1 val num1:Int = 100 2 var num2:Int = 99 关键 ...
- STM32入门系列-GPIO概念介绍
GPIO(general purpose intput output)是通用输入输出端口的简称,可以通过软件来控制其输入和输出.STM32 芯片的 GPIO 引脚与外部设备连接起来,从而实现与外部通讯 ...
- Android Choreographer 源码分析
Choreographer 的作用主要是配合 Vsync ,给上层 App 的渲染提供一个稳定的 Message 处理的时机,也就是 Vsync 到来的时候 ,系统通过对 Vsync 信号周期的调整, ...
- Vue+Antd搭配百度地图实现搜索定位等功能
前言 最近,在做vue项目的时候有做到选择地址功能,而原项目中又引入了百度地图,所以我就打算通过使用百度地图来实现地址搜索功能啦. 本次教程可能过于啰嗦,所以这里先放上预览地址供大家预览--点我预览, ...