POJ 3321 Apple Tree (dfs+ segment tree)

Source: Internet
Author: User

Main topic:

Modify the nodes on the tree, and then seek the and of the subtree.


Thinking Analysis:

DFS re-numbering, Bad street ...

#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #define MAXN 100005#define Lson num<<1,s,mid#define Rson num<<1|1,mid+1,eusing namespace Std;int num[maxn];int ED[MAXN] ; int tot[maxn<<2];void pushup (int num) {tot[num]=tot[num<<1]+tot[num<<1|1];}        void build (int num,int s,int e) {if (s==e) {tot[num]=1;    Return    } int mid= (s+e) >>1;    Build (Lson);    Build (Rson); Pushup (num);}        void update (int num,int s,int e,int pos) {if (s==e) {tot[num]^=1;    Return    } int mid= (s+e) >>1;    if (pos<=mid) update (LSON,POS);    else update (RSON,POS); Pushup (num);}    int query (int num,int s,int e,int l,int R) {if (l<=s && r>=e) {return tot[num];    } int mid= (s+e) >>1;    if (r<=mid) return query (LSON,L,R);    else if (l>mid) return query (RSON,L,R); else return query (lson,l,mid) +query (rson,mid+1,r);} int Id;int Next[maxn<<1];inT head[maxn];int to[maxn<<1];void addedge (int u,int v) {id++;    Next[id]=head[u];    To[id]=v; Head[u]=id;}    int Cnt=1;bool vis[maxn];int dfs (int cur) {num[cur]=cnt++;    Ed[num[cur]]=num[cur];        for (int p=head[cur]; p;p =next[p]) {if (vis[to[p])) continue;        Vis[to[p]]=true;    Ed[num[cur]] = max (Ed[num[cur]],dfs (to[p)); } return Ed[num[cur]];}    int main () {int n;        while (scanf ("%d", &n)!=eof) {memset (vis,false,sizeof vis);        memset (head,0,sizeof head);        memset (ed,0,sizeof ed);        Id=0,cnt=1;            for (int i=1;i<=n-1;i++) {int u,v;            scanf ("%d%d", &u,&v);            Addedge (U,V);        Addedge (V,u);        } vis[1]=true;        DFS (1);        int m;        scanf ("%d", &m);        Char str[2];        Build (1,1,cnt-1);            while (m--) {int op;            scanf ("%s%d", Str,&op); if (str[0]== ' Q ') {printf ("%d\n ", query (1,1,cnt-1,num[op],ed[num[op]));        } else update (1,1,cnt-1,num[op]); }} return 0;} /*41 21 32 42 5*/


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.