codechef Tree and Queries Solved
题目链接:
大概是:修改点值,求子树节点为0有多少个,
DFS序后,BIT 询问,修改
1 #include<bits/stdc++.h>
2
3 using namespace std;
4 typedef long long ll;
5
6 #define N 223456
7 ll val[N];
8 int l[N],r[N];
9 int t=;
int f[N];
int lowbit(int x)
{
return x&-x;
}
void update(int x,int v)
{
while (x<N)
{
f[x]+=v;
x+=lowbit(x);
}
}
ll query(int x)
{
ll s=;
while (x)
{
s+=f[x];
x-=lowbit(x);
}
return s;
}
vector<int>mp[N];
void dfs(int u,int pre)
{
l[u]=t++;
for (int i=;i<mp[u].size();i++)
{
int v=mp[u][i];
if (v==pre) continue;
dfs(v,u);
}
r[u]=t-;
}
int main()
{
int n,Q;
scanf("%d%d",&n,&Q);
for (int i=;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
mp[x].push_back(y);
mp[y].push_back(x);
}
dfs(,);
for (int i=;i<=n;i++)
{
scanf("%d",&val[i]);
if (val[i]==) update(l[i],);
}
while (Q--)
{
char s[];
scanf("%s",s);
if (s[]=='U')
{
int u,v;
scanf("%d%d",&u,&v);
if (val[u]==) update(l[u],-);
val[u]+=v;
if (val[u]==) update(l[u],);
}else
{
int x;
scanf("%d",&x);
int ans=query(r[x])-query(l[x]-);
printf("%d\n",ans);
}
}
return ;
}
codechef Tree and Queries Solved的更多相关文章
- [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 ...
- CodeChef DISTNUM2 Easy Queries 节点数组线段树
Description You are given an array A consisting of N positive integers. You have to answer Q queries ...
随机推荐
- 实体类和JSON对象之间相互转化
. [代码]工具类 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3 ...
- substring substr slice 区别
1. substring(start,end) 返回指定索引区间的字串,不改变原字符串 start 必需,开始位置的索引,一个非负的整数 end 可选,结束位置的索引(不包括其本身),如果未设置, ...
- Codeforces Round #510 #C Array Product
http://codeforces.com/contest/1042/problem/C 给你一个有n个元素序列,有两个操作:1,选取a[i]和a[j],删除a[i],将$a[i]*a[j]$赋值给a ...
- 常用模块之configpaser与shutil
configparser模块 定义:configparser翻译为配置解析,即它是用来解析配置文件的 配置文件:用于编写程序的配置信息的文件 配置文件编写格式 配置文件中只允许出现两种类型的数据 se ...
- 排序算法C语言实现——冒泡、快排、堆排对比
对冒泡.快排.堆排这3个算法做了验证,结果分析如下: 一.结果分析 时间消耗:快排 < 堆排 < 冒泡. 空间消耗:冒泡O(1) = 堆排O(1) < 快排O(logn)~O(n) ...
- 【javascript面试题】5个经典的面试题
问题1: 作用域 看一下下面的代码: (function(){ var a = b =5; })(); console.log(b); 结果会输出什么? 答案: 5 这个问题考查的要点是两个不同的作用 ...
- angular-websocket.js 使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- appium+python自动化-adb文件导入和导出(pull push)
前言 用手机连电脑的时候,有时候需要把手机(模拟器)上的文件导出到电脑上,或者把电脑的图片导入手机里做测试用,我们可以用第三方的软件管理工具直接复制粘贴,也可以直接通过adb命令导入和导出. adb ...
- 面试准备——redis
https://blog.csdn.net/yangzhong0808/article/details/81196472 http://www.imooc.com/article/36399 http ...
- Python基础数据类型之集合
Python基础数据类型之集合 集合(set)是Python基本数据类型之一,它具有天生的去重能力,即集合中的元素不能重复.集合也是无序的,且集合中的元素必须是不可变类型. 一.如何创建一个集合 #1 ...