HDU4325 树状数组+离散化

题目链接: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): 3544    Accepted Submission(s): 1745


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
 
题意  n朵花 m个时间点 第i朵花正在开放的时间是si,ti   问:在第mi个时间点有多少朵花正在开放
解析  思路是 我们记录每个时间点有多少个花是开放的   树状数组可以解决 但是数据太大会超时也存不下 数据数量还是比较少的 所以我们可以离散化一下 我们要把m也离散化进去
AC代码
 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <string.h>
 4 #include <stdlib.h>
 5 #include <iostream>
 6 #include <sstream>
 7 #include <algorithm>
 8 #include <string>
 9 #include <queue>
10 #include <map>
11 #include <vector>
12 #include <iomanip>
13 using namespace std;
14 const int maxn = 3e5+50;
15 const int maxm = 1e4+10;
16 const int inf = 0x3f3f3f3f;
17 const int mod = 998244353;
18 const double epx = 1e-10;
19 typedef long long ll;
20 const ll INF = 1e18;
21 const double pi = acos(-1.0);
22 int c[maxn],rankk[maxn];
23 int n,m,t;
24 struct node
25 {
26     int id,x;
27     bool operator<(const node &b)const
28     {
29         return x<b.x;
30     }
31 }a[maxn];
32 int lowbit(int x)
33 {
34     return x&(-x);
35 }
36 int getsum(int x)
37 {
38     int ans=0;
39     for(int i=x;i>0;i-=lowbit(i))
40         ans+=c[i];
41     return ans;
42 }
43 void update(int x,int y,int z)
44 {
45     for(int i=x;i<=y;i+=lowbit(i))
46         c[i]+=z;
47 }
48 int main()
49 {
50     int kase=1;
51     cin>>t;
52     while(t--)
53     {
54         cin>>n>>m;
55         memset(c,0,sizeof(c));
56         memset(rankk,0,sizeof(rankk));
57         int temp=2*n+m;
58         for(int i=1;i<=temp;i++)
59         {
60             scanf("%d",&a[i].x);
61             a[i].id=i;
62         }
63         sort(a+1,a+1+temp);
64         int k=1;
65         rankk[a[1].id]=k;
66         for(int i=2;i<=temp;i++)
67         {
68             if(a[i].x==a[i-1].x)
69                 rankk[a[i].id]=k;
70             else
71                 rankk[a[i].id]=++k;
72         }
73         for(int i=1;i<2*n;i+=2)
74         {
75             update(rankk[i],k,1);
76             update(rankk[i+1]+1,k,-1);
77         }
78         printf("Case #%d:
",kase++);
79         for(int i=2*n+1;i<=temp;i++)
80         {
81             printf("%d
",getsum(rankk[i]));
82         }
83     }
84 }
View Code

离散化博客    https://blog.csdn.net/xiangaccepted/article/details/73276826

数据比较水 不离散化也可以水过去。。

原文地址:https://www.cnblogs.com/stranger-/p/8672932.html