#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的更多相关文章

随机推荐

  1. bzoj 4909 [Sdoi2017]龙与地下城

    题面 https://www.lydsy.com/JudgeOnline/problem.php?id=4909 题解 目前为止仅仅在LOJ上A掉这道题(Loj真快!) 当然不是标准做法 显然我们只要 ...

  2. 130 Surrounded Regions 被围绕的区域

    给定一个二维的矩阵,包含 'X' 和 'O'(字母 O), 找到所有被 'X' 围绕的区域.并将区域里所有 'O'用 'X' 填充.例如,X X X XX O O XX X O XX O X X运行你 ...

  3. PowerShell~语法与运算符

    基本语法 变量:$开头 $a = "Hello" 条件语句:if else ) { Write-Host "偶数" } else{ Write-Host &qu ...

  4. 外文翻译 《How we decide》多巴胺的预言 第三节

    这是第二章的最后一节. 书的导言 本章第一节 本章第二节 本节阅读感言:自我批评是自我提升的妙方. 多巴胺是我们感情的源泉.多巴胺相关的神经系统在不断的记录着我们主观意识没有注意到的一个个模式,将它们 ...

  5. AJPFX关于表结构的相关语句

    //表结构的相关语句==================================== 建表语句: create table 表名(                                ...

  6. unity内存管理

    最近一直在研究unity的内存加载,因为它是游戏运行的重中之重,如果不深入理解和合理运用,很可能导致项目因内存太大而崩溃. 详细说一下细节概念:AssetBundle运行时加载:来自文件就用Creat ...

  7. 手把手教写devops全栈自动化工具(django2.1)

    简单介绍一下自己之前写的一个全栈项目,框架用的是django2.1版本 主要对paramiko模块,salstack的API二次开发. 核心组件包括:MQ,mysql,websocket,redis, ...

  8. HTTPS时代已来,你做好准备了吗?

    早在今年年初,Google在其安全博客上已经表明,从7月开始,Chrome68会将所有的HTTP网站标记为不安全.随后,Mozilla也表明,Firefox浏览器也准备将所有HTTP网站标记为不安全. ...

  9. iOS 解决iOS 9下的http请求发送失败问题

    iOS9中 因为系统要求所有的请求都必须使用https, 所以发送http请求会失败,如果想让程序能够兼容http请求 在info.plist中添加以下代码: 这里需要做的是右键info.plist文 ...

  10. python中的seteuid

    seteuid(...)    seteuid(uid)        Set the current process's effective user id.