#include<cstdio>
#include<algorithm>
#include<vector>
#define N_ 401000
#define SZ 262144
#define pii pair<int,int>
using namespace std;
int n, H[N_], m, IT[SZ + SZ], cnt;
vector<pii>G[N_];
void Put(int a, int b) {
a += SZ;
IT[a] = b;
while (a != 1) {
a >>= 1;
IT[a] = max(IT[a * 2], IT[a * 2 + 1]);
}
}
pii GetMax(int b, int e) {
b += SZ, e += SZ;
int pv = -1, Mx = -1;
while (b <= e) {
if (Mx < IT[b])Mx = IT[b], pv = b;
if (Mx < IT[e])Mx = IT[e], pv = e;
b = (b + 1) >> 1, e = (e - 1) >> 1;
}
while (pv < SZ) {
pv *= 2;
if (IT[pv] != Mx)pv++;
}
return { Mx,pv - SZ };
}
int Left[N_], Right[N_], par[N_], TT[SZ + SZ];
int Ed[N_];
vector<pii>Z[N_];
void Put(int b, int e, int c) {
b += SZ, e += SZ;
while (b <= e) {
TT[b] = TT[e] = c;
b = (b + 1) >> 1, e = (e - 1) >> 1;
}
}
int Get(int a) {
int r = 0;
a += SZ;
while (a) {
r = max(r, TT[a]);
a >>= 1;
}
return r;
}
void Do(int b, int e, int d) {
cnt++;
int cur = cnt;
pii tp = GetMax(b, e);
int mid = tp.second, h = tp.first;
Put(h + 1, d, cur);
for (auto &t : G[mid]) {
Z[Get(t.first)].push_back({ cur, t.second });
}
if (b == e) {
Ed[cur] = cnt;
return;
}
if (b < mid) {
Left[cur] = cnt + 1;
par[cnt + 1] = cur;
Do(b, mid - 1, h);
}
if (mid < e) {
Right[cur] = cnt + 1;
par[cnt + 1] = cur;
Do(mid + 1, e, h);
}
Ed[cur] = cnt;
}
long long D[N_], BIT[N_];
void Add2(int a, long long b) {
while (a <= cnt) {
BIT[a] += b;
a += (a&-a);
}
}
long long Sum(int a) {
long long r = 0;
while (a) {
r += BIT[a];
a -= (a&-a);
}
return r;
}
void Add(int b, int e, long long t) {
Add2(b, t);
Add2(e + 1, -t);
}
void Go(int a) {
int l = Left[a], r = Right[a];
if (l) {
Go(l);
}
if (r) {
Go(r);
}
if (l&&r) {
Add(l, Ed[l], D[r]);
Add(r, Ed[r], D[l]);
}
Add(a, a, D[r] + D[l]);
D[a] = D[l] + D[r];
for (auto &t : Z[a]) {
D[a] = max(D[a], Sum(t.first) + t.second);
}
}
int main() {
int i;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d", &H[i]);
Put(i, H[i]);
}
scanf("%d", &m);
long long s = 0;
for (i = 1; i <= m; i++) {
int x, y, c;
scanf("%d%d%d", &x, &y, &c);
G[x].push_back({ y,c });
s += c;
}
Do(1, n, n);
Go(1);
printf("%lld\n", s - D[1]);
}
Compilation message
constellation3.cpp: In function 'int main()':
constellation3.cpp:115:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
constellation3.cpp:117:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &H[i]);
~~~~~^~~~~~~~~~~~~
constellation3.cpp:120:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &m);
~~~~~^~~~~~~~~~
constellation3.cpp:124:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &x, &y, &c);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
19328 KB |
Output is correct |
2 |
Correct |
16 ms |
19328 KB |
Output is correct |
3 |
Correct |
16 ms |
19328 KB |
Output is correct |
4 |
Correct |
16 ms |
19328 KB |
Output is correct |
5 |
Correct |
17 ms |
19328 KB |
Output is correct |
6 |
Correct |
16 ms |
19328 KB |
Output is correct |
7 |
Correct |
16 ms |
19328 KB |
Output is correct |
8 |
Correct |
16 ms |
19328 KB |
Output is correct |
9 |
Correct |
16 ms |
19328 KB |
Output is correct |
10 |
Correct |
16 ms |
19328 KB |
Output is correct |
11 |
Correct |
16 ms |
19328 KB |
Output is correct |
12 |
Correct |
16 ms |
19328 KB |
Output is correct |
13 |
Correct |
16 ms |
19328 KB |
Output is correct |
14 |
Correct |
17 ms |
19328 KB |
Output is correct |
15 |
Correct |
16 ms |
19328 KB |
Output is correct |
16 |
Correct |
16 ms |
19328 KB |
Output is correct |
17 |
Correct |
16 ms |
19232 KB |
Output is correct |
18 |
Correct |
16 ms |
19328 KB |
Output is correct |
19 |
Correct |
16 ms |
19328 KB |
Output is correct |
20 |
Correct |
16 ms |
19328 KB |
Output is correct |
21 |
Correct |
17 ms |
19328 KB |
Output is correct |
22 |
Correct |
16 ms |
19328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
19328 KB |
Output is correct |
2 |
Correct |
16 ms |
19328 KB |
Output is correct |
3 |
Correct |
16 ms |
19328 KB |
Output is correct |
4 |
Correct |
16 ms |
19328 KB |
Output is correct |
5 |
Correct |
17 ms |
19328 KB |
Output is correct |
6 |
Correct |
16 ms |
19328 KB |
Output is correct |
7 |
Correct |
16 ms |
19328 KB |
Output is correct |
8 |
Correct |
16 ms |
19328 KB |
Output is correct |
9 |
Correct |
16 ms |
19328 KB |
Output is correct |
10 |
Correct |
16 ms |
19328 KB |
Output is correct |
11 |
Correct |
16 ms |
19328 KB |
Output is correct |
12 |
Correct |
16 ms |
19328 KB |
Output is correct |
13 |
Correct |
16 ms |
19328 KB |
Output is correct |
14 |
Correct |
17 ms |
19328 KB |
Output is correct |
15 |
Correct |
16 ms |
19328 KB |
Output is correct |
16 |
Correct |
16 ms |
19328 KB |
Output is correct |
17 |
Correct |
16 ms |
19232 KB |
Output is correct |
18 |
Correct |
16 ms |
19328 KB |
Output is correct |
19 |
Correct |
16 ms |
19328 KB |
Output is correct |
20 |
Correct |
16 ms |
19328 KB |
Output is correct |
21 |
Correct |
17 ms |
19328 KB |
Output is correct |
22 |
Correct |
16 ms |
19328 KB |
Output is correct |
23 |
Correct |
17 ms |
19476 KB |
Output is correct |
24 |
Correct |
17 ms |
19456 KB |
Output is correct |
25 |
Correct |
18 ms |
19456 KB |
Output is correct |
26 |
Correct |
17 ms |
19456 KB |
Output is correct |
27 |
Correct |
18 ms |
19456 KB |
Output is correct |
28 |
Correct |
16 ms |
19584 KB |
Output is correct |
29 |
Correct |
17 ms |
19456 KB |
Output is correct |
30 |
Correct |
18 ms |
19584 KB |
Output is correct |
31 |
Correct |
18 ms |
19584 KB |
Output is correct |
32 |
Correct |
17 ms |
19584 KB |
Output is correct |
33 |
Correct |
18 ms |
19584 KB |
Output is correct |
34 |
Correct |
19 ms |
19456 KB |
Output is correct |
35 |
Correct |
18 ms |
19584 KB |
Output is correct |
36 |
Correct |
19 ms |
19584 KB |
Output is correct |
37 |
Correct |
17 ms |
19712 KB |
Output is correct |
38 |
Correct |
17 ms |
19584 KB |
Output is correct |
39 |
Correct |
18 ms |
19456 KB |
Output is correct |
40 |
Correct |
18 ms |
19584 KB |
Output is correct |
41 |
Correct |
17 ms |
19456 KB |
Output is correct |
42 |
Correct |
19 ms |
19456 KB |
Output is correct |
43 |
Correct |
18 ms |
19584 KB |
Output is correct |
44 |
Correct |
18 ms |
19456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
19328 KB |
Output is correct |
2 |
Correct |
16 ms |
19328 KB |
Output is correct |
3 |
Correct |
16 ms |
19328 KB |
Output is correct |
4 |
Correct |
16 ms |
19328 KB |
Output is correct |
5 |
Correct |
17 ms |
19328 KB |
Output is correct |
6 |
Correct |
16 ms |
19328 KB |
Output is correct |
7 |
Correct |
16 ms |
19328 KB |
Output is correct |
8 |
Correct |
16 ms |
19328 KB |
Output is correct |
9 |
Correct |
16 ms |
19328 KB |
Output is correct |
10 |
Correct |
16 ms |
19328 KB |
Output is correct |
11 |
Correct |
16 ms |
19328 KB |
Output is correct |
12 |
Correct |
16 ms |
19328 KB |
Output is correct |
13 |
Correct |
16 ms |
19328 KB |
Output is correct |
14 |
Correct |
17 ms |
19328 KB |
Output is correct |
15 |
Correct |
16 ms |
19328 KB |
Output is correct |
16 |
Correct |
16 ms |
19328 KB |
Output is correct |
17 |
Correct |
16 ms |
19232 KB |
Output is correct |
18 |
Correct |
16 ms |
19328 KB |
Output is correct |
19 |
Correct |
16 ms |
19328 KB |
Output is correct |
20 |
Correct |
16 ms |
19328 KB |
Output is correct |
21 |
Correct |
17 ms |
19328 KB |
Output is correct |
22 |
Correct |
16 ms |
19328 KB |
Output is correct |
23 |
Correct |
17 ms |
19476 KB |
Output is correct |
24 |
Correct |
17 ms |
19456 KB |
Output is correct |
25 |
Correct |
18 ms |
19456 KB |
Output is correct |
26 |
Correct |
17 ms |
19456 KB |
Output is correct |
27 |
Correct |
18 ms |
19456 KB |
Output is correct |
28 |
Correct |
16 ms |
19584 KB |
Output is correct |
29 |
Correct |
17 ms |
19456 KB |
Output is correct |
30 |
Correct |
18 ms |
19584 KB |
Output is correct |
31 |
Correct |
18 ms |
19584 KB |
Output is correct |
32 |
Correct |
17 ms |
19584 KB |
Output is correct |
33 |
Correct |
18 ms |
19584 KB |
Output is correct |
34 |
Correct |
19 ms |
19456 KB |
Output is correct |
35 |
Correct |
18 ms |
19584 KB |
Output is correct |
36 |
Correct |
19 ms |
19584 KB |
Output is correct |
37 |
Correct |
17 ms |
19712 KB |
Output is correct |
38 |
Correct |
17 ms |
19584 KB |
Output is correct |
39 |
Correct |
18 ms |
19456 KB |
Output is correct |
40 |
Correct |
18 ms |
19584 KB |
Output is correct |
41 |
Correct |
17 ms |
19456 KB |
Output is correct |
42 |
Correct |
19 ms |
19456 KB |
Output is correct |
43 |
Correct |
18 ms |
19584 KB |
Output is correct |
44 |
Correct |
18 ms |
19456 KB |
Output is correct |
45 |
Correct |
257 ms |
42616 KB |
Output is correct |
46 |
Correct |
259 ms |
42360 KB |
Output is correct |
47 |
Correct |
262 ms |
42644 KB |
Output is correct |
48 |
Correct |
241 ms |
42360 KB |
Output is correct |
49 |
Correct |
252 ms |
42232 KB |
Output is correct |
50 |
Correct |
242 ms |
42104 KB |
Output is correct |
51 |
Correct |
265 ms |
42324 KB |
Output is correct |
52 |
Correct |
251 ms |
42784 KB |
Output is correct |
53 |
Correct |
256 ms |
42488 KB |
Output is correct |
54 |
Correct |
273 ms |
54264 KB |
Output is correct |
55 |
Correct |
270 ms |
51064 KB |
Output is correct |
56 |
Correct |
273 ms |
48760 KB |
Output is correct |
57 |
Correct |
273 ms |
47520 KB |
Output is correct |
58 |
Correct |
229 ms |
58232 KB |
Output is correct |
59 |
Correct |
246 ms |
58360 KB |
Output is correct |
60 |
Correct |
181 ms |
57464 KB |
Output is correct |
61 |
Correct |
210 ms |
39800 KB |
Output is correct |
62 |
Correct |
282 ms |
56184 KB |
Output is correct |
63 |
Correct |
212 ms |
39664 KB |
Output is correct |
64 |
Correct |
208 ms |
39160 KB |
Output is correct |
65 |
Correct |
270 ms |
56312 KB |
Output is correct |
66 |
Correct |
215 ms |
39544 KB |
Output is correct |