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

Source: Internet
Author: User
Tags time limit

1103: [POI2007] Metropolitan Meg

Time Limit:10 Sec
Memory limit:162 MB

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 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, 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 Input

5
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 Output

2
1
0
1

HINT

title Address bzoj1103: [POI2007] Metropolitan Meg Main topic:

A tree starts with a blue edge on each side and supports two operations:
1. Ask for the number of blue edges from the root to the X node
2. Given x, Y. One side of them changed into red.

The following:

Preprocess the first answer to each point
For each modification, only the subtree of the deepest node in X, Y is affected, and each point in the subtree has an answer of 1
So first do one side of the DFS sequence, the nodes in the subtree are adjacent in the DFS sequence, and then use a tree-like array to maintain the interval modification, single-point query can be www.cnblogs.com/shaokele/

AC Code

#include <cstdio> #include <algorithm>using namespace Std;const int N=250005;int n,q,x,y,cnt,tot,ans;int    Last[n],dep[n],num[n],sum[n],id[n],go[n],c[n];inline int read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | |    Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}    while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} struct edge{int to,next;}    E[n+n];void Add_edge (int u,int v) {e[++cnt]= (edge) {v,last[u]};last[u]=cnt; E[++cnt]= (Edge) {u,last[v]};last[v]=cnt;}    void dfs1 (int u,int fa) {id[u]=++tot;    Num[tot]=u;        for (int i=last[u];i;i=e[i].next) {if (E[I].TO==FA) continue;        dep[e[i].to]=dep[u]+1;    DFS1 (E[i].to,u); } Go[id[u]]=tot;}        void dfs2 (int u,int fa) {for (int i=last[u];i;i=e[i].next) {if (E[I].TO==FA) continue;        sum[id[e[i].to]]+=sum[id[u]]+1;    DFS2 (E[i].to,u); }}void Add (int k,int num) {for (int i=k;i<=n;i+=i& (-i)) C[i]+=num;}    int query (int k) {int res=0; for (int i=k;i>=1;i-=i& (-i)) res+=c[i]; return res;}    int main () {n=read ();        for (int i=1;i<n;i++) {int u=read (), V=read ();    Add_edge (U,V);    } dep[1]=1;    DFS1 (1,0);    DFS2 (1,0);    scanf ("%d\n", &q);        for (int i=1;i<=n+q-1;i++) {char ch=getchar ();            if (ch== ' W ') {scanf ("%d\n", &x);        printf ("%d\n", Sum[id[x]]+query (Id[x]));            }else{scanf ("%d%d\n", &x,&y);            if (Dep[x]<dep[y]) swap (x, y);            Add (id[x],-1);        Add (go[id[x]]+1,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.