左偏树(BZOJ4003)
左偏树打个标记,没了。
#include <cstdio>
#include <vector>
using namespace std; typedef long long ll;
const int N = ;
int n,m,e,x,tt,c[N],d[N],a[N],hd[N],nxt[N],to[N],rt[N],f[N],g[N];
ll h[N],v[N],s[N];
vector<int> vc[N];
struct nd {int l,r,d,id; ll w,ad,mu;}t[N];
void add(int x, int y) {to[++e] = y, nxt[e] = hd[x], hd[x] = e;} void pd(int x) {
t[t[x].l].mu *= t[x].mu, t[t[x].l].ad *= t[x].mu, t[t[x].l].w *= t[x].mu;
t[t[x].r].mu *= t[x].mu, t[t[x].r].ad *= t[x].mu, t[t[x].r].w *= t[x].mu;
t[t[x].l].ad += t[x].ad, t[t[x].r].ad += t[x].ad, t[t[x].l].w += t[x].ad, t[t[x].r].w += t[x].ad;
t[x].mu = , t[x].ad = ;
}
int mrg(int x, int y) {
if(!x || !y) return x+y;
if(t[x].w > t[y].w) swap(x, y);
pd(x);
t[x].r = mrg(t[x].r, y);
if(t[t[x].l].d < t[t[x].r].d) swap(t[x].l, t[x].r);
t[x].d = t[t[x].r].d+;
return x;
} void dfs(int x) {
for(int i = ; i < vc[x].size(); i++)
t[++tt].id = vc[x][i], t[tt].w = s[vc[x][i]], t[tt].mu = , rt[x] = mrg(rt[x], tt);
for(int i = hd[x]; i; i = nxt[i]) d[to[i]] = d[x]+, dfs(to[i]), rt[x] = mrg(rt[x], rt[to[i]]);
while(rt[x] && t[rt[x]].w < h[x])
f[x]++, g[t[rt[x]].id] = x, pd(rt[x]), rt[x] = mrg(t[rt[x]].l, t[rt[x]].r);
if(a[x]) t[rt[x]].mu *= v[x], t[rt[x]].ad *= v[x], t[rt[x]].w *= v[x];
else t[rt[x]].ad += v[x], t[rt[x]].w += v[x];
} int main() {
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++) scanf("%lld", &h[i]);
for(int i = ; i <= n; i++) scanf("%d%d%lld", &x, &a[i], &v[i]), add(x, i);
for(int i = ; i <= m; i++) scanf("%lld%d", &s[i], &c[i]), vc[c[i]].push_back(i);
d[] = , dfs();
for(int i = ; i <= n; i++) printf("%d\n", f[i]);
for(int i = ; i <= m; i++) printf("%d\n", d[c[i]]-d[g[i]]);
return ;
}
左偏树(BZOJ4003)的更多相关文章
- 左偏树初步 bzoj2809 & bzoj4003
看着百度文库学习了一个. 总的来说,左偏树这个可并堆满足 堆的性质 和 左偏 性质. bzoj2809: [Apio2012]dispatching 把每个忍者先放到节点上,然后从下往上合并,假设到了 ...
- 【BZOJ4003】【JLOI2015】城池攻占(左偏树)
题面 题目描述 小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池.这 n 个城池用 1 到 n 的整数表示.除 1 号城池外,城池 i 会受到另一座城池 fi 的管辖,其中 fi ...
- BZOJ 1455 罗马游戏 ——左偏树
[题目分析] 左偏树的模板题目,大概就是尽量维护树的深度保持平衡,以及尽可能的快速合并的一种堆. 感觉和启发式合并基本相同. 其实并没有快很多. 本人的左偏树代码自带大常数,借鉴请慎重 [代码] #i ...
- 【BZOJ-1455】罗马游戏 可并堆 (左偏树)
1455: 罗马游戏 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1355 Solved: 561[Submit][Status][Discuss] ...
- 【bzoj2809】[Apio2012]dispatching 左偏树
2016-05-31 15:56:57 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2809 直观的思想是当领导力确定时,尽量选择薪水少的- ...
- zoj 2334 Monkey King/左偏树+并查集
原题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1389 大致题意:N只相互不认识的猴子(每只猴子有一个战斗力值) 两只 ...
- POJ3016-K-Monotonic(左偏树+DP)
我觉得我要改一下签名了……怎么会有窝这么啰嗦的人呢? 做这题需要先学习左偏树<左偏树的特点及其应用> 然后做一下POJ3666,这题的简单版. 思路: 考虑一下维护中位数的过程原数组为A, ...
- POJ3666-Making the Grade(左偏树 or DP)
左偏树 炒鸡棒的论文<左偏树的特点及其应用> 虽然题目要求比论文多了一个条件,但是……只需要求非递减就可以AC……数据好弱…… 虽然还没想明白为什么,但是应该觉得应该是这样——求非递减用大 ...
- bzoj 1455: 罗马游戏 左偏树+并查集
1455: 罗马游戏 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 668 Solved: 247[Submit][Status] Descriptio ...
随机推荐
- css中的position
一.position语法与结构 position语法: position : static absolute relative position参数:static : 无特殊定位,对象遵循HTML定位 ...
- hexo博客图片问题
hexo博客图片问题 第一步 首先确认_config.yml 中有 post_asset_folder:true. Hexo 提供了一种更方便管理 Asset 的设定:post_asset_folde ...
- 1290 - The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
解决问题:windows下:修改my.ini 在[mysqld]内加入secure_file_priv = linux下:修改my.cnf 在[mysqld]内加入secure_file_priv = ...
- Django REST framework+Vue 打造生鲜超市(二)
三.Models设计 3.1.项目初始化 (1)进虚拟环境下安装 django2.0.2 djangorestframework和相关依赖mark,filter pillow 图片处理 pip in ...
- Python内置函数(46)——format
英文文档: format(value[, format_spec]) Convert a value to a "formatted" representation, as con ...
- 九、Python+Selenium模拟用QQ登陆腾讯课堂,并提取报名课程(练习)
研究QQ登录规则的话,得分析大量Javascript的加密解密,比较耗时间.自己也是练习很少,短时间成功不了.所以走了个捷径. Selenium是一个WEB自动化测试工具,它运行时会直接实例化出一个浏 ...
- Asp.NET Core2.0 项目实战入门视频课程_完整版
END OR START? 看到这个标题,你开不开心,激不激动呢? 没错,.net core的入门课程已经完毕了.52ABP.School项目从11月19日,第一章视频的试录制,到今天完整版出炉,离不 ...
- Mysql官方文档翻译系列-7.3.1 Establishing a Backup Policy
原文链接 (https://dev.mysql.com/doc/refman/5.7/en/backup-policy.html) 正文 To be useful, backups must be s ...
- 已操作文件的方式,新建一个用户alex
- 实现Winform端窗体关闭后刷新html网页内容
一.首先要知道刷新网页的路径: frmPointEasyToBeat fpetBeat = new frmPointEasyToBeat(bookNoteId, userInfo.UserId); f ...