"bzoj1103" "POI2007" "Metropolis" (tree-like array + differential)

Source: Internet
Author: User

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

54W 5A 1 4W 5A 4 5W 5W 2A 1 2A 1 3

Sample Output

2101

Hint

Problem: This topic, I saw the thought of the tree chain, but according to the label is a tree-like array of the topic (⊙o⊙) Ah!

Then read the HZW blog, found that why the DFS order is not recursive, this service, Leng looked at half a day.

Then he understood that he had beaten one and finally a.

1#include <cstdio>2#include <cstring>3#include <cmath>4#include <iostream>5#include <algorithm>6 using namespacestd;7 Const intn=250007;8 9 intN,m,tim;Ten intcnt,head[n],next[n*2],rea[n*2]; One intl[n*2],r[n*2],tree[n*2]; A Charch[2]; -  - voidAddintUintV) {next[++cnt]=head[u],head[u]=cnt,rea[cnt]=v;} the intLowbit (intx) {returnx& (-x);} - voidDfsintUintFA) - { -l[u]=++Tim; +      for(inti=head[u];i!=-1; i=Next[i]) -     { +         intv=Rea[i]; A         if(V==FA)Continue; at DFS (v,u); -     } -r[u]=++Tim; - } - voidAbintXintz) - { in      for(inti=x;i<=2*n;i+=lowbit (i)) tree[i]+=Z; - } to intQueryintx) + { -     intres=0; the      for(inti=x;i>=1; i-=lowbit (i)) *res+=Tree[i]; $     returnRes; Panax Notoginseng } - intMain () the { +memset (head,-1,sizeof(head)); Ascanf"%d",&n); the     intx, y; +      for(intI=1; i<n;i++) -     { $scanf"%d%d",&x,&y); $ Add (x, y), add (y,x); -     } -Dfs1,-1); the      for(intI=2; i<=n;i++)  -AB (L[i),1), AB (r[i],-1);Wuyiscanf"%d",&m); the      for(intI=1; i<=m+n-1; i++) -     { Wuscanf"%s", ch); -         if(ch[0]=='W') About         { $scanf"%d",&x); -printf"%d\n", Query (L[x])); -         } -         Else A         { +scanf"%d%d",&x,&y); thex=Max (x, y); -AB (l[x],-1), AB (R[x],1); $         } the     } the}

It's just a bit of a difference, =.

"bzoj1103" "POI2007" "Metropolis" (tree-like array + differential)

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.