MEG-Megalopolis

题目描述

Byteotia has been eventually touched by globalisation, and so has Byteasar the Postman, who once roamedthe country lanes amidst sleepy hamlets and who now dashes down the motorways. But it is those strolls inthe days of yore that he reminisces about with a touch of tenderness.

In the olden days nn Byteotian villages (numbered from 11 to nn ) were connected by bidirectional dirt roadsin such a way, that one could reach the village number 11 (called Bitburg) from any other village in exactlyone way. This unique route passed only through villages with number less or equal to that of the startingvillage. Furthermore, each road connected exactly two distinct villages without passing through any othervillage. The roads did not intersect outside the villages, but tunnels and viaducts were not unheard of.

Time passing by, successive roads were being transformed into motorways. Byteasar remembers distinctly, when each of the country roads so disappeared. Nowadays, there is not a single country lane left in Byteotia - all of them have been replaced with motorways, which connect the villages into Byteotian Megalopolis.

Byteasar recalls his trips with post to those villages. Each time he was beginning his journey with letters to some distinct village in Bitburg. He asks you to calculate, for each such journey (which took place in a specific moment of time and led from Bitburg to a specified village), how many country roads it led through.

TaskWrite a programme which:

reads from the standard input:

descriptions of roads that once connected Byteotian villages, sequence of events: Byteasar's trips and the moments when respective roads were transformed into motorways, for each trip, calculates how many country roads Byteasar has had to walk, writes the outcome to the standard output.

在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了。不过,她经常回忆起以前在乡间漫步的情景。昔日,乡下有依次编号为1..n的n个小村庄,某些村庄之间有一些双向的土路。从每个村庄都恰好有一条路径到达村庄1(即比特堡)。并且,对于每个村庄,它到比特堡的路径恰好只经过编号比它的编号小的村庄。另外,对于所有道路而言,它们都不在除村庄以外的其他地点相遇。在这个未开化的地方,从来没有过高架桥和地下铁道。随着时间的推移,越来越多的土路被改造成了公路。至今,Blue Mary还清晰地记得最后一条土路被改造为公路的情景。现在,这里已经没有土路了——所有的路都成为了公路,而昔日的村庄已经变成了一个大都市。 Blue Mary想起了在改造期间她送信的经历。她从比特堡出发,需要去某个村庄,并且在两次送信经历的间隔期间,有某些土路被改造成了公路.现在Blue Mary需要你的帮助:计算出每次送信她需要走过的土路数目。(对于公路,她可以骑摩托车;而对于土路,她就只好推车了。)

输入输出格式

输入格式:

In the first line of the standard input there is a single integer nn ( 1≤n≤250 000 ),denoting the number of villages in Byteotia. The following n-1 lines contain descriptions of the roads, in the form of two integers a , b ( 1≤a<b≤n )separated by a single space, denoting the numbers of villages connected with a road. Inthe next line there is a single integer mm ( 1≤m≤250 000 ),denoting the number of trips Byteasar has made.

The following n+m−1 lines contain descriptions of the events, in chronological order:

A description of the form "A a b "(for a<b) denotes a country road between villages a and b beingtransformed into a motorway in that particular moment.

A description of the from "W a " denotes Byteasar's trip from Bitburg to village a .

输出格式:

Your programme should write out exactly mm integers to the standard output, one a line, denoting the numberof country roads Byteasar has travelled during his successive trips.

输入输出样例

输入样例#1:

5

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

输出样例#1:

2

1

0

1

题解

题目保证输入是一棵树,有修改操作和查询操作

暴力方法很容易想到从单个节点向根节点跑最短路

正解:其实也很容易想到,我们可以利用它的dfs序,我们发现按dfs序维护树状数组就是一个点到1点经过的土路的条数,利用差分思想就可以了

在这个基础上统计区间值,用树状数组就可以了

Code

#include<bits/stdc++.h>
using namespace std;
void in(int &ans) {
ans=0;int f=1; char i=getchar();
while(i<'0' || i>'9') {if(i=='-') f=-1; i=getchar();}
while(i>='0' && i<='9') {ans=(ans<<1)+(ans<<3)+i-'0'; i=getchar();}
ans*f;
}
int n,m,len,dfc;
struct egde {
int to,next;
}e[500010];
int head[250100],dfn[250010],f[250010],dep[250010],size[250010];
void add(int a,int b) {
e[++len].to=b;
e[len].next=head[a];
head[a]=len;
}
int lowbit(int x) {
return x&-x;
}
int check(int x) {
int ans=0;
while(x) {
ans+=f[x];
x-=lowbit(x);
}
return ans;
}
void change(int x,int a) {
while(x<=n) {
f[x]+=a;
x+=lowbit(x);
}
}
void dfs(int u,int fa) {
dfn[u]=++dfc,size[u]=1;
for(int i=head[u];i;i=e[i].next) {
int to=e[i].to;
if(to!=fa) {
dep[to]=dep[u]+1;
dfs(to,u);
size[u]+=size[to];
}
}
}
int main()
{
int a,b;
in(n);
for(int i=1;i<n;i++) {
in(a); in(b);
add(a,b); add(b,a);
}
dep[1]=1; dfs(1,0);
for(int i=2;i<=n;i++) {
change(dfn[i],1); change(dfn[i]+size[i],-1);
}
in(m);
for(int i=1;i<=m+n-1;i++) {
char ch;int a,b; cin>>ch;
if(ch=='A') {
in(a);in(b);
if(dep[a]>dep[b]) swap(a,b);
change(dfn[b],-1); change(dfn[b]+size[b],1);
}
else {
in(a); cout<<check(dfn[a])<<endl;
}
}
return 0;
}

博主蒟蒻,随意转载.但必须附上原文链接

http://www.cnblogs.com/real-l/

POI2007 MEG-Megalopolis [树状数组]的更多相关文章

  1. BZOJ 1103 [POI2007]大都市meg(树状数组+dfs序)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1103 [题目大意] 给出一棵树,每条边的经过代价为1,现在告诉你有些路不需要代价了, ...

  2. BZOJ 1107: [POI2007]驾驶考试egz / Luogu P3463 [POI2007]EGZ-Driving Exam (树状数组 LIS)

    能从iii走到所有跑道 相当于 能从iii走到111和nnn. 边反向后就相当于 能从111和nnn走到iii. 为了方便叙述,把111~nnn叫做x坐标,111~(m+1)(m+1)(m+1)叫做y ...

  3. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  4. 树状数组【bzoj1103】: [POI2007]大都市meg

    1103: [POI2007]大都市meg 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了. 不过,她经常回忆起以前在乡间漫步的情景.昔日,乡 ...

  5. BZOJ 1103: [POI2007]大都市meg(dfs序,树状数组)

    本来还想链剖的,结果才发现能直接树状数组的= = 记录遍历到达点与退出点的时间,然后一开始每个到达时间+1,退出时间-1,置为公路就-1,+1,询问直接点1到该点到达时间求和就行了- - CODE: ...

  6. [BZOJ1103][POI2007]大都市meg dfs序+树状数组

    Description 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景.昔日,乡下有依次编号为1..n ...

  7. 【dfs序】【树状数组】bzoj1103 [POI2007]大都市meg

    预处理出每个点到根节点的土路数,插到一个树状数组里,然后每次修改只会对子树中的节点造成影响,于是相当于区间修改.点查询了. #include<cstdio> using namespace ...

  8. bzoj 1103: [POI2007]大都市meg【dfs序+树状数组】

    很明显的暗示,就是在树的dfs序上维护树状数组,加减的时候差分即可 #include<iostream> #include<cstdio> #include<cstrin ...

  9. 【BZOJ-1103】大都市meg 树状数组 + DFS序

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2009  Solved: 1056[Submit][Sta ...

  10. [bzoj1103][POI2007]大都市meg_dfs序_树状数组

    大都市meg bzoj-1103 POI-2007 题目大意:给定一颗n个点的树,m次操作.将一条路的边权更改成0:查询一个点到根节点的点权和.开始的时候所有边的边权都是1. 注释:$1\le n,m ...

随机推荐

  1. python3 练习题100例 (二十九)猴子吃桃问题

    题目内容: 猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个.以后每天早上都吃了前一天剩下的一半零一个.到第n天(<1<n< ...

  2. JAVA 泛型方法<T>

    public static void main(String[] args) throws Exception { String[] arr = new String[]{"1", ...

  3. Uber CEO博鳌论坛采访:看好中国市场共享经济的发展模式

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  4. LI 标签中让文章标题左对齐,日期右对齐的方法

    希望实现标题在左对齐,日期在右对齐,当直接给日期的span加上float:right时,IE8和FF都OK,但IE6/7则会换行,下面给出一个简单有效的解决办法. <!DOCTYPE html  ...

  5. ip4addr_ntoa和不可重入函数

    在网络中,有一个转换IP地址到ASIIC字符串的函数,该函数的返回值所指向的ASIIC字符串驻留在静态内存中,所以该函数不可重入. 通俗的讲,在多任务系统中,一个任务执行在调用运行这个函数的时候,其他 ...

  6. Django笔记 —— 模板高级进阶

    最近在学习Django,打算玩玩网页后台方面的东西,因为一直很好奇但却没怎么接触过.Django对我来说是一个全新的内容,思路想来也是全新的,或许并不能写得很明白,所以大家就凑合着看吧- 本篇笔记(其 ...

  7. Qt C++ 并发,并行,多线程编程系列1 什么是并发

    什么是并发,并发往简单来说就是两个或多个独立的任务同时发生,在我们的生活中也是随处可见.如果把每个人都当作一个独立的任务,那每个人可以相互独立的生活,这就是并发. 在计算机的系统里面,并发一般有两种, ...

  8. cocos2d-x 键盘和鼠标事件

    出了菜单可以响应用户事件外,cocos2d中的层(Layer)也可以响应事件.层能够自动响应窗口事件,这些事件主要是键盘和鼠标事件,cocos2d中事件处理是通过Pyglet的事件处理完成的. 1.键 ...

  9. 在阿里云上遇见更好的Oracle(二)

    从上一篇文章的反馈来看,大家还是喜欢八卦多过技术细节,那这一篇继续一些题外话,说说我对“去IOE”的看法. 对同一件事情,参与的没参与的人,讨论起来,都会有各自的立场.所以这里先申明一下,以下内容只是 ...

  10. UVA 11884 A Shooting Game(记忆化搜索)

    A and B are playing a shooting game on a battlefield consisting of square-shaped unit blocks. The bl ...