【HDU】5124lines(树状数组+离散化处理)

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2040    Accepted Submission(s): 889


 

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

 

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

BestCoder Round #20

 

 

Recommend

heyang   |   We have carefully selected several similar problems for you:  6460 6459 6458 6457 6456 

 

 

题目大意:给你在X轴上的N条线段,问你最多覆盖的点的覆盖数量

思路:通过这个题我学会了离散化的一点知识,首先把所有的坐标映射到x轴上,然后将坐标压缩。最后的时候用树状数组维护点上线段的覆盖情况就可以了

代码:


#include<stdio.h>
#include<string>
#include<string.h>
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=100000+5;

int n;
int c[maxn*2];
int b[maxn*2];
int val[maxn*2];
int lowbit(int i)
{
    return i&(-i);
}
int sum(int x)
{
    int sum = 0;
    while(x)
    {
        sum += c[x];
        x -= lowbit(x);
    }
    return sum;
}
void add(int x, int val)
{

    while(x <= maxn)
    {
        c[x] += val;
        x += lowbit(x);
    }
}
struct node1
{
    int x, y;
} a1[maxn];

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        int cnt=0;
        memset(c,0,sizeof(c));
        memset(val,0,sizeof(val));
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d",&a1[i].x,&a1[i].y);
            b[cnt++]=a1[i].x;
            b[cnt++]=a1[i].y;
        }
        sort(b,b+2*n);
        int len=unique(b,b+2*n)-b;

        for(int i=1; i<=n; i++)
        {
            int pos1=lower_bound(b,b+len,a1[i].x)-b+1;
            int pos2=lower_bound(b,b+len,a1[i].y)-b+1;
            add(pos1,1);
            add(pos2+1,-1);
        }
        int maxx=0;

        for(int i=1; i<=len; i++)
        {
            maxx=max(sum(i),maxx);
        }
        cout<<maxx<<endl;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值