Codeforces.1110F.Nearest Leaf(线段树)
\(dls\)讲过这道题,所以这不是线段树裸题吗,这场没打气气气气气=-=
现在是写着玩=v=
\(Description\)
给定一棵\(n\)个点的树。\(q\)次询问,每次询问给定\(v,l,r\),求离\(v\)最近且DFS序在\([l,r]\)之间的叶节点是哪个。
\(n,q\leq5\times10^5\)。
\(Solution\)
把询问离线。
在树上走,每次从\(fa[x]\)走到\(x\)时,设边权为\(len\)。此时距离\(x\)子树外的点的距离会增加\(len\),距离\(x\)子树内的点的距离会减少\(len\),同时这都是一段连续区间。(也可以对子树内减少\(2\times len\),对所有点加上\(len\))
所以建一棵线段树就做完了= =。
复杂度\(O((n+q)\log n)\)。
就算\(INF\)把\(n\)条边的边权全加上也爆不了long long啊,我有点傻逼→_→(当然转成一次区间减也不会有这个问题)
//733ms 142800KB
#include <cstdio>
#include <cctype>
#include <vector>
#include <algorithm>
#define gc() getchar()
typedef long long LL;
const int N=5e5+5;
const LL INF=1ll<<60;
int n,Enum,H[N],nxt[N],len[N],fa[N],sz[N];
LL dep[N],Ans[N];
struct Node{
int l,r,id;
};
std::vector<Node> vec[N];
struct Segment_Tree
{
#define ls rt<<1
#define rs rt<<1|1
#define lson l,m,ls
#define rson m+1,r,rs
#define S N<<2
LL tag[S],mn[S];
#undef S
#define Upd(rt,v) tag[rt]+=v, mn[rt]+=v
#define Update(rt) mn[rt]=std::min(mn[ls],mn[rs])
inline void PushDown(int rt)
{
Upd(ls,tag[rt]), Upd(rs,tag[rt]), tag[rt]=0;
}
void Build(int l,int r,int rt)
{
if(l==r) {mn[rt]=H[l]?INF:dep[l]; return;}
int m=l+r>>1;
Build(lson), Build(rson), Update(rt);
}
void Modify(int l,int r,int rt,int L,int R,int v)
{
if(L<=l && r<=R) {Upd(rt,v); return;}
if(tag[rt]) PushDown(rt);
int m=l+r>>1;
if(L<=m) Modify(lson,L,R,v);
if(m<R) Modify(rson,L,R,v);
Update(rt);
}
LL Query(int l,int r,int rt,int L,int R)
{
if(L<=l && r<=R) return mn[rt];
if(tag[rt]) PushDown(rt);
int m=l+r>>1;
if(L<=m)
if(m<R) return std::min(Query(lson,L,R),Query(rson,L,R));
else return Query(lson,L,R);
return Query(rson,L,R);
}
}T;
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-48,c=gc());
return now;
}
inline void AE(int u,int v)
{
nxt[v]=H[u], H[u]=v, sz[u]+=sz[v];
}
void DFS(int x)
{
static int now=0;
static LL offset=0;
int pos=++now;
if(x!=1) offset+=len[x], T.Modify(1,n,1,pos,pos+sz[x]-1,-2*len[x]);
for(int i=0,l=vec[x].size(); i<l; ++i)
Ans[vec[x][i].id]=T.Query(1,n,1,vec[x][i].l,vec[x][i].r)+offset;
for(int v=H[x]; v; v=nxt[v]) DFS(v);
if(x!=1) offset-=len[x], T.Modify(1,n,1,pos,pos+sz[x]-1,2*len[x]);
}
int main()
{
int n=read(),q=read(); ::n=n;
for(int i=2; i<=n; ++i) fa[i]=read(), dep[i]=dep[fa[i]]+(len[i]=read());
for(int i=n; i>1; --i) ++sz[i], AE(fa[i],i);//increase ording
++sz[1], T.Build(1,n,1);
for(int i=1,p; i<=q; ++i) p=read(), vec[p].push_back((Node){read(),read(),i});
DFS(1);
for(int i=1; i<=q; ++i) printf("%I64d\n",Ans[i]);
return 0;
}
Codeforces.1110F.Nearest Leaf(线段树)的更多相关文章
- CodeForces 1110F Nearest Leaf | 线段树/换根
我--又诈尸了-- 代码几乎都不会写了,打场CF居然上分啦,开心!(虽然还是比不过列表里的各路神仙) 题目链接 题目描述 一棵\(n\)个点的有根树,规定一种dfs序(规则:编号小的点优先dfs),\ ...
- Codeforces 1110F(DFS序+线段树)
题面 传送门 分析 next_id = 1 id = array of length n filled with -1 visited = array of length n filled with ...
- Buses and People CodeForces 160E 三维偏序+线段树
Buses and People CodeForces 160E 三维偏序+线段树 题意 给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a ...
- CodeForces 877E DFS序+线段树
CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...
- [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)
[Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...
- [Codeforces 1199D]Welfare State(线段树)
[Codeforces 1199D]Welfare State(线段树) 题面 给出一个长度为n的序列,有q次操作,操作有2种 1.单点修改,把\(a_x\)修改成y 2.区间修改,把序列中值< ...
- [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)
[Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- Codeforces 482B Interesting Array(线段树)
题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,如今有M个限制,每一个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 ...
随机推荐
- PyCharm设置字体风格
设置字体风格:File—Setting—Editor-Color Scheme-Color Scheme Font-把Scheme设置为:WarmNeon
- jenkins 回滚发布
#jenkins拉取文件路径 workspace=/data/wos/testtemp #备份路径 backspace=/data/wos/back #不能提Git的文件 config=/data/w ...
- C++ Primer 笔记——迭代器
iostream迭代器 1.虽然iostream类不是容器,但是标准库定义了可以用于IO的迭代器.创建一个流迭代器的时候必须指定要读写的类型.我们可以对任何具有输入运算符(>>)的类型定义 ...
- 微信小程序 如何获取用户code
1.首先需要获取code 使用 wx.login({ success: function(res) { console.log(res);//这里的返回值里面便包含code }, fail: func ...
- 用 DocumentFormat.OpenXml 和Microsoft.Office.Interop.Word 写入或者读取word文件
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
- 史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix)
在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用.为了保证其高可用,单个服务 ...
- Python自定义排序
比如自定义了一个class,并且实例化了这个类的很多个实例,并且组成一个数组.这个数组要排序,是通过这个class的某个字段来排序的.怎么排序呢? 有两种做法: 第一种是定义__cmp__( )方法: ...
- 查看python脚本的运行pid,让python脚本后台运行
ps -ef | grep Productor.py | grep -v grep # 先测试好 python3 /usr/local/software/ELK/Productor.py # 没问题 ...
- 在Git.oschina.net中配置TortoiseGit使用sshkey,无需输入账号和密码
ssh的方式 git@oschina.com:用户名/版本库t.git 此篇文章针对于这种 黄海正在开发的项目位置 https://gitee.com/dslx/BigData.g ...
- 关于 java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.String
今天遇到了这个异常,其实是自己经验欠缺所致.我是通过mybatis查询到数据库传过来的主键,是一个32位的char类型. 代码: //查询总账表的该组织总账记录,包括该条记录的主键id.账户余额Dzz ...