hdu 5124 BestCoder #20 1002 lines 离散化+(线段树。树状数组或区间覆盖)三种方法

本文详细解析了如何解决线段覆盖问题,通过三种方法:普通方法、树状数组和线段树,探讨了如何找到被最多线段覆盖的点及其覆盖次数。通过对输入线段进行离散化处理,有效降低了数据规模,提高了算法效率。

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

lines

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


 

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

BestCoder Round #20

 

 

Recommend

heyang   |   We have carefully selected several similar problems for you:  6460 6459 6458 6457 6456 

 

题意:

 n 条线段,每条线段用两个整数描述,对于第 i 条线段:xi,yi 表示该条线段的左端点和右端点。设 A 表示最多线段覆盖的点。现在需要求的是 A 被多少条线段覆盖。

分析:

方法1:

首先,介绍一种区间覆盖点数的方法,我们对于区间【1,n】都要对应一个val,初始为0,假设【l,r】被覆盖,那么我们将l对应的val+1,r+1对应的val-1,计算哪一个点被覆盖了多少次,我们统计该点的前缀和就可以(画图一目了然)

但是,本题数据范围过大,很简单,对起离散化就ok。

方法2:

树状数组+离散化

上题的思路前缀和嘛,树状数组快一点,但这题暴力也能过

方法3:

离散化+线段树:区间查询+区间增值

但没必要,更慢了。

普通方法


#include<stdio.h>
#include<string>
#include<string.h>
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
const int N=100000+5;//最大元素个数
 

struct node1
{
 	int x, y;
 }a1[N];
int b[N*2];
int val[N*2];
int main()
{
   int t;
   scanf("%d",&t);
   while(t--)
   {
   	 int n;
   	 scanf("%d",&n);
   	 
   	 memset(val,0,sizeof(val));
   	 int cnt=0;
   	 for(int i=1;i<=n;i++)
	 {
	 	scanf("%d%d",&a1[i].x,&a1[i].y);
	 	b[cnt++]=a1[i].x;
	 	b[cnt++]=a1[i].y;
	 }
	 sort(b,b+2*n);
	 int len=unique(b,b+2*n)-b;
	 
	 for(int i=1;i<=n;i++)
	 {
	 	int pos1=lower_bound(b,b+len,a1[i].x)-b+1;
	 	int pos2=lower_bound(b,b+len,a1[i].y)-b+1;
		
		val[pos1]++;
		val[pos2+1]--;
		
	 }
	 int maxx=0;
	 int summ=0;
	 for(int i=1;i<=len;i++)
	 {
	 	summ+=val[i];
	 	
	 	maxx=max(summ,maxx);
	 }
	 cout<<maxx<<endl;
   }
 
    return 0;
}

树状数组代码


#include<stdio.h>
#include<string>
#include<string.h>
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
const int N=100000+5;//最大元素个数
 
int n;//元素个数
int c[N*2];//c[i]==A[i]+A[i-1]+...+A[i-lowbit(i)+1]
 
//返回i的二进制最右边1的值
int lowbit(int i)
{
    return i&(-i);
}

//返回A[1]+...A[i]的和
 
int sum(int x){
    int sum = 0;
    while(x){
        sum += c[x];
        x -= lowbit(x);
    }
    return sum;
}
 
//令A[i] += val
 
void add(int x, int val){
	
    while(x <= N){
        c[x] += val;
        x += lowbit(x);
    }
}
struct node1
{
 	int x, y;
 }a1[N];
int b[N*2];
int val[N*2];
int main()
{
   int t;
   scanf("%d",&t);
   while(t--)
   {
   	 int n;
   	 scanf("%d",&n);
   	 memset(c,0,sizeof(c));
   	 memset(val,0,sizeof(val));
   	 int cnt=0;
   	 for(int i=1;i<=n;i++)
	 {
	 	scanf("%d%d",&a1[i].x,&a1[i].y);
	 	b[cnt++]=a1[i].x;
	 	b[cnt++]=a1[i].y;
	 }
	 sort(b,b+2*n);
	 int len=unique(b,b+2*n)-b;
	 
	 for(int i=1;i<=n;i++)
	 {
	 	int pos1=lower_bound(b,b+len,a1[i].x)-b+1;
	 	int pos2=lower_bound(b,b+len,a1[i].y)-b+1;
	 	 //  cout<<a1[i].x<<" "<<pos1<<endl;
		add(pos1,1);
		add(pos2+1,-1);
	 }
	 int maxx=0;

	 for(int i=1;i<=len;i++)
	 {
	 	maxx=max(sum(i),maxx);
	 }
	 cout<<maxx<<endl;
   }
 
    return 0;
}

线段树代码

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;
 
#define lson i<<1
#define rson i<<1|1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define ll long long
#define N 100005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)
 
int ans_sum,ans_max,ans_min;
struct node
{
    int l,r;
    int sum;
    int add;
} a[N<<2];
 
void pushdown(int i)//标记下传
{

  
        a[lson].add+=a[i].add;///给左子节点打上延迟标记
        a[rson].add+=a[i].add;///给右子节点打上延迟标记
        a[lson].sum+=a[i].add*(a[lson].r-a[lson].l+1); ///更新左子节点消息
        a[rson].sum+=a[i].add*(a[rson].r-a[rson].l+1); ///更新右子节点消息
        a[i].add = 0;  ///清除标记
}
 
void pushup(int i)
{
    a[i].sum=a[lson].sum+a[rson].sum;
}
//建立线段树 
void build(int l,int r,int i)
{
    a[i].l = l;
    a[i].r = r;
    a[i].add = 0;
    if(l == r)     
	{
	a[i].sum = 0;
		return;
	}
    int mid = (l+r)>>1;
    build(LS);
    build(RS);
    pushup(i);
}
//a[l,r]+=val 
void add_data(int l,int r,int i,int val)
{
    if(a[i].l>=l&&a[i].r<=r)
    {
        a[i].sum += val*(a[i].r-a[i].l+1);
        a[i].add += val;
        return;
    }
    pushdown(i);//标记下传
    int mid = (a[i].l+a[i].r)>>1;
    if(l<=mid) add_data(l,r,lson,val);
    if(r>mid) add_data(l,r,rson,val);
    pushup(i);
}
 
void query(int l,int r,int i)
{
    if(l <= a[i].l && a[i].r <= r)
    {
        ans_sum += a[i].sum;
        return ;
    }
    pushdown(i);
    int mid = (a[i].l+a[i].r)>>1;
    if(l<=mid) query(l,r,lson);
    if(r>mid) query(l,r,rson);
    pushup(i);
}
 struct node1
 {
 	int x, y;
 }a1[N];
int b[N*2];
int main()
{
   int t;
   scanf("%d",&t);
   while(t--)
   {
   	 int n;
   	 scanf("%d",&n);
   	 int cnt=0;
   	 for(int i=1;i<=n;i++)
	 {
	 	scanf("%d%d",&a1[i].x,&a1[i].y);
	 	b[cnt++]=a1[i].x;
	 	b[cnt++]=a1[i].y;
	 }
	 sort(b,b+2*n);
	 int len=unique(b,b+2*n)-b;
	 build(1,len,1);
	 
	 for(int i=1;i<=n;i++)
	 {
	 	int pos1=lower_bound(b,b+len,a1[i].x)-b+1;
	 	int pos2=lower_bound(b,b+len,a1[i].y)-b+1;
	 	 //  cout<<a1[i].x<<" "<<pos1<<endl;
	 	   add_data(pos1,pos2,1,1);
	 }
	 int maxx=0;
	 for(int i=1;i<=len;i++)
	 {
	 	ans_sum=0;
	 	query(i,i,1);
	 	maxx=max(ans_sum,maxx);
	 }
	 cout<<maxx<<endl;
   }

    
 
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值