[POI2007] [BZOJ1103] Metropolis Meg|dfs Order | tree-like array

Source: Internet
Author: User

1103: [POI2007] Metropolitan meg time limit:10 Sec Memory limit:162 MB
submit:1350 solved:697
[Submit] [Status] [Discuss] Description

Under the influence of the tide of economic globalization, the postman, who is accustomed to strolling in the early morning countryside, has also begun to send mail by motorbike. However, she often recalls the past in the country walks the scene. In the past, the countryside was numbered 1. N of n small villages, some villages have two-way dirt roads. From each village there is exactly one path to the village 1 (that is, Fort Lauderdale). And, for each village, its path to the fort is just passing through a village numbered smaller than its number. Also, for all roads, they are not met in locations other than villages. In this uncivilized place, there has never been a viaduct or an underground railroad. Over time, more and more dirt roads have been transformed into highways. So far, Blue Mary remembers the last dirt road being transformed into a highway. Now, there is no dirt road-all roads become highways, and the former villages have become a metropolis. Blue Mary remembered her experience of delivering letters during the makeover. She departs from Fort Lauderdale and needs to go to a village, and some dirt roads have been transformed into highways during the interval between the two messengers. Now Blue Mary needs your help: Figure out the number of dirt roads she needs to walk through every time she sends her letters. (She could ride a motorbike for the road; she had to go for the dirt road.) )

Input


The first line is a number n (1 < = N < = 2 50000).
The following n-1 lines, two integers per line, A, B (1 < = a The following line contains an integer m (1 < = m < = 2 50000), indicating that blue Mary had sent m letters during the transformation.
The following n+m-1 lines, each with a number of information in two formats, represent N+M-1 events that have occurred chronologically:
If this behavior is a B (a if this behavior is W A, then blue Mary has been sent from Fort Lauderdale to village A.

Output

There are m rows, each containing an integer representing the number of dirt roads that correspond to a given letter.

Sample Input5
1 2
1 3
1 4
4 5
4
W 5
A 1 4
W 5
A 4 5
W 5
Y |
A 1 2
A 1 3

Sample Output2
1
0
1HINT

Source

After reading the problem almost began the code tree chain, and then checked the key points

Well, it's a DFS sequence + tree array.

What we want to maintain is the number of dirt roads on each point to the 1th-point path, and if we modify a path, then the number of soil paths on the path of the deep node of the road will be 1 for each point in the root subtree to point 1th.

We maintain a DFS sequence, such as: 1455422331.

First point +1 in the stack at each point, at the point of the out stack-1.

For each modification, at the stack point-1, out of the stack point +1. Use a tree-like array to maintain prefixes and.

Don't forget to lose one last answer.

The local past, eight in the tle, no words.

#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>using namespacestd;intN,M,CNT,TOP,DFN;intt[500005],head[500005],s[250005],fa[250005],l[250005],r[200005],list[500005],next[500005];inlineintRead () {intA=0, f=1;CharC=GetChar ();  while(c<'0'|| C>'9') {if(c=='-') f=-1; C=GetChar ();}  while(c>='0'&&c<='9') {a=a*Ten+c-'0'; C=GetChar ();} returnA *F;} InlinevoidInsertintXinty) {next[++cnt]=Head[x]; HEAD[X]=CNT; LIST[CNT]=y;} InlineintLowbit (intx) {    returnx& (-x);}voidUpdateintXintval) {     for(intI=x;i<=n+n;i+=lowbit (i)) t[i]+=Val;}voidAskintx) {    intans=-1;  for(intI=x;i;i-=lowbit (i)) ans+=T[i]; printf ("%d\n", ans);}voidDfs () {s[++top]=1;  while(top) {intnow=s[top],f=fa[top--]; if(!L[now]) {L[now]=++DFN; s[++top]=Now ;  for(intI=head[now];i;i=Next[i]) {                if(list[i]==f)Continue; s[++top]=List[i]; Fa[top]=Now ; }        }        Elser[now]=++DFN; }}intMain () {n=read ();  for(intI=1; i<n;i++)    {        intU=read (), v=read (); Insert (U,V);    Insert (V,U);    } dfs ();  for(intI=1; i<=n;i++) {update (l[i],1); Update (r[i],-1);} M=read ();  for(intI=1; i<=n+m-1; i++)    {        intx, y;Charch[5]; scanf ("%s", CH); if(ch[0]=='A') {x=read (); y=read (); Update (L[y],-1); Update (R[y],1); }        Else{x=read (); ask (L[x]);} }    return 0;}

[POI2007] [BZOJ1103] Metropolis Meg|dfs Order | tree-like 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.