#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include<time.h>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;
int a[212121];
struct xy {
int t,x,y,c;
}all[414141]; int 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<int, int>>S; int tn;
vector<int>edge[414141];
vector<pair<int, int>>qr[414141];
int S_start[414141], S_end[414141];
vector<int>S_ER[414141]; int point[414141];
void insert_S(int s, int e,int parent) {
tn++;
S.insert({ -s,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];
void init_sparse(int w, int bef) {
pp[w][0] = bef;
for (int i = 0; pp[w][i] != -1; i++) {
if(i>0)need_to_fill[pp[w][i]].push_back(w);
pp[w][i + 1] = pp[pp[w][i]][i];
}
for (int nxt : edge[w]) init_sparse(nxt, w);
}
long long D[414141], depth[414141], CS[414141], rev[414141];
void dfs(int w,int bef,int dep) {
long long sum = 0;
depth[w] = dep;
for (int nxt : edge[w]) {
dfs(nxt, w, dep + 1);
sum += D[nxt];
}
CS[w] = sum;
for (int nxt : edge[w]) {
long long v = sum - D[nxt];
sparse[nxt][0] = v;
for (int who : need_to_fill[nxt]) {
int i = rev[depth[who] - (dep+1)];
int 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;
int now = point[q.first];
res += CS[now];
for (int i = 18; i >= 0; i--) {
int 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 ck[414141];
vector<int>erL;
int main() {
for (int i = 0; i < 18; i++)rev[(1 << i)] = i;
int n; scanf("%d", &n);
int i, j;
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
all[an++] = { 1,i,a[i],0 };
}
int m; scanf("%d", &m);
long long allS = 0;
for (i = 1; i <= m; i++) {
all[an].t = 2;
scanf("%d%d%d", &all[an].x, &all[an].y, &all[an].c);
allS += all[an].c;
an++;
}
sort(all, all + an, sort_y);
int st, en;
insert_S(1, n, 0);
int e9 = 1e9;
for (st = 0; st < an; st = en) {
for (en = st; all[st].y == all[en].y; en++);
// [st, en)
for (i = st; i < en; i++) {
int 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 });
int who = v->second;
S_ER[who].push_back(x);
if(ck[who]==0) erL.push_back(who);
ck[who] = 1;
}
for (auto who : erL) {
int s = S_start[who], e = S_end[who];
for (i = 0; i < S_ER[who].size(); i++) {
int x = S_ER[who][i];
point[x] = who;
int 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);
}
}
ck[who] = 0;
S.erase({ -S_start[who], who });
}
erL.clear();
for (i = st; i < en; i++) {
int 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 });
if (v == S.end())continue;
int 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:101:36: warning: unused variable 'y' [-Wunused-variable]
101 | int t = all[i].t, x = all[i].x, y = all[i].y, c = all[i].c;
| ^
constellation3.cpp:101:50: warning: unused variable 'c' [-Wunused-variable]
101 | int t = all[i].t, x = all[i].x, y = all[i].y, c = all[i].c;
| ^
constellation3.cpp:111:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | for (i = 0; i < S_ER[who].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~
constellation3.cpp:117:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
117 | if (i + 1 == S_ER[who].size()) {
| ~~~~~~^~~~~~~~~~~~~~~~~~~
constellation3.cpp:126:36: warning: unused variable 'y' [-Wunused-variable]
126 | int t = all[i].t, x = all[i].x, y = all[i].y, c = all[i].c;
| ^
constellation3.cpp:79:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | int n; scanf("%d", &n);
| ~~~~~^~~~~~~~~~
constellation3.cpp:82:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
constellation3.cpp:85:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
85 | int m; scanf("%d", &m);
| ~~~~~^~~~~~~~~~
constellation3.cpp:89:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | scanf("%d%d%d", &all[an].x, &all[an].y, &all[an].c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
39364 KB |
Output is correct |
2 |
Correct |
27 ms |
39432 KB |
Output is correct |
3 |
Correct |
31 ms |
39432 KB |
Output is correct |
4 |
Correct |
27 ms |
39428 KB |
Output is correct |
5 |
Correct |
27 ms |
39412 KB |
Output is correct |
6 |
Correct |
27 ms |
39412 KB |
Output is correct |
7 |
Correct |
34 ms |
39372 KB |
Output is correct |
8 |
Correct |
26 ms |
39428 KB |
Output is correct |
9 |
Correct |
31 ms |
39312 KB |
Output is correct |
10 |
Correct |
27 ms |
39372 KB |
Output is correct |
11 |
Correct |
25 ms |
39436 KB |
Output is correct |
12 |
Correct |
27 ms |
39348 KB |
Output is correct |
13 |
Correct |
26 ms |
39436 KB |
Output is correct |
14 |
Correct |
25 ms |
39368 KB |
Output is correct |
15 |
Correct |
27 ms |
39336 KB |
Output is correct |
16 |
Correct |
27 ms |
39480 KB |
Output is correct |
17 |
Correct |
30 ms |
39348 KB |
Output is correct |
18 |
Correct |
26 ms |
39372 KB |
Output is correct |
19 |
Correct |
28 ms |
39276 KB |
Output is correct |
20 |
Correct |
27 ms |
39380 KB |
Output is correct |
21 |
Correct |
27 ms |
39332 KB |
Output is correct |
22 |
Correct |
27 ms |
39308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
39364 KB |
Output is correct |
2 |
Correct |
27 ms |
39432 KB |
Output is correct |
3 |
Correct |
31 ms |
39432 KB |
Output is correct |
4 |
Correct |
27 ms |
39428 KB |
Output is correct |
5 |
Correct |
27 ms |
39412 KB |
Output is correct |
6 |
Correct |
27 ms |
39412 KB |
Output is correct |
7 |
Correct |
34 ms |
39372 KB |
Output is correct |
8 |
Correct |
26 ms |
39428 KB |
Output is correct |
9 |
Correct |
31 ms |
39312 KB |
Output is correct |
10 |
Correct |
27 ms |
39372 KB |
Output is correct |
11 |
Correct |
25 ms |
39436 KB |
Output is correct |
12 |
Correct |
27 ms |
39348 KB |
Output is correct |
13 |
Correct |
26 ms |
39436 KB |
Output is correct |
14 |
Correct |
25 ms |
39368 KB |
Output is correct |
15 |
Correct |
27 ms |
39336 KB |
Output is correct |
16 |
Correct |
27 ms |
39480 KB |
Output is correct |
17 |
Correct |
30 ms |
39348 KB |
Output is correct |
18 |
Correct |
26 ms |
39372 KB |
Output is correct |
19 |
Correct |
28 ms |
39276 KB |
Output is correct |
20 |
Correct |
27 ms |
39380 KB |
Output is correct |
21 |
Correct |
27 ms |
39332 KB |
Output is correct |
22 |
Correct |
27 ms |
39308 KB |
Output is correct |
23 |
Correct |
31 ms |
40024 KB |
Output is correct |
24 |
Correct |
31 ms |
40012 KB |
Output is correct |
25 |
Correct |
29 ms |
40040 KB |
Output is correct |
26 |
Correct |
31 ms |
40076 KB |
Output is correct |
27 |
Correct |
30 ms |
40108 KB |
Output is correct |
28 |
Correct |
33 ms |
40048 KB |
Output is correct |
29 |
Correct |
29 ms |
40012 KB |
Output is correct |
30 |
Correct |
30 ms |
40012 KB |
Output is correct |
31 |
Correct |
30 ms |
40080 KB |
Output is correct |
32 |
Correct |
30 ms |
40224 KB |
Output is correct |
33 |
Correct |
29 ms |
40264 KB |
Output is correct |
34 |
Correct |
30 ms |
40204 KB |
Output is correct |
35 |
Correct |
37 ms |
40268 KB |
Output is correct |
36 |
Correct |
27 ms |
39380 KB |
Output is correct |
37 |
Correct |
27 ms |
39372 KB |
Output is correct |
38 |
Correct |
29 ms |
40372 KB |
Output is correct |
39 |
Correct |
29 ms |
39864 KB |
Output is correct |
40 |
Correct |
29 ms |
40096 KB |
Output is correct |
41 |
Correct |
29 ms |
39772 KB |
Output is correct |
42 |
Correct |
29 ms |
39884 KB |
Output is correct |
43 |
Correct |
29 ms |
40068 KB |
Output is correct |
44 |
Correct |
29 ms |
39748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
39364 KB |
Output is correct |
2 |
Correct |
27 ms |
39432 KB |
Output is correct |
3 |
Correct |
31 ms |
39432 KB |
Output is correct |
4 |
Correct |
27 ms |
39428 KB |
Output is correct |
5 |
Correct |
27 ms |
39412 KB |
Output is correct |
6 |
Correct |
27 ms |
39412 KB |
Output is correct |
7 |
Correct |
34 ms |
39372 KB |
Output is correct |
8 |
Correct |
26 ms |
39428 KB |
Output is correct |
9 |
Correct |
31 ms |
39312 KB |
Output is correct |
10 |
Correct |
27 ms |
39372 KB |
Output is correct |
11 |
Correct |
25 ms |
39436 KB |
Output is correct |
12 |
Correct |
27 ms |
39348 KB |
Output is correct |
13 |
Correct |
26 ms |
39436 KB |
Output is correct |
14 |
Correct |
25 ms |
39368 KB |
Output is correct |
15 |
Correct |
27 ms |
39336 KB |
Output is correct |
16 |
Correct |
27 ms |
39480 KB |
Output is correct |
17 |
Correct |
30 ms |
39348 KB |
Output is correct |
18 |
Correct |
26 ms |
39372 KB |
Output is correct |
19 |
Correct |
28 ms |
39276 KB |
Output is correct |
20 |
Correct |
27 ms |
39380 KB |
Output is correct |
21 |
Correct |
27 ms |
39332 KB |
Output is correct |
22 |
Correct |
27 ms |
39308 KB |
Output is correct |
23 |
Correct |
31 ms |
40024 KB |
Output is correct |
24 |
Correct |
31 ms |
40012 KB |
Output is correct |
25 |
Correct |
29 ms |
40040 KB |
Output is correct |
26 |
Correct |
31 ms |
40076 KB |
Output is correct |
27 |
Correct |
30 ms |
40108 KB |
Output is correct |
28 |
Correct |
33 ms |
40048 KB |
Output is correct |
29 |
Correct |
29 ms |
40012 KB |
Output is correct |
30 |
Correct |
30 ms |
40012 KB |
Output is correct |
31 |
Correct |
30 ms |
40080 KB |
Output is correct |
32 |
Correct |
30 ms |
40224 KB |
Output is correct |
33 |
Correct |
29 ms |
40264 KB |
Output is correct |
34 |
Correct |
30 ms |
40204 KB |
Output is correct |
35 |
Correct |
37 ms |
40268 KB |
Output is correct |
36 |
Correct |
27 ms |
39380 KB |
Output is correct |
37 |
Correct |
27 ms |
39372 KB |
Output is correct |
38 |
Correct |
29 ms |
40372 KB |
Output is correct |
39 |
Correct |
29 ms |
39864 KB |
Output is correct |
40 |
Correct |
29 ms |
40096 KB |
Output is correct |
41 |
Correct |
29 ms |
39772 KB |
Output is correct |
42 |
Correct |
29 ms |
39884 KB |
Output is correct |
43 |
Correct |
29 ms |
40068 KB |
Output is correct |
44 |
Correct |
29 ms |
39748 KB |
Output is correct |
45 |
Correct |
806 ms |
121344 KB |
Output is correct |
46 |
Correct |
775 ms |
120372 KB |
Output is correct |
47 |
Correct |
788 ms |
119200 KB |
Output is correct |
48 |
Correct |
825 ms |
121444 KB |
Output is correct |
49 |
Correct |
803 ms |
118568 KB |
Output is correct |
50 |
Correct |
768 ms |
118664 KB |
Output is correct |
51 |
Correct |
776 ms |
118852 KB |
Output is correct |
52 |
Correct |
822 ms |
120628 KB |
Output is correct |
53 |
Correct |
793 ms |
120448 KB |
Output is correct |
54 |
Correct |
652 ms |
150084 KB |
Output is correct |
55 |
Correct |
640 ms |
142216 KB |
Output is correct |
56 |
Correct |
640 ms |
141888 KB |
Output is correct |
57 |
Correct |
644 ms |
137552 KB |
Output is correct |
58 |
Correct |
241 ms |
51396 KB |
Output is correct |
59 |
Correct |
206 ms |
51340 KB |
Output is correct |
60 |
Correct |
521 ms |
162016 KB |
Output is correct |
61 |
Correct |
355 ms |
97624 KB |
Output is correct |
62 |
Correct |
572 ms |
135068 KB |
Output is correct |
63 |
Correct |
310 ms |
86080 KB |
Output is correct |
64 |
Correct |
375 ms |
95568 KB |
Output is correct |
65 |
Correct |
630 ms |
136388 KB |
Output is correct |
66 |
Correct |
312 ms |
85596 KB |
Output is correct |