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& ...
随机推荐
- Django 聚合与查询集API实现侧边栏
本文从Django官方文档总结而来,将聚合的主要用法和查询集的常见方法做一归纳. 聚合 1. 聚合的产生来源于django数据库查询,通常我们使用django查询来完成增删查改,但是有时候需要更复杂的 ...
- Salt Document学习笔记2
配置文件需修改的内容及注意点: Edit the master config file: 1. Uncomment and change the user: root value to your ow ...
- Mac安装Homebrew记录
在终端输入: ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install) ...
- 基于Golang设计一套微服务架构[转]
article- @嘟嘟噜- May/26/2018 18:35:30 如何基于Golang设计一套微服务架构 微服务(Microservices),这个近几年我们经常听到.那么现在市面上的的微服 ...
- CGAffineTransform 缩放 / 旋转 / 平移
CGAffineTransform此类是一个3*3矩阵的变换. - (void)transformImageView { CGAffineTransform t = CGAffineTransform ...
- Spatial Transformer Networks
参考:http://blog.csdn.net/xbinworld/article/details/69049680 论文的关键在理解这句话: 先通过V中坐标(xtarget,ytarget)以此找到 ...
- MySQL CPU 使用率高的原因和解决方法
用户在使用 MySQL 实例时,会遇到 CPU 使用率过高甚至达到 100% 的情况.本文将介绍造成该状况的常见原因以及解决方法,并通过 CPU 使用率为 100% 的典型场景,来分析引起该状况的原因 ...
- 性能测试二十一:环境部署之mysql
在正常工作中,mysql应该部署到 一台独立的服务器上,不与tomcat共用服务器,由于成本原因,现部署到一起 为避免出错引起麻烦,先备份: 一:环境清理:先卸载系统自带的mysql 停止mysql: ...
- 格式化输出的方法:%、.format()、f
a = '123'a1 = '456'a2 = '789' %占位符 text = "a=%s"%atext1 = "a=%s,a1=%s,a2=%s"%(a, ...
- 浅析微信支付:微信支付简单介绍(小程序、公众号、App、H5)
本文是[浅析微信支付]系列文章的第二篇,主要讲解一下普通商户接入的支付方式以及其中的不同之处. 上篇文章讲了本系列的大纲,没有看过的朋友们可以看一下. 浅析微信支付:前篇大纲 微信支付是集成在微信客户 ...