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 ...
随机推荐
- 初识 Hibernate
Hibernate 框架 1.1 什么是框架? 框架是一个提供了可重用的公共结构半成品. 2.1 关于Hibernate Hibernate是数据持久层的一个轻量级框架.数据持久层的框架有很多 ...
- Dubbo服务的搭建
dubbo框架主要作用是基于RPC的远程调用服务管理,但是注册中心是用的zookeeper,搭建dubbo,首先要安装zookeeper,配置zookeeper... 实现功能如图所示:(存在2个系统 ...
- UINavgationController
UINavigationBar和UINavigationItem是iOS开发中常用的控件. 1.设置导航栏标题 self.title = @"iOS开发:iOSDevTip"; ...
- docker系列之安装配置
由于docker支持内核3.8以上的,所以我们要安装centos7系统,这个系统的内核是3.10,来支持docker使用环境 一.用UItralSo制作u盘启动 打开找到要做u盘启动的镜像 二.安装C ...
- 一次下载多个文件的解决思路-JS
一次下载多个文件的解决思路(iframe) - Eric 真实经历 最近开发项目需要做文件下载,想想挺简单的,之前也做过,后台提供下载接口,前端使用window.location.href就行了呗.不 ...
- Template--模板
模板引擎的支持 配置 模板引擎配置为TEMPLATES设置.这是一个配置列表,每个引擎一个,默认值为空.这是settings.py生成的,通过startproject命令定义了一个更有用的值: TEM ...
- MIP求解方法总结
*本文主要记录和分享学习到的知识,算不上原创 *参考文献见链接 本文主要简述了求解MIP问题的两大类(精确求解和近似求解),或者更细致地,三大类方法(精确算法,ε-近似算法和启发式算法).由于暂时不太 ...
- 推荐SQL Server Management Studio以及Visual Studio下的免费的插件 ApexSQL Complete
SQL Server 并没有代码格式化的工具,对于处理他人编写的长SQL需要手工的格式化是一件麻烦的事情. 推荐SQL Server Management Studio以及Visual Studio下 ...
- Mac 生成public_key
1.首先查看是否已经生成过public_key 打开终端查看是否已经存在SSH密钥:cd ~/.ssh 输出:No such file or directory表示没有 2.生成public_key ...
- hdu 4251 The Famous ICPC Team Again划分树入门题
The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...