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 ...
随机推荐
- New Concept English three (54)
打字练习: 27w/m 45errors We have been brought up to fear insects. We regard them as unnecessary creature ...
- DNS分别在什么情况下使用UDP和TCP?
看到一个题目问的是:DNS分别在什么情况下使用UDP和TCP? 仔细查了一下发现可查询的资料很少,大部分都把DNS所用协议归为UDP了其实不然,DNS同时占用UDP和TCP端口53,这种单个应用协议同 ...
- Java基础--CountDownLatch
CountDownLatch是线程同步辅助类,它允许一个或多个线程wait直到countdown被调用使count为0. CountDownLatch是在java1.5被引入,存在于java.util ...
- java继承实例基础
总结:多态.重写.构造方法调用 package com.a; public class fsd { int a = 23; public fsd() { System.out.println(4444 ...
- Thread之一:线程生命周期及五种状态
<Thread之一:线程生命周期及五种状态> <juc线程池原理(四): 线程池状态介绍> 一.线程的生命周期及五种基本状态 关于Java中线程的生命周期,首先看一下下面这张较 ...
- vector向量容器元素排序与查找
1.利用标准库函数sort()对vector进行排序 参考源码: #include <algorithm> #include <vector> vector<int> ...
- Scala的Json序列化
import java.util.TimeZone import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMappe ...
- 【转】bootstrap模态框(modal)使用remote方法加载数据,只能加载一次的解决办法
http://blog.csdn.net/coolcaosj/article/details/38369787 bootstrap的modal中,有一个remote选项,可以动态加载页面到modal- ...
- Linux 正文处理命令及tar vi 编辑器
root 用户不受权限设置 如果文件有x 就可以执行 对目录的权限 r:可以查看目录下面的文件名 w:可以在该目录下新建,删除,修改文件名 x:可以cd进入目录 对于文件: 可执行的文件来说需要有可读 ...
- spring特点与好处
Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development a ...