hdu 5124 lines(树状数组)
2016-07-12 22:48
309 查看
lines
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1575 Accepted Submission(s): 656
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
1 1
2 2
3 3
4 4
5 5
Sample Output
3
1
题意:x轴上很多段区间,求覆盖最多的一个点的覆盖次数
思路:首先很容易想到这个点一定可以是某一段区间的端点,然后这里给了区间,要求一个点的出现次数,想到什么?
没错,区间更新,单点求和。 当然用线段树更加直观,只是本人确实不是很喜欢线段树...于是就用树状数组实现了
与单点求和,区间求和的区别在于更新区间的时候add(l,1)和add(r+1,-1) 单点求和就是直接sum(i)就可以了~
关于为什么是这样,本人也没怎么理解深刻,就不多解释了(快上车)。
注意本题要离散化处理
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 100500
int a
,b
,p[2*N];
int c[N*2];
int maxn;
int lowbit(int x)
{
return x&-x;
}
void add(int x,int v)
{
for(int i=x; i<=maxn; i+=lowbit(i))
c[i]+=v;
}
int sum(int x)
{
int ans=0;
for(int i=x; i; i-=lowbit(i))
ans+=c[i];
return ans;
}
int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
memset(c,0,sizeof(c));
scanf("%d",&n);
maxn=2*n;
for(int i=1; i<=n; i++)
{
scanf("%d %d",&a[i],&b[i]);
p[i]=a[i],p[i+n]=b[i];
}
sort(p+1,p+1+2*n);
for(int i=1; i<=n; i++)
{
int l=lower_bound(p+1,p+1+2*n,a[i])-p;
int r=lower_bound(p+1,p+1+2*n,b[i])-p;
add(l,1);
add(r+1,-1);
}
int ans=1;
for(int i=1;i<=2*n;i++)
{
int s=sum(i);
ans=max(ans,s);
}
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- MATLAB的solve函数
- C#解析Json字符串
- 一般处理文件
- POJ 多项式加法
- 【C++笔记】函数中参数传递
- 线段树 矩形周长
- rapidxml vtd-xml pugxml tinyxml2 性能对比,备忘
- 设计模式之单例模式
- Scala 闭包(闭包是一个函数)
- JAVA程序单实例运行的实现方法
- HDU1008 Elevator
- HDU 1142 A Walk Through the Forest (记忆化搜索+Dijkstra算法)
- Maven 常用命令
- xml格式化成json
- 后台提示
- BZOJ2679 : [Usaco2012 Open]Balanced Cow Subsets
- 虚幻4:优化 CPU Profiling
- 递归那些事儿
- 移动前端开发之viewport的深入理解
- map与set的使用