POJ 3321 Apple tree structure into a linear structure + segment tree or tree array

Source: Internet
Author: User
Tags cmath

Apple Tree
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 21587 Accepted: 6551
POJ 3321 Links:

Description

There is a apple tree outside of Kaka ' s house. Every autumn, a lot of apples would 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 is connected by branches. Kaka numbers the forks by 1 to N and the root are always numbered by 1. Apples'll grow on the forks and both Apple won ' t grow on the same fork. Kaka wants to know how many apples is there in a sub-tree, for his study of the produce ability of the apple tree.

The trouble is, a new Apple may grow in an empty fork some time and Kaka could pick an apple from the tree for his Desse Rt. Can you help Kaka?

Input

The first line contains a integer  n   ( n  ≤100,000), which is the number of the forks In the tree.
the following  N  -1 lines each contain the 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 be either
" x strong> "which means the existence of the apple on fork  x  has been changed. i.e. if there is a apple on the fork and then Kaka pick it; Otherwise a new Apple has grown on the empty fork.
or
" x " which means an inquiry for the number of apples in the sub-tree above the F ork  x , including the apple (if exists) on the fork x
Note The tree was full of apples at the beginning

Output

For every inquiry, the output of the correspond answer per line.

Sample Input

33Q 1C 2Q 1

Sample Output

32

Be inspirational .

Every one you hate now, there's a hard enough once

Off Topic

This topic I think out particularly good, very enlightening thinking, line tree topic must brush the problem Ah!!! Words are not far away, let us go to the problem, began to talk.

Test Instructions

A tree with n nodes, beginning with an apple on each node. Now give the M-group operation: (C,i) is to take off the first node above the apple (if the Apple does not exist, then add an apple), (q,i) is to query the node I nodes as the root of the subtree has several apples (including the I node).

Analysis

Step 1 :

As can be seen, because the normal tree does not have the nature of the interval, so when considering the use of line tree as a solution, we need to transform the given data, first a DFS traversal of the tree, the start and end time of each point in the DFS sequence is recorded, the start time of the record is the pre-sequence traversal, The end time is a sequential traversal, and the lesson tree is re-labeled.


Step 2 :

For example, after DFS, then each node of the tree has the nature of the interval.


At this point, each node corresponds to an interval, and you can see that each node corresponding to the interval exactly "governs" its sub-tree all the nodes of the interval, then the operation of the point or subtree into the operation of the interval.

PS: If you do not understand the traversal of the tree, you may want to follow the code step-by-step debugging, or on the paper simulation process ~ "

Step 3 :

At this time, each time to update the node or query, is the line tree and tree array the most basic implementation of ...

Code Implementation

Let me give you two implementations of line tree and tree array respectively.

/****************************>>>>headfiles<<<<****************************/#include <set> #include <map> #include <list> #include <cmath> #include <queue> #include <vector > #include <cstdio> #include <string> #include <cstring> #include <iomanip> #include < iostream> #include <sstream> #include <algorithm>using namespace std;/****************************             >>>>>define<<<<<*****************************/#define FST First#define snd Second#define root 1,n,1#define lson l,mid,rt<<1#define Rson mid+1,r,rt<& Lt;1|1#define PB (a) push_back (a) #define MP (A, B) Make_pair (A, b) #define CASE (T) for (scanf ("%d", &am P T); t--;) #define FIN freopen ("Input.txt", "R", stdin) #define FOUT freopen ("Output.txt", "w", stdout)//#prag MA COMMENT (linker, "/stack:1024000000,1024000000") typedef __inT64 ll;typedef pair<int,int> pii;const int INF = 0x3f3f3f3f;const int MAXN = 100000 + 5;/***************** >>>>separator<<<<****************************/struct Node {int fir,las;} nodes[maxn];vector<vector<int> > G;bool vis[maxn];int segtree[maxn << 2];int G_Mark, N, M, u, v;void INI    T () {g.resize (n+2);    G_mark = 0;    G.clear (); Memset (Vis, false, sizeof (VIS));}    void DFS (int x) {vis[x] = true;    Nodes[x].fir = ++g_mark;        for (int i = 0; i < g[x].size (); i++) {if (Vis[g[x][i]]) continue;    DFS (G[x][i]); } Nodes[x].las = G_mark;} inline void pushup (int rt) {Segtree[rt] = Segtree[rt << 1] + segtree[rt << 1 | 1];}    void Build (int l, int r, int rt) {Segtree[rt] = r-l + 1;    if (L = = r) return;    int mid = (L + r) >> 1;    Build (Lson); Build (Rson);}        void Update (const int& POS, int l, int r, int rt) {if (L = = r) {Segtree[rt] ^= 1;  Return  } int mid = (L + r) >> 1;    if (POS <= mid) Update (POS, Lson);    Else Update (POS, Rson); Pushup (RT);} int Query (const int& L, const int& R, int L, int R, int rt) {if (L <= l && R <= R) return s    EGTREE[RT];    int mid = (L + r) >> 1, ret = 0;    if (L <= mid) ret + = Query (l, R, Lson);    if (R > Mid) ret + = Query (L, R, Rson); return ret;}    int main () {//FIN;        while (~SCANF ("%d", &n)) {init ();            for (int i = 0; i < N-1; i++) {scanf ("%d%d", &u, &v); G[u].            PB (v); G[V].        PB (U);        } DFS (1);        scanf ("%d", &m);        Char op[5];        int x, POS, L, R, ans;        Build (root);            for (int i = 0; i < M; i++) {scanf ("%s%d", Op, &x);                if (op[0] = = ' C ') {pos = Nodes[x].fir;            Update (POS, root); } else {L = Nodes[x].fir, R = Nodes[x].las;                Ans = Query (L, R, Root);            printf ("%d\n", ans); }}} return 0;}

/****************************>>>>headfiles<<<<****************************/#include <set> #include <map> #include <list> #include <cmath> #include <queue> #include <vector > #include <cstdio> #include <string> #include <cstring> #include <iomanip> #include < iostream> #include <sstream> #include <algorithm>using namespace std;/****************************             >>>>>define<<<<<*****************************/#define FST First#define snd Second#define root 1,n,1#define lson l,mid,rt<<1#define Rson mid+1,r,rt<& Lt;1|1#define PB (a) push_back (a) #define MP (A, B) Make_pair (A, b) #define CASE (T) for (scanf ("%d", &am P T); t--;) #define FIN freopen ("Input.txt", "R", stdin) #define FOUT freopen ("Output.txt", "w", stdout)//#prag MA COMMENT (linker, "/stack:1024000000,1024000000") typedef __inT64 ll;typedef pair<int,int> pii;const int INF = 0x3f3f3f3f;const int MAXN = 100000 + 5;/***************** >>>>separator<<<<****************************/struct Node {int fir,las;} nodes[maxn];vector<vector<int> > G;bool vis[maxn];int c[maxn],s[maxn];int G_Mark, N, M, U, v;void init () {G    . Resize (n+2);    G_mark = 0;    G.clear ();    memset (c,0,sizeof (C)); Memset (Vis, false, sizeof (VIS));}    void DFS (int x) {vis[x] = true;    Nodes[x].fir = ++g_mark;        for (int i = 0; i < g[x].size (); i++) {if (Vis[g[x][i]]) continue;    DFS (G[x][i]); } Nodes[x].las = G_mark;} inline int lowbit (int x) {return x& (x^ (x-1));}        void Add (int pos,int val) {while (pos <= N) {C[pos] + = val;    pos + = Lowbit (POS);    }}int Sum (int pos) {int ret = 0;        while (pos > 0) {ret + = C[pos];    pos-= Lowbit (POS); } return ret;}    int main () {//FIN;    while (~SCANF ("%d", &n)){init ();            for (int i = 0; i < N-1; i++) {scanf ("%d%d", &u, &v); G[u].            PB (v); G[V].        PB (U);        } DFS (1);        scanf ("%d", &m);        Char op[5];        int x, POS, L, R, ans;        for (int i = 1;i <= n;i++) Add (i,1), s[i] = 1;            for (int i = 0; i < M; i++) {scanf ("%s%d", Op, &x);                if (op[0] = = ' C ') {pos = Nodes[x].fir;                S[pos] ^= 1; int add = S[pos] = = 1?                1:-1;            ADD (Pos,add);                } else {L = Nodes[x].fir, R = Nodes[x].las;                if (L = = 1) ans = Sum (R);                else ans = SUM (R)-sum (L-1);            printf ("%d\n", ans); }}} return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 3321 Apple tree structure into a linear structure + segment tree or tree array

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.