题目描述

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 nnn Byteotian villages (numbered from 111 to nnn) were connected by bidirectional dirt roadsin such a way, that one could reach the village number 111 (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 nnn (1≤n≤250 0001\le n\le 250\ 0001≤n≤250 000),denoting the number of villages in Byteotia. The following n−1n-1n−1 lines contain descriptions of the roads, in the form of two integers aaa,bbb (1≤a<b≤n1\le a<b\le n1≤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 mmm(1≤m≤250 0001\le m\le 250\ 0001≤m≤250 000),denoting the number of trips Byteasar has made.

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

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

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

输出格式

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

样例

INPUT

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

OUTPUT

2

1

0

1

HINT

SOLUTION

dfs序+树状数组

其实本题也可以用dfs序+线段树或者树链剖分做。但是树状数组好写啊

根据题意描述首先可以知道本题的模型是一棵树,因为修改一条边只会对该边深度较深的点及它的子树产生影响,可以用dfs序对树上的节点重新编号之后直接用树状数组进行区间差分修改,询问时单点统计前缀和即可。(差分修改,单点统计前缀和是使用树状数组维护区间的一种简单方式)

挺好的一题,但是这么一讲莫名觉得简单。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
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-48;ch=getchar();}
return x*f;}
const int N=250100;
int n,m,id=0,dpt[N],s[N],siz[N],head[N],cnt=0,idd[N];
struct EDGE{int nxt,to;}e[2*N];
inline void add(int u,int v) {e[++cnt].nxt=head[u];e[cnt].to=v;head[u]=cnt;}
inline void add2(int x,int y) {for (;x<=n;x+=x&-x) s[x]+=y;}
inline int ask(int x) {int ans=0;for (;x;x-=x&-x) ans+=s[x];return ans;}
void dfs(int u,int fath){
idd[u]=++id;siz[u]=1;dpt[u]=dpt[fath]+1;
for (int i=head[u];i;i=e[i].nxt){
int v=e[i].to;
// printf("%d from:%d\n",v,u);
if (v==fath) continue;
dfs(v,u);siz[u]+=siz[v];}//siz[]是子树结点个数和,计算区间范围时可以用上。
}
int main(){
int i,j;
n=read();memset(head,0,sizeof(head));memset(siz,0,sizeof(siz));
for (i=1;i<n;++i) {int u=read(),v=read();add(u,v);add(v,u);}
dpt[0]=0;memset(siz,0,sizeof(siz));
dfs(1,0);memset(s,0,sizeof(s));
for (i=2;i<=n;++i) {add2(idd[i]+siz[i],-1);add2(idd[i],1);}
m=read();
for (i=1;i<=m+n-1;++i){
char ch=getchar();while (ch<'A'||ch>'Z') ch=getchar();
if (ch=='W'){
int x=read();//printf("%d\n",idd[x]);
int ans=ask(idd[x]);printf("%d\n",ans);}
else{
int x=read(),y=read();
int son=(idd[x]>idd[y])?x:y;
add2(idd[son]+siz[son],1);add2(idd[son],-1);
// puts("");
}
}
return 0;
}

LG_3459_[POI2007]MEG-Megalopolis的更多相关文章

  1. NOIp2018 复习笔记

    其实也没什么用啦,只是来占个坑 OI知识 3367 [模板]并查集 就这么做啊 没什么其他的 就是可以做tarjan LCA和Kruskal的操作 //关键函数 int getfa(int t) { ...

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

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

  3. BZOJ 1103: [POI2007]大都市meg

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

  4. [bzoj1103][POI2007]大都市meg(树状数组+dfs序)

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

  5. 【BZOJ】【1103】【POI2007】大都市meg

    dfs序 模板题,进点+1出点-1,刚好对于不在路径上的点一进一出刚好抵消,由于本题要动态修改(变成公路以后+1-1都变成0)所以在序列上套一个树状数组即可. TLE:1.递归dfs给爆了……写了个手 ...

  6. 数据结构(线段树):BZOJ 1103 [POI2007]大都市meg

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1791  Solved: 925[Submit][Stat ...

  7. BZOJ 1103: [POI2007]大都市meg( 树链剖分 )

    早上数学考挂了...欲哭无泪啊下午去写半个小时政治然后就又可以来刷题了.. 树链剖分 , 为什么跑得这么慢... ------------------------------------------- ...

  8. bzoj1103【POI2007】大都市meg

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 1544  Solved: 776 [Submit][St ...

  9. BZOJ1103 [POI2007]大都市meg 【树剖】

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 3038  Solved: 1593 [Submit][S ...

随机推荐

  1. Python笔记_第四篇_高阶编程_高阶函数_2.filter

    1. filter函数: 原型:filter(fn,lsd) 参数1为函数 参数2为序列 功能:用于过滤序列,把传入的函数一次作用域序列每个元素,根据返回的是True还是False决定是否保留该元素. ...

  2. Tensorflow函数——tf.set_random_seed(seed)

    设置图级随机seed. 依赖于随机seed的操作实际上从两个seed中获取:图级和操作级seed. 这将设置图级别的seed. 其与操作级seed的相互作用如下: 1.如果没有设置图形级别和操作see ...

  3. 计蒜客 引爆炸弹(DFS、并查集)

    在一个 n×m 的方格地图上,某些方格上放置着炸弹.手动引爆一个炸弹以后,炸弹会把炸弹所在的行和列上的所有炸弹引爆,被引爆的炸弹又能引爆其他炸弹,这样连锁下去. 现在为了引爆地图上的所有炸弹,需要手动 ...

  4. sublime text2设置快捷键打开浏览器

    1 编辑一个py文件,内容如下: import sublime, sublime_plugin import webbrowser url_map = { 'C:\\server\\www\\' : ...

  5. 寒假day04

    今天编写了毕设系统中的专家画像模块,实现了人员标签的建立与划分,同时刷了牛客网的面试相关题目. 1.如果系统的umask设置为244,创建一个新文件后,它的权限:(C) --w-r--r-- -r-x ...

  6. 一、早期(Early Stage)

    一.早期(Early Stage) 如果单纯从零基础开始,早期(Early Stage)应该是一到两个月(由于英语与中文差异比与其他语言大,中国同学至少两个月,但也不应过长.我们的经验是一般中国同学会 ...

  7. java使用io流读取windows文件乱码问题

    出现原因: 在IDEA中,使用 FileReader 读取项目中的文本文件.由于IDEA的设置,都是默认的 UTF-8 编码,所以没有任何 问题. 但是,当读取Windows系统中创建的文本文件时,由 ...

  8. NOIpDairy

    Day 0 水水比赛 Day 1 写写Dp Part1:Dp基础练习 [HNOI2002]公交车路线 秒切,点数这么少,N这么大,目测O(N)+暴力更新 5min写完 P3842 [TJOI2007] ...

  9. 四十、LAMP与LNMP加速与缓存优化进阶实战上部

    实例: 一. 所有服务器配置定时时间同步,必须通过web server上网. 有两种方式: 1.服务器A能进行上网,作为web server ,通过指定为ntp服务器,所有服务器访问这个服务器 2.服 ...

  10. day57-mysql-五种约束和sql语句逻辑执行顺序

    二.sql语句逻辑执行顺序 () SELECT () DISTINCT <select_list> 去重复 () FROM <left_table> () <join_t ...