#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (int i = (a); i < (b); i++) #define per(i, a, b) for (int i = (b) - 1; i <= (a); i--) typedef list<pair<int,pair<ll,int>>> af; int n, m; af intervals; vector <ll> t; vector <af::iterator> itory; set<pair<ll, int>> events; pair <ll, ll> sum = {0, 0}; ll inf = 1e15; void change(af::iterator a, bool erase = false) { int ia = a->first; events.erase({a->second.first, ia}); if (erase) { a->second.first = -inf; return; } a->second.first = inf; auto b = a; b++; if (b != intervals.end()) { ll start = t[a->first]; ll end = t[b->first]; ll cnt = a->second.second; ll timemerge = (end - start + cnt - 1) / cnt; a->second.first = timemerge; } if (a->second.first != inf) events.insert({a->second.first, ia}); } pair <ll, ll> contrib(af::iterator a) { ll cnt = a->second.second; ll offset = t[a->first]; return {offset * cnt, cnt * (cnt - 1) / 2}; } void merge(af::iterator a) { af::iterator b = a; b++; assert(b != intervals.end()); sum.first -= contrib(b).first + contrib(a).first; sum.second -= contrib(b).second + contrib(a).second; a->second.second += b->second.second; change(b, true); intervals.erase(b); change(a); sum = {sum.first + contrib(a).first, sum.second + contrib(a).second}; } int main(void) { ios::sync_with_stdio(false); cin >> n >> m; t = {0}; intervals = {{0, {0,1}}}; rep(i, 0, n) { ll a; cin >> a; t.push_back(a); intervals.push_back({i + 1, {0,1}}); } for (auto a = intervals.begin(); a != intervals.end(); a++) itory.push_back(a), change(a); vector<pair<int, int>> doba; vector<ll> res(m); rep(i, 0, m) { int d; cin >> d; doba.push_back({d, i}); } sort(doba.begin(), doba.end()); rep(ii, 0, m) { /* for (auto a = intervals.begin(); a != intervals.end(); a++) cout << "a"<<a->first << " " << a->second.first << " " << a->second.second << " ("<<contrib(a).first<<" "<<contrib(a).second<<") "<<"\n"; cout << "\n";*/ ll d = doba[ii].first; while (events.size() && events.begin()->first <= d) { auto a = *events.begin(); events.erase(a); /* cout << a.second << " " << a.first << " ("<<sum.first<<" "<<sum.second<<" )"<<"\n";*/ merge(itory[a.second]); } res[doba[ii].second] = sum.first + sum.second * d; } rep(i, 0, m) cout << res[i] << "\n"; }
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (int i = (a); i < (b); i++) #define per(i, a, b) for (int i = (b) - 1; i <= (a); i--) typedef list<pair<int,pair<ll,int>>> af; int n, m; af intervals; vector <ll> t; vector <af::iterator> itory; set<pair<ll, int>> events; pair <ll, ll> sum = {0, 0}; ll inf = 1e15; void change(af::iterator a, bool erase = false) { int ia = a->first; events.erase({a->second.first, ia}); if (erase) { a->second.first = -inf; return; } a->second.first = inf; auto b = a; b++; if (b != intervals.end()) { ll start = t[a->first]; ll end = t[b->first]; ll cnt = a->second.second; ll timemerge = (end - start + cnt - 1) / cnt; a->second.first = timemerge; } if (a->second.first != inf) events.insert({a->second.first, ia}); } pair <ll, ll> contrib(af::iterator a) { ll cnt = a->second.second; ll offset = t[a->first]; return {offset * cnt, cnt * (cnt - 1) / 2}; } void merge(af::iterator a) { af::iterator b = a; b++; assert(b != intervals.end()); sum.first -= contrib(b).first + contrib(a).first; sum.second -= contrib(b).second + contrib(a).second; a->second.second += b->second.second; change(b, true); intervals.erase(b); change(a); sum = {sum.first + contrib(a).first, sum.second + contrib(a).second}; } int main(void) { ios::sync_with_stdio(false); cin >> n >> m; t = {0}; intervals = {{0, {0,1}}}; rep(i, 0, n) { ll a; cin >> a; t.push_back(a); intervals.push_back({i + 1, {0,1}}); } for (auto a = intervals.begin(); a != intervals.end(); a++) itory.push_back(a), change(a); vector<pair<int, int>> doba; vector<ll> res(m); rep(i, 0, m) { int d; cin >> d; doba.push_back({d, i}); } sort(doba.begin(), doba.end()); rep(ii, 0, m) { /* for (auto a = intervals.begin(); a != intervals.end(); a++) cout << "a"<<a->first << " " << a->second.first << " " << a->second.second << " ("<<contrib(a).first<<" "<<contrib(a).second<<") "<<"\n"; cout << "\n";*/ ll d = doba[ii].first; while (events.size() && events.begin()->first <= d) { auto a = *events.begin(); events.erase(a); /* cout << a.second << " " << a.first << " ("<<sum.first<<" "<<sum.second<<" )"<<"\n";*/ merge(itory[a.second]); } res[doba[ii].second] = sum.first + sum.second * d; } rep(i, 0, m) cout << res[i] << "\n"; } |