弄完之后点进去一看,竟然是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. 20165210 Java第三周学习总结

    20165210 Java第三周学习总结 教材学习内容总结 - 第四章学习总结 编程语言的几个发展阶段: 面向机器语言 面向过程语言 面向对象语言 类: 类声明: class People { ... ...

  2. 作业派NABCD的特点分析

    Need:根据我们用户的调查,我们发现用户希望在作业派获取一些课本上的答案等类似的东西,以方便及时解决课本的问题. Approach:但是仅仅靠管理员来上传文件时园不能解决用户的问题.所以我们想让我们 ...

  3. Codeforces 786C. Till I Collapse 主席树

    题目大意: 给定一个长度为\(n\)的序列,要求将其划分为最少的若干段使得每段中不同的数字的种数不超过\(k\). 对于 \(k = 1 .. n\)输出所有的答案. \(n \leq 10^5\) ...

  4. 洛谷 4245 【模板】任意模数NTT——三模数NTT / 拆系数FFT

    题目:https://www.luogu.org/problemnew/show/P4245 三模数NTT: 大概是用3个模数分别做一遍,用中国剩余定理合并. 前两个合并起来变成一个 long lon ...

  5. 异常:java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlType

    这个是jdK版本的问题的. 本地编译的jar包是1.8的,但是跑jar包的环境jdk版本是1.9的. 升级1.9之后由于jdk当方面的取消了几个jar,所以导致编译起不来. 明天研究一下如何添加jar ...

  6. MySQL的瑞士军刀(转)

    这里主要讲mysql运维中的一些主要工具,这些工具可能大家都用过,特别是系统管理员或者做linux服务器维护的同学可能都知道这些小工具,这 里讲得会比较多一些,除了系统监控的小工具,还包括一些mysq ...

  7. 通过Azure File Service搭建基于iscsi的共享盘

    在Azure上目前已经有基于Samba协议的共享存储了. 但目前在Azure上,还不能把Disk作为共享盘.而在实际的应用部署中,共享盘是做集群的重要组件之一.比如仲裁盘.Shared Disk等. ...

  8. 第二届PHP全球开发者大会(含大会的PPT)

    PHP全球开发者大会于2016年5月14日至15日在北京召开 更多现场图片请猛击: http://t.cn/RqeP7y9 ,  http://t.cn/RqD8Typ 最后,这次大会的PPT可以在这 ...

  9. setcookie函数的注意事项

    函数说明 bool setcookie ( string $name [, string $value = "" [, int $expire = 0 [, string $pat ...

  10. HDU1695(容斥原理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...