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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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

題干:

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

題目大意:

給出一個(gè)蘋果樹,每個(gè)節(jié)點(diǎn)一開始都有蘋果

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

Q X,查詢X點(diǎn)與它的所有后代分支一共有幾個(gè)蘋果

解題報(bào)告:

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

AC代碼:(用輸入外掛,用時(shí)少了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];//因?yàn)榭赡茈p向邊? 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為當(dāng)前點(diǎn),root為根節(jié)點(diǎn) {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);//這個(gè)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 ;}

總結(jié):

? ?小錯(cuò)誤還是很多啊。。。

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: av在线播放网站 | 国产日本视频 | 麻豆日产六区 | 国产专区自拍 | caoporen超碰 | 午夜少妇 | 狠狠干欧美 | 99热青青草| 亚洲视屏在线观看 | 国产精品一区二区三区免费在线观看 | 国产aaaaaa| 亚洲人视频 | 欧美在线免费观看 | 四虎在线视频免费观看 | 欧美精品久久久久性色 | 麻豆精品 | 黄色片视频播放 | 国语av在线 | 在线免费看黄 | 亚洲欧美另类激情 | 亚洲日日骚 | 涩涩涩涩av | 欧美成人tv | 中国女人内96xxxxx | 日韩一二三区视频 | 日本黄色xxxx | 美女自拍视频 | 国产丰满大乳奶水在线视频 | 国产91福利 | 天堂一区在线观看 | 最新久久久| 奇米在线 | 99riav国产精品视频 | 北条麻妃在线一区二区 | 日韩久久一区二区三区 | 欧美性生活在线视频 | 日韩精品1区2区3区 欧美一本 | 亚洲国产精品成人综合在线 | 国产91小视频 | 亚洲色图日韩精品 | 少妇与公做了夜伦理69 | 精品一区二区三区在线免费观看 | av免费观看不卡 | 加勒比成人av | 高h视频在线播放 | 青青操免费 | 亚洲国产精品成人午夜在线观看 | 超碰一区二区 | 日本美女操 | 久久青青国产 | 自拍偷拍在线视频 | 极品另类 | 日韩中文字幕有码 | 一本在线免费视频 | 日日夜夜狠狠爱 | 中文字幕日产 | 亚洲精品视频在线 | 日本伦理中文字幕 | 黄色片免费看 | 一起操网站 | 亚洲欧洲成人精品久久一码二码 | 91精品人妻互换一区二区 | 综合网激情 | 免费性片 | 日韩av男人的天堂 | 99热首页 | 精品九九视频 | 欧洲一区在线 | 男女乱淫视频 | 久热久色 | 国产一区二区精彩视频 | melody在线高清免费观看 | 欧美一区二区三区在线播放 | 精品美女在线 | 日本一区欧美 | 亚洲激情五月婷婷 | 欧美一区二区影院 | 69视频在线观看免费 | 91黄色大片| 女女同性女同一区二区三区按摩 | 成人黄色av | 日韩网站在线观看 | 亚洲国产精品综合久久久 | 日韩免费一级 | 欧美成人天堂 | 69av在线| 青春草网站 | 久久99热这里只有精品 | 欧美做爰xxxⅹ性欧美大片 | 欧美激情在线一区二区 | 日韩中文字幕av在线 | 182tv午夜福利在线观看 | 在线观看色视频 | 懂色av蜜臀av粉嫩av分享吧最新章节 | 午夜影院在线观看免费 | h视频在线免费观看 | 日本免费一区二区在线 | 同性色老头性xxxx老头 | 日韩极品一区 |