hdu 5124(区间更新+单点求值+离散化)

lines

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


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
 
题意:一些线段互相覆盖,求这个线段上面的覆盖线段数最多的点被多少线段覆盖??
题解:看到线段就想到区间更新,然后看到点就想到单点求值,然后只有100000个线段,但是范围却有 1 - 10^9 所以离散化一下。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include <queue>
using namespace std;
const int N = 100005;
int a[N],b[N],x[2*N];
int c[2*N];
int n;
int MAX ;

int lowbit(int x){
    return x&-x;
}
void update(int idx,int v){
    for(int i=idx;i<=2*n;i+=lowbit(i)){
        c[i] +=v;
    }
}
int getsum(int idx){
    int sum = 0;
    for(int i=idx;i>=1;i-=lowbit(i)){
        sum+=c[i];
    }
    return sum;
}
int main()
{
    int tcase;
    scanf("%d",&tcase);
    while(tcase--){
        memset(c,0,sizeof(c));
        scanf("%d",&n);
        int cnt = 1;
        for(int i=1;i<=n;i++){
            scanf("%d%d",&a[i],&b[i]);
            x[cnt++] = a[i];
            x[cnt++] = b[i];
        }
        int k = 2;
        sort(x+1,x+cnt);
        for(int i=2;i<cnt;i++){
            if(x[i]==x[i-1]) continue;
            x[k++] = x[i];
        }
        for(int i=1;i<=n;i++){
            int l = lower_bound(x+1,x+k,a[i])-(x);
            int r = lower_bound(x+1,x+k,b[i])-(x);
            update(l,1);
            update(r+1,-1);
        }
        int MAX = -1;
        for(int i=1;i<=2*n;i++){
            MAX = max(MAX,getsum(i));
        }
        printf("%d\n",MAX);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/liyinggang/p/5664883.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值