我……又诈尸了……

代码几乎都不会写了,打场CF居然上分啦,开心!(虽然还是比不过列表里的各路神仙)

题目链接

题目描述

一棵\(n\)个点的有根树,规定一种dfs序(规则:编号小的点优先dfs),\(m\)次询问一个点\(u\)和一个区间\([l, r]\),求dfs序在这个区间内的叶子中,到\(u\)最小的距离。

\(n, m \le 500000\)

题解

这题……很简单……

题面一上来给个什么欧拉遍历定义……我吓得比赛中没看这题……(实际上码量对于代码几乎都不会敲的退役选手来说,不是非常友好 = = 当时做了可能也会写跪)

用线段树维护所有叶子到“当前点”(一开始是\(1\)号节点)的距离\(dis\)。

一开始以\(1\)号节点为“当前点”,dfs求距离,建树。这样\(u = 1\)的询问就可以解决了。

怎么解决其他\(u\)的询问呢?考虑移动“当前点”时,线段树会如何变化。

因为是DFS序,所以每棵子树在dfs序上都挨在一起。当“当前点”从父亲\(u\)移到儿子\(v\)\(w(u, v)\)时,子树\(v\)内所有点的\(dis\)都减去了\(w(u, v)\)(<u, v>这条边的长度),而子树\(v\)外的所有点\(dis\)都加上了\(w(u, v)\)。只需在线段树上区间修改即可。

显然,先把询问都读进来,离线处理非常优秀。空间允许的话似乎也可以主席树?(没试过 = =)

代码

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cassert>
#include <vector>
#define space putchar(' ')
#define enter putchar('\n')
using namespace std;
typedef long long ll;
template <class T>
void read(T &x){
char c;
bool op = 0;
while(c = getchar(), c < '0' || c > '9')
if(c == '-') op = 1;
x = c - '0';
while(c = getchar(), c >= '0' && c <= '9')
x = x * 10 + c - '0';
if(op) x = -x;
}
template <class T>
void write(T x){
if(x < 0) putchar('-'), x = -x;
if(x >= 10) write(x / 10);
putchar('0' + x % 10);
} const int N = 500005;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int n, m;
int ncnt, dfn[N], idx[N], ed[N];
int lcnt, llst[N], lnum[N], prel[N], nxtl[N];
ll w[N], dis[N], data[4*N], lazy[4*N], ans[N];
vector <int> son[N]; struct Query {
int id, u, l, r;
bool operator < (const Query &b) const {
return dfn[u] < dfn[b.u];
}
} qry[N];
int qpos = 1; void dfs1(int u, int pre){
dfn[u] = ++ncnt;
idx[ncnt] = u;
for(auto v : son[u]){
dis[v] = dis[u] + w[v];
dfs1(v, u);
}
if(u != 1 && ncnt == dfn[u]){
lnum[u] = ++lcnt;
llst[lcnt] = u;
}
ed[u] = ncnt;
} void modify(int k, ll x){
data[k] += x, lazy[k] += x;
}
void pushdown(int k){
if(!lazy[k]) return;
modify(k << 1, lazy[k]);
modify(k << 1 | 1, lazy[k]);
lazy[k] = 0;
}
void change(int k, int l, int r, int ql, int qr, ll x){
if(ql <= l && qr >= r) return void(modify(k, x));
pushdown(k);
int mid = (l + r) >> 1;
if(ql <= mid) change(k << 1, l, mid, ql, qr, x);
if(qr > mid) change(k << 1 | 1, mid + 1, r, ql, qr, x);
data[k] = min(data[k << 1], data[k << 1 | 1]);
}
ll query(int k, int l, int r, int ql, int qr){
if(ql <= l && qr >= r) return data[k];
pushdown(k);
int mid = (l + r) >> 1;
ll ret = INF;
if(ql <= mid) ret = query(k << 1, l, mid, ql, qr);
if(qr > mid) ret = min(ret, query(k << 1 | 1, mid + 1, r, ql, qr));
return ret;
}
void build_tree(int k, int l, int r){
if(l == r) return void(data[k] = dis[llst[l]]);
int mid = (l + r) >> 1;
build_tree(k << 1, l, mid);
build_tree(k << 1 | 1, mid + 1, r);
data[k] = min(data[k << 1], data[k << 1 | 1]);
} void dfs2(int u, int pre){
while(qry[qpos].u == u){
ans[qry[qpos].id] = query(1, 1, lcnt, qry[qpos].l, qry[qpos].r);
qpos++;
}
for(auto v : son[u]){
change(1, 1, lcnt, 1, lcnt, w[v]);
change(1, 1, lcnt, nxtl[dfn[v]], prel[ed[v]], -2 * w[v]);
dfs2(v, u);
change(1, 1, lcnt, 1, lcnt, -w[v]);
change(1, 1, lcnt, nxtl[dfn[v]], prel[ed[v]], 2 * w[v]);
}
} int main(){ read(n), read(m);
for(int i = 2, u; i <= n; i++){
read(u), read(w[i]);
son[u].push_back(i);
}
dfs1(1, 0);
for(int i = 1, t = 1; i <= n; i++){
if(lnum[idx[i]]) t = lnum[idx[i]];
prel[i] = t;
}
for(int i = n, t = lcnt; i; i--){
if(lnum[idx[i]]) t = lnum[idx[i]];
nxtl[i] = t;
}
for(int i = 1; i <= m; i++){
qry[i].id = i, read(qry[i].u), read(qry[i].l), read(qry[i].r);
qry[i].l = nxtl[qry[i].l], qry[i].r = prel[qry[i].r];
}
sort(qry + 1, qry + m + 1);
build_tree(1, 1, lcnt);
dfs2(1, 0);
for(int i = 1; i <= m; i++)
write(ans[i]), enter; return 0;
}

CodeForces 1110F Nearest Leaf | 线段树/换根的更多相关文章

  1. Codeforces.1110F.Nearest Leaf(线段树)

    题目链接 \(dls\)讲过这道题,所以这不是线段树裸题吗,这场没打气气气气气=-= 现在是写着玩=v= \(Description\) 给定一棵\(n\)个点的树.\(q\)次询问,每次询问给定\( ...

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

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

  3. codeforces#1187E. Tree Painting(树换根)

    题目链接: http://codeforces.com/contest/1187/problem/E 题意: 给出一颗树,找到一个根节点,使所有节点的子节点数之和最大 数据范围: $2 \le n \ ...

  4. CodeForces–833B--The Bakery(线段树&&DP)

    B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...

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

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

  6. CodeForces 877E DFS序+线段树

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

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

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

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

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

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

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

随机推荐

  1. thymeleaf给bootstrap自定义变量赋值

    最近在做一件事情,前端用的是thymeleaf进行渲染,thymeleaf是通过 th的标签来赋值对应的后端变量的.但是遇到一个尴尬的问题,bootstrap是可以通过data-xxx,来自定义变量的 ...

  2. [Oracle][OnlineREDO]数据库无法启动时的对应策略:

    [Oracle][OnlineREDO]数据库无法启动时的对应策略: 1. Start with mount. SQL> conn / as sysdba  SQL> startup mo ...

  3. HNOI2019 鱼 fish

    本来想写个改题记录的然后想了想改不完所以就分开写了= = https://www.luogu.org/problemnew/show/P5286 显然枚举A,D,然后鱼头和鱼身分开来考虑. 鱼身:先枚 ...

  4. Linux文件下载(转)

    wget是Linux最常用的下载命令, 一般的使用方法是: wget + 空格 + 要下载文件的url路径 例如: # wget http://www.linuxsense.org/xxxx/xxx. ...

  5. Rancher + k8s + docker 部署资料

    一.k8s 文档: https://jimmysong.io/kubernetes-handbook/concepts/deployment.html 命令行大全 https://kubernetes ...

  6. JDK+JAVA+maven+IDEA

    JDK+JAVA https://jingyan.baidu.com/article/6dad5075d1dc40a123e36ea3.html maven+IDEA http://blog.csdn ...

  7. 继承:call、apply、bind方法

    javascript 中,call 和 apply 都是为了改变某个函数运行时的上下文(context)而存在的,换句话说,就是为了改变函数体内部 this 的指向. call,apply,bind这 ...

  8. 《Linux内核设计与实现》第五章学习笔记

    <Linux内核设计与实现>第五章学习笔记 姓名:王玮怡  学号:20135116 一.与内核通信     在Linux中,系统调用是用户空间访问内核的唯一手段:除异常和陷入外,它们是内核 ...

  9. 《Linux内核设计与实现》第十八章学习笔记

    第十八章 调试 [学习时间:1小时 总结博客时间:1小时15分] [学习内容:出现bug的原因.内核调试器gdb.使用Git进行二分查找] 内核级开发的调试工作远比用户级开发艰难,它带来的风险比用户级 ...

  10. 生命游戏&一维细胞自动机 笔记

    de 生命游戏是一种简单的聚合模型,展示了事物是如何聚合的,是自动机(CA)模型的一种.由剑桥大学约翰康威发明,其规则为: 1. 每个细胞拥有八个邻居,细胞状态只有存活(黑)和死亡(白)两种: 2.处 ...