#include <bits/stdc++.h> #define REP(a,b) for(int a=0; a<(b); ++a) #define FWD(a,b,c) for(int a=(b); a<(c); ++a) #define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d) #define BCK(a,b,c) for(int a=(b); a>(c); --a) #define ALL(a) (a).begin(), (a).end() #define SIZE(a) ((int)(a).size()) #define VAR(x) #x ": " << x << " " #define popcount __builtin_popcount #define popcountll __builtin_popcountll #define gcd __gcd #define x first #define y second #define st first #define nd second #define pb push_back using namespace std; template<typename T> using vstack = stack<T, vector<T>>; template<typename T> ostream& operator<<(ostream &out, const vector<T> &v){ out << "{"; for(const T &a : v) out << a << ", "; out << "}"; return out; } template<typename S, typename T> ostream& operator<<(ostream &out, const pair<S,T> &p){ out << "(" << p.st << ", " << p.nd << ")"; return out; } typedef long long LL; typedef pair<int, int> PII; typedef long double K; typedef vector<int> VI; const int dx[] = {0,0,-1,1}; //1,1,-1,1}; const int dy[] = {-1,1,0,0}; //1,-1,1,-1}; struct cut { LL t, h; int p; }; int n, m; LL V[500010]; LL VS[500010]; vector<cut> S; int main(){ scanf("%d %d", &n, &m); FWD(i,0,n) scanf("%lld", &V[i]); V[n] = 0; ++n; sort(V, V+n); VS[n] = 0; BCK(i,n-1,-1){ VS[i] = V[i] + VS[i+1]; } S.reserve(n); S.push_back({0, 0, 0}); FWD(i,0,m){ LL s = 0; cut c; c.p = n; scanf("%lld %lld", &c.t, &c.h); while((c.t - S.back().t) * V[S.back().p] + S.back().h > c.h){ s += (VS[S.back().p] - VS[c.p]) * (c.t - S.back().t); s += (S.back().h - c.h) * (c.p - S.back().p); c.p = S.back().p; S.pop_back(); } if((c.t - S.back().t) * V[c.p - 1] + S.back().h > c.h){ int lo = S.back().p; int hi = c.p - 1; while(hi - lo > 1){ int mi = (lo + hi) / 2; if((c.t - S.back().t) * V[mi] + S.back().h > c.h) hi = mi; else lo = mi; } s += (VS[hi] - VS[c.p]) * (c.t - S.back().t); s += (S.back().h - c.h) * (c.p - hi); c.p = hi; } if(c.p < n) S.push_back(c); printf("%lld\n", s); } 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 | #include <bits/stdc++.h> #define REP(a,b) for(int a=0; a<(b); ++a) #define FWD(a,b,c) for(int a=(b); a<(c); ++a) #define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d) #define BCK(a,b,c) for(int a=(b); a>(c); --a) #define ALL(a) (a).begin(), (a).end() #define SIZE(a) ((int)(a).size()) #define VAR(x) #x ": " << x << " " #define popcount __builtin_popcount #define popcountll __builtin_popcountll #define gcd __gcd #define x first #define y second #define st first #define nd second #define pb push_back using namespace std; template<typename T> using vstack = stack<T, vector<T>>; template<typename T> ostream& operator<<(ostream &out, const vector<T> &v){ out << "{"; for(const T &a : v) out << a << ", "; out << "}"; return out; } template<typename S, typename T> ostream& operator<<(ostream &out, const pair<S,T> &p){ out << "(" << p.st << ", " << p.nd << ")"; return out; } typedef long long LL; typedef pair<int, int> PII; typedef long double K; typedef vector<int> VI; const int dx[] = {0,0,-1,1}; //1,1,-1,1}; const int dy[] = {-1,1,0,0}; //1,-1,1,-1}; struct cut { LL t, h; int p; }; int n, m; LL V[500010]; LL VS[500010]; vector<cut> S; int main(){ scanf("%d %d", &n, &m); FWD(i,0,n) scanf("%lld", &V[i]); V[n] = 0; ++n; sort(V, V+n); VS[n] = 0; BCK(i,n-1,-1){ VS[i] = V[i] + VS[i+1]; } S.reserve(n); S.push_back({0, 0, 0}); FWD(i,0,m){ LL s = 0; cut c; c.p = n; scanf("%lld %lld", &c.t, &c.h); while((c.t - S.back().t) * V[S.back().p] + S.back().h > c.h){ s += (VS[S.back().p] - VS[c.p]) * (c.t - S.back().t); s += (S.back().h - c.h) * (c.p - S.back().p); c.p = S.back().p; S.pop_back(); } if((c.t - S.back().t) * V[c.p - 1] + S.back().h > c.h){ int lo = S.back().p; int hi = c.p - 1; while(hi - lo > 1){ int mi = (lo + hi) / 2; if((c.t - S.back().t) * V[mi] + S.back().h > c.h) hi = mi; else lo = mi; } s += (VS[hi] - VS[c.p]) * (c.t - S.back().t); s += (S.back().h - c.h) * (c.p - hi); c.p = hi; } if(c.p < n) S.push_back(c); printf("%lld\n", s); } return 0; } |