#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair < int, int > PII; typedef pair < LL, LL > PLL; typedef pair < LD, LD > PDD; #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() template < typename _T > inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template < typename _T, typename... args > void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // const int N = 1e6 + 7; int n, m, Q[N]; LL val[N], ans[N], nx[N], czas[N], nad[N]; vector < PLL > v; set < PLL > S; int main() { scanf("%d%d", &n, &m); v.push_back({0, 0}); for(int i = 1; i <= n; i++) { LL x; scanf("%lld", &x); v.push_back({x, i}); } sort(all(v)); n++; for(int i = 0; i + 1 < n; i++) { nx[i] = i + 1; czas[i] = v[i + 1].first - v[i].first; S.insert({czas[i], i}); } nx[n - 1] = n; int mx = 0; for(int i = 1; i <= m; i++) { scanf("%d", &Q[i]); mx = max(mx, Q[i]); } LL gl = 0; for(LL d = 0; d <= mx; d++) { while(!S.empty() && S.begin()->first <= d) { int id = S.begin()->second; gl += (nx[id] - id) * (nx[nx[id]] - nx[id]); ans[d] += nad[id] * (nx[nx[id]] - nx[id]); S.erase(S.begin()); if(nx[id] != n) S.erase({czas[nx[id]], nx[id]}); nx[id] = nx[nx[id]]; if(nx[id] != n) { czas[id] = ceil(LD(v[nx[id]].first - v[id].first) / LD(nx[id] - id)); nad[id] = czas[id] * (nx[id] - id) - (v[nx[id]].first - v[id].first); S.insert({czas[id], id}); } } ans[d + 1] = ans[d] + gl; } for(int i = 1; i <= m; i++) { printf("%lld\n", ans[Q[i]]); } 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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair < int, int > PII; typedef pair < LL, LL > PLL; typedef pair < LD, LD > PDD; #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() template < typename _T > inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template < typename _T, typename... args > void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // const int N = 1e6 + 7; int n, m, Q[N]; LL val[N], ans[N], nx[N], czas[N], nad[N]; vector < PLL > v; set < PLL > S; int main() { scanf("%d%d", &n, &m); v.push_back({0, 0}); for(int i = 1; i <= n; i++) { LL x; scanf("%lld", &x); v.push_back({x, i}); } sort(all(v)); n++; for(int i = 0; i + 1 < n; i++) { nx[i] = i + 1; czas[i] = v[i + 1].first - v[i].first; S.insert({czas[i], i}); } nx[n - 1] = n; int mx = 0; for(int i = 1; i <= m; i++) { scanf("%d", &Q[i]); mx = max(mx, Q[i]); } LL gl = 0; for(LL d = 0; d <= mx; d++) { while(!S.empty() && S.begin()->first <= d) { int id = S.begin()->second; gl += (nx[id] - id) * (nx[nx[id]] - nx[id]); ans[d] += nad[id] * (nx[nx[id]] - nx[id]); S.erase(S.begin()); if(nx[id] != n) S.erase({czas[nx[id]], nx[id]}); nx[id] = nx[nx[id]]; if(nx[id] != n) { czas[id] = ceil(LD(v[nx[id]].first - v[id].first) / LD(nx[id] - id)); nad[id] = czas[id] * (nx[id] - id) - (v[nx[id]].first - v[id].first); S.insert({czas[id], id}); } } ans[d + 1] = ans[d] + gl; } for(int i = 1; i <= m; i++) { printf("%lld\n", ans[Q[i]]); } return 0; } |