#include <iostream> #include <ios> #include <algorithm> using namespace std; int n, m; typedef long long int ll; ll t[200001]; int d[200001]; int idx[200001]; ll v[200001]; struct P; struct G { ll sum; int start; int end; P *p; }; struct P { ll l, m; bool active; G *g1, *g2; }; inline ll f(ll x) { return x * (x-1) / 2; } void merge(G &g1, G &g2, ll &delta_sum, ll &sum) { delta_sum -= f(g1.end - g1.start) + f(g2.end - g2.start); sum -= g1.sum + g2.sum; g1.end = g2.end; g1.sum += g2.sum + (g2.end - g2.start) * (t[g2.start] - t[g1.start]); delta_sum += f(g1.end - g1.start); sum += g1.sum; if (g2.p != nullptr) { g2.p->active = false; g1.p->l = t[g2.p->g2->start] - t[g1.start]; g1.p->m = g1.end - g1.start; g1.p->g2 = g2.p->g2; } else { g1.p->active = false; g1.p = nullptr; } } G g[200001]; P p2[200001]; P *p[200001]; bool cmp(int i, int j) { return d[i] < d[j]; } bool cmp2(const P *p1, const P *p2) { return p1->l * p2->m > p1->m * p2->l; } int main() { std::ios_base::sync_with_stdio(false); cin >> n >> m; for(int i=1; i <= n; i++) cin >> t[i]; for(int i=0; i < m; i++) { cin >> d[i]; idx[i] = i; } for(int i=0; i<=n; i++) { g[i].start = i; g[i].end = i+1; g[i].sum = 0; if (i<n) { g[i].p = &p2[i]; p2[i].active = true; p2[i].g1 = &g[i]; p2[i].g2 = &g[i+1]; p2[i].l = t[i+1] - t[i]; p2[i].m = 1; p[i] = &p2[i]; } else g[i].p = nullptr; } sort(idx, idx + m, cmp); make_heap(p, p + (n - 1), cmp2); ll delta_sum = 0; ll sum = 0; int i = n; for(int j=0; j < m; j++) { ll delta = d[idx[j]]; while( i > 0 && p[0]->m * delta > p[0]->l) { if (!p[0]->active) { pop_heap(p, p + i, cmp2); i --; } else { pop_heap(p, p + i, cmp2); merge(*p[i-1]->g1, *p[i-1]->g2, delta_sum, sum); push_heap(p, p + i, cmp2); } } v[idx[j]] = delta_sum * delta - sum; } for(int j=0; j < m; j++) cout << v[j] << 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 117 118 119 120 121 122 123 124 125 126 | #include <iostream> #include <ios> #include <algorithm> using namespace std; int n, m; typedef long long int ll; ll t[200001]; int d[200001]; int idx[200001]; ll v[200001]; struct P; struct G { ll sum; int start; int end; P *p; }; struct P { ll l, m; bool active; G *g1, *g2; }; inline ll f(ll x) { return x * (x-1) / 2; } void merge(G &g1, G &g2, ll &delta_sum, ll &sum) { delta_sum -= f(g1.end - g1.start) + f(g2.end - g2.start); sum -= g1.sum + g2.sum; g1.end = g2.end; g1.sum += g2.sum + (g2.end - g2.start) * (t[g2.start] - t[g1.start]); delta_sum += f(g1.end - g1.start); sum += g1.sum; if (g2.p != nullptr) { g2.p->active = false; g1.p->l = t[g2.p->g2->start] - t[g1.start]; g1.p->m = g1.end - g1.start; g1.p->g2 = g2.p->g2; } else { g1.p->active = false; g1.p = nullptr; } } G g[200001]; P p2[200001]; P *p[200001]; bool cmp(int i, int j) { return d[i] < d[j]; } bool cmp2(const P *p1, const P *p2) { return p1->l * p2->m > p1->m * p2->l; } int main() { std::ios_base::sync_with_stdio(false); cin >> n >> m; for(int i=1; i <= n; i++) cin >> t[i]; for(int i=0; i < m; i++) { cin >> d[i]; idx[i] = i; } for(int i=0; i<=n; i++) { g[i].start = i; g[i].end = i+1; g[i].sum = 0; if (i<n) { g[i].p = &p2[i]; p2[i].active = true; p2[i].g1 = &g[i]; p2[i].g2 = &g[i+1]; p2[i].l = t[i+1] - t[i]; p2[i].m = 1; p[i] = &p2[i]; } else g[i].p = nullptr; } sort(idx, idx + m, cmp); make_heap(p, p + (n - 1), cmp2); ll delta_sum = 0; ll sum = 0; int i = n; for(int j=0; j < m; j++) { ll delta = d[idx[j]]; while( i > 0 && p[0]->m * delta > p[0]->l) { if (!p[0]->active) { pop_heap(p, p + i, cmp2); i --; } else { pop_heap(p, p + i, cmp2); merge(*p[i-1]->g1, *p[i-1]->g2, delta_sum, sum); push_heap(p, p + i, cmp2); } } v[idx[j]] = delta_sum * delta - sum; } for(int j=0; j < m; j++) cout << v[j] << endl; return 0; } |