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. HYNB Round 8: 2016 ICPC Amritapuri Regionals

    HYNB Round 8: 2016 ICPC Amritapuri Regionals A - Tim and BSTs 做法 经典的树 DP 问题. \(dp[u][i]\) 表示考虑以 u 为根 ...

  2. minifilter 算是总结吧

    FltRegisterFilter 注册过滤器 FltStartFiltering 开始过滤 InstatanceSetupCallback 实例安装回调 .当一个微过滤器加载的时候,每个存在的卷都会 ...

  3. Carthage使用

    # carthage 包管理 ## 安装过程 1) 安装homebrew ``` ruby$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githu ...

  4. [笔记]180612 for DevOps

    adb devices 识别不了安卓手机:我下的adb interface驱动下载链接:如果设备管理器中ADB Interface是黄色的,就需要先安装adb interface驱动(BD:adb i ...

  5. 手把手教你接口自动化测试 – SoapUI & Groovy【转】

    手把手教你接口自动化测试 – SoapUI & Groovy Posted on 2015-01-21 09:38 WadeXu 阅读(12741) 评论(10) 编辑 收藏 手把手教你接口自 ...

  6. 【JZOJ3397】【luoguP4556】雨天的尾巴

    description 深绘里一直很讨厌雨天. 灼热的天气穿透了前半个夏天,后来一场大雨和随之而来的洪水,浇灭了一切. 虽然深绘里家乡的小村落对洪水有着顽固的抵抗力,但也倒了几座老房子,几棵老树被连 ...

  7. COMMENT方法 用于在生成的SQL语句中添加注释内容,

    COMMENT方法 用于在生成的SQL语句中添加注释内容,例如: $this->comment('查询考试前十名分数') ->field('username,score') ->li ...

  8. linux 每天一个命令

    Nginx [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)  使用命令关闭占用80端口的程序 sudo fuser ...

  9. kubeadm安装Kubernetes 1.15 实践

    原地址参考github 一.环境准备(在全部设备上进行) 3 台 centos7.5 服务器,网络使用 Calico. IP地址 节点角色 CPU 内存 Hostname 10.0.1.45 mast ...

  10. jeecms vue-cli项目结构详解

    Vue-cli是vue官方出品的快速构建单页应用的脚手架,如果你是初次尝试Vue,不建议使用,推荐你使用普通引入javascript文件的方式进行学习,如果你已经有vue基础那么就可以用vue-cli ...