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. 一些常用的meta标签及其作用

    声明文档使用的字符编码  <meta charset='utf-8'>优先使用 IE 最新版本和 Chrome  <meta http-equiv="X-UA-Compat ...

  2. 合并百度影音的离线数据 with python 2.2 bdv格式的更新

    百度影音的bdv格式又有变化. 此次存在2种bdv格式. 格式1:每个文件夹内就一个bdv文件,文件合并后改名avi即可. 格式2:每个文件夹内一个bdv文件作为索引,其他附加guid的文件作为数据. ...

  3. 深度神经网络简述与Capsule介绍

    本人最近初学Hinton大神的论文<Dynamic Routing Between Capsules >,对深度神经网络的内容进行了简要总结,将观看“从传统神经网络的角度解读Capsule ...

  4. 大写URL转小写

    添加LowercaseRoutesMVC.dll引用.通过“管理—NuGet程序包”搜索LowercaseRoutesMVC,然后点击安装.安装成功后会自动引用LowercaseRoutesMVC.d ...

  5. Objective-C 是动态语言

    Objective-C 的动态性是由 runtime 相关的库赋予的. 当然其他语言也完全可以运行在一个 Runtime 库上而获得动态性,由于多数高级语言的诞生都对应着一种编译器,因此将编译器的特性 ...

  6. java线程学习1

    java实现多线程有三种方式:继承Thread类,重写run方法,启动使用start:实现runnable接口,重写run方法:实现callable接口,重写call方法(可以有返回值,也可以抛出异常 ...

  7. Maven的pom报错的解决方法

    如果在MyEclipse里面导入项目,导入不了,如下图 接下来可以点击Import Maven Projects里的Action那一行Resolve Later. 点击Do Not Execute(a ...

  8. 第3节 mapreduce高级:4、倒排索引的建立

    倒排索引建立 需求分析 需求:有大量的文本(文档.网页),需要建立搜索索引 最终实现的结果就是哪个单词在哪个文章当中出现了多少次 思路分析: 首选将文档的内容全部读取出来,加上文档的名字作为key,文 ...

  9. ''tensorflow.python.framework.errors_impl.ResourceExhaustedError: OOM when allocating tensor with shape[?]'' 错误分析

    这是tensorflow 一个经常性错误,错误的原因在于:显卡内存不够. 解决方法就是降低显卡的使用内存,途径有以下几种措施: 1 减少Batch 的大小 2 分析错误的位置,在哪一层出现显卡不够,比 ...

  10. BZOJ2212【POI2011】ROT:Tree Rotation 线段树合并

    题意: 给一棵n(1≤n≤200000个叶子的二叉树,可以交换每个点的左右子树,要求叶子遍历序的逆序对最少. 分析: 求逆序对我们可以想到权值线段树,所以我们对每个点建一颗线段树(为了避免空间爆炸,采 ...