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. CentOS 7 下配置 firewalld(firewall-cmd)实现 NAT 转发 软路由

    如果配合 DHCP 服务或实现更多功能. ☼ NAT 转发软路由 开启 NAT 转发之后,只要本机可以上网,不论是单网卡还是多网卡,局域网内的其他机器可以将默认网关设置为已开启 NAT 转发的服务器 ...

  2. core\stm32f10x.h(244): error: #67: expected a "}"

  3. Android学习笔记(十三) Handler

    可用于解决上一则笔记所提到的WorkerThread无法修改UI控件的问题 一.Handler.Looper和MessageQueue的基本原理 Handler把消息对象放到MessageQueue当 ...

  4. VM virtualBox设置无缝全屏

    设置之前:

  5. vba,excel,身份证,照片

    Sub 插入图片() '调整单元格大小,以适应图片大小 功能 插入身份证照片打印 - 正面在单元格d6       反面单元格d10 ActiveSheet.Pictures.Delete '清理过期 ...

  6. 模块 (Module)

    #1.模块概念的官网描述 -- Module If you quit from the Python interpreter and enter it again, the definitions y ...

  7. Ajax请求WebService跨域问题

    1.背景 用Jquery中Ajax方式在asp.net开发环境中WebService接口的调用 2.出现的问题 原因分析:浏览器同源策略的影响(即JavaScript或Cookie只能访问同域下的内容 ...

  8. cookie和session的用法用途,执行流程,区别联系

    1.为什么要有cookie/session?在客户端浏览器向服务器发送请求,服务器做出响应之后,二者便会断开连接(一次会话结束).那么下次用户再来请求服务器,服务器没有任何办法去识别此用户是谁.比如w ...

  9. ansible中yaml语法应用

    4.yaml语法应用 ansible的playbook编写是yaml语言编写,掌握yaml语法是编写playbook的必要条件,格式要求和Python相似,具体教程参考如下 yaml语言教程 附上一个 ...

  10. Android突破64K限制

    1.添加依赖 android{ defaultConfig{ ... multiDexEnabled true ... } } dependencies{ compile 'com.android.s ...