"BZOJ-1103" Metropolitan Meg tree array + DFS sequence

Source: Internet
Author: User

1103: [POI2007] Metropolitan meg time limit:10 Sec Memory limit:162 MB
submit:2009 solved:1056
[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, each line of two integers, A/b (1 < = a) The following line contains an integer m (1 < = m < = 2 50000), indicating that blue Mary has been sent during the transformation Over M-Times letter. 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 a 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
Y
A 1 4
Y
A 4 5
Y
Y |
A 1 2
A 1 3
Sample Output2
1
0
1HINT

SourceSolution

At first I thought of a tree-chain approach, but the complexity of $nlog^{2}n$ was obviously not

And then I thought, actually, it's simple.

Because a point can affect only his subtree, so we use the DFS sequence to process

Let's split it up and throw it on a tree-shaped array. Time complexity $o (NLOGN) $

Code
#include <iostream>#include<cstring>#include<algorithm>#include<cmath>#include<cstdio>using namespaceStd;inlineintRead () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();}  while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}#defineMAXN 250010intn,m;structedgenode{intNext,to, from;} edge[maxn<<1];intHead[maxn],cnt=1;voidAddedge (intUintV) {cnt++; edge[cnt].next=head[u]; head[u]=cnt; edge[cnt].to=v; edge[cnt]. from=u;}voidInsertedge (intUintv) {Addedge (u,v); Addedge (v,u);}namespacebit{intTree[maxn];inlineintLowbit (intx) {returnx&-x;} InlinevoidChange (intPosintdel) {     for(intI=pos; i<=n; i+=lowbit (i)) Tree[i]+=del;} InlineintQuery (intPOS) {    intRe=0;  for(intI=pos; I i-=lowbit (i)) Re+=Tree[i]; returnre;}}intPL[MAXN],PRE[MAXN],PR[MAXN],DFN;voidDFS (intNowintLast ) {Pl[now]=++DFN; pre[dfn]=Now ;  for(intI=head[now]; I I=edge[i].next)if(edge[i].to!=Last )    DFS (Edge[i].to,now); Pr[now]=DFN;}intMain () {N=read ();  for(intU,v,i=1; i<=n-1; i++) U=read (), v=read (), Insertedge (U,V); DFS (1,0);  for(intI=2; i<=n; i++) Bit::change (Pl[i],1), Bit::change (pr[i]+1,-1); M=read () +n-1;  while(m--)        {            Charopt[5]; scanf"%s", opt);intb; if(opt[0]=='A') A=read (), B=read (), Bit::change (pl[b],-1), Bit::change (pr[b]+1,1); if(opt[0]=='W') A=read (), printf ("%d\n", Bit::query (Pl[a])); }    return 0;}

"BZOJ-1103" Metropolitan Meg tree array + DFS sequence

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.