| ||||||||||
linesTime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 382 Accepted Submission(s): 187
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
Sample Output
Source
|
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
int X[maxn*2],N;
struct node
{
int x,y;
node(){}
node(int l,int r):x(l),y(r){}
}line[maxn];
struct IntervalTree
{
int sum[maxn<<3],add[maxn<<3];
void build(int o,int l,int r)
{
sum[o]=add[o]=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(add[o])
{
add[o<<1]+=add[o];
add[o<<1|1]+=add[o];
sum[o<<1]+=add[o];
sum[o<<1|1]+=add[o];
add[o]=0;
}
}
void pushup(int o)
{
sum[o]=max(sum[o<<1],sum[o<<1|1]);
}
void update(int o,int l,int r,int q1,int q2,int val)
{
if(q1<=l&&r<=q2)
{
add[o]+=val;
sum[o]+=val;
return ;
}
pushdown(o);
int mid=(l+r)>>1;
if(q1<=mid)update(o<<1,l,mid,q1,q2,val);
if(q2>mid)update(o<<1|1,mid+1,r,q1,q2,val);
pushup(o);
}
int query(int o,int l,int r,int q1,int q2)
{
if(q1<=l&&r<=q2)return sum[o];
pushdown(o);
int mid=(l+r)>>1;
int ans=0;
if(q1<=mid)ans=max(ans,query(o<<1,l,mid,q1,q2));
if(q2>mid) ans=max(ans,query(o<<1|1,mid+1,r,q1,q2));
return ans;
}
}tree;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&N);
int cnt=0;
for(int i=1;i<=N;i++)
{
scanf("%d%d",&line[i].x,&line[i].y);
X[cnt++]=line[i].x,X[cnt++]=line[i].y;
}
sort(X,X+cnt);
cnt=unique(X,X+cnt)-X;
tree.build(1,1,cnt);
for(int i=1;i<=N;i++)
{
int l=lower_bound(X,X+cnt,line[i].x)-X+1;
int r=lower_bound(X,X+cnt,line[i].y)-X+1;
if(l<=r)tree.update(1,1,cnt,l,r,1);
}
printf("%d\n",tree.query(1,1,cnt,1,cnt));
}
return 0;
}