#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
template <class T, class... U> void bug_h(const T& t, const U&... u) { cerr << t; ((cerr << " | " << u), ...); cerr << endl; }
#define bug(...) cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: ", bug_h(__VA_ARGS__)
#else
#define cerr if (0) cerr
#define bug(...)
#endif
#define ar array
#define all(v) std::begin(v), std::end(v)
#define sz(v) int(std::size(v))
typedef long long i64;
typedef vector<int> vi;
typedef pair<int, int> pi;
int N;
vi A;
struct segt {
int l, r; pi mx;
segt *lc, *rc;
segt(int l, int r) : l(l), r(r) {
if (l == r) mx = pi(A[l], -l);
else {
int m = (l + r) / 2;
lc = new segt(l, m), rc = new segt(m + 1, r);
mx = max(lc->mx, rc->mx);
}
}
pi qry(int ql, int qr) {
if (ql <= l && qr >= r) return mx;
if (qr <= lc->r) return lc->qry(ql, qr);
if (ql >= rc->l) return rc->qry(ql, qr);
return max(lc->qry(ql, qr), rc->qry(ql, qr));
}
} *tmax;
struct FT {
vector<i64> t;
FT(int n) : t(n) {}
void add(int l, int r, i64 x) {
if (l <= r) add(l, x), add(r + 1, -x);
}
void add(int i, i64 x) {
while (i < sz(t)) {
t[i] += x;
i |= i + 1;
}
}
i64 sum(int i) {
i64 x = 0;
while (i >= 0) {
x += t[i];
i &= i + 1, i--;
}
return x;
}
} *ft;
struct node {
vector<node *> kids;
vector<pi> items;
node *jump[18];
int l, r, low, hi;
};
vector<node *> who;
node *rec(node *parent, int l, int r, int upper) {
node *v = new node();
if (parent == NULL) parent = v;
v->jump[0] = parent;
for (int l = 1; l < 18; l++)
v->jump[l] = v->jump[l - 1]->jump[l - 1];
v->l = l;
v->r = r;
v->hi = upper;
int target = tmax->qry(l, r).first;
v->low = target + 1;
bug(l, r, v->low, v->hi);
vector<int> cuts; int p = l;
while (p <= r) {
auto x = tmax->qry(p, r);
x.second *= -1;
if (x.first == target) {
cuts.push_back(x.second);
p = x.second + 1;
} else break;
}
for (int j : cuts) who[j] = v;
if (l < cuts.at(0)) v->kids.push_back(rec(v, l, cuts[0] - 1, target));
for (int i = 0; i < sz(cuts) - 1; i++) if (cuts[i] + 1 < cuts[i + 1])
v->kids.push_back(rec(v, cuts[i] + 1, cuts[i + 1] - 1, target));
if (cuts.at(sz(cuts) - 1) < r) v->kids.push_back(rec(v, cuts.back() + 1, r, target));
return v;
}
i64 calc(node *v) {
vector<i64> y(sz(v->kids));
i64 tot = 0;
for (int i = 0; i < sz(v->kids); i++) {
y[i] = calc(v->kids[i]);
tot += y[i];
}
i64 add = 0;
for (auto u : v->kids) bug(u->l, u->r);
for (auto [i, w] : v->items) {
cerr << "ITEM " << i << ' ' << w << endl;
int low = 0, hi = sz(v->kids) - 1;
while (low < hi) {
int mid = (low + hi) / 2 + 1;
if (i >= v->kids[mid]->l) low = mid;
else hi = mid - 1;
}
int j = low;
i64 x = w;
bug(j);
if (0 <= j && j < sz(v->kids) && v->kids[j]->r >= i && v->kids[j]->l <= i) {
cerr << "CAUGHT " << j << "(subtract " << y[j] << ")" << endl;
x -= y[j];
}
x += ft->sum(i);
add = max(add, x);
}
for (int i = 0; i < sz(v->kids); i++) {
ft->add(v->l, v->kids[i]->l - 1, y[i]);
ft->add(v->kids[i]->r + 1, v->r, y[i]);
}
bug(v->l, v->r, v->low, v->hi, tot + add);
return tot + add;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N;
A.resize(N);
for (auto& x : A) cin >> x;
tmax = new segt(0, N - 1);
who.resize(N, NULL);
node *root = rec(NULL, 0, N - 1, N + 1);
int K;
cin >> K;
i64 sum = 0;
while (K--) {
int i, j, w;
cin >> i >> j >> w, i--;
sum += w;
node *v = who[i];
for (int l = 17; l >= 0; l--) {
assert(v != NULL);
if (v->jump[l]->low <= j)
v = v->jump[l];
}
assert(v != NULL);
assert(v->low <= j && j <= v->hi);
v->items.push_back({i, w});
bug(i, j, w, v->l, v->r, v->low, v->hi);
}
ft = new FT(N);
cout << sum - calc(root) << '\n';
}
Compilation message
constellation3.cpp: In function 'i64 calc(node*)':
constellation3.cpp:109:15: warning: unused variable 'u' [-Wunused-variable]
109 | for (auto u : v->kids) bug(u->l, u->r);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
1116 KB |
Output is correct |
25 |
Correct |
2 ms |
996 KB |
Output is correct |
26 |
Correct |
2 ms |
1116 KB |
Output is correct |
27 |
Correct |
3 ms |
1096 KB |
Output is correct |
28 |
Correct |
2 ms |
1116 KB |
Output is correct |
29 |
Correct |
2 ms |
984 KB |
Output is correct |
30 |
Correct |
2 ms |
1116 KB |
Output is correct |
31 |
Correct |
3 ms |
1204 KB |
Output is correct |
32 |
Correct |
3 ms |
1372 KB |
Output is correct |
33 |
Correct |
2 ms |
1372 KB |
Output is correct |
34 |
Correct |
2 ms |
1372 KB |
Output is correct |
35 |
Correct |
2 ms |
1372 KB |
Output is correct |
36 |
Correct |
1 ms |
600 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
2 ms |
1628 KB |
Output is correct |
39 |
Correct |
2 ms |
980 KB |
Output is correct |
40 |
Correct |
3 ms |
1372 KB |
Output is correct |
41 |
Correct |
2 ms |
976 KB |
Output is correct |
42 |
Correct |
2 ms |
860 KB |
Output is correct |
43 |
Correct |
2 ms |
1368 KB |
Output is correct |
44 |
Correct |
2 ms |
856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
1116 KB |
Output is correct |
25 |
Correct |
2 ms |
996 KB |
Output is correct |
26 |
Correct |
2 ms |
1116 KB |
Output is correct |
27 |
Correct |
3 ms |
1096 KB |
Output is correct |
28 |
Correct |
2 ms |
1116 KB |
Output is correct |
29 |
Correct |
2 ms |
984 KB |
Output is correct |
30 |
Correct |
2 ms |
1116 KB |
Output is correct |
31 |
Correct |
3 ms |
1204 KB |
Output is correct |
32 |
Correct |
3 ms |
1372 KB |
Output is correct |
33 |
Correct |
2 ms |
1372 KB |
Output is correct |
34 |
Correct |
2 ms |
1372 KB |
Output is correct |
35 |
Correct |
2 ms |
1372 KB |
Output is correct |
36 |
Correct |
1 ms |
600 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
2 ms |
1628 KB |
Output is correct |
39 |
Correct |
2 ms |
980 KB |
Output is correct |
40 |
Correct |
3 ms |
1372 KB |
Output is correct |
41 |
Correct |
2 ms |
976 KB |
Output is correct |
42 |
Correct |
2 ms |
860 KB |
Output is correct |
43 |
Correct |
2 ms |
1368 KB |
Output is correct |
44 |
Correct |
2 ms |
856 KB |
Output is correct |
45 |
Correct |
306 ms |
79884 KB |
Output is correct |
46 |
Correct |
312 ms |
79020 KB |
Output is correct |
47 |
Correct |
301 ms |
78156 KB |
Output is correct |
48 |
Correct |
311 ms |
79724 KB |
Output is correct |
49 |
Correct |
289 ms |
77140 KB |
Output is correct |
50 |
Correct |
289 ms |
77200 KB |
Output is correct |
51 |
Correct |
290 ms |
77432 KB |
Output is correct |
52 |
Correct |
326 ms |
79188 KB |
Output is correct |
53 |
Correct |
293 ms |
78924 KB |
Output is correct |
54 |
Correct |
693 ms |
110892 KB |
Output is correct |
55 |
Correct |
549 ms |
100180 KB |
Output is correct |
56 |
Correct |
602 ms |
94804 KB |
Output is correct |
57 |
Correct |
469 ms |
90784 KB |
Output is correct |
58 |
Correct |
121 ms |
31072 KB |
Output is correct |
59 |
Correct |
124 ms |
31224 KB |
Output is correct |
60 |
Correct |
275 ms |
127848 KB |
Output is correct |
61 |
Correct |
236 ms |
63588 KB |
Output is correct |
62 |
Correct |
587 ms |
99728 KB |
Output is correct |
63 |
Correct |
203 ms |
55600 KB |
Output is correct |
64 |
Correct |
237 ms |
61256 KB |
Output is correct |
65 |
Correct |
625 ms |
100812 KB |
Output is correct |
66 |
Correct |
203 ms |
55000 KB |
Output is correct |