[POI2007]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 Byteotian villages (numbered from to ) were connected by bidirectional dirt roadsin such a way, that one could reach the village number (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 (),denoting the number of villages in Byteotia. The following lines contain descriptions of the roads, in the form of two integers, ()separated by a single space, denoting the numbers of villages connected with a road. Inthe next line there is a single integer (),denoting the number of trips Byteasar has made.

The following lines contain descriptions of the events, in chronological order:

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

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

输出格式:

Your programme should write out exactly
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,最后一个节点编号-1,维护前缀和即可查询路径
注意输出答案时-1,因为节点1还记了一个1
#include <bits/stdc++.h>
inline void read(int &x){x = 0;char ch = getchar();char c = ch;while(ch < '0' || ch > '9')c = ch, ch = getchar();while(ch >= '0' && ch <= '9')x = x * 10 + ch - '0', ch = getchar();}
inline void read(long long &x){x = 0;char ch = getchar();char c = ch;while(ch < '0' || ch > '9')c = ch, ch = getchar();while(ch >= '0' && ch <= '9')x = x * 10 + ch - '0', ch = getchar();}
inline void swap(int &a, int &b){int tmp = a;a = b;b = tmp;}
inline void swap(long long &a, long long &b){long long tmp = a;a = b;b = tmp;}
inline int max(int &a, int &b){return a > b ? a : b;}
inline long long max(long long &a, long long &b){return a > b ? a : b;}
inline int min(int &a, int &b){return a < b ? a : b;}
inline long long min(long long &a, long long &b){return a < b ? a : b;}
inline int lowbit(int a){return a & (-a);}
inline long long lowbit(long long a){return a & (-a);} const int INF = 0x3f3f3f3f; const int MAXN = 500000 + 10;
const int MAXM = 500000 + 10; struct Edge{int u,v,next;}edge[MAXN << 1];
int head[MAXN],cnt,n,m,tmp1,tmp2;
void insert(int a, int b){edge[++cnt] = Edge{a,b,head[a]};head[a] = cnt;} int stack[MAXN], b[MAXN], top;
int bit[MAXN];
int l[MAXN << 1], r[MAXN << 1], rank;
//l[i] means the begining of i
//r[i] means the end of i inline void modify(int p, int k)
{
for(;p <= (n << 1);p += lowbit(p))
bit[p] += k;
} inline int ask(int p)
{
int ans = 0;
for(;p;p -= lowbit(p))
ans += bit[p];
return ans;
} void dfs()
{
stack[++top] = 1;
while(top)
{
int u = stack[top --];
b[u] = true;
if(l[u])
{
r[u] = ++rank;
continue;
}
stack[++top] = u;
l[u] = ++rank;
for(int pos = head[u];pos;pos = edge[pos].next)
{
int v = edge[pos].v;
if(b[v])continue;
stack[++top] = v;
b[v] = true;
}
}
} int main()
{
read(n);
for(int i = 1;i < n;++i)
{
read(tmp1);read(tmp2);
insert(tmp1, tmp2);
insert(tmp2, tmp1);
} dfs();
for(int i = 1;i <= n;++i)
{
modify(l[i], 1);
modify(r[i], -1);
}
read(m);
m = n + m - 1;
for(int i = 1;i <= m;i ++)
{
char c = getchar();
while(c != 'A' && c != 'W')c = getchar();
if(c == 'A')
{
read(tmp1);read(tmp2);
modify(l[tmp2], -1);
modify(r[tmp2], 1);
}
else
{
read(tmp1);
printf("%d\n", ask(l[tmp1]) - 1);
}
}
return 0;
}

洛谷P3459 [POI2007]MEG-Megalopolis [2017年6月计划 树上问题02]的更多相关文章

  1. 洛谷P1390 公约数的和 [2017年6月计划 数论12]

    P1390 公约数的和 题目描述 有一天,TIBBAR和LXL比赛谁先算出1~N这N个数中每任意两个不同的数的最大公约数的和.LXL还在敲一个复杂而冗长的程序,争取能在100s内出解.而TIBBAR则 ...

  2. 洛谷P1573 栈的操作 [2017年6月计划 数论11]

    P1573 栈的操作 题目描述 现在有四个栈,其中前三个为空,第四个栈从栈顶到栈底分别为1,2,3,…,n.每一个栈只支持一种操作:弹出并 压入.它指的是把其中一个栈A的栈顶元素x弹出,并马上压入任意 ...

  3. 洛谷P2429 制杖题 [2017年6月计划 数论10]

    P2429 制杖题 题目描述 求不大于 m 的. 质因数集与给定质数集有交集的自然数之和. 输入输出格式 输入格式: 第一行二个整数 n,m. 第二行 n 个整数,表示质数集内的元素 p[i]. 输出 ...

  4. 洛谷P1147 连续自然数和 [2017年6月计划 数论01]

    P1147 连续自然数和 题目描述 对一个给定的自然数M,求出所有的连续的自然数段,这些连续的自然数段中的全部数之和为M. 例子:1998+1999+2000+2001+2002 = 10000,所以 ...

  5. 洛谷P1164 小A点菜 [2017年4月计划 动态规划08]

    P1164 小A点菜 题目背景 uim神犇拿到了uoi的ra(镭牌)后,立刻拉着基友小A到了一家……餐馆,很低端的那种. uim指着墙上的价目表(太低级了没有菜单),说:“随便点”. 题目描述 不过u ...

  6. 洛谷P1244 [NOI2000] 青蛙过河 [2017年4月计划 动态规划07]

    P1244 青蛙过河 题目描述 有一条河,左边一个石墩(A区)上有编号为1,2,3,4,…,n的n只青蛙,河中有k个荷叶(C区),还有h个石墩(D区),右边有一个石墩(B区),如下图所示.n只青蛙要过 ...

  7. 洛谷P1877 [HAOI2012]音量调节 [2017年4月计划 动态规划05]

    P1877 [HAOI2012]音量调节 题目描述 一个吉他手准备参加一场演出.他不喜欢在演出时始终使用同一个音量,所以他决定每一首歌之前他都需要改变一次音量.在演出开始之前,他已经做好一个列表,里面 ...

  8. 洛谷P2347 砝码称重 [2017年4月计划 动态规划01]

    P2347 砝码称重 题目描述 设有1g.2g.3g.5g.10g.20g的砝码各若干枚(其总重<=1000), 输入输出格式 输入格式: 输入方式:a1 a2 a3 a4 a5 a6 (表示1 ...

  9. 洛谷P3459 [POI2007]MEG-Megalopolis [树链剖分]

    题目传送门 MEG 题目描述 Byteotia has been eventually touched by globalisation, and so has Byteasar the Postma ...

随机推荐

  1. Docker系列(十一):Kubernetes集群集群部署实践

    Kubernetes分布式集群架构 服务注册和服务发现问题怎么解决的? 分布式通讯的核心就是ip加端口 每个服务分配一个不变的虚拟IP+端口 系统env环境变量里有每个服务的服务名称到IP的映射 如下 ...

  2. java.io.FileNotFoundException: E:\work\work (拒绝访问。)

    转载自:https://blog.csdn.net/YQS_Love/article/details/51959776 一.问题 在使用FileInputStream或FileOutputStream ...

  3. java基础之二维数组-杨辉三角

    首先呢你要知道什么是杨辉三角? 答:杨辉三角,是二项式系数在三角形中的一种几何排列. 简单的说一下就是两个未知数和的幂次方运算后的系数问题,比如(x+y)的平方=x的平方+2xy+y的平方,这样系数就 ...

  4. 使用Colaboratory的免费GPU训练神经网络

    1 Colaboratory 介绍 Colaboratory 是一个 Google 研究项目,旨在帮助传播机器学习培训和研究成果.它是一个 Jupyter 笔记本环境,不需要进行任何设置就可以使用,并 ...

  5. Java 多线程同步和异步详解

    java线程 同步与异步 线程池 1)多线程并发时,多个线程同时请求同一个资源,必然导致此资源的数据不安全,A线程修改了B线 程的处理的数据,而B线程又修改了A线程处理的数理.显然这是由于全局资源造成 ...

  6. [Ceoi2011]Traffic

    #2387. [Ceoi2011]Traffic Online Judge:Bzoj-2387,Luogu-4700 Label:Yy,Tarjan缩点,dfs 题目描述 格丁尼亚的中心位于Kacza ...

  7. [转]C#设计模式(8)-Builder Pattern

    一. 建造者(Builder)模式 建造者模式可以将一个产品的内部表象与产品的生成过程分割开来,从而可以使一个建造过程生成具有不同的内部表象的产品对象. 对象性质的建造 有些情况下,一个对象会有一些重 ...

  8. Python之MySQL语法(增删改查)

    -- ID: 新闻的唯一标示 -- title: 新闻的标题 -- content: 新闻的内容 -- created_at: 新闻添加的时间 -- types: 新闻的类型 -- image: 新的 ...

  9. Django项目:CRM(客户关系管理系统)--70--60PerfectCRM实现CRM学生上课记录

    #urls.py """PerfectCRM URL Configuration The `urlpatterns` list routes URLs to views. ...

  10. MICROSOFT SQL SERVER 2012 序列号

    MICROSOFT SQL SERVER DEVELOPER 版(开发版) 序列号:YQWTX-G8T4R-QW4XX-BVH62-GP68Y MICROSOFT SQL SERVER ENTERPR ...