Zoj 3686 A Simple Tree Problem

Source: Internet
Author: User

 

Question:

A tree has n nodes (1 <= N <= 100000), and the root is 1. Each node has a label and the initial value is 0.

There are two types of operations

O operation: (o, x) returns the inverse (OR 1) of the tag values of node x and all its child nodes ).

Q operation: (q, x) number of output node x and the label value of the child node is 1.

The number of operations is m (1 <= M <= 10000 ).

 

Question:

Traverse each node sequentially from the root, and rename each node in the traversal Order (beg ).

In this way, the re-numbered nodes, the root node and the node of the connected subtree are continuous, so that we can operate the update and interval Inquiry between time zones.

In addition, the maximum node number (end) in a subtree is recorded over time. In this way, we update or query the range of a node and Its subtree, that is

[Beg [x], end [x].

 

Code:

[Cpp]
# Include <stdlib. h>
# Include <string. h>
# Include <stdio. h>
# Include <ctype. h>
# Include <math. h>
# Include <stack>
# Include <queue>
# Include <map>
# Include <set>
# Include <vector>
# Include <string>
# Include <iostream>
# Include <algorithm>
Using namespace std;
 
# Define ll _ int64
# Define ls rt <1
# Define rs ls | 1
# Define lson l, mid, ls
# Define rson mid + 1, r, rs
# Define middle (l + r)> 1
# Define clr_all (x, c) memset (x, c, sizeof (x ))
# Define clr (x, c, n) memset (x, c, sizeof (x [0]) * (n + 1 ))
# Define eps (1e-8)
# Define MOD 1000000007.
# Define INF 0x3f3f3f
# Define PI (acos (-1.0 ))
# Pragma comment (linker, "/STACK: 102400000,102400000 ")
Template <class T> T _ max (T x, T y) {return x> y? X: y ;}
Template <class T> T _ min (T x, T y) {return x <y? X: y ;}
Template <class T> T _ abs (T x) {return (x <0 )? -X: x ;}
Template <class T> T _ mod (T x, T y) {return (x> 0 )? X % y :( (x % y) + y) % y ;}
Template <class T> void _ swap (T & x, T & y) {T t = x; x = y; y = t ;}
Template <class T> void getmax (T & x, T y) {x = (y> x )? Y: x ;}
Template <class T> void getmin (T & x, T y) {x = (x <0 | y <x )? Y: x ;}
Int TS, cas = 1;
Const int M = 100000 + 5;
Int n, m;
Struct node {
Int beg, end;
} Mp [M];
Int fa [M], nxt [M], vec [M], tot;
Void dfs (int rt ){
Mp [rt]. beg = ++ tot;
For (int I = fa [rt]; I! =-1; I = nxt [I]) dfs (vec [I]);
Mp [rt]. end = tot;
}
Int cov [M <2], sum [M <2];
Void build (int l, int r, int rt ){
Cov [rt] = sum [rt] = 0;
If (l = r) return;
Int mid = middle;
Build (lson), build (rson );
}
Void pushDown (int l, int mid, int r, int rt ){
If (! Cov [rt]) return;
Sum [ls] = (mid-l + 1)-sum [ls], cov [ls] ^ = 1;
Sum [rs] = (r-mid)-sum [rs], cov [rs] ^ = 1;
Cov [rt] = 0;
}
Void pushUp (int rt ){
Sum [rt] = sum [ls] + sum [rs];
}
Void update (int l, int r, int rt, int L, int R ){
If (L <= l & r <= R ){
Sum [rt] = (r-l + 1)-sum [rt];
Cov [rt] ^ = 1;
Return;
}
Int mid = middle;
PushDown (l, mid, r, rt );
If (R <= mid) update (lson, L, R );
Else if (mid <L) update (rson, L, R );
Else update (lson, L, mid), update (rson, mid + 1, R );
PushUp (rt );
}
Int query (int l, int r, int rt, int L, int R ){
If (L <= l & r <= R) return sum [rt];
Int mid = middle;
PushDown (l, mid, r, rt );
If (R <= mid) return query (lson, L, R );
Else if (mid <L) return query (rson, L, R );
Else return query (lson, L, mid) + query (rson, mid + 1, R );
}
 
Void run (){
Int I, j;
Clr (fa,-1, n + 1 );
Tot = 0;
For (I = 2; I <= n; I ++ ){
Scanf ("% d", & j );
Nxt [tot] = fa [j], vec [tot] = I, fa [j] = tot ++;
}
Tot = 0;
Dfs (1 );
Build (1, n, 1 );
While (m --){
Char op [3];
Scanf ("% s % d", op, & j );
If (op [0] = 'O') update (1, n, 1, mp [j]. beg, mp [j]. end );
Else printf ("% d \ n", query (1, n, 1, mp [j]. beg, mp [j]. end ));
}
Puts ("");
}
 
Void preSof (){
}
 
Int main (){
// Freopen ("input.txt", "r", stdin );
// Freopen ("output.txt", "w", stdout );
PreSof ();
// Run ();
While ((~ Scanf ("% d", & n, & m) run ();
// For (scanf ("% d", & TS); cas <= TS; cas ++) run ();
Return 0;
}

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.