#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FOD(i, a, b) for (int i = (a); i >= (b); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) (x).begin(), (x).end()
#define __Dennis_Tran___ signed main()
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
using namespace std;
const int maxN = 2e5 + 5;
int n, a[maxN];
int p[20][maxN];
int tin[maxN], tout[maxN], timer;
long long dp[maxN];
vector <int> ke[maxN];
vector <pair <int, int>> stars[maxN];
struct rmq{
int f[20][maxN];
int Max(int x, int y) {return a[x] >= a[y] ? x : y;}
int get(int l, int r) {
int k = __lg(r - l + 1);
return Max(f[k][l], f[k][r - (1 << k) + 1]);
}
void build() {
FOR(i, 1, n) f[0][i] = i;
for (int i = 1; (1 << i) <= n; i++)
for (int j = 1; j <= n - (1 << i) + 1; j++)
f[i][j] = Max(f[i - 1][j], f[i - 1][j + (1 << i - 1)]);
}
} RMQ;
int build_Tree(int l, int r, int parent = 0) {
if (l > r) return 0;
int mid = RMQ.get(l, r);
tin[mid] = ++timer;
ke[parent].emplace_back(mid);
p[0][mid] = parent;
FOR(i, 1, 18) p[i][mid] = p[i - 1][p[i - 1][mid]];
build_Tree(l, mid - 1, mid);
build_Tree(mid + 1, r, mid);
tout[mid] = timer;
return mid;
}
struct BIT{
long long bit[maxN];
void update(int x, long long val) {
for (; x <= n; x += x&-x) bit[x] += val;
}
void update(int l, int r, long long val) {
update(l, val);
update(r + 1, - val);
}
long long get(int x) {
long long ans = 0;
for (; x; x -= x&-x) ans += bit[x];
return ans;
}
} tree1, tree2;
void DFS(int u) {
for (int v : ke[u]) {
DFS(v);
dp[u] += dp[v];
}
tree1.update(tin[u], tout[u], dp[u]);
for (auto x : stars[u]) {
dp[u] = max(dp[u], x.second + tree1.get(tin[x.first]) - tree2.get(tin[x.first]));
}
tree2.update(tin[u], tout[u], dp[u]);
}
__Dennis_Tran___{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n;
FOR(i, 1, n) cin >> a[i];
RMQ.build();
int root = build_Tree(1, n);
long long Total = 0;
int m; cin >> m; while (m--) {
int x, y, c; cin >> x >> y >> c;
int cnode = x; Total += c;
FOD(i, 18, 0) if (p[i][cnode] && a[p[i][cnode]] < y)
cnode = p[i][cnode];
stars[cnode].emplace_back(x, c);
}
DFS(root);
cout << Total - dp[root];
cerr << "Time elapsed: " << TIME << " s.\n";
return (0 ^ 0);
}
Compilation message
constellation3.cpp: In member function 'void rmq::build()':
constellation3.cpp:32:65: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
32 | f[i][j] = Max(f[i - 1][j], f[i - 1][j + (1 << i - 1)]);
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
10076 KB |
Output is correct |
2 |
Correct |
4 ms |
10076 KB |
Output is correct |
3 |
Correct |
4 ms |
10076 KB |
Output is correct |
4 |
Correct |
4 ms |
10076 KB |
Output is correct |
5 |
Correct |
4 ms |
10076 KB |
Output is correct |
6 |
Correct |
4 ms |
10072 KB |
Output is correct |
7 |
Correct |
4 ms |
10076 KB |
Output is correct |
8 |
Correct |
6 ms |
10076 KB |
Output is correct |
9 |
Correct |
6 ms |
10072 KB |
Output is correct |
10 |
Correct |
5 ms |
10076 KB |
Output is correct |
11 |
Correct |
5 ms |
10000 KB |
Output is correct |
12 |
Correct |
4 ms |
10076 KB |
Output is correct |
13 |
Correct |
6 ms |
10076 KB |
Output is correct |
14 |
Correct |
5 ms |
10060 KB |
Output is correct |
15 |
Correct |
4 ms |
10076 KB |
Output is correct |
16 |
Correct |
4 ms |
10076 KB |
Output is correct |
17 |
Correct |
4 ms |
10076 KB |
Output is correct |
18 |
Correct |
4 ms |
10076 KB |
Output is correct |
19 |
Correct |
4 ms |
10076 KB |
Output is correct |
20 |
Correct |
5 ms |
10076 KB |
Output is correct |
21 |
Correct |
4 ms |
10076 KB |
Output is correct |
22 |
Correct |
4 ms |
10076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
10076 KB |
Output is correct |
2 |
Correct |
4 ms |
10076 KB |
Output is correct |
3 |
Correct |
4 ms |
10076 KB |
Output is correct |
4 |
Correct |
4 ms |
10076 KB |
Output is correct |
5 |
Correct |
4 ms |
10076 KB |
Output is correct |
6 |
Correct |
4 ms |
10072 KB |
Output is correct |
7 |
Correct |
4 ms |
10076 KB |
Output is correct |
8 |
Correct |
6 ms |
10076 KB |
Output is correct |
9 |
Correct |
6 ms |
10072 KB |
Output is correct |
10 |
Correct |
5 ms |
10076 KB |
Output is correct |
11 |
Correct |
5 ms |
10000 KB |
Output is correct |
12 |
Correct |
4 ms |
10076 KB |
Output is correct |
13 |
Correct |
6 ms |
10076 KB |
Output is correct |
14 |
Correct |
5 ms |
10060 KB |
Output is correct |
15 |
Correct |
4 ms |
10076 KB |
Output is correct |
16 |
Correct |
4 ms |
10076 KB |
Output is correct |
17 |
Correct |
4 ms |
10076 KB |
Output is correct |
18 |
Correct |
4 ms |
10076 KB |
Output is correct |
19 |
Correct |
4 ms |
10076 KB |
Output is correct |
20 |
Correct |
5 ms |
10076 KB |
Output is correct |
21 |
Correct |
4 ms |
10076 KB |
Output is correct |
22 |
Correct |
4 ms |
10076 KB |
Output is correct |
23 |
Correct |
5 ms |
10332 KB |
Output is correct |
24 |
Correct |
5 ms |
10332 KB |
Output is correct |
25 |
Correct |
5 ms |
10332 KB |
Output is correct |
26 |
Correct |
5 ms |
10392 KB |
Output is correct |
27 |
Correct |
5 ms |
10328 KB |
Output is correct |
28 |
Correct |
5 ms |
10332 KB |
Output is correct |
29 |
Correct |
5 ms |
10332 KB |
Output is correct |
30 |
Correct |
5 ms |
10416 KB |
Output is correct |
31 |
Correct |
5 ms |
10332 KB |
Output is correct |
32 |
Correct |
6 ms |
10512 KB |
Output is correct |
33 |
Correct |
6 ms |
10332 KB |
Output is correct |
34 |
Correct |
9 ms |
10264 KB |
Output is correct |
35 |
Correct |
6 ms |
10332 KB |
Output is correct |
36 |
Correct |
5 ms |
10332 KB |
Output is correct |
37 |
Correct |
6 ms |
10328 KB |
Output is correct |
38 |
Correct |
8 ms |
10588 KB |
Output is correct |
39 |
Correct |
7 ms |
10332 KB |
Output is correct |
40 |
Correct |
5 ms |
10456 KB |
Output is correct |
41 |
Correct |
5 ms |
10332 KB |
Output is correct |
42 |
Correct |
5 ms |
10332 KB |
Output is correct |
43 |
Correct |
5 ms |
10328 KB |
Output is correct |
44 |
Correct |
5 ms |
10332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
10076 KB |
Output is correct |
2 |
Correct |
4 ms |
10076 KB |
Output is correct |
3 |
Correct |
4 ms |
10076 KB |
Output is correct |
4 |
Correct |
4 ms |
10076 KB |
Output is correct |
5 |
Correct |
4 ms |
10076 KB |
Output is correct |
6 |
Correct |
4 ms |
10072 KB |
Output is correct |
7 |
Correct |
4 ms |
10076 KB |
Output is correct |
8 |
Correct |
6 ms |
10076 KB |
Output is correct |
9 |
Correct |
6 ms |
10072 KB |
Output is correct |
10 |
Correct |
5 ms |
10076 KB |
Output is correct |
11 |
Correct |
5 ms |
10000 KB |
Output is correct |
12 |
Correct |
4 ms |
10076 KB |
Output is correct |
13 |
Correct |
6 ms |
10076 KB |
Output is correct |
14 |
Correct |
5 ms |
10060 KB |
Output is correct |
15 |
Correct |
4 ms |
10076 KB |
Output is correct |
16 |
Correct |
4 ms |
10076 KB |
Output is correct |
17 |
Correct |
4 ms |
10076 KB |
Output is correct |
18 |
Correct |
4 ms |
10076 KB |
Output is correct |
19 |
Correct |
4 ms |
10076 KB |
Output is correct |
20 |
Correct |
5 ms |
10076 KB |
Output is correct |
21 |
Correct |
4 ms |
10076 KB |
Output is correct |
22 |
Correct |
4 ms |
10076 KB |
Output is correct |
23 |
Correct |
5 ms |
10332 KB |
Output is correct |
24 |
Correct |
5 ms |
10332 KB |
Output is correct |
25 |
Correct |
5 ms |
10332 KB |
Output is correct |
26 |
Correct |
5 ms |
10392 KB |
Output is correct |
27 |
Correct |
5 ms |
10328 KB |
Output is correct |
28 |
Correct |
5 ms |
10332 KB |
Output is correct |
29 |
Correct |
5 ms |
10332 KB |
Output is correct |
30 |
Correct |
5 ms |
10416 KB |
Output is correct |
31 |
Correct |
5 ms |
10332 KB |
Output is correct |
32 |
Correct |
6 ms |
10512 KB |
Output is correct |
33 |
Correct |
6 ms |
10332 KB |
Output is correct |
34 |
Correct |
9 ms |
10264 KB |
Output is correct |
35 |
Correct |
6 ms |
10332 KB |
Output is correct |
36 |
Correct |
5 ms |
10332 KB |
Output is correct |
37 |
Correct |
6 ms |
10328 KB |
Output is correct |
38 |
Correct |
8 ms |
10588 KB |
Output is correct |
39 |
Correct |
7 ms |
10332 KB |
Output is correct |
40 |
Correct |
5 ms |
10456 KB |
Output is correct |
41 |
Correct |
5 ms |
10332 KB |
Output is correct |
42 |
Correct |
5 ms |
10332 KB |
Output is correct |
43 |
Correct |
5 ms |
10328 KB |
Output is correct |
44 |
Correct |
5 ms |
10332 KB |
Output is correct |
45 |
Correct |
189 ms |
52304 KB |
Output is correct |
46 |
Correct |
168 ms |
51980 KB |
Output is correct |
47 |
Correct |
175 ms |
51172 KB |
Output is correct |
48 |
Correct |
172 ms |
52460 KB |
Output is correct |
49 |
Correct |
194 ms |
50768 KB |
Output is correct |
50 |
Correct |
194 ms |
51024 KB |
Output is correct |
51 |
Correct |
145 ms |
51028 KB |
Output is correct |
52 |
Correct |
172 ms |
51796 KB |
Output is correct |
53 |
Correct |
205 ms |
52048 KB |
Output is correct |
54 |
Correct |
271 ms |
62548 KB |
Output is correct |
55 |
Correct |
236 ms |
58400 KB |
Output is correct |
56 |
Correct |
256 ms |
56404 KB |
Output is correct |
57 |
Correct |
268 ms |
54868 KB |
Output is correct |
58 |
Correct |
228 ms |
59252 KB |
Output is correct |
59 |
Correct |
198 ms |
58784 KB |
Output is correct |
60 |
Correct |
117 ms |
69716 KB |
Output is correct |
61 |
Correct |
178 ms |
52048 KB |
Output is correct |
62 |
Correct |
315 ms |
64084 KB |
Output is correct |
63 |
Correct |
194 ms |
51152 KB |
Output is correct |
64 |
Correct |
177 ms |
50516 KB |
Output is correct |
65 |
Correct |
395 ms |
64708 KB |
Output is correct |
66 |
Correct |
163 ms |
50616 KB |
Output is correct |