#include <bits/stdc++.h>
using namespace std;
#ifndef LOCAL
#include "nile.h"
#endif
const int N = 100'000 + 10;
struct DSU {
int id[N];
DSU() {
memset(id, -1, sizeof id);
}
int root(int u) { return id[u] < 0 ? u : id[u] = root(id[u]); }
void add(int u, int v) {
u = root(u); v = root(v);
if (u == v) return;
id[u] += id[v];
id[v] = u;
}
} fwd, bwd;
int n;
struct Artifact {
int w, a, b;
} p;
const long long MAX = 1'000'000'000'000'000;
struct IT {
long long f[N << 2];
IT() {
memset(f, 14, sizeof f);
}
void update(int s, int l, int r, int p, int x) {
if (p < l || p > r) return;
if (l == r) {
f[s] = x;
return;
}
int mid = (l + r) >> 1;
update(s << 1, l, mid, p, x); update(s << 1 | 1, mid + 1, r, p, x);
f[s] = min(f[s << 1], f[s << 1 | 1]);
}
long long query(int s, int l, int r, int u, int v) {
if (v < l || u > r) return MAX;
if (u <= l && r <= v) return f[s];
int mid = (l + r) >> 1;
return min(query(s << 1, l, mid, u, v), query(s << 1 | 1, mid + 1, r, u, v));
}
} T[2], D[2];
using Tp = tuple<int, int, int>;
priority_queue<Tp, vector<Tp>, greater<>> s;
using pii = pair<int, int>;
priority_queue<pii, vector<pii>, greater<>> f[2];
vector<long long> calculate_costs(vector<int> W, vector<int> A,
vector<int> B, vector<int> E) {
int n = W.size();
vector<Artifact> p(n);
for (int i = 0; i < n; ++i) p[i] = {W[i], A[i], B[i]};
int q = E.size();
vector<pair<int, int>> queries(q);
for (int i = 0; i < q; ++i) queries[i] = {E[i], i};
vector<long long> answer(q);
sort(queries.begin(), queries.end());
sort(p.begin(), p.end(), [&](const auto& a, const auto& b) { return a.w > b.w; });
vector<long long> d(n);
for (int i = 0; i < n; ++i) {
if (i) d[i] = d[i - 1];
d[i] += p[i].b;
}
auto cal = [&](int l, int r) {
if (r - l + 1 == 1) return 1ll * p[l].a;
if (r - l + 1 == 2) return 1ll * p[l].b + p[r].b;
long long value = d[r] - d[l] + p[l].b;
if (!((r - l + 1) & 1)) return value;
int type = l & 1;
return value + min(T[type].query(1, 0, n - 1, l, r),
D[type ^ 1].query(1, 0, n - 1, l + 1, r - 1));
};
for (int i = 1; i < n; ++i) s.push(make_tuple(p[i - 1].w - p[i].w, i - 1, i));
for (int i = 1; i < n - 1; ++i) f[i & 1].push(make_pair(p[i - 1].w - p[i + 1].w, i));
for (int i = 0; i < n; ++i) T[i & 1].update(1, 0, n - 1, i, p[i].a - p[i].b);
long long total = 0;
for (int i = 0; i < n; ++i) total += cal(i, i);
// cerr << total << "\n";
for (int i = 0; i < q; ++i) {
const auto& [d, idx] = queries[i];
{ //update
while (s.size() && get<0>(s.top()) <= d) {
auto [cmp, r1, l2] = s.top(); s.pop();
int l1 = bwd.root(r1), r2 = fwd.root(l2);
total -= cal(l1, r1);
total -= cal(l2, r2);
bwd.add(r1, l2);
fwd.add(l2, r1);
int l = bwd.root(r1), r = fwd.root(l2);
total += cal(l, r);
}
for (int type = 0; type <= 1; ++type) {
while (f[type].size() && get<0>(f[type].top()) <= d) {
auto [cmp, pos] = f[type].top(); f[type].pop();
int l = bwd.root(pos), r = fwd.root(pos);
total -= cal(l, r);
D[type].update(1, 0, n - 1, pos, p[pos].a - p[pos].b);
total += cal(l, r);
}
}
}
answer[idx] = total;
}
return answer;
}
#ifdef LOCAL
int32_t main() {
int N;
assert(1 == scanf("%d", &N));
std::vector<int> W(N), A(N), B(N);
for (int i = 0; i < N; i++)
assert(3 == scanf("%d%d%d", &W[i], &A[i], &B[i]));
int Q;
assert(1 == scanf("%d", &Q));
std::vector<int> E(Q);
for (int j = 0; j < Q; j++)
assert(1 == scanf("%d", &E[j]));
fclose(stdin);
std::vector<long long> R = calculate_costs(W, A, B, E);
int S = (int)R.size();
for (int j = 0; j < S; j++)
printf("%lld\n", R[j]);
fclose(stdout);
return 0;
}
#endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
13904 KB |
Output is correct |
2 |
Correct |
5 ms |
13904 KB |
Output is correct |
3 |
Correct |
4 ms |
13904 KB |
Output is correct |
4 |
Correct |
4 ms |
13904 KB |
Output is correct |
5 |
Correct |
5 ms |
13904 KB |
Output is correct |
6 |
Correct |
4 ms |
13768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
21960 KB |
Output is correct |
2 |
Correct |
112 ms |
24512 KB |
Output is correct |
3 |
Correct |
104 ms |
24512 KB |
Output is correct |
4 |
Correct |
84 ms |
24148 KB |
Output is correct |
5 |
Correct |
92 ms |
24520 KB |
Output is correct |
6 |
Correct |
84 ms |
24500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
22216 KB |
Output is correct |
2 |
Correct |
101 ms |
21232 KB |
Output is correct |
3 |
Correct |
116 ms |
21800 KB |
Output is correct |
4 |
Correct |
110 ms |
21708 KB |
Output is correct |
5 |
Correct |
90 ms |
20980 KB |
Output is correct |
6 |
Correct |
109 ms |
21512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
13904 KB |
Output is correct |
2 |
Correct |
5 ms |
13904 KB |
Output is correct |
3 |
Correct |
4 ms |
13904 KB |
Output is correct |
4 |
Correct |
4 ms |
13904 KB |
Output is correct |
5 |
Correct |
5 ms |
13904 KB |
Output is correct |
6 |
Correct |
4 ms |
13768 KB |
Output is correct |
7 |
Correct |
4 ms |
13648 KB |
Output is correct |
8 |
Correct |
4 ms |
13904 KB |
Output is correct |
9 |
Correct |
5 ms |
13904 KB |
Output is correct |
10 |
Correct |
5 ms |
14068 KB |
Output is correct |
11 |
Correct |
5 ms |
13904 KB |
Output is correct |
12 |
Correct |
4 ms |
13648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
13904 KB |
Output is correct |
2 |
Correct |
5 ms |
13904 KB |
Output is correct |
3 |
Correct |
4 ms |
13904 KB |
Output is correct |
4 |
Correct |
4 ms |
13904 KB |
Output is correct |
5 |
Correct |
5 ms |
13904 KB |
Output is correct |
6 |
Correct |
4 ms |
13768 KB |
Output is correct |
7 |
Correct |
101 ms |
21960 KB |
Output is correct |
8 |
Correct |
112 ms |
24512 KB |
Output is correct |
9 |
Correct |
104 ms |
24512 KB |
Output is correct |
10 |
Correct |
84 ms |
24148 KB |
Output is correct |
11 |
Correct |
92 ms |
24520 KB |
Output is correct |
12 |
Correct |
84 ms |
24500 KB |
Output is correct |
13 |
Correct |
104 ms |
22216 KB |
Output is correct |
14 |
Correct |
101 ms |
21232 KB |
Output is correct |
15 |
Correct |
116 ms |
21800 KB |
Output is correct |
16 |
Correct |
110 ms |
21708 KB |
Output is correct |
17 |
Correct |
90 ms |
20980 KB |
Output is correct |
18 |
Correct |
109 ms |
21512 KB |
Output is correct |
19 |
Correct |
4 ms |
13648 KB |
Output is correct |
20 |
Correct |
4 ms |
13904 KB |
Output is correct |
21 |
Correct |
5 ms |
13904 KB |
Output is correct |
22 |
Correct |
5 ms |
14068 KB |
Output is correct |
23 |
Correct |
5 ms |
13904 KB |
Output is correct |
24 |
Correct |
4 ms |
13648 KB |
Output is correct |
25 |
Correct |
122 ms |
23744 KB |
Output is correct |
26 |
Correct |
114 ms |
24604 KB |
Output is correct |
27 |
Correct |
135 ms |
23340 KB |
Output is correct |
28 |
Correct |
127 ms |
20620 KB |
Output is correct |
29 |
Correct |
99 ms |
23488 KB |
Output is correct |
30 |
Correct |
120 ms |
23084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
22216 KB |
Output is correct |
2 |
Correct |
101 ms |
21232 KB |
Output is correct |
3 |
Correct |
116 ms |
21800 KB |
Output is correct |
4 |
Correct |
110 ms |
21708 KB |
Output is correct |
5 |
Correct |
90 ms |
20980 KB |
Output is correct |
6 |
Correct |
109 ms |
21512 KB |
Output is correct |
7 |
Correct |
110 ms |
23668 KB |
Output is correct |
8 |
Correct |
112 ms |
25528 KB |
Output is correct |
9 |
Correct |
122 ms |
22720 KB |
Output is correct |
10 |
Correct |
135 ms |
24740 KB |
Output is correct |
11 |
Correct |
126 ms |
24524 KB |
Output is correct |
12 |
Correct |
127 ms |
22976 KB |
Output is correct |
13 |
Correct |
124 ms |
22712 KB |
Output is correct |
14 |
Correct |
106 ms |
25040 KB |
Output is correct |
15 |
Correct |
126 ms |
24512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13648 KB |
Output is correct |
2 |
Correct |
6 ms |
13904 KB |
Output is correct |
3 |
Correct |
5 ms |
13904 KB |
Output is correct |
4 |
Correct |
4 ms |
13904 KB |
Output is correct |
5 |
Correct |
4 ms |
13904 KB |
Output is correct |
6 |
Correct |
5 ms |
13904 KB |
Output is correct |
7 |
Correct |
4 ms |
13768 KB |
Output is correct |
8 |
Correct |
101 ms |
21960 KB |
Output is correct |
9 |
Correct |
112 ms |
24512 KB |
Output is correct |
10 |
Correct |
104 ms |
24512 KB |
Output is correct |
11 |
Correct |
84 ms |
24148 KB |
Output is correct |
12 |
Correct |
92 ms |
24520 KB |
Output is correct |
13 |
Correct |
84 ms |
24500 KB |
Output is correct |
14 |
Correct |
104 ms |
22216 KB |
Output is correct |
15 |
Correct |
101 ms |
21232 KB |
Output is correct |
16 |
Correct |
116 ms |
21800 KB |
Output is correct |
17 |
Correct |
110 ms |
21708 KB |
Output is correct |
18 |
Correct |
90 ms |
20980 KB |
Output is correct |
19 |
Correct |
109 ms |
21512 KB |
Output is correct |
20 |
Correct |
4 ms |
13648 KB |
Output is correct |
21 |
Correct |
4 ms |
13904 KB |
Output is correct |
22 |
Correct |
5 ms |
13904 KB |
Output is correct |
23 |
Correct |
5 ms |
14068 KB |
Output is correct |
24 |
Correct |
5 ms |
13904 KB |
Output is correct |
25 |
Correct |
4 ms |
13648 KB |
Output is correct |
26 |
Correct |
122 ms |
23744 KB |
Output is correct |
27 |
Correct |
114 ms |
24604 KB |
Output is correct |
28 |
Correct |
135 ms |
23340 KB |
Output is correct |
29 |
Correct |
127 ms |
20620 KB |
Output is correct |
30 |
Correct |
99 ms |
23488 KB |
Output is correct |
31 |
Correct |
120 ms |
23084 KB |
Output is correct |
32 |
Correct |
110 ms |
23668 KB |
Output is correct |
33 |
Correct |
112 ms |
25528 KB |
Output is correct |
34 |
Correct |
122 ms |
22720 KB |
Output is correct |
35 |
Correct |
135 ms |
24740 KB |
Output is correct |
36 |
Correct |
126 ms |
24524 KB |
Output is correct |
37 |
Correct |
127 ms |
22976 KB |
Output is correct |
38 |
Correct |
124 ms |
22712 KB |
Output is correct |
39 |
Correct |
106 ms |
25040 KB |
Output is correct |
40 |
Correct |
126 ms |
24512 KB |
Output is correct |
41 |
Correct |
109 ms |
23488 KB |
Output is correct |
42 |
Correct |
121 ms |
23888 KB |
Output is correct |
43 |
Correct |
143 ms |
22720 KB |
Output is correct |
44 |
Correct |
140 ms |
26048 KB |
Output is correct |
45 |
Correct |
137 ms |
26028 KB |
Output is correct |
46 |
Correct |
169 ms |
26048 KB |
Output is correct |
47 |
Correct |
152 ms |
26048 KB |
Output is correct |
48 |
Correct |
126 ms |
26568 KB |
Output is correct |
49 |
Correct |
152 ms |
26048 KB |
Output is correct |