//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define trav(x,a) for (const auto& x: a)
#define sz(x) (int)(x).size()
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%I64d", &(n))
#define nai(a, n) for (int _i = 0; _i < (n); _i++) ni(a[_i])
#define nal(a, n) for (int _i = 0; _i < (n); _i++) nl(a[_i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%I64d\n", (n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
const int N = 1e6 + 5;
const double eps = 1e-9;
#define log2(x) (31 - __builtin_clz(x))
struct sparse_table {
int n;
vector<int> a;
vector<vector<int>> st;
int combine(int dl, int dr) {
return a[dl] > a[dr] ? dl : dr;
}
sparse_table() {}
sparse_table(vector<int> & a) : n(a.size()), a(a), st(log2(n) + 1, vector<int>(n)) {
for (int i = 0; i < n; i++)
st[0][i] = i;
for (int j = 1; 1 << j <= n; j++)
for (int i = 0; i + (1 << j) <= n; i++)
st[j][i] = combine(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
}
// query the data on the range [l, r[
int query(int l, int r) {
int s = log2(r - l);
return combine(st[s][l], st[s][r - (1 << s)]);
}
};
multiset<pll> stX[N];
ll dp1[N], dp2[N];
int join(int i, pll jj) {
int j = jj.nd;
if (j < 0 || i == j) return i;
if (sz(stX[j]) > sz(stX[i]))
swap(i, j);
trav(x, stX[j])
stX[i].insert({x.st, x.nd - dp1[j] + dp1[i]});
dp2[i] += dp2[j] + dp1[j];
stX[j].clear();
dp2[j] = dp1[j] = 0;
return i;
}
pll dfs(int l, int r, int i, sparse_table& s) {
if (l > r) return {0, -1};
int id = l;
if (l != r) {
id = s.query(l, r+1);
int mx = s.a[id];
id = join(id, dfs(l, id - 1, mx, s));
id = join(id, dfs(id + 1, r, mx, s));
}
while (!stX[id].empty() && stX[id].begin()->st <= i) {
dp1[id] = max(dp1[id], stX[id].begin()->nd);
stX[id].erase(stX[id].begin());
}
return {dp1[id] + dp2[id], id};
}
int main() {
int n; ni(n);
vector<int> a(n+1);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
int m; ni(m);
ll ans = 0;
for (int i = 1; i <= m; i++) {
int x, y, c; scanf("%d %d %d", &x, &y, &c);
stX[x].insert({y, c});
ans += c;
}
sparse_table s(a);
printf("%lld\n", ans - dfs(1, n, n+1, s).st);
return 0;
}
Compilation message
constellation3.cpp: In function 'int main()':
constellation3.cpp:10:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
#define ni(n) scanf("%d", &(n))
~~~~~^~~~~~~~~~~~
constellation3.cpp:94:10: note: in expansion of macro 'ni'
int n; ni(n);
^~
constellation3.cpp:97:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a[i]);
~~~~~^~~~~~~~~~~~~
constellation3.cpp:10:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
#define ni(n) scanf("%d", &(n))
~~~~~^~~~~~~~~~~~
constellation3.cpp:99:10: note: in expansion of macro 'ni'
int m; ni(m);
^~
constellation3.cpp:102:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int x, y, c; scanf("%d %d %d", &x, &y, &c);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
47360 KB |
Output is correct |
2 |
Correct |
32 ms |
47360 KB |
Output is correct |
3 |
Correct |
32 ms |
47352 KB |
Output is correct |
4 |
Correct |
31 ms |
47360 KB |
Output is correct |
5 |
Correct |
33 ms |
47352 KB |
Output is correct |
6 |
Correct |
32 ms |
47360 KB |
Output is correct |
7 |
Correct |
32 ms |
47360 KB |
Output is correct |
8 |
Correct |
32 ms |
47352 KB |
Output is correct |
9 |
Correct |
31 ms |
47360 KB |
Output is correct |
10 |
Correct |
32 ms |
47352 KB |
Output is correct |
11 |
Correct |
31 ms |
47360 KB |
Output is correct |
12 |
Correct |
32 ms |
47360 KB |
Output is correct |
13 |
Correct |
32 ms |
47360 KB |
Output is correct |
14 |
Correct |
32 ms |
47360 KB |
Output is correct |
15 |
Correct |
32 ms |
47360 KB |
Output is correct |
16 |
Correct |
32 ms |
47352 KB |
Output is correct |
17 |
Correct |
33 ms |
47360 KB |
Output is correct |
18 |
Correct |
32 ms |
47360 KB |
Output is correct |
19 |
Correct |
32 ms |
47352 KB |
Output is correct |
20 |
Correct |
31 ms |
47360 KB |
Output is correct |
21 |
Correct |
32 ms |
47352 KB |
Output is correct |
22 |
Correct |
32 ms |
47360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
47360 KB |
Output is correct |
2 |
Correct |
32 ms |
47360 KB |
Output is correct |
3 |
Correct |
32 ms |
47352 KB |
Output is correct |
4 |
Correct |
31 ms |
47360 KB |
Output is correct |
5 |
Correct |
33 ms |
47352 KB |
Output is correct |
6 |
Correct |
32 ms |
47360 KB |
Output is correct |
7 |
Correct |
32 ms |
47360 KB |
Output is correct |
8 |
Correct |
32 ms |
47352 KB |
Output is correct |
9 |
Correct |
31 ms |
47360 KB |
Output is correct |
10 |
Correct |
32 ms |
47352 KB |
Output is correct |
11 |
Correct |
31 ms |
47360 KB |
Output is correct |
12 |
Correct |
32 ms |
47360 KB |
Output is correct |
13 |
Correct |
32 ms |
47360 KB |
Output is correct |
14 |
Correct |
32 ms |
47360 KB |
Output is correct |
15 |
Correct |
32 ms |
47360 KB |
Output is correct |
16 |
Correct |
32 ms |
47352 KB |
Output is correct |
17 |
Correct |
33 ms |
47360 KB |
Output is correct |
18 |
Correct |
32 ms |
47360 KB |
Output is correct |
19 |
Correct |
32 ms |
47352 KB |
Output is correct |
20 |
Correct |
31 ms |
47360 KB |
Output is correct |
21 |
Correct |
32 ms |
47352 KB |
Output is correct |
22 |
Correct |
32 ms |
47360 KB |
Output is correct |
23 |
Correct |
33 ms |
47608 KB |
Output is correct |
24 |
Correct |
34 ms |
47644 KB |
Output is correct |
25 |
Correct |
33 ms |
47616 KB |
Output is correct |
26 |
Correct |
37 ms |
47608 KB |
Output is correct |
27 |
Correct |
33 ms |
47616 KB |
Output is correct |
28 |
Correct |
34 ms |
47616 KB |
Output is correct |
29 |
Correct |
34 ms |
47616 KB |
Output is correct |
30 |
Correct |
33 ms |
47608 KB |
Output is correct |
31 |
Correct |
34 ms |
47608 KB |
Output is correct |
32 |
Correct |
40 ms |
47744 KB |
Output is correct |
33 |
Correct |
41 ms |
47616 KB |
Output is correct |
34 |
Correct |
37 ms |
47616 KB |
Output is correct |
35 |
Correct |
38 ms |
47608 KB |
Output is correct |
36 |
Correct |
52 ms |
47616 KB |
Output is correct |
37 |
Correct |
49 ms |
47616 KB |
Output is correct |
38 |
Correct |
33 ms |
47736 KB |
Output is correct |
39 |
Correct |
35 ms |
47616 KB |
Output is correct |
40 |
Correct |
42 ms |
47616 KB |
Output is correct |
41 |
Correct |
35 ms |
47616 KB |
Output is correct |
42 |
Correct |
34 ms |
47608 KB |
Output is correct |
43 |
Correct |
41 ms |
47616 KB |
Output is correct |
44 |
Correct |
35 ms |
47616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
47360 KB |
Output is correct |
2 |
Correct |
32 ms |
47360 KB |
Output is correct |
3 |
Correct |
32 ms |
47352 KB |
Output is correct |
4 |
Correct |
31 ms |
47360 KB |
Output is correct |
5 |
Correct |
33 ms |
47352 KB |
Output is correct |
6 |
Correct |
32 ms |
47360 KB |
Output is correct |
7 |
Correct |
32 ms |
47360 KB |
Output is correct |
8 |
Correct |
32 ms |
47352 KB |
Output is correct |
9 |
Correct |
31 ms |
47360 KB |
Output is correct |
10 |
Correct |
32 ms |
47352 KB |
Output is correct |
11 |
Correct |
31 ms |
47360 KB |
Output is correct |
12 |
Correct |
32 ms |
47360 KB |
Output is correct |
13 |
Correct |
32 ms |
47360 KB |
Output is correct |
14 |
Correct |
32 ms |
47360 KB |
Output is correct |
15 |
Correct |
32 ms |
47360 KB |
Output is correct |
16 |
Correct |
32 ms |
47352 KB |
Output is correct |
17 |
Correct |
33 ms |
47360 KB |
Output is correct |
18 |
Correct |
32 ms |
47360 KB |
Output is correct |
19 |
Correct |
32 ms |
47352 KB |
Output is correct |
20 |
Correct |
31 ms |
47360 KB |
Output is correct |
21 |
Correct |
32 ms |
47352 KB |
Output is correct |
22 |
Correct |
32 ms |
47360 KB |
Output is correct |
23 |
Correct |
33 ms |
47608 KB |
Output is correct |
24 |
Correct |
34 ms |
47644 KB |
Output is correct |
25 |
Correct |
33 ms |
47616 KB |
Output is correct |
26 |
Correct |
37 ms |
47608 KB |
Output is correct |
27 |
Correct |
33 ms |
47616 KB |
Output is correct |
28 |
Correct |
34 ms |
47616 KB |
Output is correct |
29 |
Correct |
34 ms |
47616 KB |
Output is correct |
30 |
Correct |
33 ms |
47608 KB |
Output is correct |
31 |
Correct |
34 ms |
47608 KB |
Output is correct |
32 |
Correct |
40 ms |
47744 KB |
Output is correct |
33 |
Correct |
41 ms |
47616 KB |
Output is correct |
34 |
Correct |
37 ms |
47616 KB |
Output is correct |
35 |
Correct |
38 ms |
47608 KB |
Output is correct |
36 |
Correct |
52 ms |
47616 KB |
Output is correct |
37 |
Correct |
49 ms |
47616 KB |
Output is correct |
38 |
Correct |
33 ms |
47736 KB |
Output is correct |
39 |
Correct |
35 ms |
47616 KB |
Output is correct |
40 |
Correct |
42 ms |
47616 KB |
Output is correct |
41 |
Correct |
35 ms |
47616 KB |
Output is correct |
42 |
Correct |
34 ms |
47608 KB |
Output is correct |
43 |
Correct |
41 ms |
47616 KB |
Output is correct |
44 |
Correct |
35 ms |
47616 KB |
Output is correct |
45 |
Correct |
308 ms |
83428 KB |
Output is correct |
46 |
Correct |
305 ms |
82936 KB |
Output is correct |
47 |
Correct |
313 ms |
83460 KB |
Output is correct |
48 |
Correct |
312 ms |
83052 KB |
Output is correct |
49 |
Correct |
311 ms |
82696 KB |
Output is correct |
50 |
Correct |
310 ms |
82440 KB |
Output is correct |
51 |
Correct |
313 ms |
82824 KB |
Output is correct |
52 |
Correct |
320 ms |
83576 KB |
Output is correct |
53 |
Correct |
327 ms |
83060 KB |
Output is correct |
54 |
Execution timed out |
1590 ms |
89324 KB |
Time limit exceeded |
55 |
Halted |
0 ms |
0 KB |
- |