1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| #include <bits/stdc++.h> using namespace std; typedef long long ll; #define endl '\n' #define Turnoff std::ios::sync_with_stdio(false) const ll Max=1e5+5; const ll Mod=998857459;
class SegTree{ public: #define mid ((L+R)>>1) #define Leftson now<<1 #define Rightson now<<1|1 struct segtree{int L,R;ll maxn;}; segtree tree[Max<<2]; void push_up(int now){ tree[now].maxn=max(tree[Leftson].maxn,tree[Rightson].maxn); return; } void build(int L,int R,int now=1){ tree[now]={L,R,0}; if(L==R)return; build(L,mid,Leftson); build(mid+1,R,Rightson); push_up(now); return; } void updata(int pos,ll val,int now=1){ if(tree[now].L==tree[now].R){ tree[now].maxn=max(tree[now].maxn,val); return; } if(pos<=tree[Leftson].R)updata(pos,val,Leftson); if(pos>=tree[Rightson].L)updata(pos,val,Rightson); push_up(now);return; } ll query(int qL,int qR,int now=1){ if(qL>qR)return 0; if(tree[now].R<=qR&&tree[now].L>=qL)return tree[now].maxn; ll maxn=0; if(tree[Leftson].R>=qL)maxn=max(maxn,query(qL,qR,Leftson)); if(tree[Rightson].L<=qR)maxn=max(maxn,query(qL,qR,Rightson)); return maxn; } }preHigh,sufHigh; ll pre[Max],suf[Max]; struct Data{ll indx,val,high;}data[Max]; bool cmp(Data x,Data y){return x.indx<y.indx;}; int main() { Turnoff; int n;cin>>n; preHigh.build(1,n,1); sufHigh.build(1,n,1); for(int i=1;i<=n;i++){ ll indx,val,high; cin>>indx>>val>>high; data[i]={indx,val,high}; } sort(data+1,data+1+n,cmp); for(int i=1;i<=n;i++){ pre[i]=max(pre[i],preHigh.query(1,data[i].high-1,1)+data[i].val); preHigh.updata(data[i].high,pre[i],1); } for(int i=n;i>=1;i--){ suf[i]=max(suf[i],sufHigh.query(1,data[i].high-1,1)+data[i].val); sufHigh.updata(data[i].high,suf[i],1); } ll ans=0; for(int i=1;i<=n;i++)ans=max(ans,pre[i]+suf[i]-data[i].val); cout<<ans<<endl; }
|