线段树+离散化+离线:HDU 4325
2016-08-04 18:49
218 查看
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.
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
区间更新,点查询;
离散所有的点!包括每朵花的花期,还有查询的时间点!!!
然后离线处理!
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
区间更新,点查询;
离散所有的点!包括每朵花的花期,还有查询的时间点!!!
然后离线处理!
#include <cstdio> #include <algorithm> #include <cstring> #include <iostream> #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define inf 0x3f3f3f3f using namespace std; const int maxn=100009; long long pos[maxn<<1],sum[maxn<<2],add[maxn<<2],q[maxn]; int n,mm,m; struct node { int l,r; } f[maxn]; void pushdown(int rt) { if(add[rt]) { add[rt<<1]+=add[rt],add[rt<<1|1]+=add[rt]; sum[rt<<1]+=add[rt],sum[rt<<1|1]+=add[rt]; add[rt]=0; } } void update(int L,int R,int l,int r,int rt) { if(L<=l&&r<=R) { add[rt]++; sum[rt]++; return; } pushdown(rt); int m=l+r>>1; if(m<L) update(L,R,rson); else if(m>=R) update(L,R,lson); else { update(L,m,lson); update(m+1,R,rson); } } int query(int t,int l,int r,int rt) { if(l==r) return sum[rt]; pushdown(rt); int m=l+r>>1; if(t>m) return query(t,rson); else return query(t,lson); } int fin(int key,int high) { int low=0,mid; while(low<=high) { mid=high+low>>1; if(pos[mid]==key) return mid; else if(pos[mid]>key) high=mid-1; else low=mid+1; } } int main() { int t; scanf("%d",&t); for(int ase=1; ase<=t; ase++) { scanf("%d%d",&n,&mm); int cnt=0; for(int i=1; i<=n; i++) { scanf("%d%d",&f[i].l,&f[i].r); pos[cnt]=f[i].l,pos[cnt+1]=f[i].r,cnt+=2; } for(int i=1;i<=mm;i++) { scanf("%d",&q[i]); pos[cnt++]=q[i]; } sort(pos,pos+cnt); m=1; for(int j=1; j<cnt; j++) if(pos[j]!=pos[j-1]) pos[m++]=pos[j]; memset(sum,0,sizeof sum); memset(add,0,sizeof add); for(int i=1; i<=n; i++) { int ll=fin(f[i].l,m-1); int rr=fin(f[i].r,m-1); update(ll,rr,0,m-1,1); } printf("Case #%d:\n",ase); for(int i=1; i<=mm; i++) { int qq=fin(q[i],m-1); printf("%d\n",query(qq,0,m-1,1)); } } return 0; }
相关文章推荐
- HDU 4325 Flowers (线段树+离散化)
- HDU-4325 线段树+离散化
- 【线段树+离散化+离线方法】杭电 hdu 3333 Turing Tree
- 【HDU - 4325 】Flowers 【树状数组+离散化】
- HDU 3333 Turing Tree 线段树+离线处理
- HDU 4325 Flowers(树状数组+离散化)
- HDU 4358 Boring counting(树的遍历+树状数组+离散化+离线处理)
- [线段树+离散化+单点查询] HDOJ - 4325 Flowers
- FJUT 3097(hdu 3333) 区间种类数 线段树+离线
- HDU-4288:Coder(线段树+离线操作)
- HDU 4288 && Codeforces 85D (线段树+离散化+离线处理)
- [线段树+离散化+单点查询] HDOJ - 4325 Flowers
- HDU 3333 & 3874 (线段树+离线询问)
- HDU 3333 & 3874 (线段树+离线询问)
- hdu 4325 Flowers(线段树+离散化)
- hdu 4630 No Pain No Game(线段树+离线操作)
- hdu 4630 No Pain No Game(线段树+离线操作)
- FJUT 3097(hdu 3333) 区间种类数 线段树+离线
- HDU 4288 Coder 【线段树+离线处理+离散化】
- [线段树+离线处理] hdu 4417 Super Mario