题目链接


\(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(线段树)的更多相关文章

  1. CodeForces 1110F Nearest Leaf | 线段树/换根

    我--又诈尸了-- 代码几乎都不会写了,打场CF居然上分啦,开心!(虽然还是比不过列表里的各路神仙) 题目链接 题目描述 一棵\(n\)个点的有根树,规定一种dfs序(规则:编号小的点优先dfs),\ ...

  2. Codeforces 1110F(DFS序+线段树)

    题面 传送门 分析 next_id = 1 id = array of length n filled with -1 visited = array of length n filled with ...

  3. Buses and People CodeForces 160E 三维偏序+线段树

    Buses and People CodeForces 160E 三维偏序+线段树 题意 给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a ...

  4. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

  5. [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)

    [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...

  6. [Codeforces 1199D]Welfare State(线段树)

    [Codeforces 1199D]Welfare State(线段树) 题面 给出一个长度为n的序列,有q次操作,操作有2种 1.单点修改,把\(a_x\)修改成y 2.区间修改,把序列中值< ...

  7. [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)

    [Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...

  8. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  9. Codeforces 482B Interesting Array(线段树)

    题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,如今有M个限制,每一个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 ...

随机推荐

  1. bzoj 2427

    非常好的一道题,可以说是树形dp的一道基础题 首先不难发现,:如果我们把有关系的两个点用有向边相连,那么就会形成一个接近树的结构.如果这是一棵完美的树,我们就可以直接在树上打背包了 但是这并不是一棵完 ...

  2. 数据库MySql的安装

    1.MySQL概述 MySQL最初是由“MySQL AB公司”开发的一套关系型数据库管理系统(RDBMS-Relation DataBase Management System).MySQL不仅是最流 ...

  3. vue和stylus在subline中显示高亮

    首先: 安装这两个插件   Vue Syntax Highlight    和    stylus 1.按住 ctrl + shift + p 2.输入:install Package 3.输入: V ...

  4. 解决beego中同时开启http和https时,https端口占用问题

    在beego的app.go文件中, 找到 // run normal mode if BConfig.Listen.EnableHTTPS { go func() { time.Sleep( * ti ...

  5. windows_agent 添加

    一:复制windows agent文件和.exe文件到c:\zabbix\目录下 二:配置zabbix_agentd.win.conf文件 hostname:设置为自定义名称,但是要和zabbix-s ...

  6. iptables-snat-dnat-设置

    nat internet iptables -t nat -A POSTROUTING -s 192.168.0.0/255.255.255.0 -o eth1 -j SNAT --to-source ...

  7. JQuery调用WCF服务

    一:创建一个wcf服务项目 [ServiceContract] public interface IService1 { [OperationContract] [WebInvoke(RequestF ...

  8. WPF应用程序内存泄漏的一些原因

    原文:Finding Memory Leaks in WPF-based applications There are numbers of blogs that folks wrote about ...

  9. h5网页在微信里打开 右上角分享到微信好友或者朋友圈

    首先你需要一个分享接口地址,然后在自定义图片 标题 描述 如下: <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js& ...

  10. mysql四大特性与四种隔离级别

    四大特性 1:原子性.事务是一个不可分割的整体,事务开始的操作,要么全部执行,要么全部不执行. 2:隔离性.同一时间,只允许一个事务请求同一组数据.不同的事务彼此之间没有干扰. 3:一致性.事务开始前 ...