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的更多相关文章
随机推荐
- [Usaco2012 Jan]Video Game
Description Bessie is playing a video game! In the game, the three letters 'A', 'B', and 'C' are the ...
- (020)[虚拟系统]Win7网络连接红叉(无解决)
该虚拟机在重装主系统前是可以连接网络的,主系统重新安装以后,导入新安装的VM以后,网络图标显示红叉. 查看设备管理,显示没有安装以太网驱动. 重新安装 Vmware Tools,未果.VMware官网 ...
- C#结构体和类的区别(转)
结构体和类的区别: 在做一个项目时,使用了较多的结构体,并且存在一些结构体的嵌套,即某结构体成员集合包含另一个结构体等,总是出现一些奇怪的错误,才终于下决心好好分析一下到底类和结构体有啥不同,虽 ...
- Masonry自动布局与UIScrolView适配
Masonry介绍 Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布局 简洁明了 并具有高可读性 而且同时支持 iOS 和 Max OS X.可以通过cocoa ...
- netcdf源码在windows上的编译
作者:朱金灿 来源:http://blog.csdn.net/clever101 今天搞搞netcdf源码在windows上的编译,折腾了半天,算是搞成了,特地记录一下过程.我的目标是要生成netcd ...
- Python behave in BDD
BDD概念 全称 Behavior-driven development 中文 行为驱动开发 概念 是敏捷软件开发技术的一种,鼓励各方人员在一个软件项目里交流合作,包括开发人员.测试人员和非技术人员或 ...
- ubuntu 下service php5-fpm restart 报错 stop: Unknown instance: 解决
问题描述: 在安装完扩展后,重启php-fpm,发现一直停止报错 stop: Unknown instance: 通过查看进程,也查询不到该主进程 解决办法: 干掉现在正在执行的进程 pkill ph ...
- sqlserver:查询锁住sql以及解锁
--查看被锁表:SELECT request_session_id spid, OBJECT_NAME( resource_associated_entity_id ) tableNameFROM s ...
- 工作笔记:复制文件--从windows到ubuntu,再到fedora
最近在测试跨平台类库,于是写了一些小程序. 当然主要利用vs进行主要的代码开发.eclipse进行linux的调试. 那么需要不时同步项目文件. 考虑到项目简单,所以没有使用svn. 1. 从wind ...
- JS的本地保存localStorage、sessionStorage用法总结
localStorage 生命周期是永久的 这意味着除非用户显示在浏览器提供的UI上清除localStorage信息,否则这些信息将永远存在. sessionStorage 生命周期为当前窗口或标签 ...