#include<stdio.h>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;
long long a[212121];
struct xy {
long long t,x,y,c;
}all[414141]; long long an;
bool sort_y(xy a, xy b) {
if (a.y != b.y)return a.y > b.y;
if (a.t != b.t)return a.t < b.t;
return a.x < b.x;
}
set<pair<pair<long long, long long>, long long>>S; long long tn;
vector<long long>edge[414141];
vector<pair<long long, long long>>qr[414141];
long long S_start[414141], S_end[414141];
vector<long long>S_ER[414141]; long long point[414141];
void insert_S(long long s, long long e,long long parent) {
tn++;
S.insert({ { -s,e },tn });
S_start[tn] = s; S_end[tn] = e;
if (parent) edge[parent].push_back(tn);
}
int pp[414141][20];
long long sparse[414141][20];;
vector<int>need_to_fill[414141][20];
void init_sparse(long long w, long long bef) {
pp[w][0] = bef;
for (long long i = 0; pp[w][i] != -1; i++) {
need_to_fill[pp[w][i]][i].push_back(w);
pp[w][i + 1] = pp[pp[w][i]][i];
}
for (long long nxt : edge[w]) init_sparse(nxt, w);
}
long long D[414141], depth[414141], CS[414141];
void dfs(long long w,long long bef,long long dep) {
long long sum = 0;
depth[w] = dep;
for (long long nxt : edge[w]) {
dfs(nxt, w, dep + 1);
sum += D[nxt];
}
CS[w] = sum;
for (long long nxt : edge[w]) {
long long v = sum - D[nxt];
sparse[nxt][0] = v;
for (long long i = 1; i < 20; i++) {
for (long long who : need_to_fill[nxt][i]) {
long long p = pp[who][i - 1];
sparse[who][i] = sparse[p][i - 1] + sparse[who][i - 1];
}
}
}
long long qmax = 0;
for (auto q : qr[w]) {
long long res = q.second;
long long now = point[q.first];
res += CS[now];
for (long long i = 19; i >= 0; i--) {
long long nxt = pp[now][i];
if (nxt == -1 || depth[nxt] <= depth[w])continue;
res += sparse[now][i];
now = nxt;
}
if(now!=w)res += sparse[now][0];
qmax = max(qmax, res);
}
D[w] = max(sum, qmax);
}
int main() {
long long n; scanf("%lld", &n);
long long i, j;
for (i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
all[an++] = { 1,i,a[i],0 };
}
long long m; scanf("%lld", &m);
long long allS = 0;
for (i = 1; i <= m; i++) {
all[an].t = 2;
scanf("%lld%lld%lld", &all[an].x, &all[an].y, &all[an].c);
allS += all[an].c;
an++;
}
sort(all, all + an, sort_y);
long long st, en;
insert_S(1, n, 0);
long long e9 = 1e9;
for (st = 0; st < an; st = en) {
for (en = st; all[st].y == all[en].y; en++);
// [st, en)
set<long long>erL;
for (i = st; i < en; i++) {
long long t = all[i].t, x = all[i].x, y = all[i].y, c = all[i].c;
if (t != 1) continue;
auto v = S.lower_bound({ { -x,-e9 },-e9 });
long long who = v->second;
S_ER[who].push_back(x);
erL.insert(who);
}
for (auto who : erL) {
long long s = S_start[who], e = S_end[who];
for (i = 0; i < S_ER[who].size(); i++) {
long long x = S_ER[who][i];
point[x] = who;
long long new_st = s, new_en = x - 1;
if (new_st <= new_en) insert_S(new_st, new_en, who);
s = x + 1;
if (i + 1 == S_ER[who].size()) {
if (s <= e) insert_S(s, e, who);
}
}
S.erase({ {-S_start[who],S_end[who] },who });
}
for (i = st; i < en; i++) {
long long t = all[i].t, x = all[i].x, y = all[i].y, c = all[i].c;
if (t != 2) continue;
auto v = S.lower_bound({ { -x,-e9 },-e9 });
if (v == S.end())continue;
long long who = v->second;
if(S_start[who] <= x && x <= S_end[who]) qr[who].push_back({ x, c });
}
}
for (i = 1; i <= tn; i++)for (j = 0; j < 20; j++)pp[i][j] = -1;
init_sparse(1, -1);
dfs(1, -1, 0);
printf("%lld\n", allS - D[1]);
return 0;
}
Compilation message
constellation3.cpp: In function 'int main()':
constellation3.cpp:96:42: warning: unused variable 'y' [-Wunused-variable]
96 | long long t = all[i].t, x = all[i].x, y = all[i].y, c = all[i].c;
| ^
constellation3.cpp:96:56: warning: unused variable 'c' [-Wunused-variable]
96 | long long t = all[i].t, x = all[i].x, y = all[i].y, c = all[i].c;
| ^
constellation3.cpp:105:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | for (i = 0; i < S_ER[who].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~
constellation3.cpp:111:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | if (i + 1 == S_ER[who].size()) {
| ~~~~~~^~~~~~~~~~~~~~~~~~~
constellation3.cpp:118:42: warning: unused variable 'y' [-Wunused-variable]
118 | long long t = all[i].t, x = all[i].x, y = all[i].y, c = all[i].c;
| ^
constellation3.cpp:73:20: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | long long n; scanf("%lld", &n);
| ~~~~~^~~~~~~~~~~~
constellation3.cpp:76:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | scanf("%lld", &a[i]);
| ~~~~~^~~~~~~~~~~~~~~
constellation3.cpp:79:20: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | long long m; scanf("%lld", &m);
| ~~~~~^~~~~~~~~~~~
constellation3.cpp:83:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | scanf("%lld%lld%lld", &all[an].x, &all[an].y, &all[an].c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
141 ms |
224068 KB |
Output is correct |
2 |
Correct |
145 ms |
224220 KB |
Output is correct |
3 |
Correct |
147 ms |
224160 KB |
Output is correct |
4 |
Correct |
142 ms |
224052 KB |
Output is correct |
5 |
Correct |
153 ms |
224160 KB |
Output is correct |
6 |
Correct |
143 ms |
224064 KB |
Output is correct |
7 |
Correct |
144 ms |
224036 KB |
Output is correct |
8 |
Correct |
144 ms |
224152 KB |
Output is correct |
9 |
Correct |
149 ms |
224168 KB |
Output is correct |
10 |
Correct |
142 ms |
224144 KB |
Output is correct |
11 |
Correct |
141 ms |
224196 KB |
Output is correct |
12 |
Correct |
142 ms |
224196 KB |
Output is correct |
13 |
Correct |
144 ms |
224204 KB |
Output is correct |
14 |
Correct |
145 ms |
224020 KB |
Output is correct |
15 |
Correct |
142 ms |
224072 KB |
Output is correct |
16 |
Correct |
141 ms |
224200 KB |
Output is correct |
17 |
Correct |
142 ms |
224120 KB |
Output is correct |
18 |
Correct |
147 ms |
224156 KB |
Output is correct |
19 |
Correct |
141 ms |
224032 KB |
Output is correct |
20 |
Correct |
143 ms |
224060 KB |
Output is correct |
21 |
Correct |
146 ms |
224116 KB |
Output is correct |
22 |
Correct |
148 ms |
224168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
141 ms |
224068 KB |
Output is correct |
2 |
Correct |
145 ms |
224220 KB |
Output is correct |
3 |
Correct |
147 ms |
224160 KB |
Output is correct |
4 |
Correct |
142 ms |
224052 KB |
Output is correct |
5 |
Correct |
153 ms |
224160 KB |
Output is correct |
6 |
Correct |
143 ms |
224064 KB |
Output is correct |
7 |
Correct |
144 ms |
224036 KB |
Output is correct |
8 |
Correct |
144 ms |
224152 KB |
Output is correct |
9 |
Correct |
149 ms |
224168 KB |
Output is correct |
10 |
Correct |
142 ms |
224144 KB |
Output is correct |
11 |
Correct |
141 ms |
224196 KB |
Output is correct |
12 |
Correct |
142 ms |
224196 KB |
Output is correct |
13 |
Correct |
144 ms |
224204 KB |
Output is correct |
14 |
Correct |
145 ms |
224020 KB |
Output is correct |
15 |
Correct |
142 ms |
224072 KB |
Output is correct |
16 |
Correct |
141 ms |
224200 KB |
Output is correct |
17 |
Correct |
142 ms |
224120 KB |
Output is correct |
18 |
Correct |
147 ms |
224156 KB |
Output is correct |
19 |
Correct |
141 ms |
224032 KB |
Output is correct |
20 |
Correct |
143 ms |
224060 KB |
Output is correct |
21 |
Correct |
146 ms |
224116 KB |
Output is correct |
22 |
Correct |
148 ms |
224168 KB |
Output is correct |
23 |
Correct |
146 ms |
224924 KB |
Output is correct |
24 |
Correct |
145 ms |
224992 KB |
Output is correct |
25 |
Correct |
153 ms |
224884 KB |
Output is correct |
26 |
Correct |
143 ms |
225088 KB |
Output is correct |
27 |
Correct |
148 ms |
224880 KB |
Output is correct |
28 |
Correct |
146 ms |
224892 KB |
Output is correct |
29 |
Correct |
158 ms |
225048 KB |
Output is correct |
30 |
Correct |
146 ms |
224956 KB |
Output is correct |
31 |
Correct |
147 ms |
224988 KB |
Output is correct |
32 |
Correct |
156 ms |
225292 KB |
Output is correct |
33 |
Correct |
147 ms |
225296 KB |
Output is correct |
34 |
Correct |
149 ms |
225192 KB |
Output is correct |
35 |
Correct |
149 ms |
225112 KB |
Output is correct |
36 |
Correct |
143 ms |
224220 KB |
Output is correct |
37 |
Correct |
150 ms |
224196 KB |
Output is correct |
38 |
Correct |
152 ms |
225712 KB |
Output is correct |
39 |
Correct |
147 ms |
224680 KB |
Output is correct |
40 |
Correct |
147 ms |
225264 KB |
Output is correct |
41 |
Correct |
146 ms |
224780 KB |
Output is correct |
42 |
Correct |
144 ms |
224716 KB |
Output is correct |
43 |
Correct |
144 ms |
225220 KB |
Output is correct |
44 |
Correct |
147 ms |
224620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
141 ms |
224068 KB |
Output is correct |
2 |
Correct |
145 ms |
224220 KB |
Output is correct |
3 |
Correct |
147 ms |
224160 KB |
Output is correct |
4 |
Correct |
142 ms |
224052 KB |
Output is correct |
5 |
Correct |
153 ms |
224160 KB |
Output is correct |
6 |
Correct |
143 ms |
224064 KB |
Output is correct |
7 |
Correct |
144 ms |
224036 KB |
Output is correct |
8 |
Correct |
144 ms |
224152 KB |
Output is correct |
9 |
Correct |
149 ms |
224168 KB |
Output is correct |
10 |
Correct |
142 ms |
224144 KB |
Output is correct |
11 |
Correct |
141 ms |
224196 KB |
Output is correct |
12 |
Correct |
142 ms |
224196 KB |
Output is correct |
13 |
Correct |
144 ms |
224204 KB |
Output is correct |
14 |
Correct |
145 ms |
224020 KB |
Output is correct |
15 |
Correct |
142 ms |
224072 KB |
Output is correct |
16 |
Correct |
141 ms |
224200 KB |
Output is correct |
17 |
Correct |
142 ms |
224120 KB |
Output is correct |
18 |
Correct |
147 ms |
224156 KB |
Output is correct |
19 |
Correct |
141 ms |
224032 KB |
Output is correct |
20 |
Correct |
143 ms |
224060 KB |
Output is correct |
21 |
Correct |
146 ms |
224116 KB |
Output is correct |
22 |
Correct |
148 ms |
224168 KB |
Output is correct |
23 |
Correct |
146 ms |
224924 KB |
Output is correct |
24 |
Correct |
145 ms |
224992 KB |
Output is correct |
25 |
Correct |
153 ms |
224884 KB |
Output is correct |
26 |
Correct |
143 ms |
225088 KB |
Output is correct |
27 |
Correct |
148 ms |
224880 KB |
Output is correct |
28 |
Correct |
146 ms |
224892 KB |
Output is correct |
29 |
Correct |
158 ms |
225048 KB |
Output is correct |
30 |
Correct |
146 ms |
224956 KB |
Output is correct |
31 |
Correct |
147 ms |
224988 KB |
Output is correct |
32 |
Correct |
156 ms |
225292 KB |
Output is correct |
33 |
Correct |
147 ms |
225296 KB |
Output is correct |
34 |
Correct |
149 ms |
225192 KB |
Output is correct |
35 |
Correct |
149 ms |
225112 KB |
Output is correct |
36 |
Correct |
143 ms |
224220 KB |
Output is correct |
37 |
Correct |
150 ms |
224196 KB |
Output is correct |
38 |
Correct |
152 ms |
225712 KB |
Output is correct |
39 |
Correct |
147 ms |
224680 KB |
Output is correct |
40 |
Correct |
147 ms |
225264 KB |
Output is correct |
41 |
Correct |
146 ms |
224780 KB |
Output is correct |
42 |
Correct |
144 ms |
224716 KB |
Output is correct |
43 |
Correct |
144 ms |
225220 KB |
Output is correct |
44 |
Correct |
147 ms |
224620 KB |
Output is correct |
45 |
Correct |
1099 ms |
326076 KB |
Output is correct |
46 |
Correct |
1071 ms |
324896 KB |
Output is correct |
47 |
Correct |
1088 ms |
323984 KB |
Output is correct |
48 |
Correct |
1063 ms |
326120 KB |
Output is correct |
49 |
Correct |
1119 ms |
322972 KB |
Output is correct |
50 |
Correct |
1058 ms |
323064 KB |
Output is correct |
51 |
Correct |
1044 ms |
323396 KB |
Output is correct |
52 |
Correct |
1092 ms |
325636 KB |
Output is correct |
53 |
Correct |
1120 ms |
325384 KB |
Output is correct |
54 |
Correct |
1242 ms |
394016 KB |
Output is correct |
55 |
Correct |
1191 ms |
370468 KB |
Output is correct |
56 |
Correct |
1067 ms |
358336 KB |
Output is correct |
57 |
Correct |
1006 ms |
350796 KB |
Output is correct |
58 |
Correct |
374 ms |
252236 KB |
Output is correct |
59 |
Correct |
358 ms |
252352 KB |
Output is correct |
60 |
Correct |
1250 ms |
439516 KB |
Output is correct |
61 |
Correct |
584 ms |
301784 KB |
Output is correct |
62 |
Correct |
1049 ms |
378028 KB |
Output is correct |
63 |
Correct |
533 ms |
288856 KB |
Output is correct |
64 |
Correct |
609 ms |
299160 KB |
Output is correct |
65 |
Correct |
1080 ms |
379592 KB |
Output is correct |
66 |
Correct |
507 ms |
288304 KB |
Output is correct |