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.
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.
Print one integer — the maximum height of the tower that can be obtained.
3 1 5 1 2 6 2 3 7 3
6
4 1 2 1 1 3 3 4 6 2 5 7 1
4
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.
- 对于b[i]=b[j],可将他们合并,a取min(a[i],a[j]),然后处理合并后的序列。
- 按b从小到大排序,作为处理的顺序。
- 按a从小到大排序,目的是构建一棵线段树,保存已经处理过的i的ans,其余的为0。(类似单调序列的思想)
- 每次处理一个b[i]时,找到所有的a[i]~a[j](a[j] < b[i],a以在线段树中顺序决定)中最大的值,并更新b[i]所对应的值。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 100010
#define lson o << 1
#define rson o << 1 | 1
typedef long long LL;
struct node{
int a,b,h,pos;
}t[N],t1[N];
bool cmp(node x,node y){return x.b > y.b;}
bool cmp1(node x,node y) {return x.a < y.a;}
LL maxn[N << 2],add;
int n;
void update(int o,int l,int r,int p)
{
if(l == r && l == p){maxn[o] = add;return;}
int mid = (l+r)>>1;
if(p <= mid) update(lson,l,mid,p);
if(p > mid) update(rson,mid+1,r,p);
maxn[o] = max(maxn[lson],maxn[rson]);
}
int ll,rr;
LL query(int o,int l,int r)
{
if(ll <= l && rr >= r) return maxn[o];
int mid = (l+r)>>1;
LL ans = -100000000;
if(ll <= mid) ans = max(ans,query(lson,l,mid));
if(rr > mid) ans = max(ans,query(rson,mid+1,r));
return ans;
}
int main()
{
scanf("%d",&n);
for(int i = 1;i <= n;i++)
scanf("%d%d%d",&t[i].a,&t[i].b,&t[i].h);
sort(t+1,t+n+1,cmp);
int cnt = 1;
t1[1] = t[1];
for(int i = 2;i <= n;i++)
{
if(t[i].b == t[i-1].b)
{
t1[cnt].a = min(t1[cnt].a,t[i].a);
t1[cnt].h += t[i].h;
}
else t1[++cnt] = t[i];
}
for(int i = 1;i <= cnt;i++) t[i] = t1[i],t1[i].pos = i;
sort(t1+1,t1+cnt+1,cmp1);
for(int i = 1;i <= cnt;i++)
t[ t1[i].pos ].pos = i;
ll = 1; rr = cnt;
for(int i = 1;i <= cnt;i++)
{
while(t1[rr].a >= t[i].b) rr--;
add = query(1,1,n);
add += t[i].h;
update(1,1,n,t[i].pos);
}
rr = cnt;
printf("%I64d\n",query(1,1,n));
return 0;
}