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 aibi 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.

因a和b数组中的数较大,因此需要离散化。可以用类似于LIS的方法进行dp转移,但因为题目要求时间复杂度为O(nlogn),所以还要用树状数组或线段树进行优化,维护1到某个半径的最大高度。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
using namespace std;
struct ss
{
long long a,b,c;
};
ss a[];
long long n,len,tree[],m=;
struct Hash : vector<int> { //离散化
void prepare() {
sort(begin(), end());
//erase(unique(begin(), end()), end());
}
long long get(long long x) {
return lower_bound(begin(), end(), x)-begin()+;
}
} has;
void upd(long long x,long long y)
{
for (;x<=m;x+=x&(-x)) tree[x]=max(tree[x],y);
}
long long sum(long long x)
{
long long p=;
for (;x;x-=x&(-x)) p=max(p,tree[x]);
return p;
}
inline bool cmp(ss a,ss b)
{
return (a.b>b.b||a.b==b.b&&a.a>b.a);
}
int main()
{
scanf("%lld",&n);
long long i;
for (i=;i<=n;i++)
scanf("%lld%lld%lld",&a[i].a,&a[i].b,&a[i].c);
has.clear();
for (i=;i<=n;i++)
has.push_back(a[i].a),has.push_back(a[i].b);
has.prepare();
for (i=;i<=n;i++)
a[i].a=has.get(a[i].a),a[i].b=has.get(a[i].b),m=max(a[i].b,m);
m*=;
sort(a+,a+n+,cmp);
//for (i=1;i<=n;i++)
// printf("%d %d %d\n",a[i].a,a[i].b,a[i].c);
//cout<<m<<endl;
memset(tree,,sizeof(tree));
long long ans=;
for (i=;i<=n;i++)
{
long long now=sum(a[i].b-)+a[i].c;
//cout<<sum(a[i].b-1)<<endl;
upd(a[i].a,now);
ans=max(ans,now);
}
printf("%lld\n",ans);
return ;
}

Codeforces 777E(离散化+dp+树状数组或线段树维护最大值)的更多相关文章

  1. BZOJ.4553.[HEOI2016&TJOI2016]序列(DP 树状数组套线段树/二维线段树(MLE) 动态开点)

    题目链接:BZOJ 洛谷 \(O(n^2)\)DP很好写,对于当前的i从之前满足条件的j中选一个最大值,\(dp[i]=d[j]+1\) for(int j=1; j<i; ++j) if(a[ ...

  2. [BZOJ 3196] 213平衡树 【线段树套set + 树状数组套线段树】

    题目链接:BZOJ - 3196 题目分析 区间Kth和区间Rank用树状数组套线段树实现,区间前驱后继用线段树套set实现. 为了节省空间,需要离线,先离散化,这样需要的数组大小可以小一些,可以卡过 ...

  3. BZOJ 1901 Zju2112 Dynamic Rankings 树状数组套线段树

    题意概述:带修改求区间第k大. 分析: 我们知道不带修改的时候直接上主席树就可以了对吧?两个版本号里面的节点一起走在线段树上二分,复杂度是O((N+M)logN). 然而这里可以修改,主席树显然是凉了 ...

  4. HDU 5877 2016大连网络赛 Weak Pair(树状数组,线段树,动态开点,启发式合并,可持久化线段树)

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Tota ...

  5. st表、树状数组与线段树 笔记与思路整理

    已更新(2/3):st表.树状数组 st表.树状数组与线段树是三种比较高级的数据结构,大多数操作时间复杂度为O(log n),用来处理一些RMQ问题或类似的数列区间处理问题. 一.ST表(Sparse ...

  6. bzoj 3110: [Zjoi2013]K大数查询 树状数组套线段树

    3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1384  Solved: 629[Submit][Stat ...

  7. [BZOJ 1901] Dynamic Rankings 【树状数组套线段树 || 线段树套线段树】

    题目链接:BZOJ - 1901 题目分析 树状数组套线段树或线段树套线段树都可以解决这道题. 第一层是区间,第二层是权值. 空间复杂度和时间复杂度均为 O(n log^2 n). 线段树比树状数组麻 ...

  8. POJ 1195 Mobile phones (二维树状数组或线段树)

    偶然发现这题还没A掉............速速解决了............. 树状数组和线段树比较下,线段树是在是太冗余了,以后能用树状数组还是尽量用......... #include < ...

  9. 【BZOJ3196】二逼平衡树(树状数组,线段树)

    [BZOJ3196]二逼平衡树(树状数组,线段树) 题面 BZOJ题面 题解 如果不存在区间修改操作: 搞一个权值线段树 区间第K大--->直接在线段树上二分 某个数第几大--->查询一下 ...

随机推荐

  1. SpringMVC -- 必知必会

    SpringMVC基于模型--视图--控制器(Model-View-Controller,MVC)模式实现,属于SpringFrameWork的后续产品,已经融合在SpringWebFlow里面.它通 ...

  2. [ BZOJ 2038 ] Hose

    \(\\\) Description 给出一个数列,多次询问,每次给出一个区间 \([l_i,r_i]\) ,问在区间中随意选两个位置不同的数,是同一个数的概率有多大. \(n,m\le 5\time ...

  3. js对象数组深度去重和深度排序

    使用collect.js处理数组和对象 https://github.com/ecrmnn/collect.js/# 引入collect.js https://github.com/ecrmnn/co ...

  4. windows 下使用sqlplus

    1.连接数据库 找到tnsnames.ora 监听器配置 TESTDB = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = "服务器端ip ...

  5. 怎样在nexus 中 搜索到远程maven仓库中的jar 文件

    怎样在nexus 中 搜索到远程maven仓库中的jar 文件 url: http://www.oschina.net/question/95712_21999 点击Administration菜单下 ...

  6. Jmeter之https请求

    Jmeter之录制https脚本,网上介绍了好多种方法,大家自行百度. 如果手写https脚本,该如何做呢? 方法:http信息头管理器,加入User-Agent参数 案例:手写百度的搜索:哈哈  请 ...

  7. C#通过post发送接收数据流

    发送数据流方法 /// <summary> /// /// </summary> /// <param name="url">目标url< ...

  8. get data from splunk

    link: http://dev.splunk.com/view/python-sdk/SP-CAAAER5 download SDK & setup with python code: im ...

  9. JAVA基础——对象流

    对象的输入输出流的作用: 用于写入对象 的信息和读取对象的信息. 使得对象持久化.    ObjectInputStream   : 对象输入流    ObjectOutPutStream  :对象输 ...

  10. React入门介绍(2)- React Component-React组件

    React Component-React组件 允许用户自由封装组件是React非常突出的特性,用户可将自己创建的组件像普通的HTML标签一样插入页面,React.CreateClass方法就是用来创 ...