【JLOI2015】城池攻占
左偏树加lazy操作即可
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <iostream>
# include <algorithm>
# define ll long long
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
IL ll Read(){
RG ll x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + c - '0';
return x * z;
}
const int MAXN(300010);
struct Left{
int id, dis;
ll lazy_c, lazy_j;
Left *ch[2];
IL Left(RG int pos){
id = pos;
lazy_c = dis = 1;
lazy_j = 0;
}
} *rt[MAXN];
struct Edge{
int to, nt;
} edge[MAXN];
int die[MAXN], up[MAXN], ft[MAXN], a[MAXN], n, m, cnt, deep[MAXN] = {0, 1}, c[MAXN];
ll h[MAXN], s[MAXN], v[MAXN];
IL void Add(RG int u, RG int to){
edge[cnt] = (Edge){to, ft[u]}; ft[u] = cnt++;
}
IL int Dis(RG Left *x){
return x == NULL ? 0 : x -> dis;
}
IL void Updata(RG Left *x){
if(Dis(x -> ch[0]) < Dis(x -> ch[1])) swap(x -> ch[0], x -> ch[1]);
x -> dis = Dis(x -> ch[1]) + 1;
}
IL void Cov(RG Left *x, RG ll add, RG ll mul){
if(x == NULL) return;
x -> lazy_c *= mul; (x -> lazy_j *= mul) += add;
(s[x -> id] *= mul) += add;
}
IL void Pushdown(RG Left *x){
if(x -> lazy_c == 1 && !x -> lazy_j) return;
Cov(x -> ch[0], x -> lazy_j, x -> lazy_c);
Cov(x -> ch[1], x -> lazy_j, x -> lazy_c);
x -> lazy_j = 0; x -> lazy_c = 1;
}
IL Left *Merge(RG Left *x, RG Left *y){
if(x == NULL) return y;
if(y == NULL) return x;
Pushdown(x); Pushdown(y);
if(s[x -> id] > s[y -> id]) swap(x, y);
x -> ch[1] = Merge(x -> ch[1], y);
Updata(x);
return x;
}
IL void Pop(RG Left *&x){
RG Left *tmp = x;
Pushdown(x);
x = Merge(x -> ch[0], x -> ch[1]);
delete tmp;
}
IL void Dfs(RG int u){
for(RG int e = ft[u]; e != -1; e = edge[e].nt){
RG int to = edge[e].to;
deep[to] = deep[u] + 1;
Dfs(to);
rt[u] = Merge(rt[u], rt[to]);
}
while(rt[u] != NULL && s[rt[u] -> id] < h[u]){
up[rt[u] -> id] = deep[c[rt[u] -> id]] - deep[u];
die[u]++; Pop(rt[u]);
}
if(a[u]) Cov(rt[u], 0, v[u]);
else Cov(rt[u], v[u], 1);
}
int main(){
Fill(ft, -1);
n = Read(); m = Read();
for(RG int i = 1; i <= n; i++)
h[i] = Read();
for(RG int i = 2, f; i <= n; i++){
f = Read(); a[i] = Read(); v[i] = Read();
if(f) Add(f, i);
}
for(RG int i = 1; i <= m; i++){
s[i] = Read(); c[i] = Read();
RG Left *tmp = new Left(i);
rt[c[i]] = Merge(rt[c[i]], tmp);
}
Dfs(1);
while(rt[1] != NULL) up[rt[1] -> id] = deep[c[rt[1] -> id]], Pop(rt[1]);
for(RG int i = 1; i <= n; i++)
printf("%d\n", die[i]);
for(RG int i = 1; i <= m; i++)
printf("%d\n", up[i]);
return 0;
}
【JLOI2015】城池攻占的更多相关文章
- BZOJ_4003_[JLOI2015]城池攻占_可并堆
BZOJ_4003_[JLOI2015]城池攻占_可并堆 Description 小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池. 这 n 个城池用 1 到 n 的整数表示.除 ...
- 【BZOJ4003】[JLOI2015]城池攻占 可并堆
[BZOJ4003][JLOI2015]城池攻占 Description 小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池. 这 n 个城池用 1 到 n 的整数表示.除 1 号 ...
- [bzoj4003][JLOI2015]城池攻占_左偏树
城池攻占 bzoj-4003 JLOI-2015 题目大意:一颗n个节点的有根数,m个有初始战斗力的骑士都站在节点上.每一个节点有一个standard,如果这个骑士的战斗力超过了这个门槛,他就会根据城 ...
- [洛谷P3261] [JLOI2015]城池攻占(左偏树)
不得不说,这道题目是真的难,真不愧它的“省选/NOI-”的紫色大火题!!! 花了我晚自习前半节课看题解,写代码,又花了我半节晚自习调代码,真的心态爆炸.基本上改得和题解完全一样了我才过了这道题!真的烦 ...
- [JLOI2015]城池攻占
题目描述 小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池.这 n 个城池用 1 到 n 的整数表示.除 1 号城池外,城池 i 会受到另一座城池 fi 的管辖,其中 fi &l ...
- [JLOI2015]城池攻占 左偏树
题目描述 小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池.这 n 个城池用 1 到 n 的整数表示.除 1 号城池外,城池 i 会受到另一座城池 fi 的管辖,其中 fi &l ...
- BZOJ4003[JLOI2015]城池攻占——可并堆
题目描述 小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池. 这 n 个城池用 1 到 n 的整数表示.除 1 号城池外,城池 i 会受到另一座城池 fi 的管辖, 其中 fi ...
- BZOJ4003 [JLOI2015]城池攻占 左偏树 可并堆
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4003 题意概括 题意有点复杂,直接放原题了. 小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑 ...
- 【左偏树】【P3261】 [JLOI2015]城池攻占
Description 小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池.这 n 个城池用 1 到 n 的整数表示.除 1 号城池外,城池 i 会受到另一座城池 fi 的管辖,其 ...
- BZOJ4003:[JLOI2015]城池攻占——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4003 https://www.luogu.org/problemnew/show/P3261 小铭 ...
随机推荐
- iOS 使用NTP时间同步服务
githup上有相关开源库, ios-ntp 导入即可使用 NetworkClock *netClock = [NetworkClock sharedNetworkClock]; netClock.n ...
- visual studio code右侧的预览面板能关闭吗?
https://segmentfault.com/q/1010000010082399 "editor.minimap.enabled":false
- win7(windows 7)系统下安装SQL2005(SQL Server 2005)图文教程
操作系统:Microsoft Windows 7 旗舰版(32位) 数据库版本:SQL Server 2005 简体中文开发板 数据库下载链接: https://pan.baidu.com/s/1cq ...
- 2个域名重定向到https域名
配置实例: [root@iZbp17q09o7e8pgg9dybd7Z conf.d]# cat company.confserver { listen 80; server_name www.yu* ...
- 在linux内核中修改TCP MSS值
MTU: Maxitum Transmission Unit 最大传输单元 MSS: Maxitum Segment Size 最大分段大小 MSS最大传输大小的缩写,是TCP协议里面的一个概念.MS ...
- 微信小程序内嵌网页 网站生成小程序
1. 进入小程序后台 - 设置 - 开发设置 2. 添加业务域名(小程序只支持https) 3. 小程序代码 <web-view src="网址"></web-v ...
- 请小心使用 ng-repeat 中的 $index
自己自学的时候,遇到$index不知道它是如何使用的,所以上网搜了一下,发现了这个关于使用$index可能会出现的一个小BUG,和大家分享一下 PS:我是小白,欢迎指正,非常感谢! 以下是全文: &q ...
- css实现多行多列的布局
1.两列多行: HTML: <div class="box1"> box1:实现两列多行布局 <ul> <li>111</li> & ...
- 获取目录-Winform
// 获取程序的基目录. System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径. System.Diagnostics.Process.G ...
- bzoj 2669 [cqoi2012]局部极小值 DP+容斥
2669: [cqoi2012]局部极小值 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 838 Solved: 444[Submit][Status ...