Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2040 Accepted Submission(s): 889 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 | We have carefully selected several similar problems for you: 6460 6459 6458 6457 6456
|
题目大意:给你在X轴上的N条线段,问你最多覆盖的点的覆盖数量
思路:通过这个题我学会了离散化的一点知识,首先把所有的坐标映射到x轴上,然后将坐标压缩。最后的时候用树状数组维护点上线段的覆盖情况就可以了
代码:
#include<stdio.h>
#include<string>
#include<string.h>
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=100000+5;
int n;
int c[maxn*2];
int b[maxn*2];
int val[maxn*2];
int lowbit(int i)
{
return i&(-i);
}
int sum(int x)
{
int sum = 0;
while(x)
{
sum += c[x];
x -= lowbit(x);
}
return sum;
}
void add(int x, int val)
{
while(x <= maxn)
{
c[x] += val;
x += lowbit(x);
}
}
struct node1
{
int x, y;
} a1[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int cnt=0;
memset(c,0,sizeof(c));
memset(val,0,sizeof(val));
for(int i=1; i<=n; i++)
{
scanf("%d%d",&a1[i].x,&a1[i].y);
b[cnt++]=a1[i].x;
b[cnt++]=a1[i].y;
}
sort(b,b+2*n);
int len=unique(b,b+2*n)-b;
for(int i=1; i<=n; i++)
{
int pos1=lower_bound(b,b+len,a1[i].x)-b+1;
int pos2=lower_bound(b,b+len,a1[i].y)-b+1;
add(pos1,1);
add(pos2+1,-1);
}
int maxx=0;
for(int i=1; i<=len; i++)
{
maxx=max(sum(i),maxx);
}
cout<<maxx<<endl;
}
}