题意:令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 线段树+离散化的更多相关文章

  1. HDU5124:lines(线段树+离散化)或(离散化思想)

    http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines ...

  2. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

  3. poj 2528 Mayor's posters(线段树+离散化)

    /* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...

  4. [poj2528] Mayor's posters (线段树+离散化)

    线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...

  5. [UESTC1059]秋实大哥与小朋友(线段树, 离散化)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1059 普通线段树+离散化,关键是……离散化后建树和查询都要按照基本法!!!RE了不知道多少次………………我真 ...

  6. poj 2528 Mayor's posters 线段树+离散化技巧

    poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...

  7. BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针

    BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针 Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间, ...

  8. D - Mayor's posters(线段树+离散化)

    题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...

  9. 主席树||可持久化线段树+离散化 || 莫队+分块 ||BZOJ 3585: mex || Luogu P4137 Rmq Problem / mex

    题面:Rmq Problem / mex 题解: 先离散化,然后插一堆空白,大体就是如果(对于以a.data<b.data排序后的A)A[i-1].data+1!=A[i].data,则插一个空 ...

随机推荐

  1. Spring Security笔记:自定义登录页

    以下内容参考了 http://www.mkyong.com/spring-security/spring-security-form-login-example/ 接上回,在前面的Hello Worl ...

  2. ArcEngine选中面要素样式修改

    //只用前五行,可以直接将选中的面要素的颜色全部修改成红色,也就是填充颜色 IRgbColor pRgbColor= new RgbColor();; pRgbColor.Red = ; pRgbCo ...

  3. manifest资源提取工具

    因业务需要,写了个manifest资源提取工具,该机制是将html文件作为入口文件进行资源抓取.原理是先简单扫html token,然后直接遍历每个tag token是否属于需要的资源(css,js, ...

  4. js自定义事件

    自定义事件的本质,创建一个对象,然后把事件的名字作为对象的一个属性,然后value是一个[],把此事件的所以回调都push进去. 写一个很基本的,没有把对象暴露出去的js的自定义事件. var eve ...

  5. WinForm程序执行JS代码的多种方法以及使用WebBrowser与JS交互

    方法一 使用微软官方组件Interop.MSScriptControl 1.msscript.ocx下载的地址   http://www.microsoft.com/downloads/details ...

  6. 从 Microsoft SQL Server 迁移到 Oracle

    来源于:http://www.oracle.com/technetwork/cn/database/migration/sqlserver-095136-zhs.html Oracle SQL Dev ...

  7. js 对象数组根据对象中的属性排序

    function createComparisonFunction(propertyName){ return function(object1,object2){ var value1 = obje ...

  8. Qt5.3.0 for Android开发环境配置

    1.去官网下载Qt5.3.0 for Android 2.去http://developer.android.com下载Ndk 和SDk            3.去http://ant.apache ...

  9. iOS开发小技巧--计算label的Size的方法总结

    计算label的Size方法 sizeWithAttributes:方法 适用于不换行的情况,宽度不受限制的情况 /// 根据指定文本和字体计算尺寸 - (CGSize)sizeWithText:(N ...

  10. iOS开发小技巧--实现毛玻璃效果的方法

    一.美工出图 二.第三方框架 -- DRNRealTimeBlur,框架继承自UIView.使用方法:创建UIView直接继承自框架的View,就有了毛玻璃效果 三.CoreImage -- 图片加高 ...