poj.org/problem?id=3321

【题意】

给一棵n个节点的树,每个节点开始有一个苹果,m次操作
1.将某个结点的苹果数异或 1
2.查询一棵子树内的苹果数
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll; const int maxn=1e5+;
const int maxm=*maxn;
int n;
struct edge
{
int to;
int nxt;
}e[maxm];
int head[maxn];
int tot;
int l[maxn],r[maxn];
int tree[maxm];
bool vis[maxn];
int cid;
int lowbit(int x)
{
return x&(-x);
} void add(int k,int x)
{
while(k<=cid)//注意是cid,不是n
{
tree[k]+=x;
k+=lowbit(k);
}
} int query(int k)
{
int ans=;
while(k)
{
ans+=tree[k];
k-=lowbit(k);
}
return ans;
}
void init()
{
memset(head,-,sizeof(head));
tot=;
cid=;
memset(vis,false,sizeof(vis));
} void addedge(int u,int v)
{
e[tot].to=v;
e[tot].nxt=head[u];
head[u]=tot++;
} void dfs(int u,int pa)
{
l[u]=++cid;
for(int i=head[u];i!=-;i=e[i].nxt)
{
int v=e[i].to;
if(v==pa) continue;
dfs(v,u);
}
r[u]=++cid;
} int main()
{
while(~scanf("%d",&n))
{
init();
int u,v;
for(int i=;i<=n-;i++)
{
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
dfs(,-);
for(int i=;i<=n;i++)
{
add(l[i],);//单点修改
}
int q;
scanf("%d",&q);
char str[];
int x;
while(q--)
{
scanf("%s%d",str,&x);
if(str[]=='Q')
{
int ans=query(r[x])-query(l[x]-);
printf("%d\n",ans);
}
else
{
if(!vis[x])
{
add(l[x],-);
vis[x]=true;
}
else
{
add(l[x],);
vis[x]=false;
}
}
}
}
return ;
}

【DFS序+单点修改区间求和】POJ 3321 Apple Tree的更多相关文章

  1. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  2. POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)

    id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - ...

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

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

  4. POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25904   Accepted: 7682 Descr ...

  5. POJ2763-Housewife Wind-树上单点修改区间求和

    这道题可以树链剖分做.但是最近在给学弟搞数据结构复习了LCA树状数组RMQ 然后就搞了一发LCA+树状数组维护. dis数组维护当前点到根节点的权值和.则dis(u,v) = dis[u]+dis[v ...

  6. poj 3321 Apple Tree dfs序+线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outsid ...

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

    题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...

  8. POJ 3321 Apple Tree DFS序 + 树状数组

    多次修改一棵树节点的值,或者询问当前这个节点的子树所有节点权值总和. 首先预处理出DFS序L[i]和R[i] 把问题转化为区间查询总和问题.单点修改,区间查询,树状数组即可. 注意修改的时候也要按照d ...

  9. POJ 3321 Apple Tree(dfs序树状数组)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=10486 题意:一颗有n个分支的苹果树,根为1,每个分支只有一个苹果,给出n- ...

随机推荐

  1. 洛谷 P2419 [USACO08JAN]牛大赛Cow Contest

    题目背景 [Usaco2008 Jan] 题目描述 N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a p ...

  2. Docker镜像的目录存储讲解

    我们成功安装完docker后,执行命令行sudo docker run hello-world, 如果是第一次执行,则会从远程拉取hello-world的镜像到本地,然后运行,显示hello worl ...

  3. Could not load OpenSSL解决

    问题 Could not load OpenSSL. You must recompile Ruby with OpenSSL support or change the sources in you ...

  4. Gym 100342F Move to Front (树状数组动态维护和查询)

    用树状数组动态和查询修改排名. 树状数组可以很方便地查询前缀和,那么可以利用这一特点,记录一个点在树状数组里最后一次出现的位置, 查询出这个位置,就可以知道这个点的排名了.更改这个点的排名的时候只要把 ...

  5. 从汇编看c++中的多态

    http://www.cnblogs.com/chaoguo1234/archive/2013/05/19/3079078.html 在c++中,当一个类含有虚函数的时候,类就具有了多态性.构造函数的 ...

  6. go get 升级所有

    go get -u all go get -u go mod update go get -u full_package_name    go get -u github.com/... // ('. ...

  7. Shell脚本中时间处理

    Shell脚本中时间处理 1.脚本内容 #!/bin/bash #环境变量 #设置环境变量和sql文件格式相符 source /etc/profileexport LD_LIBRARY_PATH=&q ...

  8. kvm虚拟迁移

    1. 虚拟迁移 迁移: 系统的迁移是指把源主机上的操作系统和应用程序移动到目的主机,并且能够在目的主机上正常运行.在没有虚拟机的时代,物理机之间的迁移依靠的是系统备份和恢复技术.在源主机上实时备份操作 ...

  9. 如何快速获取当前链接?后面的内容,location.search、页面滚动

    function request() { var urlStr = location.search; ) { theRequest={}; return; } urlStr = urlStr.subs ...

  10. centos7.2快速搭建LAMP平台

    #查看linux系统版本信息 cat /etc/redhat-release 以上是操作系统的所有信息,补充下内核信息参数介绍: 3.10.0-514.26.2.el7.x86_64 3表示主版本号, ...