#include <cstdio> #include <algorithm> #include <set> #include <cmath> using namespace std; typedef long long ll; const int MAXN = 1 << 18; const int MAXD = 1000000; int p[MAXN]; int w[MAXN]; ll c[MAXN]; int l[MAXN]; int e[MAXN]; void initFindAndUnion(int n) { for (int i = 0; i <= n; ++i) { p[i] = i; l[i] = i; e[i] = i; c[i] = 1; } } int rep(int x) { if (p[x] == x) return x; return p[x] = rep(p[x]); } void uni(int x, int y) { x = rep(x); y = rep(y); if (w[x] <= w[y]) swap(x, y); p[y] = x; c[x] += c[y]; l[x] = min(l[x], l[y]); e[x] = max(e[x], e[y]); if (w[x] == w[y]) w[x]++; } struct merging { ll d; int j; merging(ll a, int b) : d(a), j(b) {} }; bool operator<(const merging& a, const merging& b) { return (a.d == b.d ? a.j < b.j : a.d < b.d); } ll t[MAXN]; ll dp[MAXD + 1]; void initMerging(set<merging>& q, int n) { for (int i = 0; i < n; ++i) { ll delta = t[i + 1] - t[i]; q.insert(merging(delta, i)); //printf("# added merge at d = %lld\n", delta); } } void doit(int n) { set<merging> q; initMerging(q, n); initFindAndUnion(n); ll r = 0; for (int i = 0; i <= MAXD; ++i) { /*if (i < 30) printf("# currently d = %d\n", i);*/ if (i > 0) dp[i] += dp[i - 1]; dp[i] += r; while (q.begin()->d <= i) { merging mer = *q.begin(); q.erase(q.begin()); int x = rep(mer.j); int y = rep(mer.j + 1); if (x != y) { // printf("# merging of %d and %d\n", mer.j, mer.j + 1); ll dist = t[l[x]] + i * (c[x] - 1) - t[l[y]] + i; r += c[x] * c[y]; dp[i] += dist * c[y]; uni(x, y); int z = e[rep(x)]; if (z < n) { int rz = rep(z); int rzp = rep(z + 1); dist = t[l[rzp]] - t[l[rz]]; dist = ceil((double) dist / c[rz]); q.insert(merging(dist, z)); } } } } // printf("# r = %lld\n", r); } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { scanf("%lld", t + i); } doit(n); for (int i = 0; i < m; ++i) { int d; scanf("%d", &d); printf("%lld\n", dp[d]); } /*printf("###\n"); for (int i = 0; i <= 30; ++i) printf("%lld ", dp[i]); printf("\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 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | #include <cstdio> #include <algorithm> #include <set> #include <cmath> using namespace std; typedef long long ll; const int MAXN = 1 << 18; const int MAXD = 1000000; int p[MAXN]; int w[MAXN]; ll c[MAXN]; int l[MAXN]; int e[MAXN]; void initFindAndUnion(int n) { for (int i = 0; i <= n; ++i) { p[i] = i; l[i] = i; e[i] = i; c[i] = 1; } } int rep(int x) { if (p[x] == x) return x; return p[x] = rep(p[x]); } void uni(int x, int y) { x = rep(x); y = rep(y); if (w[x] <= w[y]) swap(x, y); p[y] = x; c[x] += c[y]; l[x] = min(l[x], l[y]); e[x] = max(e[x], e[y]); if (w[x] == w[y]) w[x]++; } struct merging { ll d; int j; merging(ll a, int b) : d(a), j(b) {} }; bool operator<(const merging& a, const merging& b) { return (a.d == b.d ? a.j < b.j : a.d < b.d); } ll t[MAXN]; ll dp[MAXD + 1]; void initMerging(set<merging>& q, int n) { for (int i = 0; i < n; ++i) { ll delta = t[i + 1] - t[i]; q.insert(merging(delta, i)); //printf("# added merge at d = %lld\n", delta); } } void doit(int n) { set<merging> q; initMerging(q, n); initFindAndUnion(n); ll r = 0; for (int i = 0; i <= MAXD; ++i) { /*if (i < 30) printf("# currently d = %d\n", i);*/ if (i > 0) dp[i] += dp[i - 1]; dp[i] += r; while (q.begin()->d <= i) { merging mer = *q.begin(); q.erase(q.begin()); int x = rep(mer.j); int y = rep(mer.j + 1); if (x != y) { // printf("# merging of %d and %d\n", mer.j, mer.j + 1); ll dist = t[l[x]] + i * (c[x] - 1) - t[l[y]] + i; r += c[x] * c[y]; dp[i] += dist * c[y]; uni(x, y); int z = e[rep(x)]; if (z < n) { int rz = rep(z); int rzp = rep(z + 1); dist = t[l[rzp]] - t[l[rz]]; dist = ceil((double) dist / c[rz]); q.insert(merging(dist, z)); } } } } // printf("# r = %lld\n", r); } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { scanf("%lld", t + i); } doit(n); for (int i = 0; i < m; ++i) { int d; scanf("%d", &d); printf("%lld\n", dp[d]); } /*printf("###\n"); for (int i = 0; i <= 30; ++i) printf("%lld ", dp[i]); printf("\n");*/ } |