hdu 4325 Flowers(区间离散化)

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

Flowers

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2633    Accepted Submission(s): 1290

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

题目大意:n种花,每种花的花期为[s,t],m次查询,每次查询问ti时刻有多少种花开

数据范围过大,无法直接开数组,那么就要用到离散化处理

方法类似hdu 5124

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>

using namespace std;

typedef long long ll;
const int N = 2000010;
const int INF = 0x3f3f3f3f;

int c[2 * N], a[N], b[N], used[N], d[N];

int cmp(const void *a, const void *b)
{
    return *(int *)a - *(int *)b;
}

int main()
{
    int t, n, m, f = 0;
    int p, q, k, e;
    scanf("%d", &t);
    while(t--)
    {
        f++;
        k = 0;
        memset(used, 0, sizeof(used));
        scanf("%d%d", &n, &m);
        for(int i = 0 ; i < n ; i++)
        {
            scanf("%d%d", &p, &q);
            a[i] = p;
            b[i] = q;
            c[k++] = p;
            c[k++] = p - 1;/***///这里为嘛,个人认为是防止查询的数比q要小
            c[k++] = q;
            c[k++] = q + 1;/***/
        }

        qsort(c, k, sizeof(c[0]), cmp);

        k = unique (c, c + k)- c;//去重
        int x, y, maxn = -INF;
        for(int i = 0 ; i < n ; i++)
        {
            x = lower_bound(c, c + k, a[i]) - c;
            y = lower_bound(c, c + k, b[i]) - c;
            used[x]++;
            used[y + 1]--;
            maxn = max(maxn, y + 1);
        }

        for(int i = 0 ; i < maxn ; i++)
            used[i + 1] += used[i];

        printf("Case #%d:\n", f);
        while(m--)
        {
            scanf("%d", &e);
            int s = lower_bound(c, c + k, e) - c;

            printf("%d\n", used[s]);
        }
    }
    return 0;
}
时间: 2024-08-11 03:23:34

hdu 4325 Flowers(区间离散化)的相关文章

HDU 4325 Flowers(树状数组)

Flowers Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3150    Accepted Submission(s): 1549 Problem Description As is known to all, the blooming time and duration varies between different kinds

hdoj 4325 Flowers 线段树+离散化

hdoj 4325 Flowers 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4325 思路: 直接线段树,按照花的开放区间的大小建树,要注意虽然花的周期数据可能会达到1e9,这样的话线段树开四倍时不可能的.但是我们可以看到一共可能的数据时N行,那么每行两个数,再开4倍的区间.计算下来,在离散化的帮助下,我们只需要开8*N被的线段树即可. 另外询问的数据也需要放入离散化的范围,如果不这样做,有可能在询问时使用lower_bound函数会导致数

(线段树 区间运算求点)Flowers -- hdu -- 4325

http://acm.hdu.edu.cn/showproblem.php?pid=4325 Flowers Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2577    Accepted Submission(s): 1263 Problem Description As is known to all, the blooming t

hdu 3015 Disharmony Trees (离散化+树状数组)

Disharmony Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 663    Accepted Submission(s): 307 Problem Description One day Sophia finds a very big square. There are n trees in the square. T

hdoj 2883 kebab 【时间区间离散化 + 最大流】

kebab Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1273    Accepted Submission(s): 532 Problem Description Almost everyone likes kebabs nowadays (Here a kebab means pieces of meat grilled on

HDU 3642 线段树+离散化+扫描线

题意:给你N个长方体的左下角和右上角坐标,问你空间中有多少体积是被大于两个不同的立方体覆盖的.x,y~10^6 z~500 考虑到给的z比较小,所以可以直接枚举z,然后跑二维的扫描线就好. 关于处理被不同的线段覆盖三次的问题,可以维护四个信息,cnt,once,twice,more,然后相互推出结果就好. #include <cstdio> #include <cstring> #include <vector> #include <algorithm> #

HDU 5726 GCD 区间GCD=k的个数

GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2742    Accepted Submission(s): 980 Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). There ar

POJ 2528 Mayor&#39;s posters 区间离散化线段树

点击打开链接 Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45894   Accepted: 13290 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their elector

HDU 4325 Vampire Numbers 打表

杭电服务器是慢啊.. #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <math.h> #include <set> #include <vector> #include <map> using namespace std; #define ll long long #define N