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 ...
随机推荐
- Shell编程-运算符
1.declare命令 declare声明变量类型:declare [+/-][选项] 变量名 -:给变量设定类型属性 +:取消变量的类型属性 -a:将变量声明为数组型 -i:整数型 -x:环境变量 ...
- hdoj-1046-Gridland(规律题)
题目链接 #include <algorithm> #include <iostream> using namespace std; int main() { int t; c ...
- MySQL多种安装方式选择
1.rpm包安装方式 rpm包的安装方式非常简单,这里以el6平台下的mysql-5.6.34版本为例,首先,要通过上述搜狐镜像地址下载到如下四个MySQL相关软件安装包. a.下载安装包 MySQL ...
- XE7 - ListView自测笔记
这两天主要是摸索着使用了ListView和SQLite.郁闷过,也有收获. 一.SQLite 首先记录下SQLite自己碰到的几个小问题: 1. SQLite中字符串连接符是‘||’, 换行符为 x' ...
- 已知一个数组a[N]来构造数组b[N]的有趣算法题
给定一个数组a[N],我们希望构造数组b[N],其中b[i]=a[0]*a[1]*...*a[N-1]/a[i].在构造过程要求满足:1.不使用除法:2.O(1)空间复杂度和O(n)时间复杂度:3.除 ...
- loj #6216. 雪花挂饰
(今天碰到的题怎么这么小清新 $n$ 个不相同的点,$q$ 组询问,每次给定 $l,r$,问在 $n$ 个点中,选出 $x$ 个点 $(x \in [l,r])$,用边连起来,能构成多少种不同的树 $ ...
- Broken Keyboard(模拟数组或者双重链表的运用)
这题我是大写的服气,辛辛苦苦搞了个双重链表结果还一直不对,不对就算了,书上源代码打进去还是不对,我能怎么办我也很无奈.不过这题还是让我对双重链表更加了解和运用了!还是可以的! You’re typin ...
- 洛谷【P2669】NOIP2015普及组 T1金币
我对模拟的理解:http://www.cnblogs.com/AKMer/p/9064018.html 题目传送门:https://www.luogu.org/problemnew/show/P266 ...
- 第七篇 PHP编码规范
当码农多年,始终进步不大,前面说了第一个原因是没有明确的目标:第二个原因是没有养成良好的习惯(即优秀的职业规范). 1)pear 规范 http://pear.php.net/manual/en/st ...
- Java基础--读写文件
Java读写文件需要注意异常的处理,下面是一个例子 写文件 public class WriteFile { public static void write(String file, String ...