HDU5124:lines(线段树+离散化)或(离散化思想)
http://acm.hdu.edu.cn/showproblem.php?pid=5124
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.
题意:给定 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(线段树+离散化)或(离散化思想)的更多相关文章
- POJ-2528 Mayor's posters (线段树区间更新+离散化)
题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...
- UVa 1471 Defense Lines - 线段树 - 离散化
题意是说给一个序列,删掉其中一段连续的子序列(貌似可以为空),使得新的序列中最长的连续递增子序列最长. 网上似乎最多的做法是二分查找优化,然而不会,只会值域线段树和离散化... 先预处理出所有的点所能 ...
- POJ-2528 Mayor's posters(线段树区间更新+离散化)
http://poj.org/problem?id=2528 https://www.luogu.org/problem/UVA10587 Description The citizens of By ...
- 线段树(单标记+离散化+扫描线+双标记)+zkw线段树+权值线段树+主席树及一些例题
“队列进出图上的方向 线段树区间修改求出总量 可持久留下的迹象 我们 俯身欣赏” ----<膜你抄> 线段树很早就会写了,但一直没有总结,所以偶尔重写又会懵逼,所以还是要总结一下. ...
- POJ2528:Mayor's posters(线段树区间更新+离散化)
Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
- HDU 4288 Coder 【线段树+离线处理+离散化】
题意略. 离线处理,离散化.然后就是简单的线段树了.需要根据mod 5的值来维护.具体看代码了. /* 线段树+离散化+离线处理 */ #include <cstdio> #include ...
- LightOJ 1089 - Points in Segments (II) 线段树区间修改+离散化
http://www.lightoj.com/volume_showproblem.php?problem=1089 题意:给出许多区间,查询某个点所在的区间个数 思路:线段树,由于给出的是区间,查询 ...
- poj_2528 Mayor's posters (线段树经典题+离散化方法)
由于题面中给定的wall最大长度为10 000 000:若简单用线段树势必会超时. 而注意到题面中规定了输入海报的个数<=1000:因此不妨离散化,使得线段中叶节点仅含有1000个,那么线段最大 ...
- poj2528(线段树区间替换&离散化)
题目链接: http://poj.org/problem?id=2528 题意: 第一行输入一个 t 表 t 组输入, 对于每组输入: 第一行 n 表接下来有 n 行形如 l, r 的输入, 表在区 ...
随机推荐
- PHPCMS v9在后台文章管理列表添加类别
进入PHPCMS v9后台—内容,进入PHPCMS的文章管理列表,要实现在文章标题前显示文章类别,就是可以直接在文章列表里看到类别,不需要点击进入编辑页面才可以看到,如下图: PHPCMS v9在后台 ...
- visual studio 2017使用NHibernate4.0连接oracle11g数据库
之前一直是公司用NHibernate2.1来做项目,连接oracle 10g的数据库,配置NHibernate的东西都是以前的同事做好了的,也怪自己太懒了,没尝试过配置这个东西,虽然一直在使用NHib ...
- 从头认识java-16.5 nio的数据转换
这一章节我们来讨论一些nio的数据转换. 上面一章节我们提到ByteBuffer,可是他是面向二进制数据,对于编程来说不是非常方便,因此,java添加了转换数据的工具. 1.asCharBuffer ...
- ios开发之--新手引导页的添加
以往在写启动页面的时候,有时候会直接在启动页里面写,或者自带的vc里面直接写,但是那样并不是很方便,启动页里面往往会添加很多的东西,所以封装成一个单独的类,可以直接使用,即便是后期的更换,或者是其他的 ...
- iOS根据文字字数动态确定Label宽高
我们有时候在写项目的时候,会碰到,意见反馈,还有其他地方,讲座活动细则等需要大篇展示的文本, 因为每次服务器返回的内容大小不一,所以需要动态的调整label的宽高: 在ios 6 的时候可以: -(v ...
- docker学习-docker容器
- 【Thinkphp5】结合layer弹窗 定制操作结果页面
1 打开应用公共文件页面 appliction/common.php,编写以下代码 注意: 成功消息的绿色背景部分是iframe 框架写法,如果是普通页面.就吧parent去除,改为: self ...
- 注册和删除Apache服务器的方法
Apache服务器的安装和卸载方法 下载Apache安装包 将Apache文件夹存在桌面或其他盘,输入cmd打开命令提示行 安装步骤:进入Apache安装目录下的bin目录: cd C:\Us ...
- JS-缓冲运动-对联型悬浮框
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 谈谈KV存储集群的设计要点
版权声明:本文由廖念波原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/150 来源:腾云阁 https://www.qclo ...