【HDU - 5124】 lines 【树状数组+离散化】

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)T(1≤T≤100)(the data for N>100N>100 less than 11 cases),indicating the number of test cases.
Each test case begins with an integer N(1≤N≤105)N(1≤N≤105),indicating the number of lines.
Next N lines contains two integers XiXi and Yi(1≤Xi≤Yi≤109)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
题意 :给n个线段,问被覆盖次数最多的点,它被覆盖的次数。
首先数据大,我们可以用离散化。
想一下,被覆盖次数最多的点,一定是区间的端点处,然后,区间的更新(加减),查询每一个端点,取最大就好。
线段树也可以区间更新+维护区间最大值 做到,但是明显树状数组更好用。
树状数组的常用操作
代码

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
#define first fi
#define second se
#define  LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)

const int MAXN = 1e5+11;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

struct BIT{
    int n;
    int c[MAXN<<1];
    inline int lowbit(int x) { return x&(-x);}
    void add(int x,int val){
        for(int i=x;i<=n;i+=lowbit(i))
            c[i]+=val;
    }
    int sum(int x){
        int ans=0;
        for(int i=x;i>0;i-=lowbit(i)) 
            ans+=c[i];
        return ans;
    }
}bit;  
int le[MAXN],ri[MAXN];
int X[MAXN<<1],size;

int main(){
    CLOSE();
//  fread();
//  fwrite();
    int T;scanf("%d",&T);
    while(T--){
        int n;scanf("%d",&n);size=0;
        for(int i=1;i<=n;i++){
            scanf("%d%d",&le[i],&ri[i]);
            X[size++]=le[i];X[size++]=ri[i];
        }
        sort(X,X+size); size=unique(X,X+size)-X;
        memset(bit.c,0,sizeof(bit.c)); bit.n=size+10;
        for(int i=1;i<=n;i++){
            int l=lower_bound(X,X+size,le[i])-X+1;
            int r=lower_bound(X,X+size,ri[i])-X+1;
            bit.add(l,1);bit.add(r+1,-1);
        }
        int ans=-1;
        for(int i=0;i<size;i++){  //遍历X数组就好,正好是所有的端点,同时还去重过了,很方便。
            int t=lower_bound(X,X+size,X[i])-X+1;
            ans=max(ans,bit.sum(t));
        }
        printf("%d\n",ans);
    }   
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值