#include <bits/stdc++.h> #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define all(v) (v).begin(),(v).end() #define VI vector<int> #define PII pair<int,int> #define st first #define nd second #define mp make_pair #define pb push_back #define lint long long int #define debug(x) {cerr <<#x <<" = " <<x <<endl; } #define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; } #define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; } #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }} #define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }} #define make( x) int (x); scanf("%d",&(x)); #define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y)); #define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z)); #define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define makev(v,n) VI (v); FOR(i,0,(n)) { make(a); (v).pb(a);} #define IOS ios_base::sync_with_stdio(0) #define HEAP priority_queue #define read( x) scanf("%d",&(x)); #define read2( x, y) scanf("%d%d",&(x),&(y)); #define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z)); #define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define readv(v,n) FOR(i,0,(n)) { make(a); (v).pb(a);} using namespace std; vector<int> speed; int n,N; struct info{ lint sumH, days, set_val, sumSpeed, minH; int len; info( lint sumH_, lint days_ ,lint set_val_ ,lint minH_, lint ss , int lenn ){ sumH = sumH_; days = days_; set_val = set_val_; sumSpeed = ss; len = lenn; minH = minH_; } info(){} }; vector<info> rt; void init(){ for(N=1; N<n; N<<=1); rt = vector<info>(N<<1); FOR(i,0,n) rt[i+N] = info(0,0,-1,0,speed[i],1); FOR(i,n,N) rt[i+N] = info(0,0,-1,0,0,0); FORD(i,N-1,1){ rt[i] = info(0,0,-1,0,rt[2*i].sumSpeed + rt[2*i+1].sumSpeed, rt[2*i].len + rt[2*i+1].len); } } inline void przepchaj(int v, int l, int r){ if(v>=N) return; if (rt[v].set_val != -1) { rt[2*v].sumH = rt[2*v].len*rt[v].set_val; rt[2*v].set_val = rt[v].set_val; rt[2*v].days = 0; rt[2*v].minH = rt[v].set_val; rt[2*v+1].sumH = rt[2*v+1].len*rt[v].set_val; rt[2*v+1].set_val = rt[v].set_val; rt[2*v+1].days = 0; rt[2*v+1].minH = rt[v].set_val; rt[v].set_val = -1; } if (rt[v].days != 0) { rt[2*v].sumH += rt[2*v].sumSpeed*rt[v].days; rt[2*v].days += rt[v].days; rt[2*v].minH += rt[v].days*1LL*speed[l]; rt[2*v+1].sumH += rt[2*v+1].sumSpeed*rt[v].days; rt[2*v+1].days += rt[v].days; rt[2*v+1].minH += rt[v].days*1LL*speed[(l+r)/2 + 1]; rt[v].days = 0; } } // w przedziale x,y reprezentowanym przez v // szukaj przedziału l,r void set_value(int v, int x, int y, int l, int r, lint H){ if(l > r) return ; if( l == x && r == y){ rt[v].days = 0; rt[v].sumH = H*rt[v].len; rt[v].set_val = H; rt[v].minH = H; return ; } przepchaj(v,x,y); int mid = (x+y)/2; set_value(2*v,x,mid,l,min(r,mid),H); set_value(2*v+1,mid+1,y,max(l,mid+1),r,H); rt[v].sumH = rt[2*v].sumH + rt[2*v+1].sumH; rt[v].minH = rt[2*v].minH; } lint get_valCD(int v, int x, int y, int l, int r){ if(l > r) return 0; if( l == x && r == y){ return rt[v].sumH; } przepchaj(v,x,y); int mid = (x+y)/2; lint val1 = get_valCD(2*v,x,mid,l,min(r,mid)); lint val2 = get_valCD(2*v+1,mid+1,y,max(l,mid+1),r); return val1+val2; } void add_days(int v, int x, int y, int l, int r, lint d){ if(l > r) return; if( l == x && r == y){ rt[v].sumH += d*rt[v].sumSpeed; rt[v].days += d; rt[v].minH += d*speed[l]; return; } przepchaj(v,x,y); int mid = (x+y)/2; add_days(2*v,x,mid,l,min(r,mid),d); add_days(2*v+1,mid+1,y,max(l,mid+1),r,d); rt[v].minH = rt[2*v].minH; } int find(lint b){ int v = 1, x = 0, y = N-1; if(rt[v].minH>=b) return -1; while(v<N){ przepchaj(v,x,y); int mid = (x+y)/2; if(n-1<=mid){ y = mid; v = 2*v; } else { if(rt[2*v+1].minH>=b){ v = 2*v; y = (x+y)/2; } else{ v = 2*v+1; x = (x+y)/2+1; } } } return v-N; } int main(){ int m; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { int v; scanf("%d", &v); speed.push_back(v); } sort(speed.begin(), speed.end()); init(); lint lastd = 0; for (int i = 0; i < m; i++) { lint d, b; scanf("%lld %lld", &d, &b); lint diff = d-lastd; lastd = d; add_days(1,0,N-1,0,n-1,diff); lint maxH = get_valCD(1,0,N-1,n-1,n-1); if(maxH < b){ printf("0\n"); } else{ int k = find(b); k++; lint answer = get_valCD(1,0,N-1,k,n-1) - b*1LL*(n-k); set_value(1,0,N-1,k,n-1,b); printf("%lld\n",answer); } } }
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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | #include <bits/stdc++.h> #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define all(v) (v).begin(),(v).end() #define VI vector<int> #define PII pair<int,int> #define st first #define nd second #define mp make_pair #define pb push_back #define lint long long int #define debug(x) {cerr <<#x <<" = " <<x <<endl; } #define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; } #define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; } #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }} #define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }} #define make( x) int (x); scanf("%d",&(x)); #define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y)); #define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z)); #define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define makev(v,n) VI (v); FOR(i,0,(n)) { make(a); (v).pb(a);} #define IOS ios_base::sync_with_stdio(0) #define HEAP priority_queue #define read( x) scanf("%d",&(x)); #define read2( x, y) scanf("%d%d",&(x),&(y)); #define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z)); #define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define readv(v,n) FOR(i,0,(n)) { make(a); (v).pb(a);} using namespace std; vector<int> speed; int n,N; struct info{ lint sumH, days, set_val, sumSpeed, minH; int len; info( lint sumH_, lint days_ ,lint set_val_ ,lint minH_, lint ss , int lenn ){ sumH = sumH_; days = days_; set_val = set_val_; sumSpeed = ss; len = lenn; minH = minH_; } info(){} }; vector<info> rt; void init(){ for(N=1; N<n; N<<=1); rt = vector<info>(N<<1); FOR(i,0,n) rt[i+N] = info(0,0,-1,0,speed[i],1); FOR(i,n,N) rt[i+N] = info(0,0,-1,0,0,0); FORD(i,N-1,1){ rt[i] = info(0,0,-1,0,rt[2*i].sumSpeed + rt[2*i+1].sumSpeed, rt[2*i].len + rt[2*i+1].len); } } inline void przepchaj(int v, int l, int r){ if(v>=N) return; if (rt[v].set_val != -1) { rt[2*v].sumH = rt[2*v].len*rt[v].set_val; rt[2*v].set_val = rt[v].set_val; rt[2*v].days = 0; rt[2*v].minH = rt[v].set_val; rt[2*v+1].sumH = rt[2*v+1].len*rt[v].set_val; rt[2*v+1].set_val = rt[v].set_val; rt[2*v+1].days = 0; rt[2*v+1].minH = rt[v].set_val; rt[v].set_val = -1; } if (rt[v].days != 0) { rt[2*v].sumH += rt[2*v].sumSpeed*rt[v].days; rt[2*v].days += rt[v].days; rt[2*v].minH += rt[v].days*1LL*speed[l]; rt[2*v+1].sumH += rt[2*v+1].sumSpeed*rt[v].days; rt[2*v+1].days += rt[v].days; rt[2*v+1].minH += rt[v].days*1LL*speed[(l+r)/2 + 1]; rt[v].days = 0; } } // w przedziale x,y reprezentowanym przez v // szukaj przedziału l,r void set_value(int v, int x, int y, int l, int r, lint H){ if(l > r) return ; if( l == x && r == y){ rt[v].days = 0; rt[v].sumH = H*rt[v].len; rt[v].set_val = H; rt[v].minH = H; return ; } przepchaj(v,x,y); int mid = (x+y)/2; set_value(2*v,x,mid,l,min(r,mid),H); set_value(2*v+1,mid+1,y,max(l,mid+1),r,H); rt[v].sumH = rt[2*v].sumH + rt[2*v+1].sumH; rt[v].minH = rt[2*v].minH; } lint get_valCD(int v, int x, int y, int l, int r){ if(l > r) return 0; if( l == x && r == y){ return rt[v].sumH; } przepchaj(v,x,y); int mid = (x+y)/2; lint val1 = get_valCD(2*v,x,mid,l,min(r,mid)); lint val2 = get_valCD(2*v+1,mid+1,y,max(l,mid+1),r); return val1+val2; } void add_days(int v, int x, int y, int l, int r, lint d){ if(l > r) return; if( l == x && r == y){ rt[v].sumH += d*rt[v].sumSpeed; rt[v].days += d; rt[v].minH += d*speed[l]; return; } przepchaj(v,x,y); int mid = (x+y)/2; add_days(2*v,x,mid,l,min(r,mid),d); add_days(2*v+1,mid+1,y,max(l,mid+1),r,d); rt[v].minH = rt[2*v].minH; } int find(lint b){ int v = 1, x = 0, y = N-1; if(rt[v].minH>=b) return -1; while(v<N){ przepchaj(v,x,y); int mid = (x+y)/2; if(n-1<=mid){ y = mid; v = 2*v; } else { if(rt[2*v+1].minH>=b){ v = 2*v; y = (x+y)/2; } else{ v = 2*v+1; x = (x+y)/2+1; } } } return v-N; } int main(){ int m; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { int v; scanf("%d", &v); speed.push_back(v); } sort(speed.begin(), speed.end()); init(); lint lastd = 0; for (int i = 0; i < m; i++) { lint d, b; scanf("%lld %lld", &d, &b); lint diff = d-lastd; lastd = d; add_days(1,0,N-1,0,n-1,diff); lint maxH = get_valCD(1,0,N-1,n-1,n-1); if(maxH < b){ printf("0\n"); } else{ int k = find(b); k++; lint answer = get_valCD(1,0,N-1,k,n-1) - b*1LL*(n-k); set_value(1,0,N-1,k,n-1,b); printf("%lld\n",answer); } } } |