【HDU

来源:互联网 发布:淘宝点击图片跳转链接 编辑:程序博客网 时间:2024/06/05 19:29

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;}
原创粉丝点击