#include <cstdio>
#include <iostream>
#include <set>
#include <map>
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
#include <cmath>
#include <iomanip>
#include <deque>
#include <cassert>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<int,int> PII;
#define MP make_pair
#define FOR(v,p,k) for(int v=p;v<=k;++v)
#define FORD(v,p,k) for(int v=p;v>=k;--v)
#define REP(i,n) for(int i=0;i<(n);++i)
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define PB push_back
#define ST first
#define ND second
#define SIZE(x) (int)x.size()
#define ALL(c) c.begin(),c.end()
using namespace std;
#define int long long
class info{
public:
int czas;
int wys;
int first;
int last;
info(int czas1, int wys1, int first1, int last1){
czas = czas1;
wys = wys1;
first = first1;
last = last1;
}
};
vector<int> pref;
vector<int> a;
deque<info> koszenia;
int przedzial(int l, int r){
return pref[r + 1] - pref[l];
}
int currentHeight(int idx, int wys, int czasOld, int czasNew){
return wys + a[idx] * (czasNew - czasOld);
}
int binSearch(int czasOld, int czasNew, int l, int r, int wys, int kosz){
while(true){
int m = (l + r) / 2;
int h = currentHeight(m, wys, czasOld, czasNew);
if(h > kosz){
if(h == l){
return m;
} else{
if(currentHeight(m - 1, wys, czasOld, czasNew) <= kosz){
return m;
}
r = m - 1;
}
} else{
l = m + 1;
}
}
}
#undef int
int main() {
#define int long long
//freopen("sia_big.in", "r",stdin);
//freopen("sia_biga.out", "w", stdout);
int n,m;
cin>>n>>m;
pref = vector<int>(n + 1);
a = vector<int>(n);
REP(i,n){
cin>>a[i];
pref[i + 1] = pref[i] + a[i];
}
sort(ALL(a));
koszenia.PB(info(0, 0, 0, n - 1));
REP(q,m){
//cerr<<q<<'\n';
//cerr<<koszenia.size();
int d,b;
//d - dzien
//b - wysokosc
cin>>d>>b;
vector<info> nowe;
int skoszona = 0;
while(!koszenia.empty()){
info temp = koszenia.back();
int minHeight = currentHeight(temp.first, temp.wys, temp.czas, d);
int maxHeight = currentHeight(temp.last, temp.wys, temp.czas, d);
if(maxHeight <= b){
break;
}
if(minHeight >= b){
skoszona += przedzial(temp.first, temp.last) * (d - temp.czas) + (temp.last - temp.first + 1) * (temp.wys) - b * (temp.last - temp.first + 1);
nowe.PB(info(d, b, temp.first, temp.last));
koszenia.pop_back();
continue;
} else{
int foundIdx = binSearch(temp.czas, d, temp.first, temp.last, temp.wys, b);
skoszona += przedzial(foundIdx, temp.last) * (d - temp.czas) + (temp.last - foundIdx + 1) * (temp.wys) - b * (temp.last - temp.first + 1);
nowe.push_back(info(d,b,foundIdx, temp.last));
nowe.push_back(info(temp.czas, temp.wys, temp.first, foundIdx - 1));
koszenia.pop_back();
break;
}
}
while(!nowe.empty()){
koszenia.PB(nowe.back());
nowe.pop_back();
}
cout<<skoszona<<'\n';
}
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 | #include <cstdio> #include <iostream> #include <set> #include <map> #include <algorithm> #include <iterator> #include <string> #include <vector> #include <cmath> #include <iomanip> #include <deque> #include <cassert> using namespace std; typedef long long LL; typedef long double LD; typedef pair<int,int> PII; #define MP make_pair #define FOR(v,p,k) for(int v=p;v<=k;++v) #define FORD(v,p,k) for(int v=p;v>=k;--v) #define REP(i,n) for(int i=0;i<(n);++i) #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define PB push_back #define ST first #define ND second #define SIZE(x) (int)x.size() #define ALL(c) c.begin(),c.end() using namespace std; #define int long long class info{ public: int czas; int wys; int first; int last; info(int czas1, int wys1, int first1, int last1){ czas = czas1; wys = wys1; first = first1; last = last1; } }; vector<int> pref; vector<int> a; deque<info> koszenia; int przedzial(int l, int r){ return pref[r + 1] - pref[l]; } int currentHeight(int idx, int wys, int czasOld, int czasNew){ return wys + a[idx] * (czasNew - czasOld); } int binSearch(int czasOld, int czasNew, int l, int r, int wys, int kosz){ while(true){ int m = (l + r) / 2; int h = currentHeight(m, wys, czasOld, czasNew); if(h > kosz){ if(h == l){ return m; } else{ if(currentHeight(m - 1, wys, czasOld, czasNew) <= kosz){ return m; } r = m - 1; } } else{ l = m + 1; } } } #undef int int main() { #define int long long //freopen("sia_big.in", "r",stdin); //freopen("sia_biga.out", "w", stdout); int n,m; cin>>n>>m; pref = vector<int>(n + 1); a = vector<int>(n); REP(i,n){ cin>>a[i]; pref[i + 1] = pref[i] + a[i]; } sort(ALL(a)); koszenia.PB(info(0, 0, 0, n - 1)); REP(q,m){ //cerr<<q<<'\n'; //cerr<<koszenia.size(); int d,b; //d - dzien //b - wysokosc cin>>d>>b; vector<info> nowe; int skoszona = 0; while(!koszenia.empty()){ info temp = koszenia.back(); int minHeight = currentHeight(temp.first, temp.wys, temp.czas, d); int maxHeight = currentHeight(temp.last, temp.wys, temp.czas, d); if(maxHeight <= b){ break; } if(minHeight >= b){ skoszona += przedzial(temp.first, temp.last) * (d - temp.czas) + (temp.last - temp.first + 1) * (temp.wys) - b * (temp.last - temp.first + 1); nowe.PB(info(d, b, temp.first, temp.last)); koszenia.pop_back(); continue; } else{ int foundIdx = binSearch(temp.czas, d, temp.first, temp.last, temp.wys, b); skoszona += przedzial(foundIdx, temp.last) * (d - temp.czas) + (temp.last - foundIdx + 1) * (temp.wys) - b * (temp.last - temp.first + 1); nowe.push_back(info(d,b,foundIdx, temp.last)); nowe.push_back(info(temp.czas, temp.wys, temp.first, foundIdx - 1)); koszenia.pop_back(); break; } } while(!nowe.empty()){ koszenia.PB(nowe.back()); nowe.pop_back(); } cout<<skoszona<<'\n'; } return 0; } |
English