[BZOJ1103] [POI2007] Metropolitan meg (Dfs sequence + tree array)

Source: Internet
Author: User

1103: [POI2007] Metropolitan meg time limit:10 Sec Memory limit:162 MB
submit:2859 solved:1505
[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 small villages, some villages have two
To the dirt road. 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 exactly
Only after the village numbered smaller than its number. Also, for all roads, they are not met in locations other than villages. In this not open
Never had a viaduct or an underground railroad. Over time, more and more dirt roads have been transformed into highways. So far, Blue Mary
It is also clear that the last dirt road was transformed into a highway. Now, there is no dirt road--all roads become highways, and the former
Village has become a big city. Blue Mary remembered her experience of delivering letters during the makeover. She was departing from Fort Lauderdale and needed to go to a village,
And during the interval between the two messengers, some of the dirt roads were transformed into highways. Now blue Mary needs your help: Calculating every letter she needs
The number of dirt roads to be traversed. (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 line containing an integer m
(1 < = m < = 2 50000), indicating that blue Mary had sent m letters during the transformation. The following n+m-1 lines have several information in two formats per line
, which indicates that the N+m-1 event occurred in chronological order: 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 Input
51 21 31 44 54W5A1 4W5A4 5W5W2A1 2A1 3



Sample Output
2 1 0 1

HINT

Analysis:Find each time only by asking a point to 1 of this point's path weights and, and prefixes and similarities. It is natural to associate the DFS sequence maintenance prefix and, each point is split into two numbers, in front of the weight of 1, followed by the weighted value of-1, when asked a point, only with the prefix to ask for the previous point to 1 and. When modifying, only the weights of the two points of a point are changed to 0. Because only a single point of modification is involved, you can use a tree-like array (line-segment tree). Attach the AC code:
# include <iostream># include<cstdio># include<cstring>Const intN =250002;using namespacestd;intSt[n],en[n],cnt,n,m,inde;intTree[n *2],head[n];structedge{intTo,next;} edge[2*N];voidAddedge (intUintv) {Edge E1={V,head[u]}; edge[++inde] =E1; Head[u]=Inde; Edge E2={U,head[v]}; edge[++inde] =E2; HEAD[V]=Inde;}voidDfsintUintpre) {St[u]= ++CNT;  for(inti = Head[u];i;i =Edge[i].next) {        if(edge[i].to = = pre)Continue;    DFS (EDGE[I].TO,U); } En[u]= ++CNT;}voidUpdata (intKintnum) {     while(k <= N *2) {Tree[k]+=num; K+ = (k &-k); }}intReadintk) {    intsum =0;  while(k) {sum+=Tree[k]; K-= (K &-k); }    returnsum;}Charstr;intMain () {scanf ("%d",&N); intx, y;  for(inti =1; I < n;i++) {scanf ("%d%d",&x,&y);  Addedge (x, y); } DFS (1,-1);  for(inti =2; I <= n;i++) {Updata (st[i],1); Updata (en[i],-1); } scanf ("%d",&m);  for(inti =1; I <= m + N-1; i++) {scanf ("\n%c",&str); if(str = ='W') scanf ("%d", &x), printf ("%d\n", read (st[x])); Else{scanf ("%d%d", &x,&y);if(x > Y) swap [x, y]; Updata (st[y],-1), Updata (En[y],1);} } return 0;}

[BZOJ1103] [POI2007] Metropolitan meg (Dfs sequence + 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.