#include<bits/stdc++.h> #define PII pair<int,int> #define f first #define s second #define VI vector<int> #define LL long long #define MP make_pair #define LD long double #define PB push_back #define ALL(V) V.begin(),V.end() #define abs(x) max((x),-(x)) #define PDD pair<LD,LD> #define VPII vector< PII > #define siz(V) ((int)V.size()) #define FOR(x, b, e) for(int x=b;x<=(e);x++) #define FORD(x, b, e) for(int x=b;x>=(e);x--) #define REP(x, n) for(int x=0;x<(n);x++) #define mini(a,b) a=min(a,b) #define maxi(a,b) a=max(a,b) using namespace std; int n,a,b,c,d,k,m,q,z; const int MXN=5e5+5; const int BASE=1<<19; LL in[BASE+3]; struct seg { LL sumpod=0; LL add=0;//stale LL czas=0; LL mxpod=0; LL mxadd=0;//stale LL rowne=-1; LL rowneczas=0; void print(int id) { printf("id: %d sumpod: %lld czas: %lld mxpod: %lld rowne: %lld rowneczas: %lld\n",id,sumpod,czas,mxpod,rowne,rowneczas); } }; seg t[BASE*2+3]; void upd(int id,int p,int k,LL x,LL time) { t[id].sumpod=(k-p+1)*x; t[id].czas=time; t[id].mxpod=x; t[id].rowne=x; t[id].rowneczas=time; } void shift(int id,int p,int k) { if(t[id].rowne==-1)return; int mid=(p+k)/2; upd(id*2,p,mid,t[id].rowne,t[id].rowneczas); upd(id*2+1,mid+1,k,t[id].rowne,t[id].rowneczas); t[id].rowne=-1; } void doczasu(int id,LL time) { LL delta=time-t[id].czas; t[id].sumpod+=t[id].add*delta; t[id].czas=time; t[id].mxpod+=t[id].mxadd*delta; } void calc(int id) { LL ttt=max(t[id*2].czas,t[id*2+1].czas); doczasu(id*2,ttt); doczasu(id*2+1,ttt); t[id].sumpod=t[id*2].sumpod+t[id*2+1].sumpod; t[id].mxpod=max(t[id*2].mxpod,t[id*2+1].mxpod); t[id].czas=ttt; } void insertequal(int a,int b,LL x,LL time,int p=1,int k=BASE,int id=1) { if(b<p || k<a)return; if(a<=p && k<=b) { upd(id,p,k,x,time); return; } shift(id,p,k); int mid=(p+k)/2; insertequal(a,b,x,time,p,mid,id*2); insertequal(a,b,x,time,mid+1,k,id*2+1); calc(id); } LL querypoint(LL x,LL time,int p=1,int k=BASE,int id=1)//pierwszy większy { if(p==k)return p; shift(id,p,k); doczasu(id*2,time); int mid=(p+k)/2; if(t[id*2].mxpod>x)return querypoint(x,time,p,mid,id*2); return querypoint(x,time,mid+1,k,id*2+1); } LL querysum(int a,int b,LL time,int p=1,int k=BASE,int id=1) { if(b<p || k<a)return 0; doczasu(id,time); if(a<=p&&k<=b) return t[id].sumpod; shift(id,p,k); int mid=(p+k)/2; return querysum(a,b,time,p,mid,id*2) + querysum(a,b,time,mid+1,k,id*2+1); } void inittree(int p=1,int k=BASE,int id=1) { if(p==k) { t[id].add = t[id].mxadd = in[p]; return; } int mid=(p+k)/2; inittree(p,mid,id*2); inittree(mid+1,k,id*2+1); t[id].add = t[id*2].add + t[id*2+1].add; t[id].mxadd = max(t[id*2].mxadd, t[id*2+1].mxadd); } void readLL(LL &a) { char ch=0; a=0; while(ch<33)ch=getchar_unlocked(); do { a=(a<<1)+(a<<3)+ch-'0'; ch=getchar_unlocked(); } while(ch>33); } void printLL(LL a,char b) { if(a==0){putchar_unlocked('0');putchar_unlocked(b);return;} char buf[20]; char x=0; while(a) { buf[x++]=a%10; a/=10; } while(x) putchar_unlocked(buf[--x]+48); putchar_unlocked(b); } main() { scanf("%d%d",&n,&m); FOR(i,1,n) { readLL(in[i]); } sort(in+1,in+n+1); inittree(); LL time,b; FOR(i,1,m) { readLL(time); readLL(b); int punkt=querypoint(b,time); if(punkt>n) { printLL(0,'\n'); continue; } LL r=querysum(punkt,n,time); LL res=r-(n-punkt+1)*b; printLL(res,'\n'); insertequal(punkt,n,b,time); } }
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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | #include<bits/stdc++.h> #define PII pair<int,int> #define f first #define s second #define VI vector<int> #define LL long long #define MP make_pair #define LD long double #define PB push_back #define ALL(V) V.begin(),V.end() #define abs(x) max((x),-(x)) #define PDD pair<LD,LD> #define VPII vector< PII > #define siz(V) ((int)V.size()) #define FOR(x, b, e) for(int x=b;x<=(e);x++) #define FORD(x, b, e) for(int x=b;x>=(e);x--) #define REP(x, n) for(int x=0;x<(n);x++) #define mini(a,b) a=min(a,b) #define maxi(a,b) a=max(a,b) using namespace std; int n,a,b,c,d,k,m,q,z; const int MXN=5e5+5; const int BASE=1<<19; LL in[BASE+3]; struct seg { LL sumpod=0; LL add=0;//stale LL czas=0; LL mxpod=0; LL mxadd=0;//stale LL rowne=-1; LL rowneczas=0; void print(int id) { printf("id: %d sumpod: %lld czas: %lld mxpod: %lld rowne: %lld rowneczas: %lld\n",id,sumpod,czas,mxpod,rowne,rowneczas); } }; seg t[BASE*2+3]; void upd(int id,int p,int k,LL x,LL time) { t[id].sumpod=(k-p+1)*x; t[id].czas=time; t[id].mxpod=x; t[id].rowne=x; t[id].rowneczas=time; } void shift(int id,int p,int k) { if(t[id].rowne==-1)return; int mid=(p+k)/2; upd(id*2,p,mid,t[id].rowne,t[id].rowneczas); upd(id*2+1,mid+1,k,t[id].rowne,t[id].rowneczas); t[id].rowne=-1; } void doczasu(int id,LL time) { LL delta=time-t[id].czas; t[id].sumpod+=t[id].add*delta; t[id].czas=time; t[id].mxpod+=t[id].mxadd*delta; } void calc(int id) { LL ttt=max(t[id*2].czas,t[id*2+1].czas); doczasu(id*2,ttt); doczasu(id*2+1,ttt); t[id].sumpod=t[id*2].sumpod+t[id*2+1].sumpod; t[id].mxpod=max(t[id*2].mxpod,t[id*2+1].mxpod); t[id].czas=ttt; } void insertequal(int a,int b,LL x,LL time,int p=1,int k=BASE,int id=1) { if(b<p || k<a)return; if(a<=p && k<=b) { upd(id,p,k,x,time); return; } shift(id,p,k); int mid=(p+k)/2; insertequal(a,b,x,time,p,mid,id*2); insertequal(a,b,x,time,mid+1,k,id*2+1); calc(id); } LL querypoint(LL x,LL time,int p=1,int k=BASE,int id=1)//pierwszy większy { if(p==k)return p; shift(id,p,k); doczasu(id*2,time); int mid=(p+k)/2; if(t[id*2].mxpod>x)return querypoint(x,time,p,mid,id*2); return querypoint(x,time,mid+1,k,id*2+1); } LL querysum(int a,int b,LL time,int p=1,int k=BASE,int id=1) { if(b<p || k<a)return 0; doczasu(id,time); if(a<=p&&k<=b) return t[id].sumpod; shift(id,p,k); int mid=(p+k)/2; return querysum(a,b,time,p,mid,id*2) + querysum(a,b,time,mid+1,k,id*2+1); } void inittree(int p=1,int k=BASE,int id=1) { if(p==k) { t[id].add = t[id].mxadd = in[p]; return; } int mid=(p+k)/2; inittree(p,mid,id*2); inittree(mid+1,k,id*2+1); t[id].add = t[id*2].add + t[id*2+1].add; t[id].mxadd = max(t[id*2].mxadd, t[id*2+1].mxadd); } void readLL(LL &a) { char ch=0; a=0; while(ch<33)ch=getchar_unlocked(); do { a=(a<<1)+(a<<3)+ch-'0'; ch=getchar_unlocked(); } while(ch>33); } void printLL(LL a,char b) { if(a==0){putchar_unlocked('0');putchar_unlocked(b);return;} char buf[20]; char x=0; while(a) { buf[x++]=a%10; a/=10; } while(x) putchar_unlocked(buf[--x]+48); putchar_unlocked(b); } main() { scanf("%d%d",&n,&m); FOR(i,1,n) { readLL(in[i]); } sort(in+1,in+n+1); inittree(); LL time,b; FOR(i,1,m) { readLL(time); readLL(b); int punkt=querypoint(b,time); if(punkt>n) { printLL(0,'\n'); continue; } LL r=querysum(punkt,n,time); LL res=r-(n-punkt+1)*b; printLL(res,'\n'); insertequal(punkt,n,b,time); } } |