HDU5124 Lines树状数组+离散化处理

本文介绍了一种解决区间修改与单点查询问题的高效算法。通过使用线段树或差分数组进行区间更新,并结合前缀和技巧实现单点求和,适用于大量区间操作的场景。例如,在给定一系列覆盖X轴的线段情况下,快速找出被最多线段覆盖的点及覆盖该点的线段数量。

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

http://acm.hdu.edu.cn/showproblem.php?pid=5124

区间修改、单点查询参考:https://blog.youkuaiyun.com/lanshan1111/article/details/86352346

lines

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


 

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(1≤T≤100) (the data for N>100 less than 11 cases),indicating the number of test cases.
Each test case begins with an integer N(1≤N≤105) ,indicating the number of lines.
Next N lines contains two integers Xi and 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

 

 

Source

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 100500
int a[N],b[N],p[2*N];
int c[N*2];
int maxn;
int lowbit(int x)
{
    return x&-x;
}
int lowbits(int x)
{
    return x&(-x);
}
void add(int x,int v)
{
    for(int i=x; i<=maxn; i+=lowbit(i))
        c[i]+=v;
}
void adds(int x,int v)
{
    for(int i=x; i<=maxn; i+=lowbits(i))
        c[i]+=v;
}
int sum(int x)
{
    int ans=0;
    for(int i=x; i; i-=lowbit(i))
        ans+=c[i];
    return ans;
}
int sums(int x)
{
    int ans=0;
    for(int i=x; i; i-=lowbit(i))
    {
        ans+=c[i];
        return ans;
    }
}
//区间更新,单点求和;
int main()
{
    int T,n;
    scanf("%d",&T);
    while(T--)
    {
        memset(c,0,sizeof(c));
        scanf("%d",&n);
        maxn=2*n;
        for(int i=1; i<=n; i++)
        {
            scanf("%d %d",&a[i],&b[i]);
            p[i]=a[i],p[i+n]=b[i];//离散化处理;
        }
        sort(p+1,p+1+2*n);
        for(int i=1; i<=n; i++)
        {
            int l=lower_bound(p+1,p+1+2*n,a[i])-p;
            int r=lower_bound(p+1,p+1+2*n,b[i])-p;
            add(l,1);
            add(r+1,-1);
            //单点更新;
        }
        int ans=1;
        for(int i=1; i<=2*n; i++)
        {
            int s=sum(i);
            ans=max(ans,s);
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在 IT 领域,文档格式转换是常见需求,尤其在处理多种文件类型时。本文将聚焦于利用 Java 技术栈,尤其是 Apache POI 和 iTextPDF 库,实现 doc、xls(涵盖 Excel 2003 及 Excel 2007+)以及 txt、图片等格式文件向 PDF 的转换,并实现在线浏览功能。 先从 Apache POI 说起,它是一个强大的 Java 库,专注于处理 Microsoft Office 格式文件,比如 doc 和 xls。Apache POI 提供了 HSSF 和 XSSF 两个 API,其中 HSSF 用于读写老版本的 BIFF8 格式(Excel 97-2003),XSSF 则针对新的 XML 格式(Excel 2007+)。这两个 API 均具备读取和写入工作表、单元格、公式、样式等功能。读取 Excel 文件时,可通过创建 HSSFWorkbook 或 XSSFWorkbook 对象来打开相应格式的文件,进而遍历工作簿中的每个 Sheet,获取行和列数据。写入 Excel 文件时,创建新的 Workbook 对象,添加 Sheet、Row 和 Cell,即可构建新 Excel 文件。 再看 iTextPDF,它是一个用于生成和修改 PDF 文档的 Java 库,拥有丰富的 API。创建 PDF 文档时,借助 Document 对象,可定义页面尺寸、边距等属性来定制 PDF 外观。添加内容方面,可使用 Paragraph、List、Table 等元素将文本、列表和表格加入 PDF,图片可通过 Image 类加载插入。iTextPDF 支持多种字体和样式,可设置文本颜色、大小、样式等。此外,iTextPDF 的 TextRenderer 类能将 HTML、
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值