Flowers-HDU4325(区间处理+离散化)

Problem - 4325 http://acm.hdu.edu.cn/showproblem.php?pid=4325
 
 
Problem Description
As is known to all, the blooming time and duration varies between different kinds of flowers. Now there is a garden planted full of flowers. The gardener wants to know how many flowers will bloom in the garden in a specific time. But there are too many flowers in the garden, so he wants you to help him.
 
Input
The first line contains a single integer t (1 <= t <= 10), the number of test cases.
For each case, the first line contains two integer N and M, where N (1 <= N <= 10^5) is the number of flowers, and M (1 <= M <= 10^5) is the query times. 
In the next N lines, each line contains two integer Si and Ti (1 <= Si <= Ti <= 10^9), means i-th flower will be blooming at time [Si, Ti].
In the next M lines, each line contains an integer Ti, means the time of i-th query.
 
Output
For each case, output the case number as shown and then print M lines. Each line contains an integer, meaning the number of blooming flowers.
Sample outputs are available for more details.
 
Sample Input
2 1 1 5 10 4 2 3 1 4 4 8 1 4 6
 
Sample Output
Case #1: 0 Case #2: 1 2 1
 
题目大意:
现在有好几朵花,给你几段时刻,每朵花只能在指定的时间开放
求在一个时刻 有几朵花开放
 
分析:
因为他是一个区间  所以要离散化,减小数据
 
直接上代码
 
 
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
#define N 1100000

struct node
{
    int x,y;
}a[N];
int f[N],p[N];

int main()
{
    int T,n,m,i,t=1;
    scanf("%d",&T);
    while(T--)
    {
        memset(p,0,sizeof(p));
        memset(a,0,sizeof(a));
        memset(f,0,sizeof(f));
        int k=0;
        scanf("%d %d",&n,&m);
        for(i=0;i<n;i++)
        {
            scanf("%d %d",&a[i].x,&a[i].y);
            p[k++]=a[i].x;
            p[k++]=a[i].x-1;
            p[k++]=a[i].y;
            p[k++]=a[i].y+1;
        }
        sort(p,p+k);
        int len=unique(p,p+k)-p;
        int Max=0;
        for(i=0;i<n;i++)
        {
            int u=lower_bound(p,p+len,a[i].x)-p;
            int v=lower_bound(p,p+len,a[i].y)-p;
            f[u]++;
            f[v+1]--;
            Max=max(Max,v+1);
        }
        for(i=1;i<=Max;i++)//防止TLE
        {
            f[i]+=f[i-1];
        }
        printf("Case #%d:
",t++);
        while(m--)
        {
            int b;
            scanf("%d",&b);
            int u=lower_bound(p,p+len,b)-p;
            printf("%d
",f[u]);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/linliu/p/4975461.html