HDOJ 5124 lines 【线段树 & 离散化】

来源:互联网 发布:图像处理区域填充算法 编辑:程序博客网 时间:2024/05/16 07:24

lines

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


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
251 2 2 22 43 45 100051 12 23 34 45 5
 


Sample Output
31
 


Source
BestCoder Round #20


恩,题目大意就是说,每组测试数据中的每组数据表示X轴上的一段线段,然后一定存在一个点,覆盖它的线段最多,问的就是最多的线段条数。由于数据较大,需要离散化。


#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#define maxn 100100using namespace std;int a[maxn],b[maxn];int rec[maxn<<1];struct lnode{    int l,r,c,m;};lnode node[maxn<<2];int maxi(int a,int b){    return a>b?a:b;}void pushup(int o){    node[o].m=maxi(node[o<<1].m,node[o<<1|1].m);}void build(int o,int l,int r){    node[o].l=l;    node[o].r=r;    node[o].m=0;    node[o].c=0;    if(l==r)        return ;    int mid=(l+r)>>1;    build(o<<1,l,mid);    build(o<<1|1,mid+1,r);}void pushdown(int o){    if(node[o].c)    {        node[o<<1].m+=node[o].c;        node[o<<1|1].m+=node[o].c;        node[o<<1].c+=node[o].c;        node[o<<1|1].c+=node[o].c;        node[o].c=0;    }}void update(int o,int l,int r){    if(node[o].l==l&&node[o].r==r)    {        node[o].m++;        node[o].c++;        return ;    }    pushdown(o);    int mid=(node[o].l+node[o].r)>>1;    if(r<=mid)        update(o<<1,l,r);    else if(l>mid)        update(o<<1|1,l,r);    else    {        update(o<<1,l,mid);        update(o<<1|1,mid+1,r);    }    pushup(o);}int bsearch(int l,int r,int x){    while(l<=r)    {        int mid=(l+r)>>1;        if(rec[mid]==x)            return mid;        if(rec[mid]>x)            r=mid-1;        else            l=mid+1;    }}int main(){    int t,n;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        int j=1;        for(int i=0;i<n;++i)        {            scanf("%d%d",&a[i],&b[i]);            rec[j++]=a[i];            rec[j++]=b[i];        }        sort(rec+1,rec+j);        int m=2;        for(int k=2;k<j;++k)        {            if(rec[k]!=rec[k-1])                rec[m++]=rec[k];        }        build(1,1,m);        for(int i=0;i<n;++i)        {            int l=bsearch(1,m,a[i]);            int r=bsearch(1,m,b[i]);            update(1,l,r);        }        printf("%d\n",node[1].m);    }    return 0;}


0 0
原创粉丝点击