http://acm.hdu.edu.cn/showproblem.php?pid=5124
区间修改、单点查询参考:https://blog.csdn.net/lanshan1111/article/details/86352346
linesTime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1997 Accepted Submission(s): 872 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 |
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 100500
int a[N],b[N],p[2*N];
int c[N*2];
int maxn;
int lowbit(int x)
{
return x&-x;
}
int lowbits(int x)
{
return x&(-x);
}
void add(int x,int v)
{
for(int i=x; i<=maxn; i+=lowbit(i))
c[i]+=v;
}
void adds(int x,int v)
{
for(int i=x; i<=maxn; i+=lowbits(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 sums(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;
}