Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2047 Accepted Submission(s): 893 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.
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
Source
Recommend heyang |
题目大意:给你N条线段,问你覆盖点最多的次数
思路:数据过大的,不适合我们保存线段的位置,离散化以后就基本上是左端点+1右端点-1的处理,最后区间更新即可
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=500000+5;
int a[maxn];
int c[maxn];
int n,m;
struct node
{
int l,r;
}s[maxn];
int lowbit(int x)
{
return x&(-x);
}
void add(int x,int val)
{
while(x)
{
c[x]+=val;
x-=lowbit(x);
}
}
int sum(int x)
{
int ans=0;
while(x<=m+1)
{
ans+=c[x];
x+=lowbit(x);
}
return ans;
}
int bin(int key,int n,int a[])
{
int l=0,r=n-1;
while(l<=r)
{
int m=(l+r)>>1;
if(a[m]==key)return m;
if(a[m]<key)l=m+1;
else r=m-1;
}
return -1;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(c,0,sizeof(c));
scanf("%d",&n);
int cnt=0,l,r;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&l,&r);
s[i].l=l,s[i].r=r;
a[cnt++]=l,a[cnt++]=r;
}
sort(a,a+cnt);
m=1;
for(int i=1;i<cnt;i++)
if(a[i]!=a[i-1])a[m++]=a[i];
for(int i=m-1;i>=0;i--)
if(a[i]!=a[i-1]+1)a[m++]=a[i-1]+1;
sort(a,a+m);
for(int i=1;i<=n;i++)
{
l=bin(s[i].l,m,a);
r=bin(s[i].r,m,a);
l+=2;r+=2;
add(l-1,-1);add(r,1);
}
int ans=0;
for(int i=2;i<=m+1;i++)ans=max(ans,sum(i));
printf("%d\n",ans);
}
}