noip模拟赛 旅行

分析:一个贪心的想法是每次找到根的点权和最大的点进行操作,关键是怎么维护.每次找最大值,修改后会对这条链上每个点的子树上的点造成影响,可以用线段树来维护.找最大值就是区间求最大值嘛,对子树进行操作利用dfs序维护一下就好了.记录一下最大值的位置,每次从这个位置向上跳并对它的子树进行修改直到当前点的点权为0.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int maxn = ;
typedef long long ll; int n, k, a[maxn], fa[maxn],head[maxn], to[maxn], nextt[maxn],tot = , d[maxn], l[maxn], r[maxn], pos[maxn], dfs_clock;
int maxx[maxn << ], tag[maxn << ], p[maxn << ];
ll ans; void add(int x, int y)
{
to[tot] = y;
nextt[tot] = head[x];
head[x] = tot++;
} void dfs(int u, int from, int dist)
{
d[u] = dist;
l[u] = ++dfs_clock;
pos[dfs_clock] = u;
for (int i = head[u]; i; i = nextt[i])
{
int v = to[i];
if (v != from)
{
dfs(v, u, dist + a[v]);
fa[v] = u;
}
}
r[u] = dfs_clock;
} void pushup(int o)
{
if (maxx[o * ] > maxx[o * + ])
{
maxx[o] = maxx[o * ];
p[o] = p[o * ];
}
else
{
maxx[o] = maxx[o * + ];
p[o] = p[o * + ];
}
} void build(int o, int l, int r)
{
if (l == r)
{
maxx[o] = d[pos[l]];
p[o] = pos[l];
return;
}
int mid = (l + r) >> ;
build(o * , l, mid);
build(o * + , mid + , r);
pushup(o);
} void pushdown(int o)
{
if (tag[o] != )
{
tag[o * ] += tag[o];
tag[o * + ] += tag[o];
maxx[o * ] += tag[o];
maxx[o * + ] += tag[o];
tag[o] = ;
}
} void update(int o, int l, int r, int x, int y,int v)
{
if (x <= l && r <= y)
{
maxx[o] += v;
tag[o] += v;
return;
}
pushdown(o);
int mid = (l + r) >> ;
if (x <= mid)
update(o * , l, mid, x, y, v);
if (y > mid)
update(o * + , mid + , r, x, y, v);
pushup(o);
} void change(int x)
{
while (a[x])
{
update(, , n, l[x], r[x], -a[x]);
a[x] = ;
x = fa[x];
}
} int main()
{
scanf("%d%d", &n, &k);
for (int i = ; i <= n; i++)
scanf("%d", &a[i]);
for (int i = ; i < n; i++)
{
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
dfs(, , a[]);
build(, , n);
for (int i = ; i <= k; i++)
{
ans += maxx[];
change(p[]);
}
printf("%lld\n", ans); return ;
}
noip模拟赛 旅行的更多相关文章
- NOIP模拟赛20161022
NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...
- contesthunter暑假NOIP模拟赛第一场题解
contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...
- NOIP模拟赛 by hzwer
2015年10月04日NOIP模拟赛 by hzwer (这是小奇=> 小奇挖矿2(mining) [题目背景] 小奇飞船的钻头开启了无限耐久+精准采集模式!这次它要将原矿运到泛光之源的矿 ...
- 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程
数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...
- 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...
- 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...
- 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...
- CH Round #58 - OrzCC杯noip模拟赛day2
A:颜色问题 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2358%20-%20OrzCC杯noip模拟赛day2/颜色问题 题解:算一下每个仆人到它的目的地 ...
- CH Round #52 - Thinking Bear #1 (NOIP模拟赛)
A.拆地毯 题目:http://www.contesthunter.org/contest/CH%20Round%20%2352%20-%20Thinking%20Bear%20%231%20(NOI ...
随机推荐
- Android上的线程安全
Thread-safe methods In some situations, the methods you implement might be called from more than one ...
- [BZOJ1085][SCOI2005]骑士精神 搜索
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1085 大的思路是迭代加深搜索,我们加一个明显的剪枝,当棋盘中位置不对的骑士的数目加上已经走 ...
- QT入门学习2
QT获取窗口几何布局有2类函数: 1.包含框架:x().y().frameGemetry().pos().move()... 2.不包含框架:geometry().width().height().w ...
- 如需在APP中使用腾讯QQ登陆,需提前申请获取腾讯QQ的APPKEY和APPSecret。
如需在APP中使用腾讯QQ登陆,需提前申请获取腾讯QQ的APPKEY和APPSecret. 申请流程如下: 步骤1:登陆腾讯开放平台.链接地址: http://open.qq.com/ . 步骤2:填 ...
- 5 Transforms 转移 笔记
5 Transforms 转移 笔记 Transforms Unfortunately, no one can be told what the Matrix is. You have to ...
- R Programming week 3-Loop functions
Looping on the Command Line Writing for, while loops is useful when programming but not particularly ...
- DBUtils使用技巧
BbUtils(一) 结果集概览:http://www.cnblogs.com/myit/p/4269165.html DbUtils(二) 结果集实例:http://www.cnblogs.com/ ...
- Android(java)学习笔记195:ContentProvider使用之添加数据到联系人(掌握)
1.添加联系人逻辑思路 (1)首先在raw_contacts创建一个新的id (2)在data表里面添加这个id对应的数据 2.下面通过一个案例,说明一下如何添加一条数据到联系人: (1)首先我们关注 ...
- sql 语句的优化
sql语句的优化:在大多数情况下,为了更快的遍历表结构,优化器主要是根据定义的索引来提高性能.但是在不合理的SQL语句中,优化器会删去索引进而使用全表扫描, 一般而言,这种sql被称为劣质sql,所以 ...
- fedora下yum安装gnome和kde桌面 (有问题 )
转自: http://linux.chinaunix.net/techdoc/system/2009/08/31/1133198.shtml 1.1 安装KDE桌面环境 yum groupins ...