#include <bits/stdc++.h>
#define int long long
using namespace std;
struct bit
{
vector<int> b;
void resize(int n)
{
b = vector<int>(n + 2);
}
void update(int pos, int val)
{
int n = (int)b.size() - 1;
for (int i = pos; i <= n; i += i & (-i))
{
b[i] += val;
}
}
int query(int pos)
{
int ans = 0;
for (int i = pos; i; i -= i & (-i))
{
ans += b[i];
}
return ans;
}
void update(int st, int dr, int val)
{
update(st, val);
update(dr + 1, -val);
}
};
struct rmq
{
vector<vector<pair<int, int>>> rmq;
vector<int> lg;
void build(vector<int> a, int n)
{
lg = vector<int>(n + 1);
for (int i = 2; i <= n; ++i)
{
lg[i] = lg[i / 2] + 1;
}
rmq = vector<vector<pair<int, int>>>(n + 1, vector<pair<int, int>>(lg[n] + 1));
for (int i = 1; i <= n; ++i)
{
rmq[i][0] = {a[i], i};
}
for (int j = 1; j <= lg[n]; ++j)
{
for (int i = 1; i + (1 << j) - 1 <= n; ++i)
{
rmq[i][j] = min(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]);
}
}
}
pair<int, int> query(int st, int dr)
{
int pow_2 = lg[dr - st + 1];
return min(rmq[st][pow_2], rmq[dr - (1 << pow_2) + 1][pow_2]);
}
};
struct cartesian
{
vector<int> a;
vector<vector<int>> g;
vector<vector<pair<int, int>>> stars;
vector<vector<int>> jump;
vector<int> tin;
vector<int> tout;
vector<int> dp;
int p = 0;
int root = 0;
int max_jump;
void build(vector<int> _a, int n)
{
a = _a;
dp = tin = tout = vector<int>(n + 1);
g = vector<vector<int>>(n + 1);
rmq t;
t.build(a, n);
max_jump = t.lg[n];
jump = vector<vector<int>>(n + 1, vector<int>(max_jump + 1));
stars = vector<vector<pair<int, int>>>(n + 1);
function<void(int, int, int)> construct = [&](int st, int dr, int parent)
{
pair<int, int> qui = t.query(st, dr);
if (!root)
{
root = qui.second;
}
if (parent != 0)
{
g[parent].push_back(qui.second);
g[qui.second].push_back(parent);
jump[qui.second][0] = parent;
}
else
{
jump[qui.second][0] = qui.second;
}
for (int i = 1; i <= max_jump; ++i)
{
jump[qui.second][i] = jump[jump[qui.second][i - 1]][i - 1];
}
tin[qui.second] = ++p;
if (qui.second != st)
{
construct(st, qui.second - 1, qui.second);
}
if (qui.second != dr)
{
construct(qui.second + 1, dr, qui.second);
}
tout[qui.second] = ++p;
};
construct(1, n, 0);
}
void insert(int node, int h, int coef)
{
int qui = node;
for (int i = max_jump; i >= 0; --i)
{
if (a[jump[node][i]] >= h)
{
node = jump[node][i];
}
}
stars[node].push_back({qui, coef});
}
int solve()
{
bit tree;
bit tree2;
tree.resize(p);
tree2.resize(p);
function<void(int, int)> dfs = [&](int node, int parent)
{
for (auto i : g[node])
{
if (i != parent)
{
dfs(i, node);
}
}
int sum = 0;
for (auto i : g[node])
{
if (i != parent)
{
sum += dp[i];
}
}
tree.update(tin[node], tout[node], sum);
dp[node] = sum;
for (auto chain : stars[node])
{
dp[node] = max(dp[node], chain.second + tree.query(tin[chain.first]) - tree2.query(tin[chain.first]));
}
tree2.update(tin[node], tout[node], dp[node]);
};
dfs(root, 0);
return dp[root];
}
};
int32_t main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
a[i] = n - a[i];
}
cartesian t;
t.build(a, n);
int m;
cin >> m;
int sum = 0;
for (int i = 1; i <= m; ++i)
{
int qui, wh, coef;
cin >> qui >> wh >> coef;
wh = n - wh + 1;
sum += coef;
t.insert(qui, wh, coef);
}
cout << sum - t.solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
1108 KB |
Output is correct |
24 |
Correct |
2 ms |
1108 KB |
Output is correct |
25 |
Correct |
2 ms |
1108 KB |
Output is correct |
26 |
Correct |
3 ms |
1236 KB |
Output is correct |
27 |
Correct |
2 ms |
1108 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
2 ms |
1108 KB |
Output is correct |
30 |
Correct |
3 ms |
1236 KB |
Output is correct |
31 |
Correct |
2 ms |
1108 KB |
Output is correct |
32 |
Correct |
4 ms |
1364 KB |
Output is correct |
33 |
Correct |
3 ms |
1364 KB |
Output is correct |
34 |
Correct |
3 ms |
1236 KB |
Output is correct |
35 |
Correct |
2 ms |
1236 KB |
Output is correct |
36 |
Correct |
2 ms |
1280 KB |
Output is correct |
37 |
Correct |
2 ms |
1364 KB |
Output is correct |
38 |
Correct |
2 ms |
1492 KB |
Output is correct |
39 |
Correct |
2 ms |
1108 KB |
Output is correct |
40 |
Correct |
3 ms |
1364 KB |
Output is correct |
41 |
Correct |
2 ms |
1236 KB |
Output is correct |
42 |
Correct |
3 ms |
1236 KB |
Output is correct |
43 |
Correct |
3 ms |
1364 KB |
Output is correct |
44 |
Correct |
2 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
1108 KB |
Output is correct |
24 |
Correct |
2 ms |
1108 KB |
Output is correct |
25 |
Correct |
2 ms |
1108 KB |
Output is correct |
26 |
Correct |
3 ms |
1236 KB |
Output is correct |
27 |
Correct |
2 ms |
1108 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
2 ms |
1108 KB |
Output is correct |
30 |
Correct |
3 ms |
1236 KB |
Output is correct |
31 |
Correct |
2 ms |
1108 KB |
Output is correct |
32 |
Correct |
4 ms |
1364 KB |
Output is correct |
33 |
Correct |
3 ms |
1364 KB |
Output is correct |
34 |
Correct |
3 ms |
1236 KB |
Output is correct |
35 |
Correct |
2 ms |
1236 KB |
Output is correct |
36 |
Correct |
2 ms |
1280 KB |
Output is correct |
37 |
Correct |
2 ms |
1364 KB |
Output is correct |
38 |
Correct |
2 ms |
1492 KB |
Output is correct |
39 |
Correct |
2 ms |
1108 KB |
Output is correct |
40 |
Correct |
3 ms |
1364 KB |
Output is correct |
41 |
Correct |
2 ms |
1236 KB |
Output is correct |
42 |
Correct |
3 ms |
1236 KB |
Output is correct |
43 |
Correct |
3 ms |
1364 KB |
Output is correct |
44 |
Correct |
2 ms |
1108 KB |
Output is correct |
45 |
Correct |
379 ms |
127192 KB |
Output is correct |
46 |
Correct |
378 ms |
125888 KB |
Output is correct |
47 |
Correct |
384 ms |
123412 KB |
Output is correct |
48 |
Correct |
379 ms |
127828 KB |
Output is correct |
49 |
Correct |
383 ms |
122880 KB |
Output is correct |
50 |
Correct |
352 ms |
123116 KB |
Output is correct |
51 |
Correct |
402 ms |
123184 KB |
Output is correct |
52 |
Correct |
364 ms |
125900 KB |
Output is correct |
53 |
Correct |
421 ms |
126180 KB |
Output is correct |
54 |
Correct |
701 ms |
144056 KB |
Output is correct |
55 |
Correct |
731 ms |
136260 KB |
Output is correct |
56 |
Correct |
679 ms |
132892 KB |
Output is correct |
57 |
Correct |
697 ms |
129788 KB |
Output is correct |
58 |
Correct |
552 ms |
134028 KB |
Output is correct |
59 |
Correct |
498 ms |
132808 KB |
Output is correct |
60 |
Correct |
308 ms |
152232 KB |
Output is correct |
61 |
Correct |
360 ms |
127696 KB |
Output is correct |
62 |
Correct |
673 ms |
141636 KB |
Output is correct |
63 |
Correct |
375 ms |
125672 KB |
Output is correct |
64 |
Correct |
359 ms |
123096 KB |
Output is correct |
65 |
Correct |
733 ms |
143900 KB |
Output is correct |
66 |
Correct |
343 ms |
123908 KB |
Output is correct |