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,则插一个空 ...
随机推荐
- 添加JSON Data到已经存在的JSON文件中
早上在学习<Post model至Web Api创建或是保存数据>http://www.cnblogs.com/insus/p/4343833.html ,如果你第二添加时,json文件得 ...
- [转]使用 google gson 转换Timestamp或Date类型为JSON字符串.
创建类型适配类: import java.lang.reflect.Type; import java.sql.Timestamp; import java.text.DateFormat; impo ...
- C#计算文件的MD5值实例
C#计算文件的MD5值实例 MD5 是 Message Digest Algorithm 5(信息摘要算法)的缩写,MD5 一种散列(Hash)技术,广泛用于加密.解密.数据签名和数据完整性校验等方面 ...
- Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- git push ERROR: missing Change-Id in commit message footer
今天上传代码时候报告错误:$ git push origin HEAD:refs/for/branch*Counting objects: 7, done.Delta compression usin ...
- SQL Server读懂语句运行的统计信息 SET STATISTICS TIME IO PROFILE ON
对于语句的运行,除了执行计划本身,还有一些其他因素要考虑,例如语句的编译时间.执行时间.做了多少次磁盘读等. 如果DBA能够把问题语句单独测试运行,可以在运行前打开下面这三个开关,收集语句运行的统计信 ...
- ZooKeeper 笔记(3) 实战应用之【统一配置管理】
大型应用通常会按业务拆分成一个个业务子系统,这些大大小小的子应用,往往会使用一些公用的资源,比如:需要文件上传.下载时,各子应用都会访问公用的Ftp服务器.如果把Ftp Server的连接IP.端口号 ...
- Alpha版本测试报告
请根据团队项目中软件的需求文档.功能规格说明书和技术规格说明书,写出软件的测试计划.测试过程和测试结果,并回答下述问题. 1. 在测试过程中发现了多少Bug? 2. 你是怎么进行场景测试(scenar ...
- 从零开始,将ASP.NET Core部署到Linux生产环境
研究.NET Core已经一段时间了,一直都是在Windows上开发,这2天尝试着将公司一个很简单的内部Web项目改造成了ASP.NET Core,并且部署到Linux上.生产环境如下: Linux ...
- C# 7.0 新特性4: 返回引用
本文参考Roslyn项目中的Issue:#118. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性3: 模式匹配 ...