线段树离散化hdu5124

F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist
 
      C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests 
    DIY | Web-DIY beta
Recent Contests

lines

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


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

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
int X[maxn*2],N;
struct node
{
    int x,y;
    node(){}
    node(int l,int r):x(l),y(r){}
}line[maxn];
struct IntervalTree
{
    int sum[maxn<<3],add[maxn<<3];
    void build(int o,int l,int r)
    {
        sum[o]=add[o]=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(add[o])
        {
            add[o<<1]+=add[o];
            add[o<<1|1]+=add[o];
            sum[o<<1]+=add[o];
            sum[o<<1|1]+=add[o];
            add[o]=0;
        }
    }
    void pushup(int o)
    {
        sum[o]=max(sum[o<<1],sum[o<<1|1]);
    }
    void update(int o,int l,int r,int q1,int q2,int val)
    {
        if(q1<=l&&r<=q2)
        {
            add[o]+=val;
            sum[o]+=val;
            return ;
        }
        pushdown(o);
        int mid=(l+r)>>1;
        if(q1<=mid)update(o<<1,l,mid,q1,q2,val);
        if(q2>mid)update(o<<1|1,mid+1,r,q1,q2,val);
        pushup(o);
    }
    int query(int o,int l,int r,int q1,int q2)
    {
        if(q1<=l&&r<=q2)return sum[o];
        pushdown(o);
        int mid=(l+r)>>1;
        int ans=0;
        if(q1<=mid)ans=max(ans,query(o<<1,l,mid,q1,q2));
        if(q2>mid) ans=max(ans,query(o<<1|1,mid+1,r,q1,q2));
        return ans;
    }
}tree;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&N);
        int cnt=0;
        for(int i=1;i<=N;i++)
        {
            scanf("%d%d",&line[i].x,&line[i].y);
            X[cnt++]=line[i].x,X[cnt++]=line[i].y;
        }
        sort(X,X+cnt);
        cnt=unique(X,X+cnt)-X;
        tree.build(1,1,cnt);
        for(int i=1;i<=N;i++)
        {
            int l=lower_bound(X,X+cnt,line[i].x)-X+1;
            int r=lower_bound(X,X+cnt,line[i].y)-X+1;
            if(l<=r)tree.update(1,1,cnt,l,r,1);
        }
        printf("%d\n",tree.query(1,1,cnt,1,cnt));
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值