树上的单点修改+子树查询

用dfn[u]和num[u]可以把任意子树表示成一段连续区间,此时结合树状数组就好了

#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN=(int)1e5+;
const int MAXM=(int)2e5+;
int a[MAXN],n,m;char s[];
int lb(int i){return i&-i;}//lowbit
void add(int i,int v){for(;i<=n;a[i]+=v,i+=lb(i));}
int sum(int i){int ans=;for(;i;ans+=a[i],i-=lb(i));return ans;}
int query(int i,int j){return sum(j)-sum(i-);} struct node{int v,next;}edge[MAXM];
int head[MAXN],cnt;
void addedge(int u,int v){edge[++cnt].v=v,edge[cnt].next=head[u],head[u]=cnt;}
void Addedge(int u,int v){addedge(u,v),addedge(v,u);} int dfn[MAXN],num[MAXN],index;
void dfs(int u,int fa){
dfn[u]=++index;
for(int i=head[u];i;i=edge[i].next){
int v=edge[i].v;
if(v!=fa)num[u]++,dfs(v,u),num[u]+=num[v];
}
} void Order_C(int x){query(dfn[x],dfn[x])?add(dfn[x],-):add(dfn[x],);}
void Order_Q(int x){printf("%d\n",query(dfn[x],dfn[x]+num[x]));} int main() {
scanf("%d",&n);
for(int i=,u,v;i<n;i++)scanf("%d%d",&u,&v),Addedge(u,v);
dfs(,),scanf("%d",&m);
for(int i=;i<=n;i++)add(i,);
for(int i=,x;i<=m;i++)scanf("%s%d",s,&x),s[]=='C'?Order_C(x):Order_Q(x);
return ;
}

C++-POJ3321-Apple Tree[数据结构][树状数组]的更多相关文章

  1. POJ3321 Apple Tree (树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16180   Accepted: 4836 Descr ...

  2. poj 3321:Apple Tree(树状数组,提高题)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18623   Accepted: 5629 Descr ...

  3. NYOJ 231 Apple Tree (树状数组)

    题目链接 描述 There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in t ...

  4. POJ 3321 Apple Tree 【树状数组+建树】

    题目链接:http://poj.org/problem?id=3321 Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submiss ...

  5. POJ 3321 Apple Tree (DFS + 树状数组)

    题意: 一棵苹果树有N个分叉,编号1---N(根的编号为1),每个分叉只能有一颗苹果或者没有苹果. 现在有两种操作: 1.某个分叉上的苹果从有变无或者从无边有. 2.需要统计以某个分叉为根节点时,它的 ...

  6. poj 3321 Apple Tree(一维树状数组)

    题目:http://poj.org/problem?id=3321 题意: 苹果树上n个分叉,Q是询问,C是改变状态.... 开始的处理比较难,参考了一下大神的思路,构图成邻接表 并 用DFS编号 白 ...

  7. POJ 3321 Apple Tree(树状数组)

    点我看题目  题意 : 大概是说一颗树有n个分岔,然后给你n-1对关系,标明分岔u和分岔v是有边连着的,然后给你两个指令,让你在Q出现的时候按照要求输出. 思路 :典型的树状数组.但是因为没有弄好数组 ...

  8. ACM数据结构-树状数组

    模板: int n; int tree[LEN]; int lowbit(int x){ return x&-x; } void update(int i,int d){//index,del ...

  9. 【poj 3167】Cow Patterns(字符串--KMP匹配+数据结构--树状数组)

    题意:给2个数字序列 a 和 b ,问按从小到达排序后,a中的哪些子串与b的名次匹配. a 的长度 N≤100,000,b的长度 M≤25,000,数字的大小 K≤25. 解法:[思考]1.X 暴力. ...

随机推荐

  1. 真·从零开始的Ubuntu+Apache+MySQL+PHP+phpstorm+xdebug下的debug环境搭建(纯小白向)

    费了好几天劲,终于把Linux下的开发环境如何设置搞的有点明白了,在此写篇博客庆祝一下,让大家以后少踩坑(缓慢更新中) 关键词 win10双系统装Ubuntu 下载U盘烧录工具 下载UbuntuLTS ...

  2. C++中c_str()的用法

    这个函数经常用到,总是记不住,记下来,方便翻阅 c_str()函数返回一个指向正规C字符串的指针,内容与string串相同. C++中c_str()主要用法就是为了与C语言兼容,在C语言中没有stri ...

  3. 支付宝网站支付接口配置 RSA 公钥 私钥

    个人博客 地址:http://www.wenhaofan.com/article/20190419143333 下载签名工具 访问:https://docs.open.alipay.com/291/1 ...

  4. musql 单表查询

    一 介绍 本节内容: 查询语法 关键字的执行优先级 简单查询 单条件查询:WHERE 分组查询:GROUP BY HAVING 查询排序:ORDER BY 限制查询的记录数:LIMIT 使用聚合函数查 ...

  5. C#索引器学习笔记

    本笔记摘抄自:https://www.cnblogs.com/ArmyShen/archive/2012/08/27/2659405.html,记录一下学习过程以备后续查用. 索引器允许类或者结构的实 ...

  6. Mysql快速入门(二)

    多表关联查询 JOIN 按照功能大致分为如下三类: CROSS JOIN(交叉连接) INNER JOIN(内连接或等值连接). OUTER JOIN(外连接) 交叉连接 交叉连接的关键字:CROSS ...

  7. b站德云社相声合集

    每天都做德云小可爱呀 郭德纲于谦相声合集搜索: 75314217.75079477 62444678.60874866 60745041.60514509 之前在喜马拉雅上面听过,部分高清的要会员,只 ...

  8. Flask知识总结

    1.-----------------路由设置的2种方式----------------- 查看源码,route方法里,本质是执行app.add_url_rule() 因此可以这么写(主流方式): @ ...

  9. Codeforces 577A - Multiplication Table

    Let's consider a table consisting of n rows and n columns. The cell located at the intersection of i ...

  10. 记录 Docker 的学习过程 (网络篇之跨主机互通)

    下面从node3上操作node3# docker run -d -p 8500:8500 --name consul progrium/consul -server -bootstrap node3# ...