HDOJ 5124 lines 【线段树 & 离散化】

本文介绍了一种使用线段树解决特定几何问题的方法,即寻找被最多线段覆盖的坐标点。通过离散化处理大量数据,并利用线段树进行区间更新和查询,高效地解决了问题。

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(1T100) (the data for N>100 less than 11 cases),indicating the number of test cases.
Each test case begins with an integer N(1N105) ,indicating the number of lines.
Next N lines contains two integers Xi and Yi(1XiYi109) ,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
 


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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值