BZOJ4293: [PA2015]Siano
Description
Input
Output
Sample Input
1 2 4 3
1 1
2 2
3 0
4 4
Sample Output
6
18
0
HINT
#include<cstdio>
#include<cctype>
#include<queue>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i;i=next[i])
using namespace std;
const int BufferSize=1<<16;
char buffer[BufferSize],*head,*tail;
inline char Getchar() {
if(head==tail) {
int l=fread(buffer,1,BufferSize,stdin);
tail=(head=buffer)+l;
}
return *head++;
}
typedef long long ll;
inline ll read() {
ll x=0,f=1;char c=Getchar();
for(;!isdigit(c);c=Getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=Getchar()) x=x*10+c-'0';
return x*f;
}
const int maxn=500010;
int n,m,A[maxn];
ll setv[maxn<<2],sett[maxn<<2],suma[maxn<<2],sumv[maxn<<2],maxv[maxn<<2],maxt[maxn<<2];
void maintain(int o,int l,int r) {
int lc=o<<1,rc=lc|1;
if(setv[o]>=0) {
sumv[o]=setv[o]*(r-l+1)-sett[o]*suma[o];
maxv[o]=setv[o];maxt[o]=sett[o];
}
else if(l<r) {
sumv[o]=sumv[lc]+sumv[rc];
maxv[o]=maxv[rc];maxt[o]=maxt[rc];
}
}
void pushdown(int o,int l,int r) {
int lc=o<<1,rc=lc|1,mid=l+r>>1;
if(setv[o]>=0) {
setv[lc]=setv[rc]=setv[o];
sett[lc]=sett[rc]=sett[o];
maxt[lc]=maxt[rc]=sett[o];
sumv[lc]=setv[o]*(mid-l+1)-sett[o]*suma[lc];
sumv[rc]=setv[o]*(r-mid)-sett[o]*suma[rc];
maxv[lc]=maxv[rc]=setv[o];
setv[o]=sett[o]=-1;
}
}
void build(int o,int l,int r) {
if(l==r) suma[o]=A[l];
else {
setv[o]=-1;
int mid=l+r>>1,lc=o<<1,rc=lc|1;
build(lc,l,mid);build(rc,mid+1,r);
suma[o]=suma[lc]+suma[rc];
}
}
ll ans,d,b;
int findst(int o,int l,int r) {
if(l==r) return l;
else {
pushdown(o,l,r);
int mid=l+r>>1,lc=o<<1,rc=lc|1;
if(maxv[lc]+(d-maxt[lc])*A[mid]>b) return findst(lc,l,mid);
return findst(rc,mid+1,r);
}
}
void update(int o,int l,int r,int ql,int qr) {
if(ql<=l&&r<=qr) {
ans+=sumv[o]+d*suma[o]-b*(r-l+1);
setv[o]=b;sett[o]=d;
}
else {
pushdown(o,l,r);
int mid=l+r>>1,lc=o<<1,rc=lc|1;
if(ql<=mid) update(lc,l,mid,ql,qr);
if(qr>mid) update(rc,mid+1,r,ql,qr);
}
maintain(o,l,r);
}
int main() {
n=read();m=read();
rep(i,1,n) A[i]=read();
sort(A+1,A+n+1);
build(1,1,n);
rep(i,1,m) {
d=read();b=read();
if(maxv[1]+(d-maxt[1])*A[n]<=b) puts("0");
else {
int s=findst(1,1,n);ans=0;
update(1,1,n,s,n);printf("%lld\n",ans);
}
}
return 0;
}
BZOJ4293: [PA2015]Siano的更多相关文章
- BZOJ4293 [PA2015]Siano(线段树)
传送门 这Seg确实不好写,不过因为它与ai的相对顺序无关,所以,我们在对ai排序之后,就可做了.维护一个区间最大值,维护一个和,维护一个区间赋值的懒标记,再维护一个时间变化的标记就可以了. 因为不论 ...
- 【BZOJ4293】[PA2015]Siano 线段树
[BZOJ4293][PA2015]Siano Description 农夫Byteasar买了一片n亩的土地,他要在这上面种草. 他在每一亩土地上都种植了一种独一无二的草,其中,第i亩土地的草每天会 ...
- 2018.07.23[PA2015]Siano(线段树)
[PA2015]Siano 描述 Description 农夫Byteasar买了一片n亩的土地,他要在这上面种草. 他在每一亩土地上都种植了一种独一无二的草,其中,第i亩土地的草每天会长高a[i]厘 ...
- BZOJ.4293.[PA2015]Siano(线段树)
题目链接 \(Description\) 有一片n亩的土地,要在这上面种草. 在每一亩土地上都种植了一种独一无二的草,其中,第\(i\)亩土地的草每天会长高\(a[i]\)厘米. 一共会进行\(m\) ...
- 【BZOJ】4293: [PA2015]Siano 线段树上二分
[题意]给定n棵高度初始为0的草,每天每棵草会长高a[i],m次收割,每次在d[i]天将所有>b[i]的草收割到b[i],求每次收割量.n<=500000. [算法]线段树上二分 [题解] ...
- [PA2015]Siano 单调栈
由于某人找了个单调栈的题解但是没研究透所以让我们来研究............ 首先先来考虑下面一种情况,假设第\(k\)次切割时,天数为\(d_k\),高度为\(b_k\),第\(k+1\)次切割时 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- 【bzoj4293】【PA2015】Siano
如题,首先可以考虑相对大小是不变的. 那么先sort,之后每次在线段树上二分即可. #include<bits/stdc++.h> typedef long long ll; using ...
- BZOJ 4291: [PA2015]Kieszonkowe 水题
4291: [PA2015]Kieszonkowe Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnli ...
随机推荐
- angular.extend用法实例
angular.extend:依次将第二个参数及后续的参数的第一层属性(不管是简单属性还是对象)拷贝赋给第一个参数的第一层属性,即如果是对象,则是引用的是同一个对象,并返回第一个参数对象. 实例一 ...
- jQuery插件开发代码
方法和原理在这篇博文中非常详细易懂 http://www.cnblogs.com/Wayou/p/jquery_plugin_tutorial.html 下面整理下基本知识点和基本的代码段: jQue ...
- inq to datatable group by 多列 实现
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.T ...
- PDF虚拟打印机
1.安装福昕PDF阅读器,自己就会安装上PDF虚拟打印机 2.通过office2010之后版本中自带的另存为功能转换为pdf 3.pdf虚拟打印机工具:工具1:http://www.win2pdf.c ...
- VC++ : error LNK2001: unresolved external symbol "__declspec(dllimport) public: __thiscall std::basic_string<wchar_t,struct std::char_traits<wchar_t>
最近学习Google Breakpad,将其用在了自己的项目中,编译的版本为VS2010,没有什么问题.但是为了和之前的程序兼容,需要使用VS2008版本的程序,于是又编译了VS2008版本的代码,但 ...
- shell 多行注释
:<<! 要注释的内容 要注释的内容 要注释的内容 !
- 修改git的远程仓库命令
1. 修改命令 git remte origin set-url URL 2.先删后加 git remote rm origin git remote add origin git@github.co ...
- 如何下架app
因赶数日工期成,偷得浮生半日闲.遂登录iTunes Connect,发现之前做过的小程序,想将其下架,故而有此篇随想.(温馨提示:项目被下架后再次上架该版本,不需要再次经过审核)下面是详情步骤: 1. ...
- Ajax请求跨域问题 -- 转载
几乎每种浏览器都存在默认的安全机制,都有同源策略,因为浏览器恶意的把每个外部请求的都当做是黑客攻击,相当于是对自身的保护,所以浏览器在运行脚本时会判断脚本与请求的页面是否是同一来源,这个同一来源,包括 ...
- QT编译时 cc1plus进程占用大量内存卡死问题解决
QT5.7 做一个demo编译时,内存几乎完全消耗,卡死.经尝试发现是添加资源文件过大导致(不知是单个文件过大触发还是文件总共过大触发)的.我的资源文件工136M,单个最大是125M左右. 解决方法是 ...