#include <bits/stdc++.h>
using namespace std;
#define maxim(X, Y) (X = max(X, Y))
#define all(X) begin(X), end(X)
const int INF = 2e9+5;
struct Fenwick {
int n, *a;
Fenwick(int N) : n(N), a(new int[N+1]) {
fill(a, a + N + 1, -INF);
}
void upd(int i, int v) {
for(++i; i <= n; i += i&-i) maxim(a[i], v);
}
int qry(int i) {
int v = -INF;
for(++i; i >= 1; i -= i&-i) maxim(v, a[i]);
return v;
}
};
int sI, sV;
struct ST {
#define m ((l + r) / 2)
int v;
ST *L = NULL, *R = NULL;
ST() : v(-INF) {};
void upd(int i, int val) {
sI = i, sV = val;
_upd(0, 1<<18);
}
void _upd(int l, int r) {
maxim(v, sV);
if(r - l > 1) {
if(sI < m) (L ? : L = new ST)->_upd(l, m);
if(sI >=m) (R ? : R = new ST)->_upd(m, r);
}
}
int qry(int i) {
sI = i + 1, sV = -INF;
return qry(0, 1<<18), sV;
}
void qry(int l, int r) {
if(r <= sI) return void(maxim(sV, v));
if(l < sI) {
if(L) L->qry(l, m);
if(R) R->qry(m, r);
}
}
};
struct BIT {
int n;
vector<ST*> a;
BIT(int N) : n(N), a(N+1) {
for(auto &i : a) i = new ST;
}
void update(int i, int j, int v) {
for(++i; i <= n; i += i&-i) a[i]->upd(j, v);
}
int query(int i, int j) {
sI = j + 1, sV = -INF;
for(++i; i >= 1; i -= i&-i) a[i]->qry(0, 1<<18);
return sV;
}
};
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int N; cin >> N;
array<int, 3> a[N];
vector<int> C[2];
array<int, 2> sz, c[N];
for(auto &[x, y, z] : a) {
cin >> x >> y >> z;
C[0].push_back(x);
C[1].push_back(y);
}
Fenwick *f[2];
for(int i = 0; i < 2; ++i) {
sort(all(C[i]));
C[i].erase(unique(all(C[i])), end(C[i]));
f[i] = new Fenwick(sz[i] = size(C[i]));
}
sort(a, a + N, [&](const auto &i, const auto &j) {
return i[2] < j[2];
});
BIT b(sz[0]);
for(int i = 0; i < N; ++i)
for(int k = 0; k < 2; ++k)
c[i][k] = sz[k] - 1 - (lower_bound(all(C[k]), a[i][k]) - begin(C[k]));
int ans = -INF;
for(int l = 0, r; l < N; l = r) {
for(r = l + 1; r < N && a[l][2] == a[r][2]; ++r);
for(int i = l; i < r; ++i)
maxim(ans, a[i][2] + b.query(c[i][0] - 1, c[i][1] - 1));
for(int i = l; i < r; ++i) {
int j = f[0]->qry(sz[0] - 2 - c[i][0]);
if(j > sz[1] - 1 - c[i][1])
b.update(c[i][0], sz[1] - 1 - j, a[i][0] + C[1][j]);
j = f[1]->qry(sz[1] - 2 - c[i][1]);
if(j > sz[0] - 1 - c[i][0])
b.update(sz[0] - 1 - j, c[i][1], a[i][1] + C[0][j]);
for(int k = 0; k < 2; ++k)
f[k]->upd(sz[k] - 1 - c[i][k], sz[!k] - 1 - c[i][!k]);
}
}
cout << max(ans, int(-1));
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
19 ms |
5848 KB |
Output is correct |
27 |
Correct |
14 ms |
3276 KB |
Output is correct |
28 |
Correct |
11 ms |
2772 KB |
Output is correct |
29 |
Correct |
8 ms |
1732 KB |
Output is correct |
30 |
Correct |
7 ms |
1876 KB |
Output is correct |
31 |
Correct |
12 ms |
1748 KB |
Output is correct |
32 |
Correct |
5 ms |
980 KB |
Output is correct |
33 |
Correct |
5 ms |
468 KB |
Output is correct |
34 |
Correct |
3 ms |
596 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
2 ms |
468 KB |
Output is correct |
38 |
Correct |
4 ms |
468 KB |
Output is correct |
39 |
Correct |
3 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
216 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
91 ms |
5000 KB |
Output is correct |
12 |
Correct |
41 ms |
3280 KB |
Output is correct |
13 |
Correct |
36 ms |
3896 KB |
Output is correct |
14 |
Correct |
72 ms |
4980 KB |
Output is correct |
15 |
Correct |
50 ms |
4944 KB |
Output is correct |
16 |
Correct |
39 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
216 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
91 ms |
5000 KB |
Output is correct |
12 |
Correct |
41 ms |
3280 KB |
Output is correct |
13 |
Correct |
36 ms |
3896 KB |
Output is correct |
14 |
Correct |
72 ms |
4980 KB |
Output is correct |
15 |
Correct |
50 ms |
4944 KB |
Output is correct |
16 |
Correct |
39 ms |
4940 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
396 KB |
Output is correct |
22 |
Correct |
177 ms |
4964 KB |
Output is correct |
23 |
Correct |
124 ms |
4928 KB |
Output is correct |
24 |
Correct |
78 ms |
3500 KB |
Output is correct |
25 |
Correct |
90 ms |
5020 KB |
Output is correct |
26 |
Correct |
66 ms |
5128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
216 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
91 ms |
5000 KB |
Output is correct |
12 |
Correct |
41 ms |
3280 KB |
Output is correct |
13 |
Correct |
36 ms |
3896 KB |
Output is correct |
14 |
Correct |
72 ms |
4980 KB |
Output is correct |
15 |
Correct |
50 ms |
4944 KB |
Output is correct |
16 |
Correct |
39 ms |
4940 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
396 KB |
Output is correct |
22 |
Correct |
177 ms |
4964 KB |
Output is correct |
23 |
Correct |
124 ms |
4928 KB |
Output is correct |
24 |
Correct |
78 ms |
3500 KB |
Output is correct |
25 |
Correct |
90 ms |
5020 KB |
Output is correct |
26 |
Correct |
66 ms |
5128 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
5 ms |
980 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
316 ms |
4844 KB |
Output is correct |
35 |
Correct |
231 ms |
5924 KB |
Output is correct |
36 |
Correct |
225 ms |
6572 KB |
Output is correct |
37 |
Correct |
168 ms |
7008 KB |
Output is correct |
38 |
Correct |
113 ms |
4836 KB |
Output is correct |
39 |
Correct |
34 ms |
3060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
216 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
91 ms |
5000 KB |
Output is correct |
12 |
Correct |
41 ms |
3280 KB |
Output is correct |
13 |
Correct |
36 ms |
3896 KB |
Output is correct |
14 |
Correct |
72 ms |
4980 KB |
Output is correct |
15 |
Correct |
50 ms |
4944 KB |
Output is correct |
16 |
Correct |
39 ms |
4940 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
396 KB |
Output is correct |
22 |
Correct |
177 ms |
4964 KB |
Output is correct |
23 |
Correct |
124 ms |
4928 KB |
Output is correct |
24 |
Correct |
78 ms |
3500 KB |
Output is correct |
25 |
Correct |
90 ms |
5020 KB |
Output is correct |
26 |
Correct |
66 ms |
5128 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
5 ms |
980 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
316 ms |
4844 KB |
Output is correct |
35 |
Correct |
231 ms |
5924 KB |
Output is correct |
36 |
Correct |
225 ms |
6572 KB |
Output is correct |
37 |
Correct |
168 ms |
7008 KB |
Output is correct |
38 |
Correct |
113 ms |
4836 KB |
Output is correct |
39 |
Correct |
34 ms |
3060 KB |
Output is correct |
40 |
Correct |
6 ms |
1876 KB |
Output is correct |
41 |
Correct |
12 ms |
1792 KB |
Output is correct |
42 |
Correct |
2 ms |
468 KB |
Output is correct |
43 |
Correct |
3 ms |
468 KB |
Output is correct |
44 |
Correct |
567 ms |
11020 KB |
Output is correct |
45 |
Correct |
293 ms |
35040 KB |
Output is correct |
46 |
Correct |
286 ms |
29272 KB |
Output is correct |
47 |
Correct |
250 ms |
35896 KB |
Output is correct |
48 |
Correct |
131 ms |
4952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
19 ms |
5848 KB |
Output is correct |
27 |
Correct |
14 ms |
3276 KB |
Output is correct |
28 |
Correct |
11 ms |
2772 KB |
Output is correct |
29 |
Correct |
8 ms |
1732 KB |
Output is correct |
30 |
Correct |
7 ms |
1876 KB |
Output is correct |
31 |
Correct |
12 ms |
1748 KB |
Output is correct |
32 |
Correct |
5 ms |
980 KB |
Output is correct |
33 |
Correct |
5 ms |
468 KB |
Output is correct |
34 |
Correct |
3 ms |
596 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
2 ms |
468 KB |
Output is correct |
38 |
Correct |
4 ms |
468 KB |
Output is correct |
39 |
Correct |
3 ms |
340 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
0 ms |
216 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
212 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
91 ms |
5000 KB |
Output is correct |
51 |
Correct |
41 ms |
3280 KB |
Output is correct |
52 |
Correct |
36 ms |
3896 KB |
Output is correct |
53 |
Correct |
72 ms |
4980 KB |
Output is correct |
54 |
Correct |
50 ms |
4944 KB |
Output is correct |
55 |
Correct |
39 ms |
4940 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
3 ms |
396 KB |
Output is correct |
61 |
Correct |
177 ms |
4964 KB |
Output is correct |
62 |
Correct |
124 ms |
4928 KB |
Output is correct |
63 |
Correct |
78 ms |
3500 KB |
Output is correct |
64 |
Correct |
90 ms |
5020 KB |
Output is correct |
65 |
Correct |
66 ms |
5128 KB |
Output is correct |
66 |
Correct |
1 ms |
468 KB |
Output is correct |
67 |
Correct |
1 ms |
340 KB |
Output is correct |
68 |
Correct |
1 ms |
212 KB |
Output is correct |
69 |
Correct |
1 ms |
212 KB |
Output is correct |
70 |
Correct |
5 ms |
980 KB |
Output is correct |
71 |
Correct |
1 ms |
340 KB |
Output is correct |
72 |
Correct |
1 ms |
340 KB |
Output is correct |
73 |
Correct |
316 ms |
4844 KB |
Output is correct |
74 |
Correct |
231 ms |
5924 KB |
Output is correct |
75 |
Correct |
225 ms |
6572 KB |
Output is correct |
76 |
Correct |
168 ms |
7008 KB |
Output is correct |
77 |
Correct |
113 ms |
4836 KB |
Output is correct |
78 |
Correct |
34 ms |
3060 KB |
Output is correct |
79 |
Correct |
6 ms |
1876 KB |
Output is correct |
80 |
Correct |
12 ms |
1792 KB |
Output is correct |
81 |
Correct |
2 ms |
468 KB |
Output is correct |
82 |
Correct |
3 ms |
468 KB |
Output is correct |
83 |
Correct |
567 ms |
11020 KB |
Output is correct |
84 |
Correct |
293 ms |
35040 KB |
Output is correct |
85 |
Correct |
286 ms |
29272 KB |
Output is correct |
86 |
Correct |
250 ms |
35896 KB |
Output is correct |
87 |
Correct |
131 ms |
4952 KB |
Output is correct |
88 |
Correct |
1962 ms |
278544 KB |
Output is correct |
89 |
Correct |
713 ms |
108508 KB |
Output is correct |
90 |
Correct |
1314 ms |
144196 KB |
Output is correct |
91 |
Correct |
336 ms |
71100 KB |
Output is correct |
92 |
Correct |
710 ms |
46452 KB |
Output is correct |
93 |
Correct |
559 ms |
105320 KB |
Output is correct |
94 |
Correct |
350 ms |
67608 KB |
Output is correct |
95 |
Correct |
187 ms |
6284 KB |
Output is correct |
96 |
Correct |
113 ms |
11480 KB |
Output is correct |
97 |
Correct |
61 ms |
4996 KB |
Output is correct |
98 |
Correct |
102 ms |
6772 KB |
Output is correct |
99 |
Correct |
96 ms |
6728 KB |
Output is correct |