bzoj 4003
左偏树。。。
打两个标记。。。和线段树一样,先下放cheng再下放*。
每回合并子树就行了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define N 300010
#define int long long
using namespace std;
int n,m,h[N];
int b[N],v[N];
int s[N],root[N];
vector<int>qi[N];
int c[N],ans[N],ans2[N];
int head[N],nxt[N],ver[N],tot;
void add(int a,int d)
{
tot++;nxt[tot]=head[a];head[a]=tot;ver[tot]=d;return ;
}
struct node
{
int l,r,w,d,lazy1,lazy2;
int pre;
node()
{
lazy2=;
}
}a[N*];int cnt;
int dep[N];
void push_down(int x)
{
int t1=a[x].l;int t2=a[x].r;
if(t1)
{
a[t1].w*=a[x].lazy2,a[t1].lazy1*=a[x].lazy2,a[t1].lazy2*=a[x].lazy2;
a[t1].w+=a[x].lazy1;
a[t1].lazy1+=a[x].lazy1;
}
if(t2)
{
a[t2].w*=a[x].lazy2,a[t2].lazy1*=a[x].lazy2,a[t2].lazy2*=a[x].lazy2;
a[t2].w+=a[x].lazy1;
a[t2].lazy1+=a[x].lazy1;
}
a[x].lazy1=;
a[x].lazy2=;
return ;
}
int merge(int x,int y)
{
if(!x||!y)return x+y;
if(a[x].w>a[y].w)swap(x,y);
push_down(x);
a[x].r=merge(a[x].r,y);
if(a[a[x].l].d<a[a[x].r].d)swap(a[x].l,a[x].r);
a[x].d=a[a[x].r].d+;
return x;
}
void dfs(int x)
{
root[x]=;
for(int i=;i<qi[x].size();i++)
{
a[++cnt].pre=qi[x][i];a[cnt].w=s[qi[x][i]];root[x]=merge(root[x],cnt);
}
for(int i=head[x];i;i=nxt[i])
{
dep[ver[i]]=dep[x]+;
dfs(ver[i]);
root[x]=merge(root[x],root[ver[i]]);
}
while(root[x]&&a[root[x]].w<h[x])
{
ans[x]++;
int y=a[root[x]].pre;
ans2[y]=dep[c[y]]-dep[x];
push_down(root[x]);
root[x]=merge(a[root[x]].l,a[root[x]].r);
}
if(root[x])
{
if(!b[x])
{
a[root[x]].w+=v[x];
a[root[x]].lazy1+=v[x];
}
else
{
a[root[x]].w*=v[x];
a[root[x]].lazy1*=v[x];
a[root[x]].lazy2*=v[x];
}
}
return ;
}
signed main()
{
scanf("%lld%lld",&n,&m);
for(int i=;i<=n;i++)scanf("%lld",&h[i]);
int t1;
for(int i=;i<=n;i++)
{
scanf("%lld",&t1);
add(t1,i);
scanf("%lld%lld",&b[i],&v[i]);
}
for(int i=;i<=m;i++)
{
scanf("%lld%lld",&s[i],&t1);
qi[t1].push_back(i);c[i]=t1;
}
dep[]=;
dfs();
while(root[]!=)
{
int y=a[root[]].pre;
ans2[y]=dep[c[y]]-dep[]+;
push_down(root[]);
root[]=merge(a[root[]].l,a[root[]].r);
}
for(int i=;i<=n;i++)printf("%lld\n",ans[i]);
for(int i=;i<=m;i++)printf("%lld\n",ans2[i]);
return ;
}
bzoj 4003的更多相关文章
- BZOJ 4003: [JLOI2015]城池攻占 左偏树 可并堆
https://www.lydsy.com/JudgeOnline/problem.php?id=4003 感觉就是……普通的堆啊(暴论),因为这个堆是通过递归往右堆里加一个新堆或者新节点的,所以要始 ...
- bzoj 4003: 城池攻占 左偏树
题目大意 http://www.lydsy.com/JudgeOnline/problem.php?id=4003 题解 一开始看漏条件了 题目保证当占领城池可以使攻击力乘上\(v_i\)时,一定有\ ...
- bzoj 4003 [JLOI2015]城池攻占 —— 左偏树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4003 其实蛮简单的,首先一个城市只会被其子树中的骑士经过,启发我们 dfs 序用可并堆合并子 ...
- BZOJ 4003 【JLOI2015】城池攻占
Description 小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池. 这 n 个城池用 1 到 n 的整数表示.除 1 号城池外,城池 i 会受到另一座城池 fi 的管辖, ...
- BZOJ 4003 JLOI2015 城池攻占
做法和APIO2012派遣 那道题目类似 在树上DFS,维护当前子树的小根堆 因为需要合并孩子们的信息,使用左偏树就可以了 每次弹出死亡骑士,对剩余骑士打上奖励标记 至于标记的下传和更改,只需要每次在 ...
- BZOJ 4003 左偏树
思路: 用到了左偏树合并复杂度是logn的性质 一开始先BFS一遍 打标记的左偏树 //By SiriusRen #include <cstdio> #include <cstrin ...
- BZOJ 4003 / Luogu P3261 [JLOI2015]城池攻占 (左偏树)
左偏树裸题,在树上合并儿子传上来的堆,然后小于当前结点防御值的就pop掉,pop的时候统计答案. 修改的话就像平衡树一样打懒标记就行了. 具体见代码 CODE #include<bits/std ...
- BZOJ 4003 (可并堆)
题面 小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池. 这 n 个城池用 1 到 n 的整数表示.除 1 号城池外,城池 i 会受到另一座城池 fi 的管辖, 其中 fi &l ...
- Luogu 3261 [JLOI2015]城池攻占
BZOJ 4003 需要实现一个可并堆. 每个点维护一个小根堆,然后一开始把所有骑士加入到它所在的点的小根堆当中,实际上空间是$O(m)$的,然后我们从上到下不断合并这个小根堆,合并完之后如果遇到堆顶 ...
随机推荐
- windows7 x64下maven安装和配置
http://maven.apache.org/download.cgi下载maven 环境配置 验证配置是否成功 本地仓库配置 这是原来的配置文件: 更改为: link 离线安装 eclipse m ...
- iOS 网络流量统计
在开发中,有时候需要获取流量统计信息.研究发现:通过函数getifaddrs来得到系统网络接口的信息,网络接口的信息,包含在if_data字段中, 有很多信息, 但我现在只关心ifi_ibytes, ...
- 国外远控软件DarkComet-RAT
下载地址:[点此下载] 使用步骤: 注册noip.org账号创建主机地址. 安装并配置DUC. 配置监听端口 配置NO-IP Updater 然后点击Update ,配置成功则会提示Success. ...
- [转] IIS配置文件的XML格式不正确 applicationHost.config崩溃 恢复解决办法
IIS配置文件的XML格式不正确 applicationHost.config崩溃 恢复解决办法 源文件:http://www.cnblogs.com/yuejin/p/3385584.html ...
- dotnet core 使用 MongoDB 进行高性能Nosql数据库操作
好久没有写过Blog, 每天看着开源的Java社区流口水, 心里满不是滋味. 终于等到了今年六月份 dotnet core 的正式发布, 看着dotnet 社区也一步一步走向繁荣, 一片蒸蒸日上的大好 ...
- HTML最新标准HTML5小结
写在前面 HTML5出来已经很久了,然而由于本人不是专业搞前端的,只知道有这个东西,具体概念有点模糊(其实就是一系列标准规范啦):因此去年(2015.11.09),专门对HTML5做了个简单的小结,今 ...
- C#初学单例模式
版本1:最简单的单例模式 public class MySingleton { private MySingleton() //构造函数,注意private { } private static My ...
- 一键准备Oracle安装
在Linux下安装Oracle软件之前,有相当工作需要准备,包括建立用户.配置内核参数.配置资源限制参数.配置Oracle用户环境等,十分繁琐.即便十分熟悉,也需要花费一定的精力来准备.说白了,做这些 ...
- 1.uniq去重命令讲解
uniq命令: 常见参数: -c,--count ***** 在每行旁边显示改行重复出现的次数 -d,--repeated 仅显示重复出现的行,2次或2次以上的行,默认的去重包 ...
- 自定义制作iso镜像
下载"/etc/yum.repos.d/"下的MondoRescue软件库,文件名为"mondorescue.repo".请为你的Linux OS发行版本下载正 ...