#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (n); ++i) #define SIZE(a) ((int)a.size()) typedef long long ll; const int N = 200005; const int M = 1000*1000 + 5; const ll linf = 2e18; int n, m, d[N]; ll t[N], result[M]; ll add; set<int> ids; set<int>::iterator iters[N]; priority_queue<pair<ll,int>> event; int size[N]; ll computeDist(int f, int s, int d) { ll y = max(t[s]-d, 0LL); ll c = max(t[f]-d, 0LL) + 1LL*d*size[f]; ll m = max((y-c)/(1+size[f]), 0LL); while (max(t[s]-d-m, 0LL) > c + m*size[f]) ++m; return m; } bool eat(int id, int d) { auto it = iters[id]; ++it; if (it == ids.end()) return false; int v = *it; if (computeDist(id, v, d) > 0) { return false; } ids.erase(it); iters[v] = ids.end(); add -= 1LL*size[v]*(size[v]-1)/2; add -= 1LL*size[id]*(size[id]-1)/2; result[d] += 1LL*size[v]*(max(t[id]-d, 0LL) + 1LL*size[id]*d - max(t[v]-d, 0LL)); size[id] += size[v]; add += 1LL*size[id]*(size[id]-1)/2; return true; } void solve() { REP(i, n) { iters[i] = ids.insert(i).first; size[i] = 1; if (i+1 < n) event.push({-computeDist(i, i+1, 0), i}); } for (int d = 0; d < 1000000; ++d) { while (!event.empty()) { auto now = event.top(); now.first *= -1; if (now.first > d) break; event.pop(); if (iters[now.second] == ids.end()) continue; while (eat(now.second, d)); auto it = ids.find(now.second); auto nxt = it; ++nxt; if (nxt != ids.end()) event.push({-(computeDist(*it, *nxt, d) + d), *it}); } result[d+1] = result[d] + add; if (t[*ids.begin()] <= d) result[d+1] += size[*ids.begin()]; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; REP(i, n) cin >> t[i]; solve(); REP(i, m) { ll d; cin >> d; cout << result[d] << '\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 | #include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (n); ++i) #define SIZE(a) ((int)a.size()) typedef long long ll; const int N = 200005; const int M = 1000*1000 + 5; const ll linf = 2e18; int n, m, d[N]; ll t[N], result[M]; ll add; set<int> ids; set<int>::iterator iters[N]; priority_queue<pair<ll,int>> event; int size[N]; ll computeDist(int f, int s, int d) { ll y = max(t[s]-d, 0LL); ll c = max(t[f]-d, 0LL) + 1LL*d*size[f]; ll m = max((y-c)/(1+size[f]), 0LL); while (max(t[s]-d-m, 0LL) > c + m*size[f]) ++m; return m; } bool eat(int id, int d) { auto it = iters[id]; ++it; if (it == ids.end()) return false; int v = *it; if (computeDist(id, v, d) > 0) { return false; } ids.erase(it); iters[v] = ids.end(); add -= 1LL*size[v]*(size[v]-1)/2; add -= 1LL*size[id]*(size[id]-1)/2; result[d] += 1LL*size[v]*(max(t[id]-d, 0LL) + 1LL*size[id]*d - max(t[v]-d, 0LL)); size[id] += size[v]; add += 1LL*size[id]*(size[id]-1)/2; return true; } void solve() { REP(i, n) { iters[i] = ids.insert(i).first; size[i] = 1; if (i+1 < n) event.push({-computeDist(i, i+1, 0), i}); } for (int d = 0; d < 1000000; ++d) { while (!event.empty()) { auto now = event.top(); now.first *= -1; if (now.first > d) break; event.pop(); if (iters[now.second] == ids.end()) continue; while (eat(now.second, d)); auto it = ids.find(now.second); auto nxt = it; ++nxt; if (nxt != ids.end()) event.push({-(computeDist(*it, *nxt, d) + d), *it}); } result[d+1] = result[d] + add; if (t[*ids.begin()] <= d) result[d+1] += size[*ids.begin()]; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; REP(i, n) cin >> t[i]; solve(); REP(i, m) { ll d; cin >> d; cout << result[d] << '\n'; } return 0; } |