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 ...
随机推荐
- Jarvis OJ-Level4
借助DynELF实现无libc的漏洞利用小结 #!/usr/bin/env python # coding:utf-8 from pwn import * elf = ELF('level4') wr ...
- 将Xcode的本地代码push到github仓库上
1.首先,你得有一个github账号,如果没有的话就去注册一个,通过下面图片的方式创建一个github仓库. 2.创建仓库后填写相关的信息,比如说仓库名等. 3.在xcode上进行设置,添加远程git ...
- 使用custom elements和Shadow DOM自定义标签
具体的api我就不写 官网上面多 如果不知道这个的话也可以搜索一下 目前感觉这个还是相当好用的直接上码. <!DOCTYPE html> <html lang="en&q ...
- xadmin下设置“use_bootswatch = True”无效的解决办法
环境: python 2.7 django 1.9 xadmin采用源代码的方式引入到项目中 问题: 在xadmin使用的过程中,设置“use_bootswatch = True”,企图调出主题菜单, ...
- solr 单机模式搭建
系统环境:centos 7 安装前准备 安装JDK环境 下载tomcat.solr安装包:solr下载地址:http://archive.apache.org/dist/lucene/solr/ 安装 ...
- SqlServer 2014该日志未截断,因为其开始处的记录是挂起的复制操作或变更数据捕获
环境:AlwaysOn集群 操作系统:Windows Server 2008 R2 数据库: SQL Server 2014 错误提示:“该日志未截断,因为其开始处的记录是挂起的复制操作或变更数据捕获 ...
- python index 自己实现
l = [2,3,4,223,42,56,7,389,586,845,8,894,343,46,345,3556,23,233,45,25,78,456,785,576,344,6,34,563,] ...
- Python9-字典-day5
数据类型划分:可变数据类型,不可变数据类型不可变数据类型:元祖 bool int str 可哈希可变数据类型:list,dic set 不可哈希dict key 必须是不可变数据类型,可哈希 valu ...
- 排序算法C语言实现——冒泡、快排、堆排对比
对冒泡.快排.堆排这3个算法做了验证,结果分析如下: 一.结果分析 时间消耗:快排 < 堆排 < 冒泡. 空间消耗:冒泡O(1) = 堆排O(1) < 快排O(logn)~O(n) ...
- Codeforces Round #877 (Div. 2) B. - Nikita and string
题目链接:http://codeforces.com/contest/877/problem/B Nikita and string time limit per test2 seconds memo ...