zoukankan      html  css  js  c++  java
  • Spoj-BGSHOOT

    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 R, L 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

    一堆l和r超大的区间+1、查询区间max的操作

    用个map离散一下完就是裸的线段树

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<algorithm>
     6 #include<cmath>
     7 #include<queue>
     8 #include<deque>
     9 #include<set>
    10 #include<map>
    11 #include<ctime>
    12 #define LL long long
    13 #define inf 0x7ffffff
    14 #define pa pair<int,int>
    15 #define mkp(a,b) make_pair(a,b)
    16 #define pi 3.1415926535897932384626433832795028841971
    17 using namespace std;
    18 inline LL read()
    19 {
    20     LL x=0,f=1;char ch=getchar();
    21     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    22     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    23     return x*f;
    24 }
    25 map<int,int>mp;
    26 int n,m,cnt,cnt2;
    27 int l[100010],r[100010];
    28 int a[400010];
    29 struct query{int l,r;}q[100010];
    30 struct segtree{
    31     int l,r,mx,tag;
    32 }t[400010*4];
    33 inline void pushdown(int k)
    34 {int tt=t[k].tag;t[k].tag=0;t[k<<1].mx+=tt;t[k<<1].tag+=tt;t[k<<1|1].mx+=tt;t[k<<1|1].tag+=tt;}
    35 inline void update(int k)
    36 {t[k].mx=max(t[k<<1].mx,t[k<<1|1].mx);}
    37 inline void buildtree(int now,int l,int r)
    38 {
    39     t[now].l=l;t[now].r=r;
    40     if(l==r)
    41     {
    42         t[now].mx=t[now].tag=0;
    43         return;
    44     }
    45     int mid=(l+r)>>1;
    46     buildtree(now<<1,l,mid);
    47     buildtree(now<<1|1,mid+1,r);
    48 }
    49 inline void add(int now,int x,int y,int d)
    50 {
    51     int l=t[now].l,r=t[now].r;
    52     pushdown(now);
    53     if (l==x&&r==y)
    54     {
    55         t[now].tag+=d;
    56         t[now].mx+=d;
    57         return;
    58     }
    59     int mid=(l+r)>>1;
    60     if (y<=mid)add(now<<1,x,y,d);
    61     else if (x>mid)add(now<<1|1,x,y,d);
    62     else add(now<<1,x,mid,d),add(now<<1|1,mid+1,y,d);
    63     update(now);
    64 }
    65 inline int ask(int now,int x,int y)
    66 {
    67     int l=t[now].l,r=t[now].r;
    68     pushdown(now);
    69     if (l==x&&r==y)return t[now].mx;
    70     int mid=(l+r)>>1;
    71     if (y<=mid)return ask(now<<1,x,y);
    72     else if (x>mid)return ask(now<<1|1,x,y);
    73     else return max(ask(now<<1,x,mid),ask(now<<1|1,mid+1,y));
    74 }
    75 int main()
    76 {
    77     n=read();
    78     for (int i=1;i<=n;i++)l[i]=read(),r[i]=read(),a[++cnt]=l[i],a[++cnt]=r[i];
    79     m=read();
    80     for (int i=1;i<=m;i++)q[i].l=read(),q[i].r=read(),a[++cnt]=q[i].l,a[++cnt]=q[i].r;
    81     sort(a+1,a+cnt+1);
    82     for (int i=1;i<=cnt;i++)
    83     if (i==1||a[i]!=a[i-1])mp[a[i]]=++cnt2;
    84     for (int i=1;i<=n;i++)l[i]=mp[l[i]],r[i]=mp[r[i]];
    85     for (int i=1;i<=m;i++)q[i].l=mp[q[i].l],q[i].r=mp[q[i].r];
    86     buildtree(1,1,cnt2);
    87     for (int i=1;i<=n;i++)add(1,l[i],r[i],1);
    88     for (int i=1;i<=m;i++)printf("%d
    ",ask(1,q[i].l,q[i].r));
    89 }
    Spoj BGSHOOT
  • 相关阅读:
    oracle之sqlplus讲解
    oracle数据库--启动和关闭
    linux下使用SSL代理(SSLedge)
    Titanium系列--利用js动态获取当前时间
    Titanium系列--利用Titanium开发android App实战总结
    Titanium系列--我常用的Titanium的快捷键(持续更新中。。)
    Titanium系列--安装Titanium Studio 中的Android SDK,JDK以及环境变量的配置(二)
    Titanium系列--Titanium的简介、Titanium Studio安装和配置(一)
    Happymenu新的开始
    对IEnumerable<T>和IQueryable<T>的一点见解
  • 原文地址:https://www.cnblogs.com/zhber/p/7182308.html
Copyright © 2011-2022 走看看