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的更多相关文章
随机推荐
- E. Xenia and Tree 分块 + LCA
http://codeforces.com/contest/342/problem/E 如果把询问1存起来,每到sqrt(m)的时候再处理一次. 那么总复杂度就是msqrt(m)的. 把要变颜色的节点 ...
- 用户名密码登录小程序及input与raw_input区别。
一.此次程序需要实现: 1.设定固定的用户名密码 2.用户名密码输入正确打印登录正确信息 3.仅仅运行三次登录 二.本次使用的python版本为: Windows下版本号: C:\Users\dais ...
- eclipse导入php项目
整个工程的都在一个文件夹里面 怎么把它导入到eclipse里面呢:在eclipse里新建一个与要导入的工程同名工程.
- 动手实现 React-redux(六):React-redux 总结
到这里大家已经掌握了 React-redux 的基本用法和概念,并且自己动手实现了一个 React-redux,我们回顾一下这几节都干了什么事情. React.js 除了状态提升以外并没有更好的办法帮 ...
- Excel数据直接到DataTable--->DB
1) Excel数据直接导入到临时生成的DataTable using (OleDbConnection selectConnection = new OleDbConnection("Pr ...
- Asp.net MVC + Vue.js
@{ Layout = null; } <!DOCTYPE html><html> <head> <meta charset="UTF-8" ...
- hihocoder1133 二分·二分查找之k小数
思路: 类似于快排的分治算法. 实现: #include <iostream> #include <cstdio> #include <algorithm> #in ...
- jquery实现上传图片及图片大小验证、图片预览效果代码
jquery实现上传图片及图片大小验证.图片预览效果代码 jquery实现上传图片及图片大小验证.图片预览效果代码 上传图片验证 */ function submit_upload_picture() ...
- Scala 学习记录(一)
1. 相对于java,scala的值修饰用val,变量修饰用var.值相当于java的final 修饰了. package demo object ScalaBase extends App { pr ...
- 复位电路设计——利用PLL锁定信号(lock)产生复位信号
利用PLL锁定信号(lock)产生复位信号 在FPGA刚上电的时候,系统所需的时钟一般都要经过PLL倍频,在时钟锁定(即稳定输出)以前,整个系统应处于复位状态.因此,我们可以利用PLL的锁定信号来产生 ...