题目:
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 S i and T i (1 <= S i <= T i <= 10^9), means i-th flower will be blooming at time [S i, T i].
In the next M lines, each line contains an integer T i, means the time of i-th query.
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 S i and T i (1 <= S i <= T i <= 10^9), means i-th flower will be blooming at time [S i, T i].
In the next M lines, each line contains an integer T i, 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 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
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;
}