【贪心】 BZOJ 3252:攻略
3252: 攻略
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 261 Solved: 90
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
4 3 2 1 1
1 2
1 5
2 3
2 4
Sample Output
HINT
对于100%的数据,n<=200000,1<=场景价值<=2^31-1
很容易知道贪心策略:每次选价值最高的叶子节点
但是貌似很难搞的样子
朴素算法应该是n^2的样子。。
O(n)显然不太好搞。。
所以大约优化完后是O(nlgn)左右的复杂度。。
有两种logn的方法
1.黄学长的堆。。自行百度。。我只能说代码完全看不懂。。
2.DFS序+线段树
DFS处理出一个点管辖的所有点的DFS序。
然后线段树添加,每次删除。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector> #define maxn 200001 using namespace std; vector<int>graph[maxn]; int rev[maxn],in[maxn],father[maxn],dfn[maxn],last[maxn],tot=; long long a[maxn]; bool vis[maxn]; struct tr{
int l,r,ps;
long long num,tag;
}tree[maxn*]; long long read()
{
long long x=;char ch=getchar();
while(ch<''||ch>'')ch=getchar();
while(ch>=''&&ch<='')x=x*+ch-'',ch=getchar();
return x;
} void DFS(int poi)
{
dfn[poi]=++tot;
rev[tot]=poi;
for(int i=graph[poi].size()-;i>=;i--)
{
int u=graph[poi][i];
DFS(u);
}
last[poi]=tot;
} void psh(int poi)
{
if(tree[poi].tag&&tree[poi].l!=tree[poi].r)
{
tree[poi<<].tag+=tree[poi].tag;
tree[(poi<<)|].tag+=tree[poi].tag;
tree[poi<<].num+=tree[poi].tag;
tree[(poi<<)|].num+=tree[poi].tag;
tree[poi].tag=;
}
} void update(int num,int l,int r,long long d)
{
psh(num);
if(tree[num].l==l&&tree[num].r==r)
{
tree[num].num+=d;
tree[num].tag+=d;
return;
}
int mid=(tree[num].l+tree[num].r)>>;
if(mid>=r)update(num<<,l,r,d);
else if(l>mid)update((num<<)|,l,r,d);
else update(num<<,l,mid,d),update((num<<)|,mid+,r,d);
if(tree[num].l==tree[num].r)return;
if(tree[num<<].num>tree[(num<<)|].num){tree[num].num=tree[num<<].num,tree[num].ps=tree[num<<].ps;}
if(tree[(num<<)|].num>=tree[num<<].num){tree[num].num=tree[(num<<)|].num,tree[num].ps=tree[(num<<)|].ps;}
} void build(int num,int l,int r)
{
if(l==r)
{
tree[num].l=tree[num].r=l;
tree[num].ps=l;
return;
}
int mid=(l+r)>>;
build(num<<,l,mid);
build((num<<)|,mid+,r);
tree[num].l=l,tree[num].r=r;
tree[num].ps=tree[(num<<)|].ps;
} int main()
{
long long ans=;
int n,k;
n=read(),k=read();
for(int i=;i<=n;i++)a[i]=read();
for(int i=;i<n;i++)
{
int u,v;
u=read();v=read();
graph[u].push_back(v);
father[v]=u;
} father[]=; DFS();
build(,,n); for(int i=;i<=n;i++)
update(,dfn[i],last[i],a[i]); while(k--)
{
psh();
ans+=tree[].num;
int u=rev[tree[].ps];
while(u&&!vis[u])
{
vis[u]=;
update(,dfn[u],last[u],-a[u]);
u=father[u];
}
}
printf("%lld",ans);
return ;
}
(死于太久没打tag。。)
【贪心】 BZOJ 3252:攻略的更多相关文章
- BZOJ[3252]攻略(长链剖分)
BZOJ[3252]攻略 Description 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏.今天他得到了一款新游戏<XX半岛> ...
- bzoj 3252: 攻略 -- 长链剖分+贪心
3252: 攻略 Time Limit: 10 Sec Memory Limit: 128 MB Description 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神 ...
- bzoj 3252: 攻略
3252: 攻略 Description 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏. 今天他得到了一款新游戏<XX半岛>, ...
- bzoj 3252 攻略 长链剖分思想+贪心
攻略 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 889 Solved: 423[Submit][Status][Discuss] Descrip ...
- 【刷题】BZOJ 3252 攻略
Description 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏.今天他得到了一款新游戏<XX 半岛>,这款游戏有n个场景(s ...
- BZOJ.3252.攻略(贪心 长链剖分/线段树)
题目链接 贪心,每次选价值最大的一条到根的链.比较显然(不选白不选). 考虑如何维护这个过程.一个点的价值选了就没有了,而它只会影响它子树里的点,可以用DFS序+线段树修改.而求最大值也可以用线段树. ...
- BZOJ 3252: 攻略(思路题)
传送门 解题思路 比较好想的一道思路题,结果有个地方没开\(long\) \(long\) \(wa\)了三次..其实就是模仿一下树链剖分,重新定义重儿子,一个点的重儿子为所有儿子中到叶节点权值最大的 ...
- [HYSBZ - 3252] 攻略
问题描述 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏.今天他得到了一款新游戏<XX 半岛>,这款游戏有n个场景(scene),某 ...
- 【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心
3252: 攻略 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 339 Solved: 130[Submit][Status][Discuss] D ...
随机推荐
- Java之绘制艺术图案
前面已经介绍过绘制方法.这里不再赘述. package com.caiduping; import java.awt.Color; import java.awt.Graphics; import j ...
- android自学笔记(1):android简介
Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发.尚未有统一中文名称,中国大陆地区较 多人使用“安卓 ...
- CAF(C++ actor framework)使用随笔(使用类去构建actor和使用的一些思路)
Class-based actorsA class-based actor is a subtype of event_based_actor and must implement the pure ...
- CAF(C++ actor framework)使用随笔(同步发送 异步与同步等待)(三)
c). 同步发送, 等待响应, 超时后收到1个系统消息. 贴上代码 #include <iostream> #include "caf/all.hpp" #includ ...
- vs2005_the breakpoint will not currently be hit. The source code is different from the original verison.
公司开发只能用英文版的VS2005 (雅蠛蝶) Tool-->Options-->Debugging->General: 把Require source files to exact ...
- Android 自定义CheckBox 样式
新建Android XML文件,类型选Drawable,根结点选selector,在这定义具体的样式. <?xml version="1.0" encoding=" ...
- 在ThinkPHP3.x框架中实现将原创文章第一时间推送到百度收录
前两天自己写的一篇文章“针对BootStrap中tabs控件的美化和完善”被别的网站给转载了,这也许是值得高兴的一件事情,但是有些网站并没有注明来源和作者.而去百度搜索这篇文章,排名第一的居然是那些转 ...
- javascript之数据推送
我们使用ajax与后台服务进行交互,常常是通过触发事件来单次交互,但对于有些web应用来说,需要前台与后台保持长连接,前端不定时地接收后台推送的数据信息, 例如:股票行情分析.聊天室和网页在线游戏等. ...
- 判断浏览器js代码
(function(){ var UA = {}; var ua = navigator.userAgent.toLowerCase(),s; UA.ie = (s = ua.match(/(msie ...
- struts2中的常量
struts2中的常量: 在:struts2-core-2.1.8.1\org\apache\struts2\default.properties 文件里 <!-- 配制i18n国际化--> ...