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

用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. HTML5存储(带一个粗糙的打怪小游戏案例)

    本地存储localStorage设置存储内容setItem(key,value) localStorage.setItem('leo','23'); 更新存储内容对象[key]=value对象.key ...

  2. 纪中集训2020.02.05【NOIP提高组】模拟B 组总结反思——【佛山市选2010】组合数计算,生成字符串 PPMM

    目录 JZOJ2290. [佛山市选2010]组合数计算 比赛时 之后 JZOJ2291. [佛山市选2010]生成字符串 比赛时 之后 JZOJ2292. PPMM 比赛时 之后 JZOJ2290. ...

  3. JS阻止事件冒泡与浏览器默认行为

    阻止冒泡 W3C的方法是e.stopPropagation() IE是e.cancelBubble = true; 阻止默认行为 W3C的方法e.preventDefault(), IE是e.retu ...

  4. Linux 基础操作命令

    关机和注销 shutdown -h now 立刻关机 shutdown -r now 立刻重启 shutdown -h + 1分钟后关机(重启同样用法) shutdown -h : 11点钟关机(重启 ...

  5. ovs安装教程

    原文链接:https://www.cnblogs.com/goldsunshine/p/10331606.html Open vSwitch系列之二 安装指定版本ovs   Open vSwitch系 ...

  6. JDK下载安装与环境变量配置图文教程【超详细】

    JDK下载安装与环境变量配置图文教程[超详细] 创建时间:2019年11月13日11时02分 文章目录 1. JDK介绍 1.1 什么是JDK? 1.2 JDK版本介绍 2. JDK下载与安装 3.w ...

  7. web做题记录

    2020.1.19 南邮ctf 签到题 题目:key在哪里? 在火狐浏览器中右键选择打开查看源代码,在源代码可以看到如下 因为是第一次做这个题,不知道提交啥,我先提交了“admiaanaaaaaaaa ...

  8. 1级搭建类103-Oracle 12c 单实例 FS(12.2.0.1+RHEL 7)公开

    项目文档引子系列是根据项目原型,制作的测试实验文档,目的是为了提升项目过程中的实际动手能力,打造精品文档AskScuti. 项目文档引子系列目前不对外发布,仅作为博客记录.如学员在实际工作过程中需提前 ...

  9. C# 索引器的理解和使用

    概述 此部分内容引用自MSDN文档 使用索引器可以用类似于数组的方式为对象建立索引. get 取值函数返回值. set 取值函数分配值. this 关键字用于定义索引器. value 关键字用于定义 ...

  10. 大型数据库技术实验六 实验6:Mapreduce实例——WordCount

    现有某电商网站用户对商品的收藏数据,记录了用户收藏的商品id以及收藏日期,名为buyer_favorite1. buyer_favorite1包含:买家id,商品id,收藏日期这三个字段,数据以“\t ...