#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. Educational Codeforces Round 24 A

    There are n students who have taken part in an olympiad. Now it's time to award the students. Some o ...

  2. Codeforces Round #321 (Div. 2)

    水 A - Kefa and First Steps /************************************************ * Author :Running_Time ...

  3. 状态压缩DP SRM 667 Div1 OrderOfOperations 250

    Problem Statement      Cat Noku has just finished writing his first computer program. Noku's compute ...

  4. 牛客网Java刷题知识点之自动拆装箱

    不多说,直接上干货! https://www.nowcoder.com/ta/review-java/review?query=&asc=true&order=&page=5 ...

  5. Coursera:一流大学免费在线课程平台

    https://www.coursera.org/ 微软联合创始人 Bill Gates 从公司退隐后,一直和妻子 Melinda 忙于公益事业.但离开 IT 圈并未改变他穿廉价衬衫和保持学习的习惯— ...

  6. mvc报( 检测到有潜在危险的 request.form 值 )错的解决方案

    今天在做项目中遇到了报( 检测到有潜在危险的 request.form 值 )错,百度过后解决了该问题,出此问题主要还是因为提交的Form中有HTML字符串,例如你在TextBox中输入了html标签 ...

  7. 关于margin、padding 对内联元素的影响

    内联元素和块级元素的区别是新手必须要掌握的知识点.大家可能平时注意块级元素比较多.所以这里重点让我们来讲讲常见的width height margin  padding 对inline元素的影响. 测 ...

  8. Java基础50题test3—水仙花数

    水仙花数 题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例 如:153 是一个"水仙花数", ...

  9. CF963A Alternating Sum

    思路:利用周期性转化为等比数列求和. 注意当a != b的时候 bk * inv(ak) % (109 + 9)依然有可能等于1,不知道为什么. 实现: #include <bits/stdc+ ...

  10. java写跳一跳辅助程序

    ##起初是想使用按键精灵脚本程序控制,但还是选择熟悉的java.我这里使用了工具,造成延迟问题.也求教:java控制安卓的正确姿势, 参考了.NET玩跳一跳,思路都是一样的,只不过使用ADB控制安卓的 ...