lines
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1347 Accepted Submission(s): 556
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≤10
5
)
,indicating the number of lines.
Next N lines contains two integers X
i![]()
and
Y
i
(1≤X
i
≤Y
i
≤10
9
)
,describing a line.
Each test case begins with an integer N(1≤N≤10
Next N lines contains two integers X
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
恩,题目大意就是说,每组测试数据中的每组数据表示X轴上的一段线段,然后一定存在一个点,覆盖它的线段最多,问的就是最多的线段条数。由于数据较大,需要离散化。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 100100
using namespace std;
int a[maxn],b[maxn];
int rec[maxn<<1];
struct lnode
{
int l,r,c,m;
};
lnode node[maxn<<2];
int maxi(int a,int b)
{
return a>b?a:b;
}
void pushup(int o)
{
node[o].m=maxi(node[o<<1].m,node[o<<1|1].m);
}
void build(int o,int l,int r)
{
node[o].l=l;
node[o].r=r;
node[o].m=0;
node[o].c=0;
if(l==r)
return ;
int mid=(l+r)>>1;
build(o<<1,l,mid);
build(o<<1|1,mid+1,r);
}
void pushdown(int o)
{
if(node[o].c)
{
node[o<<1].m+=node[o].c;
node[o<<1|1].m+=node[o].c;
node[o<<1].c+=node[o].c;
node[o<<1|1].c+=node[o].c;
node[o].c=0;
}
}
void update(int o,int l,int r)
{
if(node[o].l==l&&node[o].r==r)
{
node[o].m++;
node[o].c++;
return ;
}
pushdown(o);
int mid=(node[o].l+node[o].r)>>1;
if(r<=mid)
update(o<<1,l,r);
else if(l>mid)
update(o<<1|1,l,r);
else
{
update(o<<1,l,mid);
update(o<<1|1,mid+1,r);
}
pushup(o);
}
int bsearch(int l,int r,int x)
{
while(l<=r)
{
int mid=(l+r)>>1;
if(rec[mid]==x)
return mid;
if(rec[mid]>x)
r=mid-1;
else
l=mid+1;
}
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int j=1;
for(int i=0;i<n;++i)
{
scanf("%d%d",&a[i],&b[i]);
rec[j++]=a[i];
rec[j++]=b[i];
}
sort(rec+1,rec+j);
int m=2;
for(int k=2;k<j;++k)
{
if(rec[k]!=rec[k-1])
rec[m++]=rec[k];
}
build(1,1,m);
for(int i=0;i<n;++i)
{
int l=bsearch(1,m,a[i]);
int r=bsearch(1,m,b[i]);
update(1,l,r);
}
printf("%d\n",node[1].m);
}
return 0;
}