BZOJ4003 [JLOI2015]城池攻占 左偏树 可并堆
欢迎访问~原文出处——博客园-zhouzhendong
去博客园看该题解
题目传送门 - BZOJ4003
题意概括
题意有点复杂,直接放原题了。
小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池。
题解
从树根跑dfs。
对于每一个子树,合并它的所有子树所代表的堆。
死掉的就弹出就可以了。
然后修改只需要打两个懒标记就可以了。
代码
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <vector>
using namespace std;
typedef long long LL;
const int N=300005;
int n,m;
vector <int> son[N];
int c[N],ls[N],rs[N],npl[N],root[N],ans1[N],ans2[N],depth[N];
LL h[N],op[N],v[N],val[N],add[N],times[N];
void pushson(int x,LL Add,LL Times){
val[x]=val[x]*Times+Add;
add[x]=add[x]*Times+Add;
times[x]*=Times;
}
void pushdown(int x){
if (ls[x])
pushson(ls[x],add[x],times[x]);
if (rs[x])
pushson(rs[x],add[x],times[x]);
add[x]=0,times[x]=1;
}
int merge(int a,int b){
if (!a||!b)
return a+b;
if (val[a]>val[b])
swap(a,b);
pushdown(a);
rs[a]=merge(rs[a],b);
if (npl[rs[a]]>npl[ls[a]])
swap(rs[a],ls[a]);
npl[a]=npl[rs[a]]+1;
return a;
}
void pop(int &x){
pushdown(x);
x=merge(ls[x],rs[x]);
}
void dfs(int rt){
for (int i=0;i<son[rt].size();i++){
int s=son[rt][i];
depth[s]=depth[rt]+1;
dfs(s);
root[rt]=merge(root[rt],root[s]);
}
while (root[rt]&&val[root[rt]]<h[rt]){
ans2[root[rt]]=rt;
ans1[rt]++;
pop(root[rt]);
}
if (op[rt]==0)
pushson(root[rt],v[rt],1);
else
pushson(root[rt],0,v[rt]);
}
int main(){
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
scanf("%lld",&h[i]);
for (int i=1;i<=n;i++)
son[i].clear();
for (int i=2,fa;i<=n;i++){
scanf("%d%lld%lld",&fa,&op[i],&v[i]);
son[fa].push_back(i);
}
memset(root,0,sizeof root);
for (int i=1;i<=m;i++){
scanf("%lld%d",&val[i],&c[i]);
ls[i]=rs[i]=npl[i]=add[i]=0,times[i]=1;
root[c[i]]=merge(root[c[i]],i);
}
memset(ans1,0,sizeof ans1);
memset(ans2,0,sizeof ans2);
depth[1]=1;
dfs(1);
for (int i=1;i<=n;i++)
printf("%d\n",ans1[i]);
for (int i=1;i<=m;i++)
printf("%d\n",depth[c[i]]-depth[ans2[i]]);
return 0;
}
BZOJ4003 [JLOI2015]城池攻占 左偏树 可并堆的更多相关文章
- BZOJ 4003: [JLOI2015]城池攻占 左偏树 可并堆
https://www.lydsy.com/JudgeOnline/problem.php?id=4003 感觉就是……普通的堆啊(暴论),因为这个堆是通过递归往右堆里加一个新堆或者新节点的,所以要始 ...
- [BZOJ4003][JLOI2015]城池攻占(左偏树)
这题有多种做法,一种是倍增预处理出每个点往上走2^i步最少需要的初始战斗力,一种是裸的启发式合并带标记splay. 每个点合并能攻占其儿子的所有骑士,删去所有无法攻占这个城市的骑士并记录答案. 注意到 ...
- [洛谷P3261] [JLOI2015]城池攻占(左偏树)
不得不说,这道题目是真的难,真不愧它的“省选/NOI-”的紫色大火题!!! 花了我晚自习前半节课看题解,写代码,又花了我半节晚自习调代码,真的心态爆炸.基本上改得和题解完全一样了我才过了这道题!真的烦 ...
- [JLOI2015]城池攻占 左偏树
题目描述 小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池.这 n 个城池用 1 到 n 的整数表示.除 1 号城池外,城池 i 会受到另一座城池 fi 的管辖,其中 fi &l ...
- [luogu3261 JLOI2015] 城池攻占 (左偏树+标记)
传送门 Description 小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池.这 n 个城池用 1 到 n 的整数表示.除 1 号城池外,城池 i 会受到另一座城池 fi 的 ...
- BZOJ 4003 / Luogu P3261 [JLOI2015]城池攻占 (左偏树)
左偏树裸题,在树上合并儿子传上来的堆,然后小于当前结点防御值的就pop掉,pop的时候统计答案. 修改的话就像平衡树一样打懒标记就行了. 具体见代码 CODE #include<bits/std ...
- bzoj 4003 [JLOI2015]城池攻占 —— 左偏树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4003 其实蛮简单的,首先一个城市只会被其子树中的骑士经过,启发我们 dfs 序用可并堆合并子 ...
- bzoj 4003: 城池攻占 左偏树
题目大意 http://www.lydsy.com/JudgeOnline/problem.php?id=4003 题解 一开始看漏条件了 题目保证当占领城池可以使攻击力乘上\(v_i\)时,一定有\ ...
- [note]左偏树(可并堆)
左偏树(可并堆)https://www.luogu.org/problemnew/show/P3377 题目描述 一开始有N个小根堆,每个堆包含且仅包含一个数.接下来需要支持两种操作: 操作1: 1 ...
随机推荐
- pointer & iterator
pointer: address, use operator(*) to get/set the value 1) support operator(+,-), move to next posito ...
- HDU 5288 OO’s Sequence
题意: 给你一个序列, 有一个函数 F(L,R) 其中 ai 均不能 被 aL - aR整除的 函数值是这个ai个数 思路 : 反过来求 满足这样的条件的 ai 的区间,然后求和 #include& ...
- Java实现三大简单排序算法
一.选择排序 public static void main(String[] args) { int[] nums = {1,2,8,4,6,7,3,6,4,9}; for (int i=0; i& ...
- appium常用方法
1.输入中文 在capabilities中增加两项设置: capabilities.setCapability("unicodeKeyboard", "True" ...
- SpringBoot定时任务
代码做定时任务:1.开个线程,线程里面休眠去做 2.使用一些定时任务的框架去做 1.创建TimerTest类 package com.cppdy.service; import org.springf ...
- json的转换操作
toJSON 把JS对象{ 'x': 2, 'y': 3 }转为JSON对象格式的字符串 不能转化字符串 比如"{ 'x': 2, 'y': 3 }" 可以转格式不标准的jso ...
- MYSQL之 error while loading shared libraries: libtinfo.so.5: cannot open shared objectfile: No such f
环境:ubuntu18 登陆MYSQL时遇到错误:mysql: error while loading shared libraries: libtinfo.so.5: cannot open sha ...
- 批量杀掉多个pid文件中记录的pid进程, 并集成到shell脚本中
head_files=`find ./fmsConf/ -name "*.pid"` for file in $head_files do cat $file | awk rm - ...
- HTTP 599: SSL certificate problem: unable to get local issuer certificate错误
自己在用 PySpider 框架爬虫运行代码后时出现 HTTP 599: SSL certificate problem: unable to get local issuer certificate ...
- python文件的分类
# 0.获取所有的文件名称列表import os import shutilos.chdir("files")file_list = os.listdir("./&quo ...