日韩性视频-久久久蜜桃-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ò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 黄色网址在线免费播放 | 黄色性情网站 | 91 高清 在线 制服 偷拍 | 两个小y头稚嫩紧窄h文 | 国产精品99久久久久久www | 日韩欧美电影一区二区三区 | 国产手机在线播放 | 日韩精品成人免费观看视频 | 五月天开心网 | 麻豆精品在线观看 | 国产女人18毛片水真多18精品 | 国产区一二三 | 国产精品系列在线 | 91精品国产99久久久久久红楼 | 懂色一区二区二区av免费观看 | 日韩精品免费播放 | 国产成人无码精品亚洲 | 国产日本在线观看 | 欧美影音| 亚洲一区二区三区三州 | 久久综合五月婷婷 | 热久久av| 欧美特级黄色片 | 日日摸日日干 | 高清无码视频直接看 | 欧美巨乳在线 | 99成人在线观看 | 日韩av免费在线 | 日本网站在线看 | 欧美视频1区 | 欧美一区二区三区成人 | 日韩精品视频一区二区 | 四虎福利视频 | 91视频看看| 国产91在线精品 | 久久久久久久久久久久 | 99视频在线观看视频 | 日韩福利在线视频 | 久久久久99精品成人片我成大片 | 强睡邻居人妻中文字幕 | 一区二区在线视频 | 男女裸体无遮挡做爰 | 日本精品人妻无码免费大全 | 别揉我奶头啊嗯一区二区 | 亚洲精品人 | 91污片| 欧美一区二区人人喊爽 | 国产成人精品一区二区在线小狼 | 色屁屁www| 亚洲青涩 | 草在线| 国产成人精品无码免费看81 | 亚洲www色 | 国产精品久久久久久久裸模 | 国产美女一区二区三区 | av中文字幕免费观看 | 天天草视频 | 国产在线视视频有精品 | 日韩人妻无码精品久久久不卡 | 韩国电影一区二区三区 | 亚洲国产日韩一区二区 | av福利网| 免费日韩一区二区 | 久久久96人妻无码精品 | 我爱52av| 日日日干干干 | 欧美一区成人 | 亚洲午夜av在线 | 91最新在线 | 精品国产免费av | 美女靠逼app | 在线观看中文字幕一区 | 久久久精品国产sm调教 | 懂色av一区二区三区免费观看 | 国产真实乱人偷精品 | 国产性按摩 | 欧美日韩精品二区 | 深夜福利视频在线 | 国产精品无码电影在线观看 | 色屁屁影院www国产高清麻豆 | 男人的影院 | 欧美色视| 日韩第四页| 欧美极品jizzhd欧美仙踪林 | 天堂网在线资源 | 欧美一级色图 | 淫语视频 | 日韩av一区二区三区在线观看 | 俄罗斯黄色大片 | 调教一区二区三区 | 欧美精品成人一区二区三区四区 | 日本视频一区二区三区 | 日韩最新网址 | 一区二区三区久久 | 久久99精品久久久久婷婷 | 亚洲第一色播 | 色多多入口 | 久久精品无码一区二区三区 | cao我|