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
| #include <bits/stdc++.h> using namespace std; typedef long long ll; namespace FastIO { #define iL (1 << 20) char ibuf[iL],*iS = ibuf + iL,*iT = ibuf + iL; #define gc() (iS == iT ? (iT = (iS = ibuf) + fread(ibuf,1,iL,stdin),iS == iT ? EOF : *iS++) : *iS++) template<typename T> inline void read(T &a) { char ch;int sign = 0; for(ch = gc();!isdigit(ch);ch = gc()) if(ch == '-') sign = 1; a = ch & 15; for(ch = gc();isdigit(ch);ch = gc()) a = (a << 3) + (a << 1) + (ch & 15); if(sign) a = -a; } char Out[iL],*iter = Out; #define flush() fwrite(Out,1,iter - Out,stdout),iter = Out template<typename T> inline void write(T x,char end = '\n') { int c[40],l = 0;if(x < 0) *iter++ = '-',x = -x; do c[++l] = x % 10,x /= 10; while(x); while(l) *iter++ = c[l--] + '0'; *iter++ = end;flush(); } #undef iL #undef gc #undef flush } using namespace FastIO;
const int N = 1e5 + 5,Lim = 1e5; int n,m,Q; int a[N],s[N],c[N],x[N];
struct node{ int c,id; node(){} node(const int _c,const int _id): c(_c),id(_id) {} bool operator < (const node &rhs) const { return c < rhs.c;} }; priority_queue<node> pq; vector<int> P[N]; inline void Prework() { read(n);read(m);read(Q); for(int i = 1;i <= n;i++) read(a[i]),read(s[i]),read(c[i]),read(x[i]); for(int i = 1;i <= n;i++) { if(x[i] > 0) { int tim = (c[i] + x[i] - 1) / x[i]; P[min(tim,Lim)].push_back(i); } else P[Lim].push_back(i); } } int usd[N]; ll ans[N]; ll Brute(int tim) { ll ans = 0; while(!pq.empty()) pq.pop(); for(int i = 1;i <= n;i++) usd[i] = 0; for(int i = Lim;i >= 1;i--) { for(auto it : P[i]) pq.emplace(a[it] + s[it],it); if(i > tim) continue; vector<node> stk; for(int now = m;now && pq.size();) { node tmp = pq.top();pq.pop(); if(usd[tmp.id] == 0) { ans += tmp.c;usd[tmp.id]++;now--; if(c[tmp.id] > 1) pq.emplace(a[tmp.id],tmp.id); } else { int dta = min(now,c[tmp.id] - usd[tmp.id] - (i - 1) * x[tmp.id]); ans += 1ll * dta * tmp.c;usd[tmp.id] += dta;now -= dta; if(usd[tmp.id] < c[tmp.id]) stk.push_back(tmp); } } while(stk.size()) pq.push(stk.back()),stk.pop_back(); } return ans; } int main() { Prework(); ans[Lim] = Brute(Lim); while(!pq.empty()) pq.pop(); for(int i = 1;i <= n;i++) if(usd[i] == 1) pq.emplace(-a[i] - s[i],i); else if(usd[i] > 0) pq.emplace(-a[i],i); ll sum = 0; for(int i = 1;i <= n;i++) sum += usd[i]; for(int i = Lim - 1;i >= 1;i--) { ans[i] = ans[i + 1]; while(sum > i * m && pq.size()) { node tmp = pq.top(); tmp.c *= -1; if(usd[tmp.id] > 1) { int dta = min(sum - 1ll * i * m,(ll)usd[tmp.id] - 1); usd[tmp.id] -= dta;ans[i] -= 1ll * dta * tmp.c; sum -= dta; if(usd[tmp.id] == 1) pq.pop(),pq.emplace(-a[tmp.id]-s[tmp.id],tmp.id); } else ans[i] -= tmp.c,usd[tmp.id]--,sum--,pq.pop(); } } for(int _ = 1,x;_ <= Q;_++) read(x),write(ans[x]); return 0; }
|