Bzoj1103: [poi2007] Meg Metropolis

Source: Internet
Author: User
1103: [poi2007] megtime limit: 10 sec memory limit: 162 MB
Submit: 1145 solved: 569
[Submit] [Status] Description

Under the influence of the wave of economic globalization, the postman Blue Mary, who is used to walking in the village path in the early morning, also began to ride a motorcycle to deliver emails. However, she often recalled previous country walks. In the past, there were N small villages numbered 1 .. n in sequence in the countryside, and some villages had two-way dirt roads. Each village has exactly one path to reach village 1 (that is, Bitburg ). In addition, for each village, the path from the village to Bitburg exactly goes through a village with a number smaller than its number. In addition, for all roads, they do not encounter any other place except the village. There has never been a viaduct or underground railway in this unactivated area. Over time, more and more earth roads have been transformed into roads. So far, Blue Mary clearly remembers the scene where the last dirt road was transformed into a road. Now, there is no earth road-All roads have become roads, and the former village has become a metropolis. Blue Mary remembered her experience as a messenger during the transformation. Starting from Bitburg, she needs to go to a village and some dirt roads are transformed into roads during the interval between the two sending experiences. now, Blue Mary needs your help: calculate the number of paths she needs to walk every time she delivers a mail. (For roads, She can ride a motorcycle, but for dirt roads, she has to go to the cart .)

Input


The first row is a number N (1 <=n <= 2 50000 ).
The following n-1 rows have two integers A and B (1 <= A and the following row contains an integer m (1 <= m <= 2 50000 ), it indicates that Blue Mary sent m Emails during the transformation.
There are several pieces of information in two formats in the following N + M-1 lines, indicating N + M-1 events that have occurred successively by Time:
If this behavior is a B (if this behavior is w a, it indicates that Blue Mary has sent a mail from Bitburg to village.

 

Output

There are m rows. Each row contains an integer, indicating the number of paths that pass through a mail.

Sample input5
1 2
1 3
1 4
4 5
4
W 5
A 1 4
W 5
A 4 5
W 5
W 2
A 1 2
A 1 3

Sample output2
1
0
1


Hint

 

 

Source

Question:

Amazing DFS order!

First DFS, record the timestamp L [I], R [I] Then the interval (L [I], R [I]) represents its subtree

When we add (L [I], 1) Add (R [I],-1), we obtain the prefix for L [I] and the number of paths from this point to the root node.

Because if one entry and one exit + 1-1 are offset! However, if the sub-tree of I has only one + 1, the above edge will be added.

It can be understood that if I enters I from the parent node of I, Tulu + 1. If I returns to the parent node of I, Tulu-1

Next, modify the settings. directly add (L [I],-1) Add (R [I], 1 ).

This is exactly the opposite of the previous operations. In this way, no matter in the subtree of I, the contribution of all I nodes is 0, which means that this is a road.

Note the following details:

1. root Node 1 does not need to add (L [I], 1) Add (R [I],-1), because 1 does not have a parent node and starts from 1, the initial number of soil paths must be 0.

2. Modify (x, y) The Path depends on who is the son. We can record Dep to determine, or record fa. In short, we only need to change the son information.

PS: Tree link splitting can lead to 100 points (14 test points), and other points have timed out...

Code:

 1 {$inline on} 2 {$M 10000000,0,maxlongint} 3 const maxn=300000+100; 4 type node=record 5      go,next:longint; 6      end; 7  8 var  e:array[0..2*maxn] of node; 9      head,dep,l,r:array[0..maxn] of longint;10      s:array[0..2*maxn] of longint;11      i,n,x,y,tot,clock:longint;12      ch:char;13      procedure swap(var x,y:longint);inline;14       var t:longint;15       begin16         t:=x;x:=y;y:=t;17       end;18      procedure insert(x,y:longint);inline;19       begin20         inc(tot);21         e[tot].go:=y;e[tot].next:=head[x];head[x]:=tot;22       end;23 procedure dfs(x:longint);inline;24  var i,y:longint;25  begin26    inc(clock);l[x]:=clock;27    i:=head[x];28    while i<>0 do29     begin30      y:=e[i].go;31      if dep[y]=0 then32       begin33         dep[y]:=dep[x]+1;34         dfs(y);35       end;36      i:=e[i].next;37     end;38    inc(clock);r[x]:=clock;39  end;40 procedure add(x,y:longint);inline;41  begin42    while x<=2*n do43      begin44       inc(s[x],y);45       inc(x,x and (-x));46      end;47  end;48 function sum(x:longint):longint;inline;49  begin50    sum:=0;51    while x>0 do52      begin53       inc(sum,s[x]);54       dec(x,x and (-x));55      end;56  end;57 procedure init;58  begin59    readln(n);60    for i:=1 to n-1 do begin readln(x,y);insert(x,y);insert(y,x);end;61    dep[1]:=1;62    dfs(1);63    //for i:=1 to n do writeln(i,‘ ‘,l[i],‘ ‘,r[i],‘ ‘,dep[i]);64    for i:=2 to n do begin add(l[i],1);add(r[i],-1);end;65  end;66 procedure solveask;inline;67  begin68    readln(x);writeln(sum(l[x]));69  end;70 procedure solvechange;inline;71  begin72    readln(x,y);73    if dep[x]<dep[y] then swap(x,y);74    add(l[x],-1);add(r[x],1);75  end;76 procedure main;77  begin78    readln;79    while not eof do80     begin81      read(ch);82      case ch of83      ‘W‘:solveask;84      ‘A‘:solvechange;85      end;86     end;87  end;88 begin89   assign(input,‘input.txt‘);assign(output,‘output.txt‘);90   reset(input);rewrite(output);91   init;92   main;93   close(input);close(output);94 end.   
View code

 

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.