poj 3321 Apple Tree
来源:互联网 发布:网络咨询医生兼职招聘 编辑:程序博客网 时间:2024/06/06 13:21
Description
There is an apple tree outside of kaka’s house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree.
The tree has
The trouble is that a new apple may grow on an empty fork some time and kaka may pick an apple from the tree for his dessert. Can you help kaka?
Input
The first line contains an integer
The following
The next line contains an integer
The following
“
or
“
Note the tree is full of apples at the beginning
Output
For every inquiry, output the correspond answer per line.
Sample Input
31 21 33Q 1C 2Q 1
Sample Output
32
Source
POJ Monthly–2007.08.05, Huang, Jinsong
题目大意:
给以一颗根为1,一开始点权全部为1的树,支持两种操作
Cx :如果x 节点权值为1,就变成0,否则变为1Qx :查询以x 为根的子树的点权和
solution
- 很裸的dfs序+很裸的线段树…感觉没什么好说的啊…
code
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;template<typename T>void input(T &x) { x=0; T a=1; register char c=getchar(); for(;c<'0'||c>'9';c=getchar()) if(c=='-') a=-1; for(;c>='0'&&c<='9';c=getchar()) x=x*10+c-'0'; x*=a; return;}#define MAXN 100010struct Edge { int u,v,next; Edge(int u=0,int v=0,int next=0): u(u),v(v),next(next) {}};Edge edge[MAXN];int head[MAXN],cnt;void addedge(int u,int v) { edge[++cnt]=Edge(u,v,head[u]); head[u]=cnt; return;}int dfn[MAXN],last[MAXN],timee;void dfs(int now) { dfn[now]=++timee; for(int i=head[now];i;i=edge[i].next) if(!dfn[edge[i].v]) dfs(edge[i].v); last[now]=timee; return;}struct Segment_Tree { int l,r,sum;};Segment_Tree t[MAXN<<2];void updata(int now) { t[now].sum=t[now<<1].sum+t[now<<1|1].sum; return;}void build(int now,int l,int r) { t[now].l=l,t[now].r=r; if(l==r) { t[now].sum=1; return; } int mid=l+r>>1; build(now<<1,l,mid); build(now<<1|1,mid+1,r); updata(now); return;}void Modify(int now,int pos) { int l=t[now].l,r=t[now].r; if(l==r) { t[now].sum^=1; return; } int mid=l+r>>1; if(pos<=mid) Modify(now<<1,pos); else Modify(now<<1|1,pos); updata(now); return;}int query(int now,int L,int R) { int l=t[now].l,r=t[now].r; if(l==L&&R==r) return t[now].sum; int mid=l+r>>1; if(R<=mid) return query(now<<1,L,R); else if(L>mid) return query(now<<1|1,L,R); else { int a=query(now<<1,L,mid), b=query(now<<1|1,mid+1,R); return a+b; }}int main() { int n,m; input(n); for(int i=1;i<n;i++) { int u,v; input(u),input(v); addedge(u,v); } dfs(1); build(1,dfn[1],last[1]); input(m); char op[5]; int x; while(m--) { scanf("%s",op); input(x); if(op[0]=='C') Modify(1,dfn[x]); else printf("%d\n",query(1,dfn[x],last[x])); } return 0;}
- POJ 3321 Apple Tree
- POJ 3321 Apple Tree
- poj 3321 Apple Tree
- POJ 3321 Apple Tree
- Poj 3321 Apple Tree
- poj 3321 apple tree
- poj 3321 Apple Tree
- poj 3321 Apple Tree
- POJ 3321 Apple Tree
- POJ--3321--Apple Tree
- POJ-3321-Apple Tree
- poj 3321 Apple Tree
- POJ 3321 Apple Tree
- POJ 3321 Apple Tree
- POJ 3321 Apple Tree
- POJ 3321 Apple Tree
- POJ:3321 Apple Tree
- POJ 3321 Apple Tree
- Tomcat目录结构
- bzoj1375 [Baltic2002]Bicriterial routing 双调路径 (两个值的最短路)
- MongoDB:10-MongoDB 监控
- 我的错题集-updating
- javaPAT甲级1002
- poj 3321 Apple Tree
- 面试时遇到由rand5生成rand7
- 点击Button按钮,解析在ListView上
- 市面上几种常见的网络推广方式
- zabbix server 报错 zabbix server is not running: the information displayed may not be current
- 在同一台服务器上的不同站点使用不同的SSL证书
- 在vi按了ctrl+s后
- 自定义浏览器右键菜单
- 动态库与静态库优缺点比较