#include <algorithm>
#include <cstdio>
#include <queue>
#include <utility>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define MP make_pair
#define FT first
#define SD second
#define INT(x) int x; scanf("%d", &x)
typedef long long LL;
typedef vector<int> VI;
class DISJOINT {
VI link;
public:
DISJOINT(int n) : link(n) {
REP(i,n)
link[i] = i;
}
int find(int i) {
int r = i;
while (link[r] != r)
r = link[r];
while (i != r) {
int j = link[i];
link[i] = r;
i = j;
}
return i;
}
void unify(int i, int j) {
link[find(i)] = find(j);
}
};
LL t[200000];
pair<LL, int> d[200000];
LL res[200000];
DISJOINT blocks(0);
int num[200000];
struct Prio {
Prio(LL a, int b = 1) : a(a), b(b) {}
LL a;
int b;
bool operator<(const Prio& that) const {
return a * that.b > that.a * b;
}
bool operator==(const Prio& that) const {
return a * that.b == that.a * b;
}
};
Prio prio(int i) {
return Prio(t[blocks.find(i + 1)] - t[i], num[i]);
}
int main() {
INT(n);
INT(m);
REP(i,n) scanf("%lld", &t[i]);
REP(j,m) scanf("%lld", &d[j].FT);
REP(j,m) d[j].SD = j;
sort(t, t + n);
sort(d, d + m);
blocks = DISJOINT(n);
REP(i,n) t[i] = t[i];
REP(i,n) num[i] = 1;
priority_queue<pair<Prio, int> > q;
REP(i,n-1) q.push(MP(prio(i), i));
LL wait = 0, pressure = 0;
int last = 0;
REP(j,m) {
wait += pressure * (d[j].FT - last);
int first = blocks.find(0);
if (t[first] < d[j].FT) {
wait += num[first] * (d[j].FT - t[first]);
t[first] = d[j].FT;
if (first < n - 1) q.push(MP(prio(first), first));
}
while (!q.empty()) {
pair<Prio, int> x = q.top();
if (blocks.find(x.SD) != x.SD || !(x.FT == prio(x.SD))) {
q.pop();
continue;
}
if (x.FT < Prio(d[j].FT)) break;
q.pop();
int y = blocks.find(x.SD + 1);
wait += num[y] * (t[x.SD] + LL(num[x.SD]) * d[j].FT - t[y]);
pressure += LL(num[x.SD]) * num[y];
t[y] = t[x.SD];
num[y] += num[x.SD];
blocks.unify(x.SD, y);
if (y < n - 1) q.push(MP(prio(y), y));
}
res[d[j].SD] = wait;
last = d[j].FT;
}
REP(j,m) printf("%lld\n", res[j]);
}
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 | #include <algorithm> #include <cstdio> #include <queue> #include <utility> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define MP make_pair #define FT first #define SD second #define INT(x) int x; scanf("%d", &x) typedef long long LL; typedef vector<int> VI; class DISJOINT { VI link; public: DISJOINT(int n) : link(n) { REP(i,n) link[i] = i; } int find(int i) { int r = i; while (link[r] != r) r = link[r]; while (i != r) { int j = link[i]; link[i] = r; i = j; } return i; } void unify(int i, int j) { link[find(i)] = find(j); } }; LL t[200000]; pair<LL, int> d[200000]; LL res[200000]; DISJOINT blocks(0); int num[200000]; struct Prio { Prio(LL a, int b = 1) : a(a), b(b) {} LL a; int b; bool operator<(const Prio& that) const { return a * that.b > that.a * b; } bool operator==(const Prio& that) const { return a * that.b == that.a * b; } }; Prio prio(int i) { return Prio(t[blocks.find(i + 1)] - t[i], num[i]); } int main() { INT(n); INT(m); REP(i,n) scanf("%lld", &t[i]); REP(j,m) scanf("%lld", &d[j].FT); REP(j,m) d[j].SD = j; sort(t, t + n); sort(d, d + m); blocks = DISJOINT(n); REP(i,n) t[i] = t[i]; REP(i,n) num[i] = 1; priority_queue<pair<Prio, int> > q; REP(i,n-1) q.push(MP(prio(i), i)); LL wait = 0, pressure = 0; int last = 0; REP(j,m) { wait += pressure * (d[j].FT - last); int first = blocks.find(0); if (t[first] < d[j].FT) { wait += num[first] * (d[j].FT - t[first]); t[first] = d[j].FT; if (first < n - 1) q.push(MP(prio(first), first)); } while (!q.empty()) { pair<Prio, int> x = q.top(); if (blocks.find(x.SD) != x.SD || !(x.FT == prio(x.SD))) { q.pop(); continue; } if (x.FT < Prio(d[j].FT)) break; q.pop(); int y = blocks.find(x.SD + 1); wait += num[y] * (t[x.SD] + LL(num[x.SD]) * d[j].FT - t[y]); pressure += LL(num[x.SD]) * num[y]; t[y] = t[x.SD]; num[y] += num[x.SD]; blocks.unify(x.SD, y); if (y < n - 1) q.push(MP(prio(y), y)); } res[d[j].SD] = wait; last = d[j].FT; } REP(j,m) printf("%lld\n", res[j]); } |
English