#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tt;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
const int mn = 2e5 + 5;
struct item {
int core, height, lb, rb, value;
item() : core(0), height(0), lb(0), rb(0), value(0) {}
item (int l, int r) :
core(0), height(0), lb(l), rb(r), value(0) {}
item (int c, int h, int l, int r, int v) :
core(c), height(h), lb(l), rb(r), value(v) {}
bool operator < (const item &o) const {
if (rb == o.rb) return lb > o.lb;
return rb < o.rb;
}
friend istream& operator >> (istream &inp, item &cur) {
inp >> cur.core >> cur.height >> cur.value;
return inp;
}
} star[mn];
struct IT {
vector<int> lazy;
IT (int sz) : lazy(4 * sz, INT_MAX) {}
void update (int a, int b, int val, int k, int l, int r) {
if (b < l || r < a) return;
if (a <= l && r <= b) {
lazy[k] = min(lazy[k], val);
return;
}
int mid = (l + r) >> 1;
update(a, b, val, 2 * k, l, mid);
update(a, b, val, 2 * k + 1, mid + 1, r);
}
int query (int pos, int k, int l, int r) {
int ans = lazy[k], mid = (l + r) >> 1;
if (l == r) return ans;
if (l <= pos && pos <= mid)
return min(ans, query(pos, 2 * k, l, mid));
return min(ans, query(pos, 2 * k + 1, mid + 1, r));
}
} tree(mn);
struct DSU {
vector<ll> par, weight;
DSU (int sz) : par(sz + 1), weight(sz + 1) {}
pl get (int u) {
if (!par[u]) return {u, weight[u]};
pl cur = get(par[u]);
weight[u] += cur.second - weight[cur.first];
par[u] = cur.first;
return {par[u], weight[u] + weight[par[u]]};
}
void unite (int p, int u) {
p = get(p).first, u = get(u).first;
if (p != u) par[u] = p;
}
void incr (int u, ll val) { weight[u] += val; }
};
vector<int> pos[mn];
ll dp[mn], sumTree[mn];
int a[mn];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n; cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
int m; cin >> m; ll sum = 0;
for (int i = 1; i <= m; i++) {
cin >> star[i];
sum += star[i].value;
pos[star[i].core].push_back(i);
}
// construct lb
vector<pii> line; line.emplace_back(INT_MAX, 0);
for (int i = 1; i <= n; i++) {
while (line.back().first <= a[i]) line.pop_back();
line.emplace_back(a[i], i);
for (int u : pos[i]) {
auto it = lower_bound(all(line), make_pair(star[u].height, 0), greater<pii>());
star[u].lb = prev(it)->second + 1;
}
}
// construct rb
line.clear(); line.emplace_back(INT_MAX, n + 1);
for (int i = n; i >= 1; i--) {
while (line.back().first <= a[i]) line.pop_back();
line.emplace_back(a[i], i);
for (int u : pos[i]) {
auto it = lower_bound(all(line), make_pair(star[u].height, 0), greater<pii>());
star[u].rb = prev(it)->second - 1;
}
}
sort(star + 1, star + 1 + m);
// sweep ranges
vector<int> roots; IT tree(n);
DSU dsuDP(m), dsuSum(m);
for (int i = 1; i <= m; i++) {
while (roots.size() && star[i].lb <= star[roots.back()].lb) {
// i is the new parent of roots.back()
sumTree[i] += dp[roots.back()], dsuDP.unite(i, roots.back()), dsuSum.unite(i, roots.back());
roots.pop_back();
}
roots.push_back(i);
dsuSum.incr(i, sumTree[i]);
tree.update(star[i].lb, star[i].rb, i, 1, 1, n);
int j = tree.query(star[i].core, 1, 1, n);
dp[i] = max(sumTree[i], dsuSum.get(j).second - dsuDP.get(j).second + star[i].value);
dsuDP.incr(i, dp[i]);
}
ll ans = 0;
for (int u : roots) ans += dp[u];
cout << sum - ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
13916 KB |
Output is correct |
2 |
Correct |
2 ms |
13920 KB |
Output is correct |
3 |
Correct |
3 ms |
13916 KB |
Output is correct |
4 |
Correct |
3 ms |
13916 KB |
Output is correct |
5 |
Correct |
3 ms |
13916 KB |
Output is correct |
6 |
Correct |
4 ms |
13916 KB |
Output is correct |
7 |
Correct |
3 ms |
13916 KB |
Output is correct |
8 |
Correct |
3 ms |
13916 KB |
Output is correct |
9 |
Correct |
3 ms |
13916 KB |
Output is correct |
10 |
Correct |
3 ms |
13916 KB |
Output is correct |
11 |
Correct |
3 ms |
13916 KB |
Output is correct |
12 |
Correct |
3 ms |
13916 KB |
Output is correct |
13 |
Correct |
3 ms |
13916 KB |
Output is correct |
14 |
Correct |
6 ms |
13912 KB |
Output is correct |
15 |
Correct |
3 ms |
13740 KB |
Output is correct |
16 |
Correct |
3 ms |
13916 KB |
Output is correct |
17 |
Correct |
4 ms |
13916 KB |
Output is correct |
18 |
Correct |
2 ms |
13916 KB |
Output is correct |
19 |
Correct |
3 ms |
13916 KB |
Output is correct |
20 |
Correct |
3 ms |
13916 KB |
Output is correct |
21 |
Correct |
3 ms |
13916 KB |
Output is correct |
22 |
Correct |
3 ms |
13916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
13916 KB |
Output is correct |
2 |
Correct |
2 ms |
13920 KB |
Output is correct |
3 |
Correct |
3 ms |
13916 KB |
Output is correct |
4 |
Correct |
3 ms |
13916 KB |
Output is correct |
5 |
Correct |
3 ms |
13916 KB |
Output is correct |
6 |
Correct |
4 ms |
13916 KB |
Output is correct |
7 |
Correct |
3 ms |
13916 KB |
Output is correct |
8 |
Correct |
3 ms |
13916 KB |
Output is correct |
9 |
Correct |
3 ms |
13916 KB |
Output is correct |
10 |
Correct |
3 ms |
13916 KB |
Output is correct |
11 |
Correct |
3 ms |
13916 KB |
Output is correct |
12 |
Correct |
3 ms |
13916 KB |
Output is correct |
13 |
Correct |
3 ms |
13916 KB |
Output is correct |
14 |
Correct |
6 ms |
13912 KB |
Output is correct |
15 |
Correct |
3 ms |
13740 KB |
Output is correct |
16 |
Correct |
3 ms |
13916 KB |
Output is correct |
17 |
Correct |
4 ms |
13916 KB |
Output is correct |
18 |
Correct |
2 ms |
13916 KB |
Output is correct |
19 |
Correct |
3 ms |
13916 KB |
Output is correct |
20 |
Correct |
3 ms |
13916 KB |
Output is correct |
21 |
Correct |
3 ms |
13916 KB |
Output is correct |
22 |
Correct |
3 ms |
13916 KB |
Output is correct |
23 |
Correct |
4 ms |
13912 KB |
Output is correct |
24 |
Correct |
4 ms |
13916 KB |
Output is correct |
25 |
Correct |
5 ms |
13916 KB |
Output is correct |
26 |
Correct |
4 ms |
13916 KB |
Output is correct |
27 |
Correct |
4 ms |
13916 KB |
Output is correct |
28 |
Correct |
4 ms |
13912 KB |
Output is correct |
29 |
Correct |
5 ms |
13916 KB |
Output is correct |
30 |
Correct |
5 ms |
13916 KB |
Output is correct |
31 |
Correct |
4 ms |
13916 KB |
Output is correct |
32 |
Correct |
4 ms |
13916 KB |
Output is correct |
33 |
Correct |
4 ms |
13964 KB |
Output is correct |
34 |
Correct |
5 ms |
13916 KB |
Output is correct |
35 |
Correct |
4 ms |
13980 KB |
Output is correct |
36 |
Correct |
4 ms |
13872 KB |
Output is correct |
37 |
Correct |
4 ms |
13912 KB |
Output is correct |
38 |
Correct |
4 ms |
13916 KB |
Output is correct |
39 |
Correct |
4 ms |
13916 KB |
Output is correct |
40 |
Correct |
4 ms |
13916 KB |
Output is correct |
41 |
Correct |
4 ms |
13868 KB |
Output is correct |
42 |
Correct |
4 ms |
13916 KB |
Output is correct |
43 |
Correct |
4 ms |
13916 KB |
Output is correct |
44 |
Correct |
4 ms |
13876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
13916 KB |
Output is correct |
2 |
Correct |
2 ms |
13920 KB |
Output is correct |
3 |
Correct |
3 ms |
13916 KB |
Output is correct |
4 |
Correct |
3 ms |
13916 KB |
Output is correct |
5 |
Correct |
3 ms |
13916 KB |
Output is correct |
6 |
Correct |
4 ms |
13916 KB |
Output is correct |
7 |
Correct |
3 ms |
13916 KB |
Output is correct |
8 |
Correct |
3 ms |
13916 KB |
Output is correct |
9 |
Correct |
3 ms |
13916 KB |
Output is correct |
10 |
Correct |
3 ms |
13916 KB |
Output is correct |
11 |
Correct |
3 ms |
13916 KB |
Output is correct |
12 |
Correct |
3 ms |
13916 KB |
Output is correct |
13 |
Correct |
3 ms |
13916 KB |
Output is correct |
14 |
Correct |
6 ms |
13912 KB |
Output is correct |
15 |
Correct |
3 ms |
13740 KB |
Output is correct |
16 |
Correct |
3 ms |
13916 KB |
Output is correct |
17 |
Correct |
4 ms |
13916 KB |
Output is correct |
18 |
Correct |
2 ms |
13916 KB |
Output is correct |
19 |
Correct |
3 ms |
13916 KB |
Output is correct |
20 |
Correct |
3 ms |
13916 KB |
Output is correct |
21 |
Correct |
3 ms |
13916 KB |
Output is correct |
22 |
Correct |
3 ms |
13916 KB |
Output is correct |
23 |
Correct |
4 ms |
13912 KB |
Output is correct |
24 |
Correct |
4 ms |
13916 KB |
Output is correct |
25 |
Correct |
5 ms |
13916 KB |
Output is correct |
26 |
Correct |
4 ms |
13916 KB |
Output is correct |
27 |
Correct |
4 ms |
13916 KB |
Output is correct |
28 |
Correct |
4 ms |
13912 KB |
Output is correct |
29 |
Correct |
5 ms |
13916 KB |
Output is correct |
30 |
Correct |
5 ms |
13916 KB |
Output is correct |
31 |
Correct |
4 ms |
13916 KB |
Output is correct |
32 |
Correct |
4 ms |
13916 KB |
Output is correct |
33 |
Correct |
4 ms |
13964 KB |
Output is correct |
34 |
Correct |
5 ms |
13916 KB |
Output is correct |
35 |
Correct |
4 ms |
13980 KB |
Output is correct |
36 |
Correct |
4 ms |
13872 KB |
Output is correct |
37 |
Correct |
4 ms |
13912 KB |
Output is correct |
38 |
Correct |
4 ms |
13916 KB |
Output is correct |
39 |
Correct |
4 ms |
13916 KB |
Output is correct |
40 |
Correct |
4 ms |
13916 KB |
Output is correct |
41 |
Correct |
4 ms |
13868 KB |
Output is correct |
42 |
Correct |
4 ms |
13916 KB |
Output is correct |
43 |
Correct |
4 ms |
13916 KB |
Output is correct |
44 |
Correct |
4 ms |
13876 KB |
Output is correct |
45 |
Correct |
126 ms |
34644 KB |
Output is correct |
46 |
Correct |
124 ms |
34384 KB |
Output is correct |
47 |
Correct |
138 ms |
34852 KB |
Output is correct |
48 |
Correct |
133 ms |
34288 KB |
Output is correct |
49 |
Correct |
147 ms |
34496 KB |
Output is correct |
50 |
Correct |
124 ms |
34384 KB |
Output is correct |
51 |
Correct |
126 ms |
34388 KB |
Output is correct |
52 |
Correct |
132 ms |
34916 KB |
Output is correct |
53 |
Correct |
126 ms |
34388 KB |
Output is correct |
54 |
Correct |
231 ms |
35336 KB |
Output is correct |
55 |
Correct |
244 ms |
35272 KB |
Output is correct |
56 |
Correct |
230 ms |
35020 KB |
Output is correct |
57 |
Correct |
223 ms |
35028 KB |
Output is correct |
58 |
Correct |
194 ms |
34212 KB |
Output is correct |
59 |
Correct |
174 ms |
34604 KB |
Output is correct |
60 |
Correct |
85 ms |
34728 KB |
Output is correct |
61 |
Correct |
156 ms |
34348 KB |
Output is correct |
62 |
Correct |
228 ms |
35276 KB |
Output is correct |
63 |
Correct |
144 ms |
34308 KB |
Output is correct |
64 |
Correct |
158 ms |
33876 KB |
Output is correct |
65 |
Correct |
246 ms |
35036 KB |
Output is correct |
66 |
Correct |
167 ms |
34608 KB |
Output is correct |