您的位置:首页 > 其它

hdu 4325 Flowers(区间离散化)

2015-11-18 15:25 337 查看
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


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

2

1 1

5 10
4
2 3
1 4
4 8

1

4
6

[align=left]Sample Output[/align]

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
, b
, used
, d
;

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: