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 的输入, 表在区 ...
随机推荐
- ubuntu下使用sublime text进行C编程开发尝鲜
1 选择编译系统 2 编写文件,编译(Ctrl+B)运行(Shift+Ctrl+B)
- 工欲善其事 之 Web 前端调试工具格式化混淆过的 JS 代码
工欲善其事 之 Web 前端调试工具格式化混淆过的 JS 代码 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致&q ...
- 超全面的JavaWeb笔记day08<Tomcat&Web应用&HTTP协议>
1.常用软件体系结构 BS:浏览器/服务器 CS:客户端/服务器 WEB资源 动态资源 JSP Servlet 静态资源 html 常用服务器 Tomcat Weblogic Resin JBOSS ...
- Ext3.4-EXT之嵌套表格的实现
其中使用到的"RowExpander.js"为extjs官方示例中自带的. 实现这个嵌套表格要注意两点技巧: 1 提供给外层表格的dataStore的数据源以嵌套数组的形式表示细节 ...
- Javascript中的感叹号和函数function
js函数前加分号和感叹号是什么意思?有什么用?:http://www.cnblogs.com/mq0036/p/4605255.html function与感叹号:https://swordair.c ...
- python2.0_day21_bbs系统评论自动加载+文章创建
day20中我们已经实现了bbs系统的前端展示,后台admin管理,以及前端动态显示顶部\登录和评论的分级展示功能.其中评论的分级展示功能最为复杂.上一节中我们只是在文章明细页面中加了一个button ...
- ionic简单路由及页面传参
1)页面跳转及传参方法 angular.module('app.routes', [])//routes路由模型 .config(function($stateProvider, $urlRouter ...
- 关于android定位的坐标系问题
按照正常的思路,我们通过GPS或者基站定位等方式获取到经纬度信息后,把它放到地图上,就能够完成定位.但实际上,我们很有可能会在实际操作中发现,我们的定位出现了较大的偏移.这是因为我国出于国家安全(或者 ...
- linux 个性化设置shell提示
1.linux 用户登录过程中 相关文件执行顺序: /etc/profile → /etc/profile.d/*.sh → ~/.bash_profile → ~/.bashrc → [/etc/b ...
- java高级---->Thread之BlockingQueue的使用
今天我们通过实例来学习一下BlockingQueue的用法.梦想,可以天花乱坠,理想,是我们一步一个脚印踩出来的坎坷道路. BlockingQueue的实例 官方文档上的对于BlockingQueue ...