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

版权声明:抱最大的希望,为最大的努力,做最坏的打算。 https://blog.csdn.net/qq_37748451/article/details/86549821
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;
}

猜你喜欢

转载自blog.csdn.net/qq_37748451/article/details/86549821
今日推荐