#include <bits/stdc++.h> using namespace std; typedef long long int ll; struct Q { ll s, t; }; struct P { ll pos, d, res; }; inline bool sort_pos(P a, P b) { return a.pos < b.pos; } inline bool sort_d(P a, P b) { return a.d < b.d; } ll start[200001]; int main() { ios_base::sync_with_stdio(false); ll n, m; cin >> n >> m; vector<Q> data; Q tmp; cin >> tmp.t; tmp.s = 1; ll ssum = 0, scalar = 0, sum = 0, abel = 0; for(ll i = 1; i < n; i++) { ll a; cin >> a; if(a == tmp.t) tmp.s++; else { data.push_back(tmp); sum += tmp.s; ssum += tmp.s * (tmp.s + 1) / 2; scalar += tmp.t * tmp.s; tmp.t = a; tmp.s = 1; } } ssum += tmp.s * (tmp.s + 1) / 2; sum += tmp.s; scalar += tmp.t * tmp.s; data.push_back(tmp); data.push_back(tmp); ll tmps = sum; for(ll i = 0; i < (ll)(data.size() - 2); i++) { tmps -= data[i].s; abel += data[i].s * tmps; } vector<P> query; for(ll j = 0; j < m; j++) { P tmp; tmp.pos = j; cin >> tmp.d; query.push_back(tmp); } bool was = false; sort(query.begin(), query.end(), sort_d); ll res = 0; for(ll j = 0; j < m; j++) { if(j == 0) { ll d = query[j].d; res = ssum * d - scalar; start[0] = max(data[0].t - d, 0ll); for(ll i = 0; i < (ll)(data.size() - 1); i++) { ll si = data[i].s; res += si * start[i]; start[i + 1] = max(start[i] + d * si, data[i + 1].t - d); } query[j].res = res; } else if(!was) { ll d = query[j].d; res = ssum * d - scalar; start[0] = max(data[0].t - d, 0ll); int ctr = 0; for(ll i = 0; i < (ll)(data.size() - 1); i++) { ll si = data[i].s; res += si * start[i]; if(start[i] + d * si > data[i + 1].t - d) ctr++; start[i + 1] = max(start[i] + d * si, data[i + 1].t - d); } if(ctr == n) was = true; query[j].res = res; } else { ll d = query[j].d; res = ssum * d - scalar; start[0] = max(data[0].t - d, 0ll); res += start[0] * sum + d * abel; query[j].res = res; } } sort(query.begin(), query.end(), sort_pos); for(ll j = 0; j < m; j++) { cout << query[j].res << endl; } 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 | #include <bits/stdc++.h> using namespace std; typedef long long int ll; struct Q { ll s, t; }; struct P { ll pos, d, res; }; inline bool sort_pos(P a, P b) { return a.pos < b.pos; } inline bool sort_d(P a, P b) { return a.d < b.d; } ll start[200001]; int main() { ios_base::sync_with_stdio(false); ll n, m; cin >> n >> m; vector<Q> data; Q tmp; cin >> tmp.t; tmp.s = 1; ll ssum = 0, scalar = 0, sum = 0, abel = 0; for(ll i = 1; i < n; i++) { ll a; cin >> a; if(a == tmp.t) tmp.s++; else { data.push_back(tmp); sum += tmp.s; ssum += tmp.s * (tmp.s + 1) / 2; scalar += tmp.t * tmp.s; tmp.t = a; tmp.s = 1; } } ssum += tmp.s * (tmp.s + 1) / 2; sum += tmp.s; scalar += tmp.t * tmp.s; data.push_back(tmp); data.push_back(tmp); ll tmps = sum; for(ll i = 0; i < (ll)(data.size() - 2); i++) { tmps -= data[i].s; abel += data[i].s * tmps; } vector<P> query; for(ll j = 0; j < m; j++) { P tmp; tmp.pos = j; cin >> tmp.d; query.push_back(tmp); } bool was = false; sort(query.begin(), query.end(), sort_d); ll res = 0; for(ll j = 0; j < m; j++) { if(j == 0) { ll d = query[j].d; res = ssum * d - scalar; start[0] = max(data[0].t - d, 0ll); for(ll i = 0; i < (ll)(data.size() - 1); i++) { ll si = data[i].s; res += si * start[i]; start[i + 1] = max(start[i] + d * si, data[i + 1].t - d); } query[j].res = res; } else if(!was) { ll d = query[j].d; res = ssum * d - scalar; start[0] = max(data[0].t - d, 0ll); int ctr = 0; for(ll i = 0; i < (ll)(data.size() - 1); i++) { ll si = data[i].s; res += si * start[i]; if(start[i] + d * si > data[i + 1].t - d) ctr++; start[i + 1] = max(start[i] + d * si, data[i + 1].t - d); } if(ctr == n) was = true; query[j].res = res; } else { ll d = query[j].d; res = ssum * d - scalar; start[0] = max(data[0].t - d, 0ll); res += start[0] * sum + d * abel; query[j].res = res; } } sort(query.begin(), query.end(), sort_pos); for(ll j = 0; j < m; j++) { cout << query[j].res << endl; } return 0; } |