#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(V) V.begin(),V.end()
ll W[400010], A[200010], seg[524288], rt[524288], as[1048576], lc[1048576], par[400010], hlnum[400010], hlseg[400010], hltop[400010], hlpar[400010];
vector<pair<int, int>> H;
vector<int> T[400010];
tuple<int, int, int> R[400010];
//hlnumはhl分解後の「セグ木上での番号」、hlsegはhl分解後のセグ木のx番目の「頂点番号」、hltopはその頂点が属する鎖の一番根に近いやつの「頂点番号」、hlparは鎖の繋がっている親ノードの「頂点番号」
int wgh(int x) {
int a = 1;
for (int j : T[x])a += wgh(j);
W[x] = a;
return a;
}
void hld(int x, int me, int& num) {
int mw = 0, ms = 0;
hlnum[x] = num;
hlseg[num] = x;
hltop[x] = me;
hlpar[x] = hlpar[me];
if (T[x].size() == 0)return;
for (int i : T[x]) {
if (W[i] > mw)ms = i, mw = W[i];
}
num++;
hld(ms, me, num);
for (int i : T[x]) {
if (i != ms) {
hlpar[i] = x;
num++;
hld(i, i, num);
}
}
}
void raq_as(int s, int t, ll v, int k = 1, int l = 0, int r = 524287) {
if (t < s)return;
if (t < l || r < s)return;
if (s <= l && r <= t) {
as[k] += v;
return;
}
raq_as(s, t, v, k * 2, l, (l + r) / 2);
raq_as(s, t, v, k * 2 + 1, (l + r) / 2 + 1, r);
}
ll rsq_lc(int s, int t, int k = 1, int l = 0, int r = 524287) {
if (t < s)return 0;
if (t < l || r < s)return 0;
if (s <= l && r <= t) {
return lc[k];
}
ll lv = rsq_lc(s, t, k * 2, l, (l + r) / 2);
ll rv = rsq_lc(s, t, k * 2 + 1, (l + r) / 2 + 1, r);
return lv + rv;
}
void add_lc(int x, ll v) {
x += 524288;
while (x) {
lc[x] += v;
x /= 2;
}
return;
}
ll get_as(int x) {
x += 524288;
ll ans = 0;
while (x) {
ans += as[x];
x /= 2;
}
return ans;
}
void solve2(int x, ll dif) {
int tp = hlnum[x];
int tpp = hlnum[hltop[x]];
add_lc(tp, dif);//lcへの加算
raq_as(tpp, tp, dif);//asの加算
if (hlpar[x] == 0)return;
solve2(hlpar[x], dif);
}
void solve(int x, int y, ll val, int exc) {
int tp = hlnum[x];//セグ木上での番号
int tpp = hlnum[hltop[x]];//鎖のtopのセグ木上での番号
if (get<2>(R[hltop[x]]) < y) {
val += rsq_lc(tpp, tp);//lcの和
val -= get_as(exc);
if (T[x].size()) {
val += get_as(tp + 1);//asの点取得
}
solve(hlpar[x], y, val, tpp);
}
else {
int ok = tpp, ng = tp + 1, mid;
while (ok + 1 != ng) {
mid = (ok + ng) / 2;
if (get<2>(R[hlseg[mid]]) < y) {
ng = mid;
}
else {
ok = mid;
}
}
val += rsq_lc(ok, tp);//lcの和
val -= get_as(exc);
if (T[x].size())
val += get_as(tp + 1);
ll alr = get_as(ok);
if (alr < val) {
raq_as(tpp, ok, val - alr);//asの加算
if (hlpar[x] == 0)return;
solve2(hlpar[x], val - alr);
}
}
}
int rmq(int s, int t, int k = 1, int l = 0, int r = 262143) {
if (t < l || r < s)return INT32_MIN;
if (s <= l && r <= t) {
return seg[k];
}
int lv = rmq(s, t, k * 2, l, (l + r) / 2);
int rv = rmq(s, t, k * 2 + 1, (l + r) / 2 + 1, r);
return max(lv, rv);
}
void ruq(int s, int t, int v, int k = 1, int l = 0, int r = 262143) {
if (t < l || r < s)return;
if (s <= l && r <= t) {
rt[k] = v;
return;
}
if (rt[k]) {
rt[k * 2] = rt[k];
rt[k * 2 + 1] = rt[k];
rt[k] = 0;
}
ruq(s, t, v, k * 2, l, (l + r) / 2);
ruq(s, t, v, k * 2 + 1, (l + r) / 2 + 1, r);
}
int gets(int x) {
x += 262144;
int ans = 0;
while (x) {
if (rt[x])ans = rt[x];
x /= 2;
}
return ans;
}
void make_skytree(int l, int r, int& num) {
int m = rmq(l, r), me = num;
pair<int, int> ser = { m,l };
auto it = lower_bound(all(H), ser);
ser = { m,r + 1 };
auto it2 = lower_bound(all(H), ser);
int li = l, t;
for (auto i = it;;i++) {
if (i == it2)
t = r;
else {
t = (*i).second - 1;
ruq(t + 1, t + 1, me);
}
if (li <= t) {
num++;
ruq(li, t, num);
T[me].push_back(num);
R[num] = make_tuple(li, t, m);
par[num] = me;
make_skytree(li, t, num);
}
li = t + 2;
if (i == it2)break;
}
}
int main() {
for (int i = 0;i < 524288;i++)seg[i] = 0, rt[i] = 0;
for (int i = 0;i < 1048576;i++)as[i] = 0, lc[i] = 0;
for (int i = 0;i < 400010;i++)W[i] = hlnum[i] = hlseg[i] = hltop[i] = hlpar[i] = par[i] = 0;
int N, M;
cin >> N;
R[1] = make_tuple(1, N, N + 3);
for (int i = 0;i < N;i++) {
cin >> A[i];
seg[i + 262145] = A[i];
H.push_back({ A[i],i + 1 });
}
sort(all(H));
for (int i = 262143;i >= 0;i--) {
seg[i] = max(seg[i * 2], seg[i * 2 + 1]);
}
int a = 1;
make_skytree(1, N, a);
wgh(1);
a = 1;
hld(1, 1, a);
cin >> M;
ll X, Y, C, xi, alc = 0;
vector<tuple<ll, ll, ll>> V(M);
for (int i = 0;i < M;i++) {
cin >> X >> Y >> C;
xi = gets(X);
alc += C;
V[i] = make_tuple(Y, xi, C);
}
sort(all(V));
for (int i = 0;i < M;i++) {
tie(Y, xi, C) = V[i];
solve(xi, Y, C, 0);
}
cout << alc - get_as(1) << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
53120 KB |
Output is correct |
2 |
Correct |
33 ms |
53120 KB |
Output is correct |
3 |
Correct |
32 ms |
53120 KB |
Output is correct |
4 |
Correct |
35 ms |
53120 KB |
Output is correct |
5 |
Correct |
37 ms |
53112 KB |
Output is correct |
6 |
Correct |
34 ms |
53288 KB |
Output is correct |
7 |
Correct |
32 ms |
53120 KB |
Output is correct |
8 |
Correct |
33 ms |
53240 KB |
Output is correct |
9 |
Correct |
36 ms |
53216 KB |
Output is correct |
10 |
Correct |
35 ms |
53112 KB |
Output is correct |
11 |
Correct |
36 ms |
53112 KB |
Output is correct |
12 |
Correct |
33 ms |
53240 KB |
Output is correct |
13 |
Correct |
33 ms |
53112 KB |
Output is correct |
14 |
Correct |
36 ms |
53224 KB |
Output is correct |
15 |
Correct |
33 ms |
53120 KB |
Output is correct |
16 |
Correct |
40 ms |
53240 KB |
Output is correct |
17 |
Correct |
34 ms |
53248 KB |
Output is correct |
18 |
Correct |
35 ms |
53216 KB |
Output is correct |
19 |
Correct |
33 ms |
53248 KB |
Output is correct |
20 |
Correct |
32 ms |
53240 KB |
Output is correct |
21 |
Correct |
33 ms |
53112 KB |
Output is correct |
22 |
Correct |
37 ms |
53120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
53120 KB |
Output is correct |
2 |
Correct |
33 ms |
53120 KB |
Output is correct |
3 |
Correct |
32 ms |
53120 KB |
Output is correct |
4 |
Correct |
35 ms |
53120 KB |
Output is correct |
5 |
Correct |
37 ms |
53112 KB |
Output is correct |
6 |
Correct |
34 ms |
53288 KB |
Output is correct |
7 |
Correct |
32 ms |
53120 KB |
Output is correct |
8 |
Correct |
33 ms |
53240 KB |
Output is correct |
9 |
Correct |
36 ms |
53216 KB |
Output is correct |
10 |
Correct |
35 ms |
53112 KB |
Output is correct |
11 |
Correct |
36 ms |
53112 KB |
Output is correct |
12 |
Correct |
33 ms |
53240 KB |
Output is correct |
13 |
Correct |
33 ms |
53112 KB |
Output is correct |
14 |
Correct |
36 ms |
53224 KB |
Output is correct |
15 |
Correct |
33 ms |
53120 KB |
Output is correct |
16 |
Correct |
40 ms |
53240 KB |
Output is correct |
17 |
Correct |
34 ms |
53248 KB |
Output is correct |
18 |
Correct |
35 ms |
53216 KB |
Output is correct |
19 |
Correct |
33 ms |
53248 KB |
Output is correct |
20 |
Correct |
32 ms |
53240 KB |
Output is correct |
21 |
Correct |
33 ms |
53112 KB |
Output is correct |
22 |
Correct |
37 ms |
53120 KB |
Output is correct |
23 |
Correct |
40 ms |
53376 KB |
Output is correct |
24 |
Correct |
39 ms |
53376 KB |
Output is correct |
25 |
Correct |
38 ms |
53428 KB |
Output is correct |
26 |
Correct |
39 ms |
53376 KB |
Output is correct |
27 |
Correct |
39 ms |
53368 KB |
Output is correct |
28 |
Correct |
42 ms |
53368 KB |
Output is correct |
29 |
Correct |
37 ms |
53248 KB |
Output is correct |
30 |
Correct |
45 ms |
53376 KB |
Output is correct |
31 |
Correct |
43 ms |
53240 KB |
Output is correct |
32 |
Correct |
39 ms |
53496 KB |
Output is correct |
33 |
Correct |
38 ms |
53368 KB |
Output is correct |
34 |
Correct |
37 ms |
53368 KB |
Output is correct |
35 |
Correct |
37 ms |
53376 KB |
Output is correct |
36 |
Correct |
34 ms |
53240 KB |
Output is correct |
37 |
Correct |
40 ms |
53368 KB |
Output is correct |
38 |
Correct |
38 ms |
53624 KB |
Output is correct |
39 |
Correct |
44 ms |
53368 KB |
Output is correct |
40 |
Correct |
44 ms |
53496 KB |
Output is correct |
41 |
Correct |
41 ms |
53240 KB |
Output is correct |
42 |
Correct |
37 ms |
53248 KB |
Output is correct |
43 |
Correct |
46 ms |
53472 KB |
Output is correct |
44 |
Correct |
36 ms |
53272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
53120 KB |
Output is correct |
2 |
Correct |
33 ms |
53120 KB |
Output is correct |
3 |
Correct |
32 ms |
53120 KB |
Output is correct |
4 |
Correct |
35 ms |
53120 KB |
Output is correct |
5 |
Correct |
37 ms |
53112 KB |
Output is correct |
6 |
Correct |
34 ms |
53288 KB |
Output is correct |
7 |
Correct |
32 ms |
53120 KB |
Output is correct |
8 |
Correct |
33 ms |
53240 KB |
Output is correct |
9 |
Correct |
36 ms |
53216 KB |
Output is correct |
10 |
Correct |
35 ms |
53112 KB |
Output is correct |
11 |
Correct |
36 ms |
53112 KB |
Output is correct |
12 |
Correct |
33 ms |
53240 KB |
Output is correct |
13 |
Correct |
33 ms |
53112 KB |
Output is correct |
14 |
Correct |
36 ms |
53224 KB |
Output is correct |
15 |
Correct |
33 ms |
53120 KB |
Output is correct |
16 |
Correct |
40 ms |
53240 KB |
Output is correct |
17 |
Correct |
34 ms |
53248 KB |
Output is correct |
18 |
Correct |
35 ms |
53216 KB |
Output is correct |
19 |
Correct |
33 ms |
53248 KB |
Output is correct |
20 |
Correct |
32 ms |
53240 KB |
Output is correct |
21 |
Correct |
33 ms |
53112 KB |
Output is correct |
22 |
Correct |
37 ms |
53120 KB |
Output is correct |
23 |
Correct |
40 ms |
53376 KB |
Output is correct |
24 |
Correct |
39 ms |
53376 KB |
Output is correct |
25 |
Correct |
38 ms |
53428 KB |
Output is correct |
26 |
Correct |
39 ms |
53376 KB |
Output is correct |
27 |
Correct |
39 ms |
53368 KB |
Output is correct |
28 |
Correct |
42 ms |
53368 KB |
Output is correct |
29 |
Correct |
37 ms |
53248 KB |
Output is correct |
30 |
Correct |
45 ms |
53376 KB |
Output is correct |
31 |
Correct |
43 ms |
53240 KB |
Output is correct |
32 |
Correct |
39 ms |
53496 KB |
Output is correct |
33 |
Correct |
38 ms |
53368 KB |
Output is correct |
34 |
Correct |
37 ms |
53368 KB |
Output is correct |
35 |
Correct |
37 ms |
53376 KB |
Output is correct |
36 |
Correct |
34 ms |
53240 KB |
Output is correct |
37 |
Correct |
40 ms |
53368 KB |
Output is correct |
38 |
Correct |
38 ms |
53624 KB |
Output is correct |
39 |
Correct |
44 ms |
53368 KB |
Output is correct |
40 |
Correct |
44 ms |
53496 KB |
Output is correct |
41 |
Correct |
41 ms |
53240 KB |
Output is correct |
42 |
Correct |
37 ms |
53248 KB |
Output is correct |
43 |
Correct |
46 ms |
53472 KB |
Output is correct |
44 |
Correct |
36 ms |
53272 KB |
Output is correct |
45 |
Correct |
1057 ms |
72928 KB |
Output is correct |
46 |
Correct |
1012 ms |
72780 KB |
Output is correct |
47 |
Correct |
1035 ms |
72908 KB |
Output is correct |
48 |
Correct |
1010 ms |
72668 KB |
Output is correct |
49 |
Correct |
1031 ms |
72564 KB |
Output is correct |
50 |
Correct |
1027 ms |
72432 KB |
Output is correct |
51 |
Correct |
1040 ms |
72452 KB |
Output is correct |
52 |
Correct |
1066 ms |
73160 KB |
Output is correct |
53 |
Correct |
1022 ms |
72804 KB |
Output is correct |
54 |
Correct |
864 ms |
87572 KB |
Output is correct |
55 |
Correct |
893 ms |
83372 KB |
Output is correct |
56 |
Correct |
819 ms |
80996 KB |
Output is correct |
57 |
Correct |
826 ms |
79588 KB |
Output is correct |
58 |
Correct |
524 ms |
66276 KB |
Output is correct |
59 |
Correct |
536 ms |
66580 KB |
Output is correct |
60 |
Correct |
655 ms |
96100 KB |
Output is correct |
61 |
Correct |
759 ms |
70420 KB |
Output is correct |
62 |
Correct |
786 ms |
86500 KB |
Output is correct |
63 |
Correct |
671 ms |
69092 KB |
Output is correct |
64 |
Correct |
756 ms |
69988 KB |
Output is correct |
65 |
Correct |
787 ms |
86512 KB |
Output is correct |
66 |
Correct |
716 ms |
69348 KB |
Output is correct |