hdoj 5124 lines 【离散化 + 线段树】

本文深入解析了一道使用线段树解决覆盖问题的编程题,包括输入输出格式、问题描述、解题思路及代码实现,旨在帮助读者理解如何运用线段树在数据结构中解决实际问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



lines

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


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
 




题意:给定n条平行于x轴的线段,设被这些线段覆盖次数最多的点为A,让你求出A被覆盖的次数。


先把坐标离散化,然后就是线段树区间更新维护区间最大值了。


AC代码:


#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (100000+10)
#define MAXM (50000000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
struct Tree
{
    int l, r;
    int Max;
    int lazy;
};
Tree tree[MAXN<<2];
int x[MAXN], y[MAXN];
void PushUp(int o){
    tree[o].Max = max(tree[ll].Max, tree[rr].Max);
}
void PushDown(int o)
{
    if(tree[o].lazy)
    {
        tree[ll].lazy += tree[o].lazy;
        tree[rr].lazy += tree[o].lazy;
        tree[ll].Max += tree[o].lazy;
        tree[rr].Max += tree[o].lazy;
        tree[o].lazy = 0;
    }
}
void build(int o, int l, int r)
{
    tree[o].Max = tree[o].lazy = 0;
    tree[o].l = l, tree[o].r = r;
    if(l == r)
        return ;
    int mid = (l + r) >> 1;
    build(lson);
    build(rson);
}
int rec[MAXN*2];
int Find(int val, int l, int r)
{
    while(r >= l)
    {
        int mid = (l + r) >> 1;
        if(rec[mid] == val)
            return mid;
        else if(rec[mid] > val)
            r = mid-1;
        else
            l = mid+1;
    }
}
void update(int o, int L, int R, int v)
{
    if(L <= tree[o].l && R >= tree[o].r)
    {
        tree[o].lazy += v;
        tree[o].Max += v;
        return ;
    }
    PushDown(o);
    int mid = (tree[o].l + tree[o].r) >> 1;
    if(R <= mid)
        update(ll, L, R, v);
    else if(L > mid)
        update(rr, L, R, v);
    else
    {
        update(ll, L, mid, v);
        update(rr, mid+1, R, v);
    }
    PushUp(o);
}
int main()
{
    int t; Ri(t);
    W(t)
    {
       int n; Ri(n);
       int len = 1;
       for(int i = 0; i < n; i++)
       {
           Ri(x[i]); Ri(y[i]);
           rec[len++] = x[i];
           rec[len++] = y[i];
       }
       sort(rec+1, rec+len);
       int R = 2;
       for(int i = 2; i < len; i++)
           if(rec[i] != rec[i-1])
                rec[R++] = rec[i];
       sort(rec+1, rec+R);
       build(1, 1, R-1);
       for(int i = 0; i < n; i++)
       {
           if(x[i] > y[i])
               swap(x[i], y[i]);
           int l = Find(x[i], 1, R-1);
           int r = Find(y[i], 1, R-1);
           update(1, l, r, 1);
       }
       printf("%d\n", tree[1].Max);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值