lines
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 620 Accepted Submission(s): 288
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.
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
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
题意就是求区间的最大值的,可以用线段树+离散化处理,使得坐标区间变小。
如[2,5],[3, 100]离散化后,先排序{2,3,5,100},对应下标值可以变为{1,2,3,4} ,对应时可以去重(见代码)。
区间变为[1,3],[2,4],区间的大小关系不变。
#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
#define LL __int64
#define N 200005
struct node
{
int l,r;
int val,add;
} f[N*3];
struct line1 //记录原始区间的左右端点
{
int l,r;
} a[N];
struct line2 //对初始线段长度按非递减排序
{
int val,id;
} b[N*2];
bool cmp(line2 a,line2 b)
{
return a.val<b.val;
}
void Creat(int t,int l,int r)
{
f[t].l=l;
f[t].r=r;
f[t].val=f[t].add=0;
if(l==r)
return ;
int tmp=t<<1,mid=(l+r)>>1;
Creat(tmp,l,mid);
Creat(tmp|1,mid+1,r);
}
void Updata(int t,int l,int r)
{
if(f[t].l==l&&f[t].r==r)
{
f[t].add+=1;
f[t].val+=1;
return ;
}
int tmp=t<<1,mid=(f[t].l+f[t].r)>>1;
if(f[t].add)
{
f[tmp].add+=f[t].add;
f[tmp].val+=f[t].add;
f[tmp|1].add+=f[t].add;
f[tmp|1].val+=f[t].add;
f[t].add=0;
}
if(r<=mid)
Updata(tmp,l,r);
else if(l>mid)
Updata(tmp|1,l,r);
else
{
Updata(tmp,l,mid);
Updata(tmp|1,mid+1,r);
}
f[t].val=max(f[tmp].val,f[tmp|1].val);
}
int main()
{
int i,j,n,T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=1,j=1; i<=n; ++i)
{
scanf("%d%d",&a[i].l,&a[i].r);
b[j].val=a[i].l;
b[j++].id=-i;
b[j].val=a[i].r;
b[j++].id=i;
}
sort(b+1,b+j,cmp);
int t=0; //给原来线段重新赋值
int x=0;
for(i=1; i<j; ++i) //把初始线段左右断点值离散化
{
if(x!=b[i].val)
{
++t;
x=b[i].val;
}
if(b[i].id<0)
a[-1*b[i].id].l=t;
else
a[b[i].id].r=t;
}
Creat(1,1,t); //线段长度从1到t
for(i=1; i<=n; ++i)
{
Updata(1,a[i].l,a[i].r);
}
cout<<f[1].val<<endl;
}
return 0;
}