P3459 [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 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 \cdots n\)的\(n\)个小村庄,某些村庄之间有一些双向的土路。从每个村庄都恰好有一条路径到达村庄\(1\)(即比特堡)。并且,对于每个村庄,它到比特堡的路径恰好只经过编号比它的编号小的村庄。另外,对于所有道路而言,它们都不在除村庄以外的其他地点相遇。在这个未开化的地方,从来没有过高架桥和地下铁道。随着时间的推移,越来越多的土路被改造成了公路。至今,\(Blue \ Mary\)还清晰地记得最后一条土路被改造为公路的情景。现在,这里已经没有土路了——所有的路都成为了公路,而昔日的村庄已经变成了一个大都市。\(Blue \ Mary\)想起了在改造期间她送信的经历。她从比特堡出发,需要去某个村庄,并且在两次送信经历的间隔期间,有某些土路被改造成了公路。现在\(Blue \ Mary\)需要你的帮助:计算出每次送信她需要走过的土路数目。(对于公路,她可以骑摩托车;而对于土路,她就只好推车了。)

输入输出格式

输入格式:

In the first line of the standard input there is a single integer \(n\) (\(1 \leq n \leq 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 \leq a<b \leq n\))separated by a single space, denoting the numbers of villages connected with a road. Inthe next line there is a single integer \(m\)(\(1 \leq m \leq 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 \(m\) integers to the standard output, one a line, denoting the numberof country roads Byteasar has travelled during his successive trips.

输入输出样例

输入样例:

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

输出样例:

2
1
0
1

思路

数据过水。 --logeadd

简单地说,给你一棵树,树上的边权都是\(1\),接下来有两种操作,一种把一条边的边权改为\(0\),另一种询问某个结点到根节点\(1\)的边权之和。这不就是一道树剖板子题吗?于是这样想的我在考场上被卡掉了一个点(不过听\(logeadd\)巨佬说洛谷上交树剖也能过)。

树剖的时间复杂度为\(O(n \log ^2n)\),显然会挂,能不能优化到\(O(n \log n)\)呢?对于每次修改操作,被修改边所指向的子树中的所有结点的答案都会减少一,我们不如在线段树中直接记录答案,每次只进行一次区间修改,也就是修改子树,询问时单点询问,这样时间复杂度就可以变为\(O(n \log n)\)了。

从另一个角度来想,我们处理出树的\(dfs\)序,然后要用一种支持区间修改和单点查询的时间复杂度都不大于\(O(\log n)\)的数据结构,同样,我们也可以大力一发树状数组过了这道题。这里提供的是线段树做法。

AC代码

#include<bits/stdc++.h>
using namespace std;
const int MAXN=250005;
int n,m,tot,sz[MAXN],dfn[MAXN],dep[MAXN],a[MAXN];
int cnt,top[MAXN],to[MAXN<<1],nex[MAXN<<1];
struct SegmentTree
{
int l,r,data,tag;
#define l(a) tree[a].l
#define r(a) tree[a].r
#define d(a) tree[a].data
#define t(a) tree[a].tag
}tree[MAXN<<2];
int read()
{
int re=0;char ch=getchar();
while(!isdigit(ch)) ch=getchar();
while(isdigit(ch)) re=(re<<3)+(re<<1)+ch-'0',ch=getchar();
return re;
}
char readc()
{
char ch=getchar();
while(!isalpha(ch)) ch=getchar();
return ch;
}
void dfs(int now)
{
dfn[now]=++tot,a[tot]=dep[now],sz[now]=1;
for(int i=top[now];i;i=nex[i])
{
if(dfn[to[i]]) continue;
dep[to[i]]=dep[now]+1;
dfs(to[i]);
sz[now]+=sz[to[i]];
}
}
void build(int p,int ll,int rr)
{
l(p)=ll,r(p)=rr;
if(ll==rr)
{
d(p)=a[ll];
return ;
}
int mid=(ll+rr)>>1;
build(p<<1,ll,mid);
build(p<<1|1,mid+1,rr);
d(p)=d(p<<1)+d(p<<1|1);
}
void pushdown(int p)
{
if(t(p))
{
d(p<<1)-=t(p)*(r(p<<1)-l(p<<1)+1),d(p<<1|1)-=t(p)*(r(p<<1|1)-l(p<<1|1)+1);
t(p<<1)+=t(p),t(p<<1|1)+=t(p);
t(p)=0;
}
}
void change(int p,int ll,int rr)
{
if(ll<=l(p)&&r(p)<=rr)
{
d(p)-=r(p)-l(p)+1;
t(p)++;
return ;
}
pushdown(p);
int mid=(l(p)+r(p))>>1;
if(ll<=mid) change(p<<1,ll,rr);
if(rr>mid) change(p<<1|1,ll,rr);
d(p)=d(p<<1)+d(p<<1|1);
}
int ask(int p,int des)
{
if(l(p)==r(p)) return d(p);
pushdown(p);
int mid=(l(p)+r(p))>>1;
if(des<=mid) return ask(p<<1,des);
else return ask(p<<1|1,des);
}
int main()
{
n=read();
for(int i=0;i<n-1;i++)
{
int x=read(),y=read();
to[++cnt]=y,nex[cnt]=top[x],top[x]=cnt;
to[++cnt]=x,nex[cnt]=top[y],top[y]=cnt;
}
dfs(1);
build(1,1,n);
m=read()+n-1;
while(m--)
{
char opt=readc();
if(opt=='A')
{
int x=read(),y=read();
if(dfn[x]<dfn[y]) swap(x,y);
change(1,dfn[x],dfn[x]+sz[x]-1);
}
else if(opt=='W') printf("%d\n",ask(1,dfn[read()]));
}
return 0;
}

Luogu P3459 [POI2007]MEG-Megalopolis(线段树)的更多相关文章

  1. luogu P2574 XOR的艺术 (线段树)

    luogu P2574 XOR的艺术 (线段树) 算是比较简单的线段树. 当区间修改时.\(1 xor 1 = 0,0 xor 1 = 1\)所以就是区间元素个数减去以前的\(1\)的个数就是现在\( ...

  2. 【原创】洛谷 LUOGU P3373 【模板】线段树2

    P3373 [模板]线段树 2 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.将某区间每一个数乘上x 3.求出某区间每一个数的和 输入输出格式 输入格式: 第 ...

  3. 【原创】洛谷 LUOGU P3372 【模板】线段树1

    P3372 [模板]线段树 1 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别 ...

  4. Luogu P1198 BZOJ 1012 最大数 (线段树)

    手动博客搬家: 本文发表于20170821 14:32:05, 原地址https://blog.csdn.net/suncongbo/article/details/77449455 URL: (Lu ...

  5. 【Luogu P3834】可持久化线段树(主席树)

    Luogu P3834 可持久化数据结构就是支持在历史版本上进行查询和修改操作的数据结构. 主席树就是对线段树的改进,使之可持久化. 前置知识:动态开点线段树 我们利用权值(值域)线段树统计区间内的数 ...

  6. 「Luogu P5494 【模板】线段树分裂」

    (因为没有认证,所以这道题就由Froggy上传) 线段树分裂用到的地方确实并不多,luogu上以前也没有这道模板题,所以就出了一道,实在是想不出怎么出模板了,所以这道题可能可以用一些其他的算法水过去. ...

  7. [Luogu P2824] [HEOI2016/TJOI2016]排序 (线段树+二分答案)

    题面 传送门:https://www.luogu.org/problemnew/show/P2824 Solution 这题极其巧妙. 首先,如果直接做m次排序,显然会T得起飞. 注意一点:我们只需要 ...

  8. luogu P3373 【模板】线段树 2

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.将某区间每一个数乘上x 3.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含三个整数N.M.P,分别 ...

  9. Luogu P4097 [HEOI2013]Segment 李超线段树

    题目链接 \(Click\) \(Here\) 李超线段树的模板.但是因为我实在太\(Naive\)了,想象不到实现方法. 看代码就能懂的东西,放在这里用于复习. #include <bits/ ...

随机推荐

  1. winform程序捕获全局异常,对错误信息写入日志并弹窗

    使用场景:在winform程序中如果没对方法进行try catch操作,若方法内出错,则整个程序报错并退出,如下图 如果程序已在客户手中,若没对错误的详细信息进行拍照,我们则不知道错误原因是什么.我们 ...

  2. Linq学习笔记(第一部分)

    本文分享自lliulun的30分钟linq教程,地址:http://www.cnblogs.com/liulun/archive/2013/02/26/2909985.html 一:与LINQ有关的语 ...

  3. Echart使用过的属性总结

    改变坐标轴颜色与粗细: axisLine: { lineStyle: {//设置轴的颜色 color: '#CD0000', width: 1,//轴的宽度 } } 改变坐标轴上刻度的间隔与倾斜方向: ...

  4. druapl-note1 本地开发上传模块不提示Ftp的警告

    刚安装完drupal之后,通过drupalxray 看到其它drupal网站安装的一些模块,下载好模块并安装时,提示需要输入Ftp信息. 但是本地开发不输入Ftp信息的(也不清楚自己的系统是否开启Ft ...

  5. LoadRunner函数的介绍

    LoadRunner函数的介绍 LoadRunner函数 一:通用函数 LoadRunner的通用函数以lr为前缀,可以在任何协议中使用.可以如下分类: 信息相关的函数: lr_error_messa ...

  6. 图论最短路径算法——Dijkstra

    说实在的,这算法很简单,很简单,很简单--因为它是贪心的,而且码量也小,常数比起SPFA也小. 主要思想 先初始化,dis[起点]=0,其它皆为无限大. 还要有一个bz数组,bz[i]表示i是否确定为 ...

  7. 【转载】TCP演进简述

    TCP演进简述 http://www.cnblogs.com/fll/ 一.互联网概述 TCP,即传输控制协议,是目前网络上使用的最多的传输协议,我们知道,整个互联网的体系结构是以IP协议提供的无连接 ...

  8. 使用应用程序(Java/Python)访问MaxCompute Lightning进行数据开发

    MaxCompute Lightning是MaxCompute产品的交互式查询服务,支持以PostgreSQL协议及语法连接访问Maxcompute项目,让您使用熟悉的工具以标准 SQL查询分析Max ...

  9. 连通图,set——cf1037E

    看了题解又调了很久,用set来维护当前满足条件的pair<degree[v],v> 离线操作,先建好一张图,然后建立好集合,每次删边后都把集合里不满足条件的点删去,同时更新集合 /* 离线 ...

  10. display和position以及其余标签的使用

    今天主要学习了两大标签display和position:中文名字是显示和位置,这两个元素在前端的学习还是很重要的,因为在css的布局里面会经常用到这两种元素. 还有一些其余的标签例:margin,pa ...