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. CSS-学习笔记五

    1.  权重: 内联:A ID:B Class:C 标签:D 继承:0 2.  文字阴影text-shadow 3.  文字缩进text-index 4.  文本换行 5.  文本溢出 6.  圆角 ...

  2. HDU 1398 Square Coins 平方硬币 (普通母函数,水)

    题意: 有17种硬币,每种的面值为编号的平方,比如 1,4,9,16.....给出一个数字,求组成这个面值有多少种组法? 思路: 用普通母函数解,主要做的就是模拟乘法,因为硬币是无限的,所以每个构造式 ...

  3. CSS声明各个浏览器私有属性的命名前缀

    -moz代表firefox浏览器私有属性-ms代表IE浏览器私有属性-webkit代表chrome.safari私有属性-o代表opera私有属性

  4. sort函数的使用

    此篇当作自己的笔记(水平太菜,这都一直没搞明白) sort()函数的用法1)sort函数包含在头文件<algroithm>中,还要结合using namespace std2)sort有三 ...

  5. iview 验证 trigger: 'blur,change', 同时加两个,省的每次还想input 还是 select

    iview 验证 trigger: 'blur,change', 同时加两个,省的每次还想input 还是 select dataRuleValidate: { name: [{ required: ...

  6. springboot上传linux文件无法浏览,提示404错误

    1.配置文件地址置换 @Componentclass WebConfigurer implements WebMvcConfigurer { @Autowired ConfigUtil bootdoC ...

  7. ext笔记

    命名   The top-level namespaces and the actual class names should be CamelCased. Everything else shoul ...

  8. iterator方法和for方法 遍历数据库user表结果集ResultSet

    首先,把连接数据库的语句做成工具类,因为会一直用到这几句 代码如下: package com.swift.jdbc; import java.sql.Connection; import java.s ...

  9. java在线聊天项目 使用SWT快速制作登录窗口,可视化窗口Design 更换窗口默认皮肤(切换Swing自带的几种皮肤如矩形带圆角)

    SWT成功激活后 new一个JDialog 调整到Design视图 默认的视图模式是BorderLayout,无论你怎么拖拽,只能放到东西南北中的位置上 所以,我们把视图模式调整为AbsoluteLa ...

  10. Spring根据XML配置文件注入对象类型属性

    这里有dao.service和Servlet三个地方 通过配过文件xml生成对象,并注入对象类型的属性,降低耦合 dao文件代码: package com.swift; public class Da ...