#include<bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, b, a) for(int i = (b), _a = (a); i >= _a; --i)
#define REP(i, a, b) for(int i = (a), _b = (b); i < _b; ++i)
#define REPD(i, b, a) for(int i = (b), _a = (a); i > _a; --i)
#define MASK(i) (1LL << (i))
#define BIT(mask, i) (((mask) >> (i)) & 1)
#define __builtin_popcount __builtin_popcountll
#define all(val) val.begin(), val.end()
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sz(v) (int)v.size()
#define fi first
#define se second
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll Rand(ll l, ll r) {return l + rng() % (r - l + 1);}
template<class X, class Y>
bool minimize(X &x, const Y &y) {
if (x > y) {
x = y;
return true;
} else return false;
}
template<class X, class Y>
bool maximize(X &x, const Y &y) {
if (x < y) {
x = y;
return true;
} else return false;
}
template<class T>
T Abs(const T &x) {
return (x < 0 ? -x : x);
}
const int mod = 1e9 + 7;
const int inf = 1e9;
const int N = 2e5 + 5;
const int K = 19;
struct fenwicktree {
int n;
vector<ll> bit;
fenwicktree(int _n) {
n = _n;
bit.assign(n + 1, 0);
}
void upd(int i, ll val) {
for(; i <= n; i += i & -i) bit[i] += val;
}
void upd(int l, int r, ll val) {
upd(l, val);
upd(r + 1, -val);
}
ll get(int i) {
ll res(0);
for(; i; i -= i & -i) res += bit[i];
return res;
}
};
int n, m, timeDFS;
int a[N], P[N][K], in[N], out[N];
vector<int> g[N];
vector<pii> stars[N];
pii rmq[N][K];
ll dp[N];
int getroot(int l, int r) {
int k = 31 - __builtin_clz(r - l + 1);
return -max(rmq[l][k], rmq[r - MASK(k) + 1][k]).se;
}
int dfs(int l, int r, int par = 0) {
if(l > r) return 0;
int root = getroot(l, r);
in[root] = ++timeDFS;
g[par].push_back(root);
P[root][0] = par;
REP(i, 1, K) P[root][i] = P[P[root][i - 1]][i - 1];
dfs(l, root - 1, root);
dfs(root + 1, r, root);
out[root] = timeDFS;
return root;
}
void process() {
cin >> n;
FOR(i, 1, n) {
cin >> a[i];
rmq[i][0] = {a[i], -i};
}
REP(j, 1, K) FOR(i, 1, n - MASK(j) + 1) {
rmq[i][j] = max(rmq[i][j - 1], rmq[i + MASK(j - 1)][j - 1]);
}
int root = dfs(1, n);
ll TotalCost(0);
cin >> m;
FOR(i, 1, m) {
int x, y, w; cin >> x >> y >> w;
TotalCost += w;
int u = x;
FORD(i, K - 1, 0) if(P[u][i] > 0 && a[P[u][i]] < y)
u = P[u][i];
stars[u].emplace_back(x, w);
}
fenwicktree bit(n);
function<ll(int)> solve = [&] (int root) {
for(int v : g[root])
dp[root] += solve(v);
bit.upd(in[root], out[root], dp[root]);
for(auto [v, w] : stars[root]) {
maximize(dp[root], w + bit.get(in[v]));
}
bit.upd(in[root], out[root], -dp[root]);
return dp[root];
};
cout << TotalCost - solve(root) << '\n';
}
signed main() {
if(fopen("test.inp","r")) {
freopen("test.inp","r",stdin);
freopen("test.out","w",stdout);
}
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int ntests = 1;
// cin >> ntests;
while(ntests--) process();
// cerr << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << " s!\n";
return 0;
}
// https://oj.uz/problem/view/JOI20_constellation3
Compilation message
constellation3.cpp: In function 'int main()':
constellation3.cpp:137:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
137 | freopen("test.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
constellation3.cpp:138:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
138 | freopen("test.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16728 KB |
Output is correct |
2 |
Correct |
2 ms |
16728 KB |
Output is correct |
3 |
Correct |
2 ms |
16728 KB |
Output is correct |
4 |
Correct |
3 ms |
16728 KB |
Output is correct |
5 |
Correct |
2 ms |
16732 KB |
Output is correct |
6 |
Correct |
2 ms |
16732 KB |
Output is correct |
7 |
Correct |
3 ms |
16732 KB |
Output is correct |
8 |
Correct |
3 ms |
16732 KB |
Output is correct |
9 |
Correct |
2 ms |
16884 KB |
Output is correct |
10 |
Correct |
2 ms |
16732 KB |
Output is correct |
11 |
Correct |
2 ms |
16832 KB |
Output is correct |
12 |
Correct |
3 ms |
16728 KB |
Output is correct |
13 |
Correct |
2 ms |
16732 KB |
Output is correct |
14 |
Correct |
3 ms |
16880 KB |
Output is correct |
15 |
Correct |
4 ms |
16732 KB |
Output is correct |
16 |
Correct |
3 ms |
16732 KB |
Output is correct |
17 |
Correct |
2 ms |
16732 KB |
Output is correct |
18 |
Correct |
2 ms |
16732 KB |
Output is correct |
19 |
Correct |
2 ms |
16732 KB |
Output is correct |
20 |
Correct |
2 ms |
16732 KB |
Output is correct |
21 |
Correct |
3 ms |
16732 KB |
Output is correct |
22 |
Correct |
3 ms |
16732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16728 KB |
Output is correct |
2 |
Correct |
2 ms |
16728 KB |
Output is correct |
3 |
Correct |
2 ms |
16728 KB |
Output is correct |
4 |
Correct |
3 ms |
16728 KB |
Output is correct |
5 |
Correct |
2 ms |
16732 KB |
Output is correct |
6 |
Correct |
2 ms |
16732 KB |
Output is correct |
7 |
Correct |
3 ms |
16732 KB |
Output is correct |
8 |
Correct |
3 ms |
16732 KB |
Output is correct |
9 |
Correct |
2 ms |
16884 KB |
Output is correct |
10 |
Correct |
2 ms |
16732 KB |
Output is correct |
11 |
Correct |
2 ms |
16832 KB |
Output is correct |
12 |
Correct |
3 ms |
16728 KB |
Output is correct |
13 |
Correct |
2 ms |
16732 KB |
Output is correct |
14 |
Correct |
3 ms |
16880 KB |
Output is correct |
15 |
Correct |
4 ms |
16732 KB |
Output is correct |
16 |
Correct |
3 ms |
16732 KB |
Output is correct |
17 |
Correct |
2 ms |
16732 KB |
Output is correct |
18 |
Correct |
2 ms |
16732 KB |
Output is correct |
19 |
Correct |
2 ms |
16732 KB |
Output is correct |
20 |
Correct |
2 ms |
16732 KB |
Output is correct |
21 |
Correct |
3 ms |
16732 KB |
Output is correct |
22 |
Correct |
3 ms |
16732 KB |
Output is correct |
23 |
Correct |
4 ms |
16984 KB |
Output is correct |
24 |
Correct |
4 ms |
16988 KB |
Output is correct |
25 |
Correct |
3 ms |
16988 KB |
Output is correct |
26 |
Correct |
3 ms |
16880 KB |
Output is correct |
27 |
Correct |
3 ms |
16988 KB |
Output is correct |
28 |
Correct |
3 ms |
16988 KB |
Output is correct |
29 |
Correct |
5 ms |
16988 KB |
Output is correct |
30 |
Correct |
4 ms |
16988 KB |
Output is correct |
31 |
Correct |
5 ms |
16988 KB |
Output is correct |
32 |
Correct |
3 ms |
16988 KB |
Output is correct |
33 |
Correct |
3 ms |
16988 KB |
Output is correct |
34 |
Correct |
3 ms |
16988 KB |
Output is correct |
35 |
Correct |
3 ms |
16988 KB |
Output is correct |
36 |
Correct |
3 ms |
16988 KB |
Output is correct |
37 |
Correct |
3 ms |
16988 KB |
Output is correct |
38 |
Correct |
3 ms |
16984 KB |
Output is correct |
39 |
Correct |
3 ms |
16988 KB |
Output is correct |
40 |
Correct |
3 ms |
16988 KB |
Output is correct |
41 |
Correct |
3 ms |
16988 KB |
Output is correct |
42 |
Correct |
3 ms |
16984 KB |
Output is correct |
43 |
Correct |
3 ms |
16988 KB |
Output is correct |
44 |
Correct |
3 ms |
16988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16728 KB |
Output is correct |
2 |
Correct |
2 ms |
16728 KB |
Output is correct |
3 |
Correct |
2 ms |
16728 KB |
Output is correct |
4 |
Correct |
3 ms |
16728 KB |
Output is correct |
5 |
Correct |
2 ms |
16732 KB |
Output is correct |
6 |
Correct |
2 ms |
16732 KB |
Output is correct |
7 |
Correct |
3 ms |
16732 KB |
Output is correct |
8 |
Correct |
3 ms |
16732 KB |
Output is correct |
9 |
Correct |
2 ms |
16884 KB |
Output is correct |
10 |
Correct |
2 ms |
16732 KB |
Output is correct |
11 |
Correct |
2 ms |
16832 KB |
Output is correct |
12 |
Correct |
3 ms |
16728 KB |
Output is correct |
13 |
Correct |
2 ms |
16732 KB |
Output is correct |
14 |
Correct |
3 ms |
16880 KB |
Output is correct |
15 |
Correct |
4 ms |
16732 KB |
Output is correct |
16 |
Correct |
3 ms |
16732 KB |
Output is correct |
17 |
Correct |
2 ms |
16732 KB |
Output is correct |
18 |
Correct |
2 ms |
16732 KB |
Output is correct |
19 |
Correct |
2 ms |
16732 KB |
Output is correct |
20 |
Correct |
2 ms |
16732 KB |
Output is correct |
21 |
Correct |
3 ms |
16732 KB |
Output is correct |
22 |
Correct |
3 ms |
16732 KB |
Output is correct |
23 |
Correct |
4 ms |
16984 KB |
Output is correct |
24 |
Correct |
4 ms |
16988 KB |
Output is correct |
25 |
Correct |
3 ms |
16988 KB |
Output is correct |
26 |
Correct |
3 ms |
16880 KB |
Output is correct |
27 |
Correct |
3 ms |
16988 KB |
Output is correct |
28 |
Correct |
3 ms |
16988 KB |
Output is correct |
29 |
Correct |
5 ms |
16988 KB |
Output is correct |
30 |
Correct |
4 ms |
16988 KB |
Output is correct |
31 |
Correct |
5 ms |
16988 KB |
Output is correct |
32 |
Correct |
3 ms |
16988 KB |
Output is correct |
33 |
Correct |
3 ms |
16988 KB |
Output is correct |
34 |
Correct |
3 ms |
16988 KB |
Output is correct |
35 |
Correct |
3 ms |
16988 KB |
Output is correct |
36 |
Correct |
3 ms |
16988 KB |
Output is correct |
37 |
Correct |
3 ms |
16988 KB |
Output is correct |
38 |
Correct |
3 ms |
16984 KB |
Output is correct |
39 |
Correct |
3 ms |
16988 KB |
Output is correct |
40 |
Correct |
3 ms |
16988 KB |
Output is correct |
41 |
Correct |
3 ms |
16988 KB |
Output is correct |
42 |
Correct |
3 ms |
16984 KB |
Output is correct |
43 |
Correct |
3 ms |
16988 KB |
Output is correct |
44 |
Correct |
3 ms |
16988 KB |
Output is correct |
45 |
Correct |
155 ms |
73052 KB |
Output is correct |
46 |
Correct |
141 ms |
72880 KB |
Output is correct |
47 |
Correct |
143 ms |
73148 KB |
Output is correct |
48 |
Correct |
210 ms |
72788 KB |
Output is correct |
49 |
Correct |
146 ms |
72920 KB |
Output is correct |
50 |
Correct |
164 ms |
72788 KB |
Output is correct |
51 |
Correct |
142 ms |
72848 KB |
Output is correct |
52 |
Correct |
156 ms |
73276 KB |
Output is correct |
53 |
Correct |
138 ms |
73052 KB |
Output is correct |
54 |
Correct |
223 ms |
85332 KB |
Output is correct |
55 |
Correct |
215 ms |
81748 KB |
Output is correct |
56 |
Correct |
228 ms |
79296 KB |
Output is correct |
57 |
Correct |
255 ms |
78160 KB |
Output is correct |
58 |
Correct |
187 ms |
82256 KB |
Output is correct |
59 |
Correct |
177 ms |
82320 KB |
Output is correct |
60 |
Correct |
125 ms |
92500 KB |
Output is correct |
61 |
Correct |
155 ms |
72236 KB |
Output is correct |
62 |
Correct |
248 ms |
88404 KB |
Output is correct |
63 |
Correct |
160 ms |
71888 KB |
Output is correct |
64 |
Correct |
150 ms |
71708 KB |
Output is correct |
65 |
Correct |
261 ms |
88556 KB |
Output is correct |
66 |
Correct |
165 ms |
71764 KB |
Output is correct |