# Codeforces Round #401 (Div. 2)E. Hanoi Factory(离散化+线段树）

xiaoxiao2021-03-26  4

E. Hanoi Factory

time limit per test:1 second

memory limit per test:256 megabytes

input:standard input

output:standard output

Of course you have heard the famous task about Hanoi Towers, but did you know that there is a special factory producing the rings for this wonderful game? Once upon a time, the ruler of the ancient Egypt ordered the workers of Hanoi Factory to create as high tower as possible. They were not ready to serve such a strange order so they had to create this new tower using already produced rings.

There are n rings in factory’s stock. The i-th ring has inner radius ai, outer radius bi and height hi. The goal is to select some subset of rings and arrange them such that the following conditions are satisfied: • Outer radiuses form a non-increasing sequence, i.e. one can put the j-th ring on the i-th ring only if bj ≤ bi. • Rings should not fall one into the the other. That means one can place ring j on the ring i only if bj > ai. • The total height of all rings used should be maximum possible.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of rings in factory’s stock.

The i-th of the next n lines contains three integers ai, bi and hi (1 ≤ ai, bi, hi ≤ 109, bi > ai) — inner radius, outer radius and the height of the i-th ring respectively.

Output

Print one integer — the maximum height of the tower that can be obtained.

Examples

Input 3 1 5 1 2 6 2 3 7 3

Output 6

Input 4 1 2 1 1 3 3 4 6 2 5 7 1

Output 4

Note

In the first sample, the optimal solution is to take all the rings and put them on each other in order 3, 2, 1.

In the second sample, one can put the ring 3 on the ring 4 and get the tower of height 3, or put the ring 1 on the ring 2 and get the tower of height 4. 题意和正解：http://blog.csdn.net/qq_33183401/article/details/59092936 这里给出线段树解法。对于每一个零件，我们想找到一个最大的底使它最高，此题明显需要o（nlogn）的方法，所以我们需要一个线段树来加快找的过程，由于ai，bi值较大，我们需要离散化（这部分借鉴别人的）。 代码：

#include<bits/stdc++.h> #include<stdlib.h> #define ll long long using namespace std; const int N=1000010; ll sum[N<<2]; void pushup(int rt) { sum[rt]=max(sum[rt<<1],sum[rt<<1|1]); } void update(int l,int r,int pos,ll val,int rt) { if(l==r&&l==pos) { sum[rt]=val; return ; } int mid=(r+l)>>1; if(pos<=mid) update(l,mid,pos,val,rt<<1); else update(mid+1,r,pos,val,rt<<1|1); pushup(rt); } ll query(int l,int r,int L,int R,int rt) { ll cnt=0; if(L<=l&&R>=r) return sum[rt]; int mid=(r+l)>>1; if(L<=mid) cnt=max(cnt,query(l,mid,L,R,rt<<1)); if(R>mid) cnt=max(cnt,query(mid+1,r,L,R,rt<<1|1)); return cnt; } struct node { int in,out,h; bool operator < (const node&t)const{ return out>t.out||(out==t.out&&in>t.in); } }a[N],e; struct Hash :vector<int> { void prepare(){ sort(begin(),end()); } ll get(ll x) { return lower_bound(begin(),end(),x)-begin()+1; } }has; int main() { int n,m; m=0; ll ans; has.clear(); memset(sum,0,sizeof(sum)); scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d%d%d",&a[i].in,&a[i].out,&a[i].h); has.push_back(a[i].in); has.push_back(a[i].out); } has.prepare(); for(int i=0;i<n;i++) { a[i].in=has.get(a[i].in); a[i].out=has.get(a[i].out); m=max(m,a[i].out); } sort(a,a+n); for(int i=0;i<n;i++) { ans=query(1,m,1,a[i].out-1,1); update(1,m,a[i].in,ans+a[i].h,1); } printf("%I64d\n",query(1,m,1,m,1)); }