POJ 3321-Apple Tree(樹狀陣列)
Apple Tree
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 23729 | Accepted: 7132 |
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 N forks which are connected by branches. Kaka numbers the forks by 1 toN and the root is always numbered by 1. Apples will grow on the forks and two apple won't grow on the same fork. kaka wants to know how many apples are there in a sub-tree, for his study of the produce ability of the apple tree.
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 N (N ≤ 100,000) , which is the number of the forks in the tree.
The following N - 1 lines each contain two integers u and v, which means forku and forkv are connected by a branch.
The next line contains an integer M (M ≤ 100,000).
The following M lines each contain a message which is either
"C x" which means the existence of the apple on fork
x has been changed. i.e. if there is an apple on the fork, then Kaka pick it; otherwise a new apple has grown on the empty fork.
or
"Q x" which means an inquiry for the number of apples in the sub-tree above the forkx, including the apple (if exists) on the fork x
Note the tree is full of apples at the beginning
Output
Sample Input
3 1 2 1 3 3 Q 1 C 2 Q 1
Sample Output
3 2
Source
題目意思:
是有一棵果樹,書上有n個分叉點,從1~n給各個分叉點編號。蘋果長在分叉點的地方,一個分叉點最多隻能長一個蘋果。求這棵樹上一共有多少個蘋果。
最開始的時候樹上長滿蘋果,即每個分叉點都有一個蘋果。
先輸入n,表示分叉點個數;再輸入u,v兩個分叉點表示這兩個點由樹枝連在一起;
C x,表示x點得蘋果狀態發生變化:若以前有,則吃掉變無;若以前沒有,則長出一個蘋果。
Q x,表示查詢x點這棵子樹上的蘋果數目。
用樹狀陣列,方便統計子樹的權值和。
這個是下面程式中sum函式所求的值。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=100000+2;
struct node1
{
int next,tail;//第i條邊相連的節點是edge[i].tail
//連線的下一條邊的序號是edge[i].next
} edge[maxn];//邊表
struct node2
{//以節點i為根的子樹在後根序列中的區間為(apple[i].l,apple[i].r)
int r,l;
} apple[maxn];//蘋果樹
int s[maxn];//s[i]是節點i相連的第i條邊
int c[maxn];//c樹狀陣列
int a[maxn];//a[i]是後根遍歷中第i個節點的權值
int cnt;//後根遍歷序號
void DFS(int u)//初始u=1
{//從節點u出發,計算每個節點為根的子樹區間(apple[].l,apple[].r)
int i;
apple[u].l=cnt;
for(i=s[u]; i!=-1; i=edge[i].next)
DFS(edge[i].tail);
apple[u].r=cnt++;
}
int lowbit(int x)//計算二進位制數x右方的第一位1對應的權
{
return x&(-x);
}
void change(int x)//從a[x]開始調整一棵子樹
{
int i;
if(a[x])//有蘋果,都加一
for(i=x; i<cnt; i+=lowbit(i))
c[i]++;
else//蘋果被吃掉了
for(i=x; i<cnt; i+=lowbit(i))
c[i]--;
}
int sum(int x)//計算節點x上的蘋果總數
{
int i,res=0;
for(i=x; i>0; i-=lowbit(i))
res+=c[i];
return res;
}
int main()
{
int i,m,n,t1,t2,t;
char str[3];
scanf("%d",&n);//樹的節點數
memset(s,-1,sizeof(s[0])*(n+1));
memset(c,0,sizeof(c[0])*(n+1));
memset(apple,0,sizeof(apple[0])*(n+1));
for(i=0; i<n-1; ++i)
{
scanf("%d%d",&t1,&t2);//讀第i條邊(t1,t2)
edge[i].tail=t2;//第i條邊連線t2
edge[i].next=s[t1];//其後繼指標指向t1連線的上一條邊
s[t1]=i;//節點t1相連的邊序號i
}
cnt=1;
DFS(1);//節點1開始DFS,計算每個節點的在後根遍歷的順序值,節點權值設為1
for(i=1; i<=n; ++i)
{
a[i]=1;//長滿蘋果
change(i);//構造樹狀陣列c
}
scanf("%d",&m);
while(m--)
{
scanf("%s%d",str,&t);//命令標誌、節點序號
if(str[0]=='Q')//輸出節點上的蘋果樹
printf("%d\n",sum(apple[t].r)-sum(apple[t].l-1));
else
{//計算節點上的變化情況
a[apple[t].r]=(a[apple[t].r]+1)%2;//有無蘋果的轉換
change(apple[t].r);//更新這棵子樹
}
}
return 0;
}
相關文章
- POJ-2352 Stars(樹狀陣列)陣列
- HDU 1541 & POJ 2352 Stars (樹狀陣列)陣列
- 樹狀陣列陣列
- POJ3468 A Simple Problem with Integers---樹狀陣列(區間問題)陣列
- 解析樹狀陣列陣列
- POJ 3468 【區間修改+區間查詢 樹狀陣列 | 線段樹 | 分塊】陣列
- 二維樹狀陣列陣列
- 樹狀陣列詳解陣列
- 樹狀陣列基礎陣列
- 樹狀陣列模板題 & (樹狀陣列 1:單點修改,區間查詢)陣列
- 樹狀陣列快速入門陣列
- 樹狀陣列和逆序對陣列
- 【筆記/模板】樹狀陣列筆記陣列
- 樹狀陣列模板+習題集陣列
- 樹狀陣列(我是真小白)陣列
- 資料結構——樹狀陣列資料結構陣列
- 樹狀陣列3種基本操作陣列
- HDU 1556 Color the ball(線段樹|樹狀陣列)陣列
- CF1967C. Fenwick Tree-運算元展開,樹狀陣列的結構陣列
- bzoj3155: Preprefix sum(樹狀陣列)陣列
- 【luogu3368】模板 樹狀陣列 2陣列
- 洛谷題單指南-二叉堆與樹狀陣列-P3368 【模板】樹狀陣列 2陣列
- HDU 2689 Sort it【樹狀陣列求逆序對】陣列
- CHOJ 4201 樓蘭圖騰【樹狀陣列】陣列
- AC自動機+字典序+樹狀陣列陣列
- TZOJ 8472 : Tree (重鏈剖分+線段樹) POJ 3237
- 樹狀陣列(BIT)—— 一篇就夠了陣列
- 樹狀陣列(待補)(生硬 公式 用法 證明)陣列公式
- 資料結構之真別多想—樹狀陣列資料結構陣列
- bzoj2743: [HEOI2012]採花(樹狀陣列)陣列
- 樹狀陣列入門陣列
- LeetCode C++ 劍指 Offer 51. 陣列中的逆序對【歸併排序/樹狀陣列/線段樹】LeetCodeC++陣列排序
- PHP 陣列轉樹結構/樹結構轉陣列PHP陣列
- HDU 6274 Master of Sequence(思維+樹狀陣列+二分)AST陣列
- HDU 1556【區間更新+單點查詢 樹狀陣列】陣列
- 求區間不同數的個數【樹狀陣列求解】陣列
- 實驗室外的攻防戰 UOJ#180 [樹狀陣列]陣列
- 樹狀陣列的區間查詢與區間修改陣列
- POJ 2777 Count Color (線段樹+狀態壓縮)