【POJ3321】Apple Tree

树上单点修改,子树查询

可以在这棵树的dfs序上建线段树维护

PS:modify、query的时候传入x的dfs序即可

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 const int N=100010,M=400010;
 5 int size,head[N],next[N],to[N],vis[N];
 6 int st[N],ed[N],rl[M],rr[M],sum[M],cnt,n;
 7 char ch[5];
 8 void uni(int x,int y){
 9     size++;
10     next[size]=head[x];
11     head[x]=size;
12     to[size]=y;
13 }
14 void dfs(int u){
15     st[u]=++cnt;vis[u]=1;
16     for (int e=head[u];e;e=next[e])
17         if (!vis[to[e]]) dfs(to[e]);
18     ed[u]=cnt;
19 }
20 void update(int i){
21     sum[i]=sum[i<<1]+sum[i<<1|1];
22 }
23 void build(int i,int l,int r){
24     rl[i]=l;rr[i]=r;
25     if (l==r){
26         sum[i]=1;return;
27     }
28     int mid=(l+r)>>1;
29     build(i<<1,l,mid);
30     build(i<<1|1,mid+1,r);
31     update(i);
32 }
33 int query(int i,int L,int R){
34     int l=rl[i],r=rr[i];
35     if (L==l&&r==R) return sum[i];
36     int mid=(l+r)>>1,ans=0;
37     if (L<=mid) ans=query(i<<1,L,R);
38     if (R>mid) ans+=query(i<<1|1,L,R);
39     return ans;
40 }
41 void modify(int i,int x){
42     int l=rl[i],r=rr[i];
43     if (l==r){
44         sum[i]^=1;return;
45     }
46     int mid=(l+r)>>1;
47     if (x<=mid) modify(i<<1,x);
48     else modify(i<<1|1,x);
49     update(i);
50 }
51 int main(){
52     int x,y,q;
53     scanf("%d",&n);
54     size=cnt=0;
55     for (int i=1;i<n;i++){
56         scanf("%d %d",&x,&y);
57         uni(x,y);uni(y,x);
58     }
59     dfs(1);
60     build(1,1,n);
61     scanf("%d",&q);
62     while (q--){
63         scanf("%s",ch);
64         scanf("%d",&x);
65         if (ch[0]==‘C‘) modify(1,st[x]);
66         else printf("%d\n",query(1,st[x],ed[x]));
67     }
68     return 0;
69 }

STD

时间: 2025-01-01 05:39:27

【POJ3321】Apple Tree的相关文章

【POJ 3321】 Apple Tree (dfs重标号设区间+树状数组求和)

[POJ 3321] Apple Tree (dfs重标号设区间+树状数组求和) Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21966   Accepted: 6654 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. K

【POJ 2486】 Apple Tree(树型dp)

[POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8981   Accepted: 2990 Description Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There are N nodes in the tree. Each

【Leetcode】Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 思路:后序遍历比起先序遍历以及中序遍历要稍微复杂一点,可以考虑用两个stack进行操作,

【Leetcode】Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] 思路:使用

【Leetcode】Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order trave

【HackerRank】Utopian tree

The Utopian tree goes through 2 cycles of growth every year. The first growth cycle of the tree occurs during the monsoon, when it doubles in height. The second growth cycle of the tree occurs during the summer, when its height increases by 1 meter.

【BZOJ】2631: tree LCT

[题意]给定n个点的树,每个点初始权值为1,m次操作:1.x到y的点加值,2.断一条边并连一条边,保证仍是树,3.x到y的点乘值,4.x到y的点权值和取模.n,m<=10^5. [算法]Link-Cut Tree [题解]区间加和区间乘标记的处理:[BZOJ]1798: [Ahoi2009]Seq 维护序列seq 线段树 splay上维护要注意: 1.上传时加本身. 2.改值的时候不能影响到0点. 3.所有改变点的儿子的地方都要上传,所有改变点的父亲的地方都要下传. 除了rotate,还有acc

【题解】Digit Tree

[题解]Digit Tree CodeForces - 716E 呵呵以为是数据结构题然后是淀粉质还行... 题目就是给你一颗有边权的树,问你有多少路径,把路径上的数字顺次写出来,是\(m\)的倍数. 很明显可以点分治嘛,我们可以按照图上的样子,把一条路径本来是\(12345678\)的路径,变成\(1234|5678\),我们记录图中左边的那种路径为\(f\)(往根),右边的那种路径为\(g\)(从根),记右边的那种到分治中心的深度为\(d\),那么这条路径就可以被表示成\(f\times 1

【POJ 2486】 Apple Tree (树形DP)

Apple Tree Description Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There are N nodes in the tree. Each node has an amount of apples. Wshxzt starts her happy trip at one node. She can eat up all the apple