#include <cstdio> #include <algorithm> using namespace std; #define LL long long #define MAX_N 500000 #define BLOCK 708 int n,m; LL T[MAX_N]; LL V[MAX_N]; LL S[MAX_N]; struct BLO{LL inc,add,sum,i;BLO(){inc=0;add=0;sum=0;i=1;}} B[1000]; int N; LL sumT(int a,int b){ if(b>=n)b=n-1; if(a==0)return S[b]; return S[b]-S[a-1]; } inline LL get(int i){ return V[i]*B[i/BLOCK].i+B[i/BLOCK].inc*T[i]+B[i/BLOCK].add; } inline void inc(LL delta){ for(int a=0;a<N;++a){ B[a].inc+=delta; } } inline void update(int i){ LL s=0; for(int a=i*BLOCK;a<(i+1)*BLOCK;++a){ V[a]=get(a); s+=V[a]; } B[i].inc=0; B[i].add=0; B[i].sum=s; B[i].i=1; } inline void set(int q,int w,LL v){ int b1=q/BLOCK; int b2=w/BLOCK; if(b1==b2){ update(b1); LL s=0; for(int a=q;a<=w;++a){ s+=v-V[a]; V[a]=v; } B[b1].sum+=s; }else{ update(b1); LL s=0; for(int a=q;a<(b1+1)*BLOCK;++a){ s+=v-V[a]; V[a]=v; } B[b1].sum+=s; update(b2); s=0; for(int a=b2*BLOCK;a<=w;++a){ s+=v-V[a]; V[a]=v; } B[b2].sum+=s; for(int a=b1+1;a<b2;++a){ B[a].inc=0; B[a].add=v; B[a].sum=0; B[a].i=0; } } } inline LL sum(int q,int w){ LL ret=0; int b1=q/BLOCK; int b2=w/BLOCK; if(b1==b2){ for(int a=q;a<=w;++a){ ret+=get(a); } }else{ for(int a=q;a<(b1+1)*BLOCK;++a){ ret+=get(a); } for(int a=b2*BLOCK;a<=w;++a){ ret+=get(a); } for(int a=b1+1;a<b2;++a){ ret+=B[a].add*BLOCK+B[a].inc*sumT(a*BLOCK,(a+1)*BLOCK-1)+B[a].sum; } } return ret; } int main(){ scanf("%d%d",&n,&m); N=(n-1)/BLOCK+1; for(int a=0;a<n;++a)scanf("%lld",T+a); sort(T,T+n); S[0]=T[0]; for(int a=1;a<n;++a)S[a]=S[a-1]+T[a]; LL last=0; while(m--){ LL d,b; scanf("%lld%lld",&d,&b); inc(d-last); int i=n; int l=0,r=n-1; while(l<=r){ int s=(l+r)/2; if(get(s)<b){ l=s+1; }else{ r=s-1; i=s; } } if(i!=n){ LL ans=sum(i,n-1); set(i,n-1,b); printf("%lld\n",ans-sum(i,n-1)); }else printf("0\n"); last=d; } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | #include <cstdio> #include <algorithm> using namespace std; #define LL long long #define MAX_N 500000 #define BLOCK 708 int n,m; LL T[MAX_N]; LL V[MAX_N]; LL S[MAX_N]; struct BLO{LL inc,add,sum,i;BLO(){inc=0;add=0;sum=0;i=1;}} B[1000]; int N; LL sumT(int a,int b){ if(b>=n)b=n-1; if(a==0)return S[b]; return S[b]-S[a-1]; } inline LL get(int i){ return V[i]*B[i/BLOCK].i+B[i/BLOCK].inc*T[i]+B[i/BLOCK].add; } inline void inc(LL delta){ for(int a=0;a<N;++a){ B[a].inc+=delta; } } inline void update(int i){ LL s=0; for(int a=i*BLOCK;a<(i+1)*BLOCK;++a){ V[a]=get(a); s+=V[a]; } B[i].inc=0; B[i].add=0; B[i].sum=s; B[i].i=1; } inline void set(int q,int w,LL v){ int b1=q/BLOCK; int b2=w/BLOCK; if(b1==b2){ update(b1); LL s=0; for(int a=q;a<=w;++a){ s+=v-V[a]; V[a]=v; } B[b1].sum+=s; }else{ update(b1); LL s=0; for(int a=q;a<(b1+1)*BLOCK;++a){ s+=v-V[a]; V[a]=v; } B[b1].sum+=s; update(b2); s=0; for(int a=b2*BLOCK;a<=w;++a){ s+=v-V[a]; V[a]=v; } B[b2].sum+=s; for(int a=b1+1;a<b2;++a){ B[a].inc=0; B[a].add=v; B[a].sum=0; B[a].i=0; } } } inline LL sum(int q,int w){ LL ret=0; int b1=q/BLOCK; int b2=w/BLOCK; if(b1==b2){ for(int a=q;a<=w;++a){ ret+=get(a); } }else{ for(int a=q;a<(b1+1)*BLOCK;++a){ ret+=get(a); } for(int a=b2*BLOCK;a<=w;++a){ ret+=get(a); } for(int a=b1+1;a<b2;++a){ ret+=B[a].add*BLOCK+B[a].inc*sumT(a*BLOCK,(a+1)*BLOCK-1)+B[a].sum; } } return ret; } int main(){ scanf("%d%d",&n,&m); N=(n-1)/BLOCK+1; for(int a=0;a<n;++a)scanf("%lld",T+a); sort(T,T+n); S[0]=T[0]; for(int a=1;a<n;++a)S[a]=S[a-1]+T[a]; LL last=0; while(m--){ LL d,b; scanf("%lld%lld",&d,&b); inc(d-last); int i=n; int l=0,r=n-1; while(l<=r){ int s=(l+r)/2; if(get(s)<b){ l=s+1; }else{ r=s-1; i=s; } } if(i!=n){ LL ans=sum(i,n-1); set(i,n-1,b); printf("%lld\n",ans-sum(i,n-1)); }else printf("0\n"); last=d; } return 0; } |