码迷,mamicode.com
首页 > 其他好文 > 详细

SPOJ BGSHOOT 线段树

时间:2017-03-20 20:41:19      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:lan   sort   oss   one   abi   tar   输出   ati   tin   

BGSHOOT - Shoot and kill

 

The problem is about Mr.BG who is a great hunter. Today he has gone to a dense forest for hunting and killing animals.

Sadly, he has only one bullet in his gun. He wants to kill as many animals as possible with only one bullet.

He has already known the information of duration availability of all animals of the forest.

So, he is planning to shoot at a time so that he could kill maximum animal. 

Input

Input begins with an integer N denoting total numbers of animals.

Next N lines contains the duration of availability of animal denoting by X (Starting time) and Y (Ending time) .

Then, there will be Q, denoting the total numbers of queries to be answer.

Each query giving two integer L and RL denoting the time hunter will come to forest and begins shooting

and R denoting last time upto which he will stay at forest for hunting.

Output

For each query output an integer denoting maximum numbers of animals he could kill by shooting at a time during L and R (inclusive).

Constraints:

1<=N,Q<=100000

1<=X,Y,L,R<=1000000000

Example

Input:
4
1 2
2 3
4 5
6 7
4
1 5
2 3
4 7
5 7

Output:
2
2
1
1

 

 

题意:有N个动物,第i个动物的活动时间属于[Xi, Yi]。
有Q次询问,每个询问代表你会在[Li, Ri]时间内打猎。
对于每个询问,输出在当前活动时间内动物最多的时刻的动物数。

 

题解:离散化后扔到线段树里搞一搞  裸的模板题

 

#include<iostream>  
#include<cstdio>  
#include<cstring>  
#include<algorithm>  
using namespace std;  
struct node{  
    int num,laz;  
}tree[1600005];  
int l[100005],r[100005],num[400005],x[100005],y[100005];  
void build(int t,int l,int r){  
    if(l==r){  
        tree[t].num=tree[t].laz=0;  
        return ;  
    }  
    int mid=l+r>>1;  
    build(t<<1,l,mid);  
    build(t<<1|1,mid+1,r);  
    tree[t].laz=0;  
    tree[t].num=max(tree[t<<1].num,tree[t<<1|1].num);  
}  
void change(int t,int l,int r,int x,int y){  
    if(x<=l&&y>=r){  
        tree[t].laz+=1;  
        return ;  
    }  
    int mid=l+r>>1;  
    if(x>mid)change(t<<1|1,mid+1,r,x,y);  
    else if(y<=mid)change(t<<1,l,mid,x,y);  
    else{  
        change(t<<1,l,mid,x,y);  
        change(t<<1|1,mid+1,r,x,y);  
    }  
}  
void travel(int t,int l,int r,int sum){  
    if(l==r){  
        sum+=tree[t].laz;  
        tree[t].num=sum;  
        return ;  
    }  
    int mid=l+r>>1;  
    travel(t<<1,l,mid,sum+tree[t].laz);  
    travel(t<<1|1,mid+1,r,sum+tree[t].laz);  
    tree[t].num=max(tree[t<<1].num,tree[t<<1|1].num);  
}  
int ans=0;  
void query(int t,int l,int r,int x,int y){  
    if(x<=l&&y>=r){  
        ans=max(ans,tree[t].num);  
        return ;  
    }  
    int mid=l+r>>1;  
    if(y<=mid)query(t<<1,l,mid,x,y);  
    else if(x>mid)query(t<<1|1,mid+1,r,x,y);  
    else{  
        query(t<<1,l,mid,x,y);  
        query(t<<1|1,mid+1,r,x,y);  
    }  
}  
int main(){  
    int i,j,n,q,cnt=0;  
    scanf("%d",&n);  
    for(i=1;i<=n;i++){  
        scanf("%d%d",&l[i],&r[i]);  
        num[++cnt]=l[i];  
        num[++cnt]=r[i];  
    }  
    scanf("%d",&q);  
    for(i=1;i<=q;i++){  
        scanf("%d%d",&x[i],&y[i]);  
        num[++cnt]=x[i];  
        num[++cnt]=y[i];  
    }  
    sort(num+1,num+1+cnt);  
    cnt=unique(num+1,num+1+cnt)-num-1;  
    build(1,1,cnt);  
    for(i=1;i<=n;i++){  
        int t1=lower_bound(num+1,num+1+cnt,l[i])-num;  
        int t2=lower_bound(num+1,num+1+cnt,r[i])-num;  
        change(1,1,cnt,t1,t2);  
    }  
    travel(1,1,cnt,0);  
    for(i=1;i<=q;i++){  
        int t1=lower_bound(num+1,num+1+cnt,x[i])-num;  
        int t2=lower_bound(num+1,num+1+cnt,y[i])-num;  
        ans=0;  
        query(1,1,cnt,t1,t2);  
        printf("%d\n",ans);  
    }  
    return 0;  
}  

 

SPOJ BGSHOOT 线段树

标签:lan   sort   oss   one   abi   tar   输出   ati   tin   

原文地址:http://www.cnblogs.com/MjLee/p/6591344.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!