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 int long long
#define INF 1000000000000000
#define lchild(i) (i*2 + 1)
#define rchild(i) (i*2 + 2)
#define mid(l, u) ((l+u)/2)
#define x(p) p.first
#define y(p) p.second
#define MOD 998244353
using namespace std;
const int N = 1e5 + 5;
int seg[4*N];
int lazy[4*N];
struct site{
int x, g, e;
};
int build(int l, int u, int i){
lazy[i] = 0;
if(l==u) return seg[i] = -INF;
return seg[i] = max(build(l, mid(l, u), lchild(i)), build(mid(l, u)+1, u, rchild(i)));
}
int query(int l, int u, int i, int ll, int uu){
if(lazy[i]){
seg[i]+=lazy[i];
if(l!=u){
lazy[lchild(i)] += lazy[i];
lazy[rchild(i)] += lazy[i];
}
lazy[i] = 0;
}
if(l>=ll && u<=uu) return seg[i];
if(l>uu || u<ll) return -INF;
return max(query(l, mid(l, u), lchild(i), ll, uu), query(mid(l, u)+1, u, rchild(i), ll, uu));
}
int update(int l, int u, int i, int ll, int uu, int upval){
if(lazy[i]){
seg[i]+=lazy[i];
if(l!=u){
lazy[lchild(i)] += lazy[i];
lazy[rchild(i)] += lazy[i];
}
lazy[i] = 0;
}
if(l>=ll && u<=uu){
lazy[i] = upval;
seg[i]+=lazy[i];
if(l!=u){
lazy[lchild(i)] += lazy[i];
lazy[rchild(i)] += lazy[i];
}
lazy[i] = 0;
return seg[i];
}
if(l>uu || u<ll) return seg[i];
return seg[i] = max(update(l, mid(l, u), lchild(i), ll, uu, upval), update(mid(l, u)+1, u, rchild(i), ll, uu, upval));
}
int update1(int l, int u, int i, int ll, int upval){
if(lazy[i]){
seg[i]+=lazy[i];
if(l!=u){
lazy[lchild(i)] += lazy[i];
lazy[rchild(i)] += lazy[i];
}
lazy[i] = 0;
}
if(l>=ll && u<=ll){
return seg[i] = upval;
}
if(l>ll || u<ll) return seg[i];
return seg[i] = max(update1(l, mid(l, u), lchild(i), ll, upval), update1(mid(l, u)+1, u, rchild(i), ll, upval));
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
site arr[n];
for(int i = 0;i<n;i++){
cin>>arr[i].x>>arr[i].g>>arr[i].e;
}
build(0, n-1, 0);
priority_queue<pair<int, int>> ss;
int tosub = 0;
int prev = arr[n-1].x;
for(int i = n-1;i>=0;i--){
int toadd = arr[i].e - (prev - arr[i].x);
prev = arr[i].x;
tosub+=toadd;
ss.push({arr[i].e - tosub, i});
}
int mp[n];
for(int i = n-1;i>=0;i--) {
mp[ss.top().second] = i;
//cout<<(ss.top().second)<<" "<<i<<endl;
ss.pop();
}
set<pair<int, int>> s;
tosub = 0;
prev = arr[n-1].x;
int ans = 0;
for(int i = n-1;i>=0;i--){
int toadd = arr[i].e - (prev - arr[i].x);
prev = arr[i].x;
tosub+=toadd;
s.insert({arr[i].e - tosub, i}); //then >= -tosub
update1(0, n-1, 0, mp[i], 0);
update(0, n-1, 0, 0, n-1, arr[i].g);
//cout<<"ADDING "<<arr[i].g<<endl;
auto it = s.lower_bound({-tosub, 0});
int lower = mp[(*it).second];
ans = max(ans, query(0, n-1, 0, lower, n-1));
//cout<<"PROCESSING INDEX "<<i<<"\n";
/*for(int j = 0;j<n;j++){
query(0, n-1, 0, j, j);
//cout<<"INDEX "<<j<<" "<<query(0, n-1, 0, j, j)<<"\n";
}*/
}
cout<<ans<<"\n";
}
/*
1 a
1 b
3 1
1 d
2 2
2 1
3 2
1 e
2 1
2 5
1 c
3 2
2 2
3 2
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |