This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
const int inf = 1000000005;
struct segtree{
struct Node{
int mx, mn;
Node(){
mx = -inf, mn = inf;
}
Node(int x){
mx = x, mn = x;
}
Node operator+(Node a){
Node rt;
rt.mx = max(mx, a.mx);
rt.mn = min(mn, a.mn);
return rt;
}
};
int k;
vector<Node> v;
segtree(int n){
k = 1;
while(k < n) k*=2;
v.resize(2*k, Node());
}
void update(int in, int x){
in+=k;
if(x == -1) v[in] = Node();
else v[in] = Node(x);
in/=2;
while(in > 0){
v[in] = v[2*in]+v[2*in+1];
in/=2;
}
}
Node get(int l, int r, int nd, int a, int b){
if(b < l || a > r) return Node();
if(a >= l && b <= r) return v[nd];
int c = (a+b)/2;
return get(l, r, 2*nd, a, c) + get(l, r, 2*nd+1, c+1, b);
}
Node get(int l, int r){
return get(l, r, 1, 0, k-1);
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n; cin >> n;
vector<int> H(n), A(n), B(n);
for(int i = 0; i < n; i++){
cin >> H[i] >> A[i] >> B[i];
}
segtree S(n);
for(int i = 0; i < n; i++){
if(i-B[i] < 0 && i-A[i] >= 0) S.update(i, H[i]);
}
vector<vector<int>> st(n), ed(n);
for(int i = 0; i < n; i++){
if(i-B[i] >= 0) st[i-B[i]].push_back(i);
if(i-A[i] >= 0) ed[i-A[i]].push_back(i);
if(i+A[i] < n) st[i+A[i]].push_back(i);
if(i+B[i] < n) ed[i+B[i]].push_back(i);
}
int ans = -1;
for(int i = 0; i < n; i++){
for(int j: st[i]) S.update(j, H[j]);
segtree::Node nd;
if(i-A[i] >= 0) nd = nd+S.get(max(0, i-B[i]), i-A[i]);
if(i+A[i] < n) nd = nd+S.get(i+A[i], min(i+B[i], n-1));
if(nd.mx > -inf){
ans = max(ans, max(abs(H[i]-nd.mx), abs(H[i]-nd.mn)));
}
for(int j: ed[i]) S.update(j, -1);
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |