日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【POJ - 3321】 Apple Tree(dfs序 + 线段树维护 或 dfs序 + 树状数组维护)

發布時間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【POJ - 3321】 Apple Tree(dfs序 + 线段树维护 或 dfs序 + 树状数组维护) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

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 to?N?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 fork?u?and fork?v?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 fork?x, including the apple (if exists) on the fork x
Note the tree is full of apples at the beginning

Output

For every inquiry, output the correspond answer per line.

Sample Input

3 1 2 1 3 3 Q 1 C 2 Q 1

Sample Output

3 2

題目大意:

給出一個蘋果樹,每個節點一開始都有蘋果

C X,如果X點有蘋果,則拿掉,如果沒有,則新長出一個(即:可以用異或處理)

Q X,查詢X點與它的所有后代分支一共有幾個蘋果

解題報告:

? ? ?dfs序,然后用線段樹單點更新+區間查詢一下就可以了。(dfs序是專門用來處理一棵樹的子樹查詢的,需要用線段樹或樹狀數組維護查詢)

AC代碼:(用輸入外掛,用時少了200ms左右,不知道rank1的63ms是怎么做到的)

#include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> using namespace std; const int MAX = 100000 + 5; int n,m; int cnt,id; int head[MAX*10]; int q[MAX*4],in[MAX*4],out[MAX*4]; struct Edge {int fr;int to;int ne; } e[MAX * 4];//因為可能雙向邊? struct TREE {int l,r;int val; } tree[MAX * 8]; void add(int u,int v) {e[++cnt].to = v;e[cnt].fr = u;e[cnt].ne = head[u];head[u] = cnt; } void pushup(int cur) {tree[cur].val = tree[cur*2].val + tree[cur*2+1].val; } void build(int l,int r,int cur) {tree[cur].l = l;tree[cur].r = r;if(l == r) {tree[cur].val = 1;return ;//又忘寫return了。。。 }int m = (l+r)/2;//剛開始沒寫build遞歸。。。 build(l,m,cur*2);build(m+1,r,cur*2+1);pushup(cur); } void dfs(int cur,int root) //cur為當前點,root為根節點 {q[++id] =cur;in[cur] = id;for(int i = head[cur]; i!=-1;i = e[i].ne) {dfs(e[i].to,cur);}out[cur] = id; } void update(int tar,int cur) {if(tree[cur].l == tree[cur].r) {tree[cur].val ^= 1;return ; }if(tar <= tree[cur*2].r) update(tar,cur*2);//剛開始寫成tree[cur*2].l了else update(tar,cur*2+1);pushup(cur); } int query(int pl,int pr,int cur) {if(pl <= tree[cur].l && pr >= tree[cur].r) {return tree[cur].val;}int res = 0;if(pl <= tree[cur*2].r) res += query(pl,pr,cur*2);if(pr >= tree[cur*2+1].l) res += query(pl,pr,cur*2+1);return res; } int read() {int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f; } int main() {cin>>n;char op[10];int x;cnt = id = 0;memset(head,-1,sizeof head);int u,v;for(int i = 1; i<n; i++) {//scanf("%d%d",&u,&v);u = read();v = read();add(u,v);}dfs(1,0);//這個0 是沒用的 build(1,n,1); // printf("**********\n"); // for(int i = 1; i<=5; i++) { // printf("%d| ",tree[i].val); // } // printf("\n****************\n");scanf("%d",&m);while(m--) {scanf("%s",op);if(op[0] == 'C') {//scanf("%d",&x);x = read();update(in[x],1);}else {//scanf("%d",&x);x = read();int ans = query(in[x],out[x],1);printf("%d\n",ans);}} return 0 ;}

總結:

? ?小錯誤還是很多啊。。。

總結

以上是生活随笔為你收集整理的【POJ - 3321】 Apple Tree(dfs序 + 线段树维护 或 dfs序 + 树状数组维护)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。