左偏树。。。

打两个标记。。。和线段树一样,先下放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的更多相关文章

  1. BZOJ 4003: [JLOI2015]城池攻占 左偏树 可并堆

    https://www.lydsy.com/JudgeOnline/problem.php?id=4003 感觉就是……普通的堆啊(暴论),因为这个堆是通过递归往右堆里加一个新堆或者新节点的,所以要始 ...

  2. bzoj 4003: 城池攻占 左偏树

    题目大意 http://www.lydsy.com/JudgeOnline/problem.php?id=4003 题解 一开始看漏条件了 题目保证当占领城池可以使攻击力乘上\(v_i\)时,一定有\ ...

  3. bzoj 4003 [JLOI2015]城池攻占 —— 左偏树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4003 其实蛮简单的,首先一个城市只会被其子树中的骑士经过,启发我们 dfs 序用可并堆合并子 ...

  4. BZOJ 4003 【JLOI2015】城池攻占

    Description 小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池. 这 n 个城池用 1 到 n 的整数表示.除 1 号城池外,城池 i 会受到另一座城池 fi 的管辖, ...

  5. BZOJ 4003 JLOI2015 城池攻占

    做法和APIO2012派遣 那道题目类似 在树上DFS,维护当前子树的小根堆 因为需要合并孩子们的信息,使用左偏树就可以了 每次弹出死亡骑士,对剩余骑士打上奖励标记 至于标记的下传和更改,只需要每次在 ...

  6. BZOJ 4003 左偏树

    思路: 用到了左偏树合并复杂度是logn的性质 一开始先BFS一遍 打标记的左偏树 //By SiriusRen #include <cstdio> #include <cstrin ...

  7. BZOJ 4003 / Luogu P3261 [JLOI2015]城池攻占 (左偏树)

    左偏树裸题,在树上合并儿子传上来的堆,然后小于当前结点防御值的就pop掉,pop的时候统计答案. 修改的话就像平衡树一样打懒标记就行了. 具体见代码 CODE #include<bits/std ...

  8. BZOJ 4003 (可并堆)

    题面 小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池. 这 n 个城池用 1 到 n 的整数表示.除 1 号城池外,城池 i 会受到另一座城池 fi 的管辖, 其中 fi &l ...

  9. Luogu 3261 [JLOI2015]城池攻占

    BZOJ 4003 需要实现一个可并堆. 每个点维护一个小根堆,然后一开始把所有骑士加入到它所在的点的小根堆当中,实际上空间是$O(m)$的,然后我们从上到下不断合并这个小根堆,合并完之后如果遇到堆顶 ...

随机推荐

  1. PDO运用

  2. gulp-uglify 与gulp.watch()配合使用时遇到的重复压缩问题

    今天学习gulp时候,用到gulp-uglify压缩js模块,遇到的一个问题-当用gulp.watch来监听js文件的变动时出现重复压缩的问题 目录结构如下: gulpfile.js代码如下: var ...

  3. [Android]使用MVP解决技术债务(翻译)

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5892671.html 使用MVP解决技术债务 原文:https ...

  4. iOS---The maximum number of apps for free development profiles has been reached.

    真机调试免费App ID出现的问题The maximum number of apps for free development profiles has been reached.免费应用程序调试最 ...

  5. swift学习笔记1——基础部分

    之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...

  6. SQLSERVER 2012 收缩日志

    select log_reuse_wait_desc from sys.databases where name='tfs_CARDLANWEB' backup log tfs_CARDLANWEB ...

  7. C#语言基础——递归

    递归 一.概念conception: 函数体内调用本函数自身,直到符合某一条件不再继续调用. 二.应满足条件factor: (1)有反复执行的过程(调用自身): (2)有跳出反复执行过程的条件(函数出 ...

  8. Eclipse使用Maven创建web3.0项目

    安装Maven插件 这一步不细说了,自己下载的Eclipse-JAVA EE 版已自带 Maven插件 开始创建 文本1New一个 Maven Web App项目:File-->New--> ...

  9. Bootstrap 快速人门案例——前端最火的插件

    今天,我给小白们分享一下比较流行的Bootstrap框架,它在工作中得到许多公司的青睐,因此对于升职和加薪很重要.同时,我们可以快速完成开发任务,减少发开周期,有不对的地方望大家指正. 如果你想走的更 ...

  10. EZchip花1.3亿美元买Tilera然后以8亿美元把自己与Tilera一起卖掉

    2014年7月EZchip花1.3亿美元收购的Tilera2015年10 Mellanox 8亿美元收购EZchip,2016年1月完成.EZchip转手卖掉Tilera与自己? http://www ...