调了半天居然还能是线段树写错了,药丸

  这题大概是类似一个树形DP的东西。设$dp[i]$为修完i这棵子树的最小代价,假设当前点为$x$,但是转移的时候我们不知道子节点到底有没有一条越过$x$的路。如果我们枚举每条路去转移,会发现这条路沿线上的其他子树的答案难以统计,那怎么办呢,我们可以让这条路向上回溯的时候顺便记录一下,于是有$val[i]$表示必修i这条路,并且修完当前子树的最小代价。

  则有转移$dp[x]=min(val[j])$,且$j$这条路必须覆盖$x$。

  $val[i]=(\sum dp[son])-dp[sonx]+val[i]$,且$i$这条路必须覆盖$sonx$。

  转移用线段树来维护就好,至于怎么判断某条路是否覆盖两个点,只要递归到某条路的起点的时候把$val[i]$改为$(\sum dp[son])+cost[i]$,递归到某条路终点的时候把$val[i]$改为$inf$就好了。

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=;
const ll inf=1e15;
struct poi{ll sum, delta;}tree[maxn<<];
struct tjm{int too, pre;}e[maxn<<], e2[maxn<<], e3[maxn<<];
struct qaq{int x, y, cost, pos;}q[maxn];
ll dp[maxn];
int n, m, x, y, tot, tot2, tot3, tott, l[maxn], r[maxn], last[maxn], last2[maxn], last3[maxn];
inline void read(int &k)
{
int f=; k=; char c=getchar();
while(c<'' || c>'') c=='-' && (f=-), c=getchar();
while(c<='' && c>='') k=k*+c-'', c=getchar();
k*=f;
}
inline void add(int x, int y){e[++tot]=(tjm){y, last[x]}; last[x]=tot;}
inline void add2(int x, int y){e2[++tot2]=(tjm){y, last2[x]}; last2[x]=tot2;}
inline void add3(int x, int y){e3[++tot3]=(tjm){y, last3[x]}; last3[x]=tot3;}
inline void up(int x) {tree[x].sum=min(tree[x<<].sum, tree[x<<|].sum);}
inline void addone(int x, int l, int r, ll delta)
{
tree[x].delta=min(inf, tree[x].delta+delta);
tree[x].sum=min(inf, tree[x].sum+delta);
}
inline void down(int x, int l, int r)
{
int mid=(l+r)>>;
addone(x<<, l, mid, tree[x].delta);
addone(x<<|, mid+, r, tree[x].delta);
tree[x].delta=;
}
void build(int x, int l, int r)
{
if(l==r) {tree[x].sum=inf; return;}
int mid=(l+r)>>;
build(x<<, l, mid); build(x<<|, mid+, r);
up(x);
}
void update(int x, int l, int r, int cx, ll delta)
{
if(l==r) {tree[x].sum=delta; return;}
down(x, l, r);
int mid=(l+r)>>;
if(cx<=mid) update(x<<, l, mid, cx, delta);
else update(x<<|, mid+, r, cx, delta);
up(x);
}
void change(int x, int l, int r, int cl, int cr, ll delta)
{
if(cl>cr) return;
if(cl<=l && r<=cr) {addone(x, l, r, delta); return;}
down(x, l, r);
int mid=(l+r)>>;
if(cl<=mid) change(x<<, l, mid, cl, cr, delta);
if(cr>mid) change(x<<|, mid+, r, cl, cr, delta);
up(x);
}
ll query(int x, int l, int r, int cl, int cr)
{
if(cl>cr) return inf;
if(cl<=l && r<=cr) return tree[x].sum;
down(x, l, r);
int mid=(l+r)>>; ll ans=inf;
if(cl<=mid) ans=query(x<<, l, mid, cl, cr);
if(cr>mid) ans=min(ans, query(x<<|, mid+, r, cl, cr));
return ans;
}
void dfs1(int x, int fa)
{
l[x]=++tott;
for(int i=last[x], too;i;i=e[i].pre)
if((too=e[i].too)!=fa) dfs1(too, x);
r[x]=tott;
}
inline int find(int x)
{
int l=, r=m+;
while(l<r)
{
int mid=(l+r)>>;
if(q[mid].pos>=x) r=mid;
else l=mid+;
}
return l;
}
void dfs2(int x, int fa)
{
ll sum=;
for(int i=last[x], too;i;i=e[i].pre)
if((too=e[i].too)!=fa) dfs2(too, x), sum=min(inf, sum+dp[too]);
if(x==) {dp[]=sum; return;}
for(int i=last2[x];i;i=e2[i].pre) update(, , m, e2[i].too, min(inf, q[e2[i].too].cost+sum));
for(int i=last3[x];i;i=e3[i].pre) update(, , m, e3[i].too, inf);
for(int i=last[x], too;i;i=e[i].pre)
if((too=e[i].too)!=fa) change(, , m, find(l[too]), find(r[too]+)-, sum-dp[too]);
dp[x]=query(, , m, find(l[x]), find(r[x]+)-);
}
inline bool cmp(qaq a, qaq b){return a.pos<b.pos;}
int main()
{
read(n); read(m); build(, , m);
for(int i=;i<n;i++) read(x), read(y), add(x, y), add(y, x);
dfs1(, );
for(int i=;i<=m;i++) read(q[i].x), read(q[i].y), read(q[i].cost), q[i].pos=l[q[i].x];
sort(q+, q++m, cmp); q[m+].pos=n+;
for(int i=;i<=m;i++) add2(q[i].x, i), add3(q[i].y, i);
dfs2(, );
if(dp[]>=inf) return puts("-1"), ;
printf("%lld\n", dp[]);
}

Codeforces 671D. Roads in Yusland(树形DP+线段树)的更多相关文章

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

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

  2. Codeforces Round #530 (Div. 2) F (树形dp+线段树)

    F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...

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

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

  4. POJ 3162 Walking Race 树形DP+线段树

    给出一棵树,编号为1~n,给出数m 漂亮mm连续n天锻炼身体,每天会以节点i为起点,走到离i最远距离的节点 走了n天之后,mm想到知道自己这n天的锻炼效果 于是mm把这n天每一天走的距离记录在一起,成 ...

  5. Codeforces 629D Babaei and Birthday Cakes DP+线段树

    题目:http://codeforces.com/contest/629/problem/D 题意:有n个蛋糕要叠起来,能叠起来的条件是蛋糕的下标比前面的大并且体积也比前面的大,问能叠成的最大体积 思 ...

  6. hdu5293 Tree chain problem 树形dp+线段树

    题目:pid=5293">http://acm.hdu.edu.cn/showproblem.php?pid=5293 在一棵树中,给出若干条链和链的权值.求选取不相交的链使得权值和最 ...

  7. poj3162(树形dp+线段树求最大最小值)

    题目链接:https://vjudge.net/problem/POJ-3162 题意:给一棵树,求每个结点的树上最远距离,记为a[i],然后求最大区间[l,r]满足区间内的max(a[i])-min ...

  8. 【洛谷5298】[PKUWC2018] Minimax(树形DP+线段树合并)

    点此看题面 大致题意: 有一棵树,给出每个叶节点的点权(互不相同),非叶节点\(x\)至多有两个子节点,且其点权有\(p_x\)的概率是子节点点权较大值,有\(1-p_x\)的概率是子节点点权较小值. ...

  9. Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)

    题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...

随机推荐

  1. Towards Accurate Multi-person Pose Estimation in the Wild 论文阅读

    论文概况 论文名:Towards Accurate Multi-person Pose Estimation in the Wild 作者(第一作者)及单位:George Papandreou, 谷歌 ...

  2. .NET工程师 技能清单

    第一次写博客,先说自己对自己的职业定位.NET全栈跨语言工程师 .首先说明自己是微软的狂热粉丝,几乎所有技术都在.NET下进行. 接下来对微软目前的.NET上的技术进行进一步了解,列出一个清单或者说是 ...

  3. Paper Reading - Deep Captioning with Multimodal Recurrent Neural Networks ( m-RNN ) ( ICLR 2015 ) ★

    Link of the Paper: https://arxiv.org/pdf/1412.6632.pdf Main Points: The authors propose a multimodal ...

  4. Hexo博客 云服务器搭建

    下载nodejs: https://nodejs.org/dist/v10.15.1/node-v10.15.1-linux-x64.tar.xz 解压:tar zxv       解压后编译:   ...

  5. redis利用key计时与计数

    计时 Setex 命令为指定的 key 设置值及其过期时间.如果 key 已经存在, SETEX 命令将会替换旧的值 基本命令: redis 127.0.0.1:6379> SETEX KEY_ ...

  6. Linux学习——echo和read命令用法

    转载自http://www.runoob.com/linux/linux-comm-read.html http://www.178linux.com/76331 echo命令 本文列举了echo命令 ...

  7. 20162316刘诚昊 第八周实验报告:实验二 Java面向对象程序设计

    实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验要求 1.没有Linux基础的同学建议先学习<L ...

  8. [codecademy]css

    Great work! You've learned the basics of CSS structure and syntax. We'll continue to build on these ...

  9. Java中的网络编程-2

    Socket编程:(一般的网络编程) <1> 两个 JAVA 应用程序可通过一个双向的网络通信连接, 实现数据交换, 这个双向链路的一段称为一个 Socket. <2> Soc ...

  10. APUE(unix环境高级编程)第三版---first day---部署书中实例的运行环境(apue.h)

    操作环境:RHEL7.0 部署apue.h实例运行环境 1.下载头文件src.3e.tar.gz 2.解压 tar zxvf src.3e.tar.gz 3.创建普通用户(我仿照书上创建的sar用户) ...