您的位置:首页 > 其它

【HDU - 4325 】Flowers 【树状数组+离散化】

2017-10-19 18:18 513 查看
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.

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,e],多次询问时间t 时有几朵花开。

分析,区间更新(加减),单点查询。明显的树状数组。

我用的是白书的离散化方法。

树状数组的常用操作

代码一

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
#define first fi
#define second se
#define  LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)

const int MAXN = 2e5+10;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

struct BIT{
int n;
int c[MAXN<<1];
inline int lowbit(int<
1182d
/span> x) { return x&(-x);}
void add(int x,int val){
for(int i=x;i<=n;i+=lowbit(i))
c[i]+=val;
}
int sum(int x){
int ans=0;
for(int i=x;i>0;i-=lowbit(i))
ans+=c[i];
return ans;
}
}bit;
struct Node{
int s,e;
}Q[MAXN];
int X[MAXN<<1],size;
int ask[MAXN];
int main(){
CLOSE();
//  fread();
//  fwrite();
int T;scanf("%d",&T);int ncase=0;
while(T--){
int n,m;scanf("%d%d",&n,&m);size=0;
n<<2;memset(bit.c,0,sizeof(bit.c));
for(int i=1;i<=n;i++){
scanf("%d%d",&Q[i].s,&Q[i].e);
X[size++]=Q[i].s; X[size++]=Q[i].e;
}
for(int i=1;i<=m;i++){
scanf("%d",&ask[i]);
X[size++]=ask[i];
}
bit.n=size+10;
sort(X,X+size);  size=unique(X,X+size)-X;
for(int i=1;i<=n;i++){
int l=lower_bound(X,X+size,Q[i].s)-X+1;
int r=lower_bound(X,X+size,Q[i].e)-X+1;
//  printf("l= %d r= %d\n",l,r);
bit.add(l,1);bit.add(r+1,-1);
}
printf("Case #%d:\n",++ncase);
for(int i=1;i<=m;i++){
int t=lower_bound(X,X+size,ask[i])-X+1;
printf("%d\n",bit.sum(t));
}
}
return 0;
}


代码二,网上看大佬们的代码都是手写去重,手写二分查找,肯定有好处的 , 有哪位大佬路过指点一下 好在哪?Orz。

这里贴下代码:

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
#define first fi
#define second se
#define  LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)

const int MAXN = 200008;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

struct BIT{
int n;
int c[MAXN<<1];
inline int lowbit(int x) { return x&(-x);}
void add(int x,int val){
for(int i=x;i<=n;i+=lowbit(i))
c[i]+=val;
}
int sum(int x){
int ans=0;
for(int i=x;i>0;i-=lowbit(i))
ans+=c[i];
return ans;
}
}bit;
struct Node{
int s,e;
}Q[MAXN];
int X[MAXN<<1],size,sz;
int ask[MAXN];
int Bin(int num,int R){
int l=0,r=R-1,mid;
while(l<=r){
mid=(l+r)>>1;
if(X[mid]==num) return mid;
if(X[mid]<num) l=mid+1;
else r=mid-1;
}
}
int main(){
CLOSE();
//  fread();
//  fwrite();
int T;scanf("%d",&T);int ncase=0;
while(T--){
int n,m;scanf("%d%d",&n,&m);
size=0,sz=1;
memset(bit.c,0,sizeof(bit.c));
for(int i=1;i<=n;i++){
scanf("%d%d",&Q[i].s,&Q[i].e);
X[size++]=Q[i].s; X[size++]=Q[i].e;
}
for(int i=1;i<=m;i++){
scanf("%d",&ask[i]);
X[size++]=ask[i];
}
// 离散
sort(X,X+size);
for(int i=1;i<size;i++)
if(X[i]!=X[i-1])  X[sz++]=X[i];
bit.n=sz+10;
for(int i=1;i<=n;i++){
int l=Bin(Q[i].s,sz)+1;
int r=Bin(Q[i].e,sz)+1;
bit.add(l,1);bit.add(r+1,-1);
}
printf("Case #%d:\n",++ncase);
for(int i=1;i<=m;i++){
int t=Bin(ask[i],sz)+1;
printf("%d\n",bit.sum(t));
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: