弄完之后点进去一看,竟然是div1的D题……最近真是天天被题虐哭

推荐这一篇博客 https://www.cnblogs.com/Sakits/p/8085598.html 感觉讲清楚了,也是基本照着这个写的

一开始题意没有读清楚,这题保证了所以树链都是从下往上的,所以才可以设计dp。

要把询问映射到树的dfs序上,快速计算子树中的最小值,想到可以使用线段树。

这个线段树还真是不好写。

Code:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll; const int N = 5e5 + ;
const ll inf = (ll) << ; int n, m, tot = , head[N], dfsc = , st[N], ed[N];
ll f[N];
vector <int> in[N], out[N]; struct Edge {
int to, nxt;
} e[N << ]; inline void add(int from, int to) {
e[++tot].to = to;
e[tot].nxt = head[from];
head[from] = tot;
} struct Querys {
int x, y, pos;
ll cost;
} q[N]; bool cmp(const Querys a, const Querys b) {
return a.pos < b.pos;
} template <typename T>
inline void read(T &X) {
X = ;
char ch = ;
T op = ;
for(; ch > ''|| ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} void dfs(int x, int fat) {
st[x] = ++dfsc;
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(y == fat) continue;
dfs(y, x);
}
ed[x] = dfsc;
} inline void chkMin(ll &x, ll y) {
if(y < x) x = y;
} inline ll min(ll x, ll y) {
return x > y ? y : x;
} namespace SegT {
ll s[N << ], tag[N << ]; #define lc p << 1
#define rc p << 1 | 1
#define mid ((l + r) >> 1) inline void up(int p) {
if(p) s[p] = min(s[lc], s[rc]);
} inline void done(int p, ll v) {
tag[p] = min(inf, tag[p] + v);
s[p] = min(inf, s[p] + v);
} inline void down(int p) {
if(tag[p] == 0LL) return;
done(lc, tag[p]), done(rc, tag[p]);
tag[p] = 0LL;
} void build(int p, int l, int r) {
s[p] = inf, tag[p] = 0LL;
if(l == r) return; build(lc, l, mid);
build(rc, mid + , r);
} void modifyP(int p, int l, int r, int x, ll v) {
if(x == l && r == x) {
s[p] = v;
return;
} down(p);
if(x <= mid) modifyP(lc, l, mid, x, v);
else modifyP(rc, mid + , r, x, v);
up(p);
} void modify(int p, int l, int r, int x, int y, ll v) {
if(x > y) return;
if(x <= l && y >= r) {
done(p, v);
return;
} down(p);
if(x <= mid) modify(lc, l, mid, x, y, v);
if(y > mid) modify(rc, mid + , r, x, y, v);
up(p);
} ll query(int p, int l, int r, int x, int y) {
if(x > y) return inf;
if(x <= l && y >= r) return s[p];
down(p);
ll res = inf;
if(x <= mid) res = min(res, query(lc, l, mid, x, y));
if(y > mid) res = min(res, query(rc, mid + , r, x, y));
return res;
} #undef mid
#undef lc
#undef rc } using namespace SegT; inline int bfind(int x) {
ll ln = , rn = m + , mid, res;
for(; ln <= rn; ) {
mid = (ln + rn) / ;
if(q[mid].pos >= x) res = mid, rn = mid - ;
else ln = mid + ;
}
return res;
} void solve(int x, int fat) {
ll sum = ;
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(y == fat) continue;
solve(y, x);
sum = min(inf, sum + f[y]);
} if(x == ) {
f[] = sum;
return;
} for(unsigned int i = ; i < in[x].size(); i++)
modifyP(, , m, in[x][i], min(inf, q[in[x][i]].cost + sum));
for(unsigned int i = ; i < out[x].size(); i++)
modifyP(, , m, out[x][i], inf);
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(y == fat) continue;
modify(, , m, bfind(st[y]), bfind(ed[y] + ) - , sum - f[y]);
} f[x] = query(, , m, bfind(st[x]), bfind(ed[x] + ) - );
} int main() {
read(n), read(m);
build(, , m);
for(int x, y, i = ; i < n; i++) {
read(x), read(y);
add(x, y), add(y, x);
}
dfs(, ); for(int i = ; i <= m; i++) {
read(q[i].x), read(q[i].y), read(q[i].cost);
q[i].pos = st[q[i].x];
} sort(q + , q + + m, cmp);
q[m + ].pos = n + ; for(int i = ; i <= m; i++)
in[q[i].x].push_back(i), out[q[i].y].push_back(i); solve(, ); /* for(int i = 1; i <= n; i++)
printf("%lld ", f[i]);
printf("\n"); */ if(f[] >= inf) puts("-1");
else printf("%lld\n", f[]); return ;
}

感觉还是超过了能力范围……

CF 671D Roads in Yusland的更多相关文章

  1. codesforces 671D Roads in Yusland

    Mayor of Yusland just won the lottery and decided to spent money on something good for town. For exa ...

  2. Codeforces 671D Roads in Yusland [树形DP,线段树合并]

    洛谷 Codeforces 这是一个非正解,被正解暴踩,但它还是过了. 思路 首先很容易想到DP. 设\(dp_{x,i}\)表示\(x\)子树全部被覆盖,而且向上恰好延伸到\(dep=i\)的位置, ...

  3. codeforces 671D Roads in Yusland & hdu 5293 Tree chain problem

    dp dp优化 dfs序 线段树 算是一个套路.可以处理在树上取链的问题.

  4. Codeforces 671D. Roads in Yusland(树形DP+线段树)

    调了半天居然还能是线段树写错了,药丸 这题大概是类似一个树形DP的东西.设$dp[i]$为修完i这棵子树的最小代价,假设当前点为$x$,但是转移的时候我们不知道子节点到底有没有一条越过$x$的路.如果 ...

  5. 【CF671D】Roads in Yusland(贪心,左偏树)

    [CF671D]Roads in Yusland(贪心,左偏树) 题面 洛谷 CF 题解 无解的情况随便怎么搞搞提前处理掉. 通过严密(大雾)地推导后,发现问题可以转化成这个问题: 给定一棵树,每条边 ...

  6. [Codeforces671D]Roads in Yusland

    [Codeforces671D]Roads in Yusland Tags:题解 题意 luogu 给定以1为根的一棵树,有\(m\)条直上直下的有代价的链,求选一些链把所有边覆盖的最小代价.若无解输 ...

  7. 【CF617D】Roads in Yusland

    [CF617D]Roads in Yusland 题面 蒯的洛谷的 题解 我们现在已经转化好了题目了,戳这里 那么我们考虑怎么求这个东西,我们先判断一下是否所有的边都能被覆盖,不行的话输出\(-1\) ...

  8. Codeforces 671 D. Roads in Yusland

    题目描述 Mayor of Yusland just won the lottery and decided to spent money on something good for town. Fo ...

  9. 【CodeForces】671 D. Roads in Yusland

    [题目]D. Roads in Yusland [题意]给定n个点的树,m条从下往上的链,每条链代价ci,求最少代价使得链覆盖所有边.n,m<=3*10^5,ci<=10^9,time=4 ...

随机推荐

  1. How to install php 7.x on CentOS 7

    Step 1: Setup the Webtatic YUM repo Precompiled PHP 7.x binaries are available for CentOS 7 from the ...

  2. Spring转账业务_XML配置事物控制

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  3. python模块导入的问题

    从模块导入函数时,通常可以使用 import somemodule 或者 from somemodule import somefunction 或者 from somemodule import s ...

  4. BZOJ3141:[HNOI2013]旅行

    浅谈队列:https://www.cnblogs.com/AKMer/p/10314965.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem.p ...

  5. 前用户sudo免密码

      Ubuntu 设置当前用户sudo免密码 方法1 # 备份 /etc/sudoers sudo cp /etc/sudoers . #打开 /etc/sudoers sudo visudo # 在 ...

  6. 在Azure上搭建L2TP服务器

    L2TP是常用的一种point-site的VPN.而目前在Azure上的VPN Gateway只支持IPsec和SSTP两种.如果客户需要L2TP服务器,需要自己在VM中搭建.本文将介绍如何在Azur ...

  7. JSON-lib框架,转换JSON、XML

    json-lib工具包 下载地址: http://sourceforge.net/projects/json-lib/json-lib还需要以下依赖包: jakarta commons-lang 2. ...

  8. Java-API:java.util.ArrayList

    ylbtech-Java-API:java.util.ArrayList 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 0. https://docs.orac ...

  9. AngularJS:SQL

    ylbtech-AngularJS:SQL 1.返回顶部 1. AngularJS SQL 在前面章节中的代码也可以用于读取数据库中的数据. 使用 PHP 从 MySQL 中获取数据 AngularJ ...

  10. 本地测试html文件时CSS效果显示, 但是当django的服务器上运行时效果不显示

    本地测试时各种效果都显示, 但是当在django服务器上测试时, 效果却不显示, 原因是我将css文件放在一个static文件夹里, 没有在settings中设置static_dir选项.将stati ...