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. Eclipse的调试功能的10个小窍门

    你可能已经看过一些类似“关于调试的N件事”的文章了.但我想我每天大概在调试上会花掉1个小时,这是非常多的时间了.所以非常值得我们来了解一些用得到的功能,可以帮我们节约很多时间.所以在这个主题上值得我再 ...

  2. 拼凑sql语句另外一个方法

    经常拼凑sql语句,经常是手工拼写 也可以利用字典另外一个模式拼凑 这里采用的是Dictionary中的Aggregate方法. 代码如下: static void Main(string[] arg ...

  3. Cocos2d-x3.0触摸

    cocos2d-x 3.0開始採用C++11,并開始脱离OC风格,在触摸处理上也作出了改变 C++11带来了Lambda表达式(匿名函数),它将简化程序编写,可使代码更清晰易懂 在旧2.x版本号的触摸 ...

  4. HTML&CSS精选笔记_CSS高级技巧

    CSS高级技巧 CSS精灵技术 需求分析 CSS精灵是一种处理网页背景图像的方式.它将一个页面涉及到的所有零星背景图像都集中到一张大图中去,然后将大图应用于网页,这样,当用户访问该页面时,只需向服务发 ...

  5. MySQL建表字段类型

    1.数据库:在MySQL中,要存储数据,必须从数据库开始,因此首先要创建数据库,但由于学校的MySQL服务器对学生数据帐号有限止,学生不得创建数据库,故每个学生的帐号中已事先由信息中心为大家建立了一个 ...

  6. PHP防止跨站表单提交与同站跨页伪造表单的攻击

    在以前的防止跨站攻击的时候,使用了验证提交的页面是否是同一个站点,这样可以防止普通的攻击,ereg("blog.qita.in",$_SERVER['HTTP_REFERER']) ...

  7. php学习一:语法规则

    1.书写规则 在html中嵌入php的时候,需要有结束语,即<?php ...?>,在靠近结束符号的最后一个语句可以不用写分号: 但是在单独的php中,最后可以不用以?>来结尾; 2 ...

  8. 在js中通过call或者apply实现继承

    通过call或者apply可以实现函数里面this的改变,利用这一特点,可以实现继承 代码如下所示: /*父类*/ function Parent(add,net,no,teacher) { this ...

  9. 《C++ Primer Plus》12.7 队列模拟 学习笔记

    Heather银行打算在Food Heap超市开设一个自动柜员机(ATM).Food Heap超市的管理者担心排队使用ATM的人流会干扰超市的交通,希望限制排队等待的人数.Heather银行希望对顾客 ...

  10. JDBC--Result 获取返回集合

    package jdbc.chap05; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql. ...