/*
IN THE NAME OF GOD
*/
#include <bits/stdc++.h>
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;
#define F first
#define S second
#define Mp make_pair
#define pb push_back
#define pf push_front
#define size(x) ((ll)x.size())
#define all(x) (x).begin(),(x).end()
#define kill(x) cout << x << '\n', exit(0);
#define fuck(x) cout << "(" << #x << " , " << x << ")" << endl
#define endl '\n'
const int N = 3e5+23, lg = 18;
ll Mod = 1e9+7; //998244353;
inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;}
inline ll poww(ll a, ll b, ll mod=Mod) {
ll ans = 1;
a=MOD(a, mod);
while (b) {
if (b & 1) ans = MOD(ans*a, mod);
b >>= 1;
a = MOD(a*a, mod);
}
return ans;
}
int n, a[3][N], mx[2*N], mn[2*N], ans;
vector<int> comp[3], vec[N];
int get(int id, int x) {
return lower_bound(all(comp[id]), x) - comp[id].begin() + 1;
}
void upd(int ind, int val) {
mn[ind] = min(mn[ind], val), mx[ind] = max(mx[ind], val);
ind/=2;
while(ind > 0) {
mx[ind] = max(mx[2*ind], mx[2*ind+1]);
mn[ind] = min(mn[2*ind], mn[2*ind+1]);
ind/=2;
}
}
int qrymx(int r, int ind=1, int lc=1, int rc=(1<<lg)+1) {
if(lc>=r) return 0;
if(rc<=r) return mx[ind];
int mid=(lc+rc)/2;
return max(qrymx(r,2*ind,lc,mid), qrymx(r,2*ind+1,mid,rc));
}
int qry(int l, int val, int ind=1, int lc=1, int rc=(1<<lg)+1) {
if(rc<=l) return 0;
if(ind>=(1<<lg)) {
if(mn[ind] < val) return comp[1][lc-1];
return 0;
}
int mid = (lc+rc)/2;
if(lc>=l) {
if(mn[2*ind+1] < val) return qry(l,val,2*ind+1,mid,rc);
return qry(l,val,2*ind,lc,mid);
}
if(mn[2*ind+1] < val) return qry(l,val,2*ind+1,mid,rc);
return qry(l,val,2*ind,lc,mid);
}
void solve() {
for(int i=1; i<=n; i++) {
vec[get(0, a[0][i])].pb(i);
}
for(int i=1; i<=n; i++) {
for(auto it : vec[i]) {
int maxi=qrymx(get(1,a[1][it])+1);
int res = qry(get(1,a[1][it])+1, maxi);
if(res<1e9 && res != 0 && maxi != 0) ans = max(ans,a[0][it]+maxi+res);
}
for(auto it : vec[i]) {
upd(get(1,a[1][it])+(1<<lg)-1, a[2][it]);
}
}
}
void reset() {
fill(mx, mx+2*N, 0);
fill(mn, mn+2*N, 1e9);
for(int i=1; i<=n; i++) vec[i].clear();
}
int main () {
ios_base::sync_with_stdio(false), cin.tie(0);
cin>>n;
for(int i=1; i<=n; i++) {
for(int j=0; j<3; j++) {
cin>>a[j][i]; comp[j].pb(a[j][i]);
}
}
for(int i=0; i<3; i++) {
sort(all(comp[i]));
comp[i].resize(unique(all(comp[i]))-comp[i].begin());
}
fill(mn, mn+2*N, 1e9);
solve();
reset();
for(int i=1; i<=n; i++) swap(a[1][i], a[2][i]);
swap(comp[1], comp[2]);
solve();
reset();
for(int i=1; i<=n; i++) swap(a[1][i], a[0][i]);
swap(comp[1], comp[0]);
solve();
reset();
for(int i=1; i<=n; i++) swap(a[1][i], a[2][i]);
swap(comp[1], comp[2]);
solve();
cout<<(ans==0 ? -1 : ans)<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
12888 KB |
Output is correct |
2 |
Correct |
4 ms |
14936 KB |
Output is correct |
3 |
Correct |
6 ms |
14984 KB |
Output is correct |
4 |
Correct |
4 ms |
12892 KB |
Output is correct |
5 |
Correct |
6 ms |
12016 KB |
Output is correct |
6 |
Correct |
5 ms |
12124 KB |
Output is correct |
7 |
Correct |
6 ms |
12172 KB |
Output is correct |
8 |
Correct |
6 ms |
12120 KB |
Output is correct |
9 |
Correct |
5 ms |
12124 KB |
Output is correct |
10 |
Correct |
5 ms |
12124 KB |
Output is correct |
11 |
Correct |
8 ms |
12120 KB |
Output is correct |
12 |
Correct |
5 ms |
12124 KB |
Output is correct |
13 |
Correct |
5 ms |
12048 KB |
Output is correct |
14 |
Correct |
6 ms |
12124 KB |
Output is correct |
15 |
Incorrect |
8 ms |
12124 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
12888 KB |
Output is correct |
2 |
Correct |
4 ms |
14936 KB |
Output is correct |
3 |
Correct |
6 ms |
14984 KB |
Output is correct |
4 |
Correct |
4 ms |
12892 KB |
Output is correct |
5 |
Correct |
6 ms |
12016 KB |
Output is correct |
6 |
Correct |
5 ms |
12124 KB |
Output is correct |
7 |
Correct |
6 ms |
12172 KB |
Output is correct |
8 |
Correct |
6 ms |
12120 KB |
Output is correct |
9 |
Correct |
5 ms |
12124 KB |
Output is correct |
10 |
Correct |
5 ms |
12124 KB |
Output is correct |
11 |
Correct |
8 ms |
12120 KB |
Output is correct |
12 |
Correct |
5 ms |
12124 KB |
Output is correct |
13 |
Correct |
5 ms |
12048 KB |
Output is correct |
14 |
Correct |
6 ms |
12124 KB |
Output is correct |
15 |
Incorrect |
8 ms |
12124 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12120 KB |
Output is correct |
2 |
Correct |
8 ms |
12124 KB |
Output is correct |
3 |
Correct |
5 ms |
12052 KB |
Output is correct |
4 |
Correct |
8 ms |
12124 KB |
Output is correct |
5 |
Correct |
5 ms |
12092 KB |
Output is correct |
6 |
Correct |
5 ms |
12120 KB |
Output is correct |
7 |
Correct |
5 ms |
12124 KB |
Output is correct |
8 |
Correct |
5 ms |
12124 KB |
Output is correct |
9 |
Correct |
5 ms |
12124 KB |
Output is correct |
10 |
Correct |
5 ms |
12136 KB |
Output is correct |
11 |
Correct |
200 ms |
17640 KB |
Output is correct |
12 |
Incorrect |
122 ms |
15856 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12120 KB |
Output is correct |
2 |
Correct |
8 ms |
12124 KB |
Output is correct |
3 |
Correct |
5 ms |
12052 KB |
Output is correct |
4 |
Correct |
8 ms |
12124 KB |
Output is correct |
5 |
Correct |
5 ms |
12092 KB |
Output is correct |
6 |
Correct |
5 ms |
12120 KB |
Output is correct |
7 |
Correct |
5 ms |
12124 KB |
Output is correct |
8 |
Correct |
5 ms |
12124 KB |
Output is correct |
9 |
Correct |
5 ms |
12124 KB |
Output is correct |
10 |
Correct |
5 ms |
12136 KB |
Output is correct |
11 |
Correct |
200 ms |
17640 KB |
Output is correct |
12 |
Incorrect |
122 ms |
15856 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12120 KB |
Output is correct |
2 |
Correct |
8 ms |
12124 KB |
Output is correct |
3 |
Correct |
5 ms |
12052 KB |
Output is correct |
4 |
Correct |
8 ms |
12124 KB |
Output is correct |
5 |
Correct |
5 ms |
12092 KB |
Output is correct |
6 |
Correct |
5 ms |
12120 KB |
Output is correct |
7 |
Correct |
5 ms |
12124 KB |
Output is correct |
8 |
Correct |
5 ms |
12124 KB |
Output is correct |
9 |
Correct |
5 ms |
12124 KB |
Output is correct |
10 |
Correct |
5 ms |
12136 KB |
Output is correct |
11 |
Correct |
200 ms |
17640 KB |
Output is correct |
12 |
Incorrect |
122 ms |
15856 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12120 KB |
Output is correct |
2 |
Correct |
8 ms |
12124 KB |
Output is correct |
3 |
Correct |
5 ms |
12052 KB |
Output is correct |
4 |
Correct |
8 ms |
12124 KB |
Output is correct |
5 |
Correct |
5 ms |
12092 KB |
Output is correct |
6 |
Correct |
5 ms |
12120 KB |
Output is correct |
7 |
Correct |
5 ms |
12124 KB |
Output is correct |
8 |
Correct |
5 ms |
12124 KB |
Output is correct |
9 |
Correct |
5 ms |
12124 KB |
Output is correct |
10 |
Correct |
5 ms |
12136 KB |
Output is correct |
11 |
Correct |
200 ms |
17640 KB |
Output is correct |
12 |
Incorrect |
122 ms |
15856 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
12888 KB |
Output is correct |
2 |
Correct |
4 ms |
14936 KB |
Output is correct |
3 |
Correct |
6 ms |
14984 KB |
Output is correct |
4 |
Correct |
4 ms |
12892 KB |
Output is correct |
5 |
Correct |
6 ms |
12016 KB |
Output is correct |
6 |
Correct |
5 ms |
12124 KB |
Output is correct |
7 |
Correct |
6 ms |
12172 KB |
Output is correct |
8 |
Correct |
6 ms |
12120 KB |
Output is correct |
9 |
Correct |
5 ms |
12124 KB |
Output is correct |
10 |
Correct |
5 ms |
12124 KB |
Output is correct |
11 |
Correct |
8 ms |
12120 KB |
Output is correct |
12 |
Correct |
5 ms |
12124 KB |
Output is correct |
13 |
Correct |
5 ms |
12048 KB |
Output is correct |
14 |
Correct |
6 ms |
12124 KB |
Output is correct |
15 |
Incorrect |
8 ms |
12124 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |