CF 671D Roads in Yusland
弄完之后点进去一看,竟然是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的更多相关文章
- codesforces 671D Roads in Yusland
Mayor of Yusland just won the lottery and decided to spent money on something good for town. For exa ...
- Codeforces 671D Roads in Yusland [树形DP,线段树合并]
洛谷 Codeforces 这是一个非正解,被正解暴踩,但它还是过了. 思路 首先很容易想到DP. 设\(dp_{x,i}\)表示\(x\)子树全部被覆盖,而且向上恰好延伸到\(dep=i\)的位置, ...
- codeforces 671D Roads in Yusland & hdu 5293 Tree chain problem
dp dp优化 dfs序 线段树 算是一个套路.可以处理在树上取链的问题.
- Codeforces 671D. Roads in Yusland(树形DP+线段树)
调了半天居然还能是线段树写错了,药丸 这题大概是类似一个树形DP的东西.设$dp[i]$为修完i这棵子树的最小代价,假设当前点为$x$,但是转移的时候我们不知道子节点到底有没有一条越过$x$的路.如果 ...
- 【CF671D】Roads in Yusland(贪心,左偏树)
[CF671D]Roads in Yusland(贪心,左偏树) 题面 洛谷 CF 题解 无解的情况随便怎么搞搞提前处理掉. 通过严密(大雾)地推导后,发现问题可以转化成这个问题: 给定一棵树,每条边 ...
- [Codeforces671D]Roads in Yusland
[Codeforces671D]Roads in Yusland Tags:题解 题意 luogu 给定以1为根的一棵树,有\(m\)条直上直下的有代价的链,求选一些链把所有边覆盖的最小代价.若无解输 ...
- 【CF617D】Roads in Yusland
[CF617D]Roads in Yusland 题面 蒯的洛谷的 题解 我们现在已经转化好了题目了,戳这里 那么我们考虑怎么求这个东西,我们先判断一下是否所有的边都能被覆盖,不行的话输出\(-1\) ...
- Codeforces 671 D. Roads in Yusland
题目描述 Mayor of Yusland just won the lottery and decided to spent money on something good for town. Fo ...
- 【CodeForces】671 D. Roads in Yusland
[题目]D. Roads in Yusland [题意]给定n个点的树,m条从下往上的链,每条链代价ci,求最少代价使得链覆盖所有边.n,m<=3*10^5,ci<=10^9,time=4 ...
随机推荐
- poj1325
给出一系列任务,每个任务可以在机器A的某个模式,或者在机器B的某个模式下完成.机器A和B每切换一次模式需要重启一次.问完成这些任务,最少需要重启机器多少次? 把任务看作边 “重启”操作看作点 这道题就 ...
- redis实战之事务与持久化
1. 事务描述 (1)什么是事务 事务,就是把一堆事情绑在一起,按顺序的执行,都成功了才算完成,否则恢复之前的样子 事务必须服从ACID原则,ACID原则分别是原子性(atomicity).一致性(c ...
- maven module开发 自动打包
http://blog.csdn.net/u011113713/article/details/52413903 http://blog.csdn.net/sisyphus_z/article/det ...
- Centos7 安装 MongoDB4.0
目录 安装包下载 MongoDB安装 启动数据库 补充 小结 诚邀访问我的个人博客:我在马路边 更好的阅读体验点击查看原文:Centos7安装MongoDB4.0 原创博客,转载请注明出处 @ 由于项 ...
- volatile关键字及内存可见性
先看一段代码: package com.java.juc; public class TestVolatile { public static void main(String[] args) { T ...
- Linux sed 批量替换多个文件中的字符串【转载】
原文网址:http://blog.sina.com.cn/s/blog_730edb930100qzz5.html 比如,要将目录/modules下面所有文件中的zhangsan都修改成lis ...
- 自己写的工具:把Evernote(印象笔记)的笔记导入到博客(Blog)中
Evernote是个强大的工具, 这个伴随了我快4年的工具让我积累好多笔记.但是,如何把evernote(印象笔记)中的笔记发布到博客中呢? 自己空闲时候用python 3写了个工具Evernote2 ...
- maven学习7 settings.xml解析
maven的配置文件settings.xml存在于两个地方: 1.安装的地方:${M2_HOME}/conf/settings.xml 2.用户的目录:${user.home}/.m2/setting ...
- java中的死锁现象
死锁是这样一种情形:多个线程同时被阻塞,它们中的一个或者全部都在等待某个资源被释放.由于线程被无限期地阻塞,因此程序不可能正常终止. java 死锁产生的四个必要条件: 1.互斥使用,即当资源被一个线 ...
- 第四章 Java并发编程基础
线程简介 什么是线程? 现代操作系统在一个运行程序时,会为其创建一个进程.例如,启动一个Java程序,操作系统就会创建一个Java进程.现代操作系统调度的最小单元是线程,也叫轻量进程(Light We ...