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 ...
随机推荐
- MYSQL 注射精华
前言鄙人今天心血来潮突然想写篇文章,鄙人从来没写过文章,如果有错误的地方请多多指教.本文需要有基础的SQL语句知识才可以更好的理解.建议想学习的人多去了解一下SQL语句和编程语言,知己知彼才能百战百胜 ...
- 51nod 1242 斐波那契数列的第N项——数学、矩阵快速幂
普通算法肯定T了,所以怎么算呢?和矩阵有啥关系呢? 打数学符号太费时,就手写了: 所以求Fib(n)就是求矩阵 | 1 1 |n-1 第一行第一列的元素. | 1 0 | 其实学过线代 ...
- [POJ] 1135 Domino Effect
Domino Effect Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12147 Accepted: 3046 Descri ...
- systemverilog(3)之Randomize
what to randomize? (1) primary input data <==one data (2)encapsulated input data <== muti grou ...
- 【java】 field 和 variable 区别及相关术语解释
Having said that, the remainder of this tutorial uses the following general guidelines when discussi ...
- python--前端CSS
一.CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义了如何显示HTML元素,给HTML设置样式,让他更加美观. 当浏览器读到这个样式表, 他就会按照这个样式来对文档进行 ...
- python中的list、tuple和dictionary
列表 列表是python中最基本的数据结构之一,并且列表的数据项不需要具有相同的数据类型,创建一个列表,只需把逗号分隔的不同数据项使用方括号括起来即可.具体的定义式如下: list=['变量1','变 ...
- Oracle联合主键
转https://www.cnblogs.com/king-xg/p/6721272.html alter table tablename add constraint unionkeyname pr ...
- wei UI使用
1.前言 通过前面系列文章的学习与讲解,相信大家已经对微信的开发有了一个全新的认识.后端基本能够基于盛派的第三方sdk搞定大部分事宜,剩下的就是前端了.关于手机端的浏览器的兼容性问题相信一直是开发者们 ...
- linux 搭建apache 服务器
1.查看apache服务器 /etc/init.d/httpd status 若没有,则使用yum -y install httpd 安装软件 2.设置开机启动 chkconfig httpd o ...