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 ...
随机推荐
- shell脚本,awk实现文件a的每行数据与文件b的相对应的行的值相减,得到其绝对值。
解题思路 文件 shu 是下面这样的.220 34 50 70553 556 32 211 1 14 98 33 文件 jian是下面这样的.1082 想要得到结果是下面这样的.210 24 40 6 ...
- sqlite查看所有表名、判断表是否存在,字段名及字段信息
sqlite查看所有表名.判断表是否存在,字段名及字段信息 sqlite查看所有表名及字段名查询table,type 段是'table',name段是table的名字, select name f ...
- ES6新数据类型map与set
一.map,简单的键值对映射,具有很快的查找速度 1. 初始化map,map的键名可以使用其他数据类型,对象的属性名只能使用字符串或symbol 使用二维数组 var m = new Map([['n ...
- C++高精度乘法
#include <cstdio> #include <iostream> #include <algorithm> void highPrecision (int ...
- [CODEVS] 3955 最长严格上升子序列(加强版)
题目描述 Description 给一个数组a1, a2 ... an,找到最长的上升降子序列ab1<ab2< .. <abk,其中b1<b2<..bk. 输出长度即可. ...
- The Fourth Day
迭代器 迭代器:迭代的工具 .什么是迭代:指的是一个重复的过程,每次重复称为一次迭代,并且每次重复的结果是下一次重复的初始值 例: while True: print('====>'') l=[ ...
- MIP经典问题:旅行商问题 (traveling salesman problem)
*本文主要记录和分享学习到的知识,算不上原创. *参考文献见链接. 旅行商问题.背包问题都是0-1规划问题中最为经典的问题. 通常来说,当我们学习并熟悉一种求解混合整数问题的技巧时,可以用这种技巧来求 ...
- python中set()函数的用法
set顾名思义是集合,里面不能包含重复的元素,接收一个list作为参数 list1=[1,2,3,4] s=set(list1) print(s) #逐个遍历 for i in s: print(i) ...
- shell脚本举例
1.有时在写一些以循环方式运行的监控脚本,设置时间间隔是必不可少的,下面是一个Shell进度条的脚本演示在脚本中生成延时. #!/bin/bash b='' for ((i=0;$i<=100; ...
- 【01】let和const命令
let和const命令 魔芋总结: 01,let声明变量,只在代码块{}内有效. 02,不存在变量提升,只能先声明,再使用.否则报错. 03,暂时性死区 如果代码块中存在let和const声明的变 ...