#HDU 4325 Flowers 【离散化、区间加减法】
2016-04-28 11:01
218 查看
题目:
Flowers
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2866 Accepted Submission(s): 1404
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
Author
BJTU
Source
2012 Multi-University Training Contest 3
Recommend
zhoujiaqi2010
思路: 先给出N个时间区间, 问在时间点 Xi 上共有多少层时间区间重叠。
线段树区域累加,由于区域过大,在处理前先进行离散化。
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <vector> #include <algorithm> #define MAXN 1000000 int lazy[2 * MAXN]; int t[2 * MAXN], a[2 * MAXN]; //int be[MAXN], ed[MAXN]; //int hips[2 * MAXN]; //double endhips[4 * MAXN]; //int maxn; using namespace std; void BuildTree(int l, int r, int x){ if (l == r) { t[x] = a[l]; return; } int m = (l + r) >> 1; BuildTree(l, m, x << 1); BuildTree(m + 1, r, x << 1 | 1); t[x] = t[x << 1] + t[x << 1 | 1]; return; } void PushDown(int l, int r, int x){ int m = (l + r) >> 1; if (lazy[x]){ t[x << 1] += lazy[x] * (m - l + 1); t[x << 1 | 1] += lazy[x] * (r - m); lazy[x << 1 | 1] += lazy[x]; lazy[x << 1] += lazy[x]; lazy[x] = 0; } return; } void Modify(int pos, int val, int l, int r, int x){ if (pos == l && r == l) { t[x] = val; return; } int m = (l + r) >> 1; if (pos <= m) { Modify(pos, val, l, m, x << 1); } else Modify(pos, val, m + 1, r, x << 1 | 1); t[x] = t[x << 1] + t[x << 1 | 1]; return; } void SegModify(int L, int R, int val, int l, int r, int x){ if (l == L&&r == R){ t[x] += (R - L + 1)*val; lazy[x] += val; return; } PushDown(l, r, x); int m = (l + r) >> 1; if (R <= m)SegModify(L, R, val, l, m, x << 1); else if (L>m)SegModify(L, R, val, m + 1, r, x << 1 | 1); else { SegModify(L, m, val, l, m, x << 1); SegModify(m + 1, R, val, m + 1, r, x << 1 | 1); } t[x] = t[x << 1] + t[x << 1 | 1]; return; } int Query(int L, int R, int l, int r, int x){ if (L == l && R == r) return t[x]; PushDown(l, r, x); int m = (l + r) >> 1; if (R <= m)return Query(L, R, l, m, x << 1); else if (L>m)return Query(L, R, m + 1, r, x << 1 | 1); else return Query(L, m, l, m, x << 1) + Query(m + 1, R, m + 1, r, x << 1 | 1); } int main() { int q; cin >> q; for (size_t i = 0; i < q; i++) { memset(t, 0, sizeof(t)); //memset(hips, 0, sizeof(hips)); //memset(endhips, 0, sizeof(endhips)); memset(lazy, 0, sizeof(lazy)); cout << "Case #" << i + 1 << ":\n"; int nn, mm; cin >> nn >> mm; //maxn = 0; int n = 1; while (n<MAXN / 2) { n *= 2; } for (size_t i = 0; i < nn; i++) { int be, ed; cin >> be >> ed; SegModify(be, ed, 1, 1, n, 1); } for (size_t i = 0; i < mm; i++) { int quer; cin >> quer; cout << Query(quer, quer, 1, n, 1) << "\n"; } /*for (size_t i = 0; i < nn; i++) { cin >> be[i] >> ed[i]; hips[maxn] = be[i]; maxn++; hips[maxn] = ed[i]; maxn++; } int temp = 0; sort(hips, hips + maxn); for (size_t i = 0; i < maxn; i++) { if (endhips[temp] != hips[i]) { temp += 2; endhips[temp] = hips[i]; } } temp++; for (size_t i = 1; i < temp; i++) { if (!endhips[i]) { endhips[i] = (endhips[i - 1] + endhips[i + 1]) / 2; } } int n = 1; while (n<10) { n *= 2; } for (size_t i = 0; i < nn; i++) { SegModify(*upper_bound(endhips, endhips + temp, be[i]), *lower_bound(endhips, endhips + temp, ed[i]), 1, 1, n, 1); } for (size_t i = 0; i < mm; i++) { int query; cin >> query; query = lower_bound(endhips, endhips + temp, query) - endhips; cout << Query(query,query,1,n,1)<<"\n"; }*/ } return 0; }
相关文章推荐
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析
- C++实现汉诺塔算法经典实例
- PHP实现克鲁斯卡尔算法实例解析