luoguT21777
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll;
int n, m;
ll a[500005], uu, vv, lst, qzh[500005];
struct SGT{
ll sum[2000005], tag[2000005], fla[2000005], zdz[2000005];
void update(int o, int l, int r, ll k){
sum[o] += (qzh[r]-qzh[l-1]) * k;
tag[o] += k;
zdz[o] += a[r] * k;
}
void reset(int o, int l, int r, ll k){
sum[o] = (r-l+1) * k;
zdz[o] = fla[o] = k;
tag[o] = 0;
}
void pushDown(int o, int l, int r, int lson, int rson, int mid){
if(fla[o]!=-1){
reset(lson, l, mid, fla[o]);
reset(rson, mid+1, r, fla[o]);
fla[o] = -1;
}
if(tag[o]){
update(lson, l, mid, tag[o]);
update(rson, mid+1, r, tag[o]);
tag[o] = 0;
}
}
int queryPos(int o, int l, int r, ll k){
if(l==r) return l;
else{
int mid=(l+r)>>1;
int lson=o<<1;
int rson=lson|1;
pushDown(o, l, r, lson, rson, mid);
if(zdz[lson]>=k) return queryPos(lson, l, mid, k);
else return queryPos(rson, mid+1, r, k);
}
}
ll querySum(int o, int l, int r, int x, int y){
if(l>=x && r<=y) return sum[o];
else{
int mid=(l+r)>>1;
int lson=o<<1;
int rson=lson|1;
ll ans=0;
pushDown(o, l, r, lson, rson, mid);
if(x<=mid) ans += querySum(lson, l, mid, x, y);
if(mid<y) ans += querySum(rson, mid+1, r, x, y);
return ans;
}
}
void modify(int o, int l, int r, int x, int y, ll k){
if(l>=x && r<=y) reset(o, l, r, k);
else{
int mid=(l+r)>>1;
int lson=o<<1;
int rson=lson|1;
pushDown(o, l, r, lson, rson, mid);
if(x<=mid) modify(lson, l, mid, x, y, k);
if(mid<y) modify(rson, mid+1, r, x, y, k);
sum[o] = sum[lson] + sum[rson];
zdz[o] = zdz[rson];
}
}
}sgt;
int main(){
cin>>n>>m;
for(int i=1; i<=n; i++) scanf("%lld", &a[i]);
sort(a+1, a+1+n);
for(int i=1; i<=n; i++) qzh[i] = qzh[i-1] + a[i];
memset(sgt.fla, -1, sizeof(sgt.fla));
while(m--){
scanf("%lld %lld", &uu, &vv);
sgt.update(1, 1, n, uu-lst);
lst = uu;
if(sgt.zdz[1]<vv){
printf("0\n");
continue;
}
int pos=sgt.queryPos(1, 1, n, vv);
printf("%lld\n", sgt.querySum(1, 1, n, pos, n)-(n-pos+1)*vv);
sgt.modify(1, 1, n, pos, n, vv);
}
return 0;
}
luoguT21777的更多相关文章
随机推荐
- IDEA中创建HBASE工程
IntelliJ IDEA+maven的HBase开发环境搭建 hbase 配置(hbase-site.xml)和基本使用 16010端口可以查看Hbase web信息
- 在服务器上执行hbase的jar包
hadoop命令执行hbase应用jar包时的环境变量加载问题 Apache HBase ™ Reference Guide HBase, MapReduce, and the CLASSPATH
- stack(单调栈) POJ 2082 Terrible Sets
题目传送门 题意:紧贴x轴有一些挨着的矩形,给出每个矩形的长宽,问能组成的最大矩形面积为多少 分析:用堆栈来维护高度递增的矩形,遇到高度小的,弹出顶部矩形直到符合递增,顺便计算矩形面积,且将弹出的宽度 ...
- Codeforces Round #295 (Div. 2) B. Two Buttons
B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- 当css样式表遇到层
(附:White-space:pre可以是样式表里卖弄body的属性,作用是保持html源代码的空格与换行,等同<pre>标签.) Css样式表可以通过被封在层里的方式来限制页面所修饰的内 ...
- azkaban-web-start.sh启动时出现Table 'execution_flows' is marked as crashed and should be repaired Query错误的解决办法(图文详解)
问题详情 [hadoop@master bin]$ ./azkaban-web-start.sh Using Hadoop Using Hive from /home/hadoop/app/hive ...
- ceph集群一键部署脚本
分布式存储ceph相信大家比较熟悉了.某项目临时要做一个40个节点的存储集群.所以写了这个脚本. 一键部署脚本如下: git clone https://github.com/luckman666/d ...
- 8 Explicit Animations 指明的动画 笔记
8 Explicit Animations 指明的动画 笔记 If you want something done right, do it yourself. 如果你想让事情做好,那就自动来 ...
- array_keys
<?php$array = array(0 => 100, "color" => "red");print_r(array_keys($arr ...
- oid和节点名称
由于单篇文档最大字限制是40000个字符,不能将OID附上,因此写出我是如何得到这些OID的. 1.安装NET-SNMP yum install net-snmp yum install net-sn ...