hdu5124 线段树+离散化
题意:令a[l..r]都+1,求a[1..n]的最大值
裸的成段更新+区间最值,但是本题坐标范围很大(10^9),所以需要离散化
顺便离散化模板get
离散化的基本思路:
设一共有m个数,范围1--n (n>>m)
先用数组X[1..m]存下这些数,然后对X从小到大排序
每次读入一个数p时,在X中二分查找p,p在数组X中的位置对应的数组下标就是p离散化之后的值
这样就成功把范围1--n的数压缩到了1--m以内。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> #define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define lc rt << 1
#define rc rt << 1 | 1 using namespace std; const int MAXN = ; struct node
{
int l, r;
}; node D[MAXN];
int limit, Q;
int maxi[ MAXN << ];
int lazy[ MAXN << ];
int N;
int X[MAXN]; void build( int l, int r, int rt )
{
maxi[rt] = lazy[rt] = ;
if ( l == r ) return;
int m = ( l + r ) >> ;
build( lson );
build( rson );
return;
} void PushUp( int rt )
{
maxi[rt] = max( maxi[lc], maxi[rc] );
return;
} void PushDown( int rt )
{
if ( lazy[rt] )
{
lazy[lc] += lazy[rt];
lazy[rc] += lazy[rt];
maxi[lc] += lazy[rt];
maxi[rc] += lazy[rt];
lazy[rt] = ;
}
return;
} void update( int L, int R, int l, int r, int rt )
{
if ( L <= l && r <= R )
{
lazy[rt] += ;
maxi[rt] += ;
return;
}
PushDown( rt );
int m = ( l + r ) >> ;
if ( L <= m ) update( L, R, lson );
if ( R > m ) update( L, R, rson );
PushUp( rt );
return;
} int query( int L, int R, int l, int r, int rt )
{
if ( L <= l && r <= R )
{
return maxi[rt];
}
PushDown( rt );
int m = ( l + r ) >> ; int res = -;
if ( L <= m ) res = max( res, query( L, R, lson ) );
if ( R > m ) res = max( res, query( L, R, rson ) );
PushUp( rt );
return res;
} int Bin(int key,int n,int X[]) {
int l = , r = n - ;
while (l <= r) {
int m = (l + r) >> ;
if (X[m] == key) return m;
if (X[m] < key) l = m + ;
else r = m - ;
}
return -;
} int main()
{
int T;
scanf( "%d", &T );
while ( T-- )
{
scanf( "%d", &Q );
int nnd=;
N = ;
for ( int i = ; i < Q; ++i )
{
int u, v;
scanf( "%d%d", &u, &v );
if (v>N) N=v;
D[i].l = u, D[i].r = v;
X[nnd++]=u;
X[nnd++]=v;
}
//build( 1, N, 1 );
//for (int i=0;i<Q;i++)
// update( D[i].l, D[i].r, 1, N, 1 ); sort(X , X + nnd);
int m = ;
for (int i = ; i < nnd; i ++) {
if (X[i] != X[i-]) X[m ++] = X[i];
}
for (int i = m - ; i > ; i --) {
if (X[i] != X[i-] + ) X[m ++] = X[i-] + ;
}
sort(X , X + m);
build(,m-,);
for (int i = ; i < Q ; i ++) {
int l = Bin(D[i].l, m, X);
int r = Bin(D[i].r, m, X);
update(l, r , , m - , );
} //int ans = query( 1, N , 1, N, 1 );
int ans=query(,m-,,m-,);
printf( "%d\n", ans);
//update( D[i].l, D[i].r, 1, N, 1 );
}
return ;
}
hdu5124 线段树+离散化的更多相关文章
- HDU5124:lines(线段树+离散化)或(离散化思想)
http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines ...
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- poj 2528 Mayor's posters(线段树+离散化)
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
- [poj2528] Mayor's posters (线段树+离散化)
线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...
- [UESTC1059]秋实大哥与小朋友(线段树, 离散化)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1059 普通线段树+离散化,关键是……离散化后建树和查询都要按照基本法!!!RE了不知道多少次………………我真 ...
- poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
- BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针
BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针 Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间, ...
- D - Mayor's posters(线段树+离散化)
题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...
- 主席树||可持久化线段树+离散化 || 莫队+分块 ||BZOJ 3585: mex || Luogu P4137 Rmq Problem / mex
题面:Rmq Problem / mex 题解: 先离散化,然后插一堆空白,大体就是如果(对于以a.data<b.data排序后的A)A[i-1].data+1!=A[i].data,则插一个空 ...
随机推荐
- ASP.NET MVC的Web Api的实练
学习ASP.NET MVC一年多来,现在该学学Web Api了.API与ASP.NET MVC的Controller差不多.前者只是返回数据序列化和发送给客户端: 后者返回View或Render Vi ...
- struts2: 通过流输出实现exce导出
参考下面代码,在Action中加一个方法: // 导出excel public String excel() throws Exception { StringBuffer excelBuf = ne ...
- ReactNative运行提示缺少文件xxxRootView.h解决方法
我们经常在github获取源码后运行会出现缺少 "RCTRootView.h" notfound" 文件的错误,对于这种错误我们怎么解决了. 1.cd到项目根目录 删除项 ...
- nios II--实验7——数码管IP硬件部分
数码管 硬件开发 新建原理图 打开Quartus II 11.0,新建一个工程,File -> New Project Wizard…,忽略Introduction,之间单击 Next> ...
- java中的static详解
如果一个类成员被声明为static,它就能够在类的任何对象创建之前被访问,而不必引用任何对象.static 成员的最常见的例子是main( ) .因为在程序开始执行时必须调用main() ,所以它被声 ...
- SVN 修改log信息报错的解决方案
要实现允许修改log这个功能,只需要在hooks目录下增加一个名为:pre-revprop-change.bat的文件,重启svn即可.该文件内容为:------------------------- ...
- 清空KindEditor富文本编辑器里面的内容方法
//清空KindEditorKindEditor.instances[0].html(""); 0表示第一个KindEditor编辑器对象 详情见链接:http://www.new ...
- 东大OJ-快速排序
1236: Simple Sort 时间限制: 1 Sec 内存限制: 128 MB 提交: 195 解决: 53 [提交][状态][讨论版] 题目描述 You are given n ...
- [Bundling and Minification ] 一、如何绑定
绑定和压缩(缩小)是ASP.NET 4.5出现的用来提高程序性能的两个重要的技术.绑定(Bundling)是将多个文件合并为一个文件,压缩(Minification)主要是将文件缩小,如Js .CSS ...
- Shell命令_Cron使用
chkconfig crond on d表示damon,后台进程 chkconfig --list | grep crond crontab [选项] 选项: -e: 编辑crontab定时任务 -l ...