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 ...
随机推荐
- 网站标题被篡改成北京赛车、PK10的解决处理办法
客户网站于近日被跳转到赌博网站,打开后直接跳转到什么北京赛车,PK10等内容的网站上去,客户网站本身做了百度的推广,导致所有访问用户都跳转到赌博网站上去,给客户带来很大的经济损失,再一个官方网站的形象 ...
- Leecode刷题之旅-C语言/python-136只出现一次的数字
/* * @lc app=leetcode.cn id=136 lang=c * * [136] 只出现一次的数字 * * https://leetcode-cn.com/problems/singl ...
- R语言绘图:箱线图
使用ggplot2绘制箱线图 ######*****绘制箱线图代码*****####### data1$学区房 <- factor(data1$school, levels = 0:1, lab ...
- Vue 去脚手架插件,自动加载vue文件,style怎么办
书接上上会,因为当时也没想好怎么办,所以装聋作哑的忽略了Vue文件中的Style,Vue的做法我看着晕乎乎的,细想的话,无非就是自动填写到dom中,所担心的无非是命名冲突. 在一个项目中(像我这种自娱 ...
- 12 TCP服务器 进程 线程 非阻塞
1.单进程服务器 from socket import * serSocket = socket(AF_INET, SOCK_STREAM) # 重复使用绑定的信息 serSocket.setsock ...
- P1332 血色先锋队
P1332 血色先锋队 题目描述 巫妖王的天灾军团终于卷土重来,血色十字军组织了一支先锋军前往诺森德大陆对抗天灾军团,以及一切沾有亡灵气息的生物.孤立于联盟和部落的血色先锋军很快就遭到了天灾军团的重重 ...
- localStorage简析
声明:引用自http://www.cnblogs.com/st-leslie/p/5617130.html 一.什么是localStorage.sessionStorage 在HTML5中,新加入了一 ...
- 第二十三篇 logging模块(******)
日志非常重要,而且非常常用,可以通过logging模块实现. 热身运动 import logging logging.debug("debug message") logging. ...
- 第四篇 Python循环
While 循环 For 循环
- 解决灰色shader与mask冲突的方案
Shader "Custom/Opaque" { Properties { [PerRendererData] _MainTex ("Sprite Texture&quo ...