POI2007 MEG-Megalopolis [树状数组]
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 [树状数组]的更多相关文章
- BZOJ 1103 [POI2007]大都市meg(树状数组+dfs序)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1103 [题目大意] 给出一棵树,每条边的经过代价为1,现在告诉你有些路不需要代价了, ...
- 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 ...
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- 树状数组【bzoj1103】: [POI2007]大都市meg
1103: [POI2007]大都市meg 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了. 不过,她经常回忆起以前在乡间漫步的情景.昔日,乡 ...
- BZOJ 1103: [POI2007]大都市meg(dfs序,树状数组)
本来还想链剖的,结果才发现能直接树状数组的= = 记录遍历到达点与退出点的时间,然后一开始每个到达时间+1,退出时间-1,置为公路就-1,+1,询问直接点1到该点到达时间求和就行了- - CODE: ...
- [BZOJ1103][POI2007]大都市meg dfs序+树状数组
Description 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景.昔日,乡下有依次编号为1..n ...
- 【dfs序】【树状数组】bzoj1103 [POI2007]大都市meg
预处理出每个点到根节点的土路数,插到一个树状数组里,然后每次修改只会对子树中的节点造成影响,于是相当于区间修改.点查询了. #include<cstdio> using namespace ...
- bzoj 1103: [POI2007]大都市meg【dfs序+树状数组】
很明显的暗示,就是在树的dfs序上维护树状数组,加减的时候差分即可 #include<iostream> #include<cstdio> #include<cstrin ...
- 【BZOJ-1103】大都市meg 树状数组 + DFS序
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2009 Solved: 1056[Submit][Sta ...
- [bzoj1103][POI2007]大都市meg_dfs序_树状数组
大都市meg bzoj-1103 POI-2007 题目大意:给定一颗n个点的树,m次操作.将一条路的边权更改成0:查询一个点到根节点的点权和.开始的时候所有边的边权都是1. 注释:$1\le n,m ...
随机推荐
- 初识python 面向对象
what the f**k!!这个知识点学不好的最大元凶就是,我还单身??? python基础(四): 面向对象的三个特点: 封装,继承,多态 类: 对象是面向对象编程的核心,在使用对象的过程中,为了 ...
- Java常考面试题
Java常考面试题 1. 什么是Java虚拟机?为什么Java被称作是“平台无关的编程语言”? 答:Java虚拟机是一个可以执行Java字节码的虚拟机进程.Java源文件被编译成能被Java虚拟机执行 ...
- Java中的IO流体系
Java为我们提供了多种多样的IO流,我们可以根据不同的功能及性能要求挑选合适的IO流,如图10-7所示,为Java中IO流类的体系. 注:这里只列出常用的类,详情可以参考JDK API文档.粗体标注 ...
- jmeter之Synchronizing Timer的理解
该功能类似loadrunner的集合点,一般按照jmeter的树形结构,放在需要设置集合点的请求之前,两个参数的意思,我们先看官网的解释: 大概意思就是: Number of Simulated Us ...
- 30分钟快速搭建Web CRUD的管理平台--django神奇魔法
加上你的准备的时间,估计30分钟完全够用了,因为最近在做爬虫管理平台,想着快速开发,没想到python web平台下有这么非常方便的框架,简洁而优雅.将自己的一些坑总结出来,方便给大家的使用. 准备环 ...
- Linux环境搭建系列之sorl服务器的安装部署
http://blog.csdn.net/upxiaofeng/article/details/51425732
- Gradle下载依赖jar包位置修改
gradle会下载相关需要依赖的jar包,默认的本地存放地址是:C:/Users/(用户名)/.gradle/caches/modules-2/files-2.1,很多人和我一样不愿意放在C盘,所以需 ...
- 机器视觉必知-GenICam相机通用接口标准
机器视觉必知-GenICam相机通用接口标准 GenICam(相机通用接口): 一种通用软件接口 通用相机接口标准 目前机器视觉行业所使用的相机几乎均以相同方式来进行配置,即:---通过在注册表中的读 ...
- xshell连接不到虚拟机,安装ssh服务
刚安装的虚拟机镜像是Ubuntu 16.04版本,防火墙已经关闭. 测试: 检查虚拟机分配的ip地址. 1.虚拟机ping宿主机:可以ping通 2.宿主机ping虚拟机:可以ping通 3.检查网络 ...
- Week2 Teamework from Z.XML 软件分析与用户需求调查(三)必应助手体验评测
评测人:毛宇 肖俊鹏 说明:言辞激烈,请勿介意 我花了2天的时间来试用这个软件<必应缤纷桌面手机助手>,有了很多体会,这里,我来谈一下这款软件在体验部分的表现情况. 体验部分主要分为三个部 ...