http://acm.hdu.edu.cn/showproblem.php?pid=5124

Problem Description
John has several lines. The lines are covered on the X axis. Let A is a point which is covered by the most lines. John wants to know how many lines cover A.
 
Input
The first line contains a single integer T(1≤T≤100)(the data for N>100 less than 11 cases),indicating the number of test cases.
Each test case begins with an integer N(1≤N≤105),indicating the number of lines.
Next N lines contains two integers Xi and Yi(1≤Xi≤Yi≤109),describing a line.
 
Output
For each case, output an integer means how many lines cover A.
 
Sample Input
2
5
1 2
2 2
2 4
3 4
5 1000
5
11
2 2
3 3
4 4
5 5
 
Sample Output
3
1
 题目解析:

题意:给定 n 个区间,问最多重复的子区间?

题解:(离散化思想)讲所有的数都排个序,将区间的左值定为 1 ,右值定为 -1 ,这样对所有的数搜一遍过去找最大的值即可;或者用线段树+离散化。

一:线段树

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#define N 100010
using namespace std;
struct li
{
int x,y;
}line[N];
struct node
{
int l,r;
int lz;
}q[*N];
int n,tt,X[*N];
int maxx;
void build(int l,int r,int rt)
{
q[rt].l=l;
q[rt].r=r;
q[rt].lz=;
if(l==r)
return ;
int mid=(l+r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
return ;
}
void pushdown(int rt)
{
if(q[rt].lz)
{
q[rt<<].lz+=q[rt].lz;
q[rt<<|].lz+=q[rt].lz;
q[rt].lz=;
}
}
void update(int lf,int rf,int l,int r,int rt)
{
if(lf<=l&&rf>=r)
{
q[rt].lz+=;
return ;
}
pushdown(rt);
int mid=(l+r)>>;
if(lf<=mid) update(lf,rf,l,mid,rt<<);
if(rf>mid) update(lf,rf,mid+,r,rt<<|);
return ;
}
void query(int l,int r,int rt)
{ if(l==r)
{
maxx=max(maxx,q[rt].lz);
return ;
}
pushdown(rt);
int mid=(l+r)>>;
query(l,mid,rt<<);
query(mid+,r,rt<<|);
return ;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
tt=;
maxx=-;
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d%d",&line[i].x,&line[i].y);
X[tt++]=line[i].x;
X[tt++]=line[i].y;
}
sort(X,X+tt);
int sum=unique(X,X+tt)-X;
build(,sum,);
for(int i=;i<n;i++)
{
int le=lower_bound(X,X+sum,line[i].x)-X+;
int re=lower_bound(X,X+sum,line[i].y)-X+;
if(le<=re) update(le,re,,sum,);
}
query(,sum,);
printf("%d\n",maxx);
}
return ;
}

二:离散化:思路:可以把一条线段分出两个端点离散化,左端点被标记为-1,右端点被标记为1,然后排序,如果遇到标记为-1,cnt++,否则cnt--;找出cnt的最大值。

#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 200010
using namespace std; struct node
{
int x,c;
bool operator<(const node &a)const
{
return (x<a.x)||(x==a.x&&c<a.c);
}
}p[maxn]; int t;
int n; int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=; i<n; i++)
{
int s,e;
scanf("%d%d",&s,&e);
p[i*].x=s;
p[i*].c=-;
p[i*+].x=e;
p[i*+].c=;
}
sort(p,p+*n);
int cnt=; int ans=;
for(int i=; i<*n; i++)
{
if(p[i].c==-)
{
cnt++;
}
else
cnt--;
ans=max(ans,cnt);
}
printf("%d\n",ans);
}
return ;
}

可惜做BC时,这两种方法都没想出来,悲催!

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <algorithm>
#include <iostream>
#define PI acos( -1.0 )
using namespace std;
typedef long long ll; const int NO = 1e5 + ;
struct ND
{
int x, y;
}st[NO<<];
int n; bool cmp( const ND &a, const ND &b )
{
if( a.x == b.x ) return a.y > b.y;
return a.x < b.x;
} int main()
{
int T;
scanf( "%d",&T );
while( T-- )
{
scanf( "%d", &n );
int cur = ;
for( int i = ; i < n; ++i )
{
scanf( "%d", &st[cur].x );
st[cur++].y = ;
scanf( "%d", &st[cur].x );
st[cur++].y = -;
}
sort( st, st+cur, cmp );
int ans = ;
int Max = ;
for( int i = ; i < cur; ++i )
{
ans += st[i].y;
Max = max( ans, Max );
}
printf( "%d\n", Max );
}
return ;
}

HDU5124:lines(线段树+离散化)或(离散化思想)的更多相关文章

  1. POJ-2528 Mayor's posters (线段树区间更新+离散化)

    题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...

  2. UVa 1471 Defense Lines - 线段树 - 离散化

    题意是说给一个序列,删掉其中一段连续的子序列(貌似可以为空),使得新的序列中最长的连续递增子序列最长. 网上似乎最多的做法是二分查找优化,然而不会,只会值域线段树和离散化... 先预处理出所有的点所能 ...

  3. POJ-2528 Mayor's posters(线段树区间更新+离散化)

    http://poj.org/problem?id=2528 https://www.luogu.org/problem/UVA10587 Description The citizens of By ...

  4. 线段树(单标记+离散化+扫描线+双标记)+zkw线段树+权值线段树+主席树及一些例题

    “队列进出图上的方向 线段树区间修改求出总量 可持久留下的迹象 我们 俯身欣赏” ----<膜你抄>     线段树很早就会写了,但一直没有总结,所以偶尔重写又会懵逼,所以还是要总结一下. ...

  5. POJ2528:Mayor's posters(线段树区间更新+离散化)

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

  6. POJ 2528 Mayor's posters (线段树区间更新+离散化)

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

  7. HDU 4288 Coder 【线段树+离线处理+离散化】

    题意略. 离线处理,离散化.然后就是简单的线段树了.需要根据mod 5的值来维护.具体看代码了. /* 线段树+离散化+离线处理 */ #include <cstdio> #include ...

  8. LightOJ 1089 - Points in Segments (II) 线段树区间修改+离散化

    http://www.lightoj.com/volume_showproblem.php?problem=1089 题意:给出许多区间,查询某个点所在的区间个数 思路:线段树,由于给出的是区间,查询 ...

  9. poj_2528 Mayor's posters (线段树经典题+离散化方法)

    由于题面中给定的wall最大长度为10 000 000:若简单用线段树势必会超时. 而注意到题面中规定了输入海报的个数<=1000:因此不妨离散化,使得线段中叶节点仅含有1000个,那么线段最大 ...

  10. poj2528(线段树区间替换&离散化)

    题目链接: http://poj.org/problem?id=2528 题意: 第一行输入一个 t 表 t 组输入, 对于每组输入: 第一行  n 表接下来有 n 行形如 l, r 的输入, 表在区 ...

随机推荐

  1. Windows "计划任务"功能设置闹钟~

    相信很多人和我一样在使用电脑时都会遇到这样一个麻烦:不知道如何在windows 中设置一个闹铃.当我们在“开始”菜单的所有程序中找了一遍又一遍,甚至使用Everything.exe做全盘的搜索,都没有 ...

  2. [ACM] poj 2017 Speed Limit

    Speed Limit Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 17030   Accepted: 11950 Des ...

  3. Template类的使用指南【python】

    转自:http://www.jb51.net/article/55011.htm

  4. STM32学习之路之MDK安装篇

  5. 编写高性能的 Lua 代码

    前言 Lua是一门以其性能著称的脚本语言,被广泛应用在很多方面,尤其是游戏.像<魔兽世界>的插件,手机游戏<大掌门><神曲><迷失之地>等都是用Lua来 ...

  6. COM组件技术名称解释

    GUID:全局唯一标识. CLSID 或 ProgID :唯一地表示一个组件服务程序,那么根据这些ID,就可以加载运行组件,并为客户端程序提供服务了. IID :唯一的表示接口ID. COM 组件是运 ...

  7. php数据访问之查询关键字

    本文根据数据库中的car表做一个汽车查询页面,巩固php查询关键字操作,感兴趣的小伙伴们可以参考一下   本文实例为大家分享了php查询操作的实现代码,供大家参考,具体内容如下 一.一个关键字查询 主 ...

  8. diamond types are not supported at this language level

    在intellij导入git项目之后出现 diamond types are not supported at this language level错误 或者String等报错 File->P ...

  9. nginx proxy模块

    环境: user:192.168.100.169 nginx代理:192.168.100.175 tomcat:192.168.100.175 域名:www.vijay.com  --->192 ...

  10. AB压力测试工具

    1.安装AB工具: yum install httpd-tools 2.测试: ab -n -c http://localhost.com/ 其中-n表示请求数,-c表示并发数 3.测试结果 [roo ...