BZOJ.4293.[PA2015]Siano(线段树)
\(Description\)
有一片n亩的土地,要在这上面种草。
在每一亩土地上都种植了一种独一无二的草,其中,第\(i\)亩土地的草每天会长高\(a[i]\)厘米。
一共会进行\(m\)次收割,其中第\(i\)次收割在第\(d[i]\)天,并把所有高度大于等于\(b[i]\)的部分全部割去。求每次收割得到的草的高度总和是多少。

/*
无论怎么收割,长得快的草一定是不矮于长得慢的;而询问与下标无关
所以按生长速度排序,某一时刻的高度一定是递增的,这样就可以二分了
注意很多longlong
小于x直接输出0!
Reset在查询里!所以查询中要有PushUp!
*/
#include<cstdio>
#include<cctype>
#include<algorithm>
#define gc() getchar()
#define now node[rt]
#define lson node[node[rt].ls]
#define rson node[node[rt].rs]
typedef long long LL;
const int N=5e5+5;
LL A[N],sum[N];
struct Seg_Tree
{
int tot;
struct Node
{
LL maxn,sum,tag/*num of days*/,flag/*reset(>=0) or not(-1)*/;
int l,r,ls,rs;
Node() {flag=-1;}
}node[N<<1];
inline void PushUp(int rt)
{
now.sum = lson.sum + rson.sum;
now.maxn = rson.maxn;
// now.maxn = std::max(lson.maxn, rson.maxn);
}
inline void Reset(int rt,LL f)
{
now.sum=1LL*(now.r-now.l+1)*f, now.maxn=f;
now.tag=0, now.flag=f;
}
inline void Update(int rt,LL tg)
{
now.tag += tg;
now.sum += (sum[now.r]-sum[now.l-1])*tg;
now.maxn += A[now.r]*tg;//最大值肯定是最高的那个
}
void PushDown(int rt)
{
if(~now.flag)
{
Reset(now.ls,now.flag), Reset(now.rs,now.flag),
now.flag=-1;
}
if(now.tag)//reset后重新赋上的tag
{
Update(now.ls,now.tag), Update(now.rs,now.tag),
now.tag=0;
}
}
void Build(int l,int r)
{
int p=tot++;
node[p].l=l, node[p].r=r;
// node[p].tag=0, node[p].flag=-1;
if(l==r) ;
// node[p].ls = node[p].rs = -1,
// node[p].maxn = node[p].sum = A[l];
else
{
int m=l+r>>1;
node[p].ls=tot, Build(l,m);
node[p].rs=tot, Build(m+1,r);
// PushUp(p);
}
}
int Find(int rt,LL x)
{
if(now.l==now.r) return now.l;
PushDown(rt);
if(lson.maxn>=x) return Find(now.ls,x);
return Find(now.rs,x);
}
LL Query_Sum(int rt,int L,int R,LL x)//Reset N[L~R] to x
{
LL res=0;
if(L<=now.l && now.r<=R) {res=now.sum; Reset(rt,x); return res;}//!
PushDown(rt);
int m=now.l+now.r>>1;
if(L<=m) res=Query_Sum(now.ls,L,R,x);
if(m<R) res+=Query_Sum(now.rs,L,R,x);
PushUp(rt);//!
return res;
}
// LL Query_Sum(int rt,int p,LL x)//Reset N[L~R] to x
// {
// if(now.r < p) return 0;
// LL res;
// if(now.l >= p) {res=now.sum; Reset(rt,x); return res;}//!
// PushDown(rt);
// res=Query_Sum(now.ls,p,x)+Query_Sum(now.rs,p,x);
// PushUp(rt);
// return res;
// }
}t;
#undef now
inline LL read()
{
LL now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("4293.in","r",stdin);
#endif
LL n=read(),m=read();
for(int i=1; i<=n; ++i) A[i]=read();
std::sort(A+1,A+1+n);
t.Build(1,n);
for(int i=1; i<=n; ++i) sum[i]=sum[i-1]+A[i];
for(LL res,day,x,las=0,pos; m; --m,las=day)
{
day=read(),x=read();
t.Update(0, day-las);
if(t.node[0].maxn < x) {puts("0"); continue;}//很有效 and必须要加!
pos=t.Find(0,x), printf("%lld\n",t.Query_Sum(0,pos,n,x)-(n-pos+1)*x);//longlong
// pos=t.Find(0,x), printf("%lld\n",t.Query_Sum(0,pos,x)-(n-pos+1)*x);//longlong
}
return 0;
}
BZOJ.4293.[PA2015]Siano(线段树)的更多相关文章
- 【BZOJ4293】[PA2015]Siano 线段树
[BZOJ4293][PA2015]Siano Description 农夫Byteasar买了一片n亩的土地,他要在这上面种草. 他在每一亩土地上都种植了一种独一无二的草,其中,第i亩土地的草每天会 ...
- BZOJ4293 [PA2015]Siano(线段树)
传送门 这Seg确实不好写,不过因为它与ai的相对顺序无关,所以,我们在对ai排序之后,就可做了.维护一个区间最大值,维护一个和,维护一个区间赋值的懒标记,再维护一个时间变化的标记就可以了. 因为不论 ...
- 【BZOJ】4293: [PA2015]Siano 线段树上二分
[题意]给定n棵高度初始为0的草,每天每棵草会长高a[i],m次收割,每次在d[i]天将所有>b[i]的草收割到b[i],求每次收割量.n<=500000. [算法]线段树上二分 [题解] ...
- Bzoj 2752 高速公路 (期望,线段树)
Bzoj 2752 高速公路 (期望,线段树) 题目链接 这道题显然求边,因为题目是一条链,所以直接采用把边编上号.看成序列即可 \(1\)与\(2\)号点的边连得是. 编号为\(1\)的点.查询的时 ...
- BZOJ.3938.Robot(李超线段树)
BZOJ UOJ 以时间\(t\)为横坐标,位置\(p\)为纵坐标建坐标系,那每个机器人就是一条\(0\sim INF\)的折线. 用李超线段树维护最大最小值.对于折线分成若干条线段依次插入即可. 最 ...
- BZOJ.1558.[JSOI2009]等差数列(线段树 差分)
BZOJ 洛谷 首先可以把原序列\(A_i\)转化成差分序列\(B_i\)去做. 这样对于区间加一个等差数列\((l,r,a_0,d)\),就可以转化为\(B_{l-1}\)+=\(a_0\),\(B ...
- BZOJ 3779: 重组病毒(线段树+lct+树剖)
题面 escription 黑客们通过对已有的病毒反编译,将许多不同的病毒重组,并重新编译出了新型的重组病毒.这种病毒的繁殖和变异能力极强.为了阻止这种病毒传播,某安全机构策划了一次实验,来研究这种病 ...
- BZOJ 3123 森林(函数式线段树)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3123 题意: 思路:总的来说,查询区间第K小利用函数式线段树的减法操作.对于两棵树的合并 ...
- BZOJ 2124等差子序列 线段树&&hash
[题目描述 Description] 给一个 1 到 N 的排列{Ai},询问是否存在 1<=p1<p2<p3<p4<p5<…<pLen<=N(Len& ...
随机推荐
- 编码器AE & VAE
学习总结于国立台湾大学 :李宏毅老师 自编码器 AE (Auto-encoder) & 变分自动编码器VAE(Variational Auto-encoder) ...
- Linux下clock计时函数学习
平时在Linux和Winows下都有编码的时候,移植代码的时候免不了发现一些问题.1. 你到底准不准?关于clock()计时函数首先是一段简单的测试代码,功能为测试从文本文件读取数据并赋值给向量最后打 ...
- STOMP Over WebSocket
Show Table of Contents What is STOMP? STOMP is a simple text-orientated messaging protocol. It defin ...
- mysql系列三、mysql开启缓存、设置缓存大小、缓存过期机制
一.开启缓存 mysql 开启查询缓存可以有两种方法来开启一种是使用set命令来进行开启,另一种是直接修改my.ini文件来直接设置都是非常的简单的哦. 开启缓存,设置缓存大小,具体实施如下: 1.修 ...
- 『转载』hadoop 1.X到2.X的变化
表1新旧hadoop脚本/变量/位置变化表 改变项 原框架中 新框架中(Yarn) 备注 配置文件位置 ${hadoop_home_dir}/conf ${hadoop_home_dir}/etc/h ...
- sqlserver2008 链接服务器 2000
背景 这个项目就有意思了,我用的是sqlserver2008,对方用的是sqlserver2000,还装在windows2000上,是个很老的系统了.两方要对接,对方技术太菜,自己竟然不会转2000. ...
- Android:Service
Android Service: http://www.apkbus.com/android-15649-1-1.html android service 的各种用法(IPC.AIDL): http: ...
- js实现星级评分效果(非常规5个li代码)
1. 前言 此方案受到JS单行写一个评级组件启发,自己写了一个简单Demo. 功能有正常滑动,动态显示实心星星个数:当点击确认,则保持当前的实心星星个数:再移动时未点击,则离开后还是保持之前的状态. ...
- 分布式调用技术 RPC VS REST
一 分布式调用大体上就分为两类,RPC式的,REST式的,两者的区别主要是就是: 1. RPC是面向动作的(方法调用) 2. REST是面向资源的(URL表示资源,HTTP动词表示动作) 从变现形式来 ...
- compile php with openssl on mac osx error 填坑
从源码手动编译 PHP 时出现如下错误: Default 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Undefined symbols for arch ...