【HDU】5124 转换为最大前缀 或者 离散化线段树/树状数组

博客介绍了如何通过设置权值并进行排序,使用最大前缀和的方法来解决HDU 5124题目的详细思路。

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

 

ohn 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

思路:对于每一条 line ,令 xi 的权值为1 , yi+1 的权值为 -1 , 对所有的节点排序, 求最大前缀和 

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define PI acos(-1.0)
#define E 1e-6
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define maxn 3e5+10
using namespace std;
#define ll long long int
typedef pair<ll,ll>line;
line a[200010];
int main()
{
   int t;
   scanf("%d",&t);
   while(t--)
   {
      int n;
      scanf("%d",&n);
      for(int i=0;i<2*n;i++)
      {
        scanf("%lld",&a[i].first);
        a[i].second=1;
        scanf("%lld",&a[++i].first);
        a[i].first++;
        a[i].second=-1;
      }
      sort(a,a+n*2);
      int ans=0;
      int MAX=0;
      for(int i=0;i<2*n;i++)
      {
        MAX+=a[i].second;
        ans=max(ans,MAX);
      }
      printf("%d\n",ans);
   }
   return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值