hdu 5124 lines (线段树+离散化)

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(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

题意就是求区间的最大值的,可以用线段树+离散化处理,使得坐标区间变小。

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



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值