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离散一下完就是裸的线段树

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
map<int,int>mp;
int n,m,cnt,cnt2;
int l[],r[];
int a[];
struct query{int l,r;}q[];
struct segtree{
int l,r,mx,tag;
}t[*];
inline void pushdown(int k)
{int tt=t[k].tag;t[k].tag=;t[k<<].mx+=tt;t[k<<].tag+=tt;t[k<<|].mx+=tt;t[k<<|].tag+=tt;}
inline void update(int k)
{t[k].mx=max(t[k<<].mx,t[k<<|].mx);}
inline void buildtree(int now,int l,int r)
{
t[now].l=l;t[now].r=r;
if(l==r)
{
t[now].mx=t[now].tag=;
return;
}
int mid=(l+r)>>;
buildtree(now<<,l,mid);
buildtree(now<<|,mid+,r);
}
inline void add(int now,int x,int y,int d)
{
int l=t[now].l,r=t[now].r;
pushdown(now);
if (l==x&&r==y)
{
t[now].tag+=d;
t[now].mx+=d;
return;
}
int mid=(l+r)>>;
if (y<=mid)add(now<<,x,y,d);
else if (x>mid)add(now<<|,x,y,d);
else add(now<<,x,mid,d),add(now<<|,mid+,y,d);
update(now);
}
inline int ask(int now,int x,int y)
{
int l=t[now].l,r=t[now].r;
pushdown(now);
if (l==x&&r==y)return t[now].mx;
int mid=(l+r)>>;
if (y<=mid)return ask(now<<,x,y);
else if (x>mid)return ask(now<<|,x,y);
else return max(ask(now<<,x,mid),ask(now<<|,mid+,y));
}
int main()
{
n=read();
for (int i=;i<=n;i++)l[i]=read(),r[i]=read(),a[++cnt]=l[i],a[++cnt]=r[i];
m=read();
for (int i=;i<=m;i++)q[i].l=read(),q[i].r=read(),a[++cnt]=q[i].l,a[++cnt]=q[i].r;
sort(a+,a+cnt+);
for (int i=;i<=cnt;i++)
if (i==||a[i]!=a[i-])mp[a[i]]=++cnt2;
for (int i=;i<=n;i++)l[i]=mp[l[i]],r[i]=mp[r[i]];
for (int i=;i<=m;i++)q[i].l=mp[q[i].l],q[i].r=mp[q[i].r];
buildtree(,,cnt2);
for (int i=;i<=n;i++)add(,l[i],r[i],);
for (int i=;i<=m;i++)printf("%d\n",ask(,q[i].l,q[i].r));
}

Spoj BGSHOOT

Spoj-BGSHOOT的更多相关文章

  1. SPOJ BGSHOOT - Shoot and kill (线段树 区间修改 区间查询)

    BGSHOOT - Shoot and kill no tags  The problem is about Mr.BG who is a great hunter. Today he has gon ...

  2. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  3. SPOJ DQUERY D-query(主席树)

    题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...

  4. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  5. 【填坑向】spoj COT/bzoj2588 Count on a tree

    这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...

  6. SPOJ bsubstr

    题目大意:给你一个长度为n的字符串,求出所有不同长度的字符串出现的最大次数. n<=250000 如:abaaa 输出: 4 2 1 1 1 spoj上的时限卡的太严,必须使用O(N)的算法那才 ...

  7. 【SPOJ 7258】Lexicographical Substring Search

    http://www.spoj.com/problems/SUBLEX/ 好难啊. 建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量.该状态能走到的所有状态的f值的和 ...

  8. 【SPOJ 1812】Longest Common Substring II

    http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...

  9. 【SPOJ 8222】Substrings

    http://www.spoj.com/problems/NSUBSTR/ clj课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...

随机推荐

  1. sqlserver中drop、truncate和delete语句的用法

    虽然小编不建议大家去用命令删除数据库表中的东西,但是这些删除命令总有用的着的地方. 说到删除表数据的关键字,大家记得最多的可能就是delete了 然而我们做数据库开发,读取数据库数据.对另外的两兄弟用 ...

  2. ThreadLocal使用,应用场景,源码实现,内存泄漏

    首先,ThreadLocal 不是用来解决共享对象的多线程访问问题的,一般情况下,通过ThreadLocal.set() 到线程中的对象是该线程自己使用的对象,其他线程是不需要访问的,也访问不到的.各 ...

  3. NBUT 1115 Cirno's Trick (水)

    题意: 给出多个double数,去掉其最小的和最大的,再对余下的求均值. 思路: 再输入时将最大和最小去掉,顺便统计非最值的和,输出时除一下个数即可. #include <bits/stdc++ ...

  4. PCL点云处理可视化——法向显示错误“no override found for vtk actor”解决方法

    转:https://blog.csdn.net/bflong/article/details/79137692 参照:https://blog.csdn.net/imsaws/article/deta ...

  5. 单源最短路Dijstra

    #include<iostream> #include<cstring> #define INF 0x3f3f3f3f using namespace std; ][],d[] ...

  6. 使用bat脚本调用py文件直接获取应用的包名和targetversion

    背景: 在上一篇已经介绍过如何利用python调用aapt获取包名 https://www.cnblogs.com/reseelei-despair/p/11078750.html 但是因为每次都要修 ...

  7. 无法连接Elasticsearch解决方案

    前言 最近还是在弄ELK,并且在测试Logstash从kafka消费日志(最后输出到Elasticsearch). 测试完毕后,在kibana中,并没有发现Elasticsearch中的数据. 后来装 ...

  8. Python9-函数-day9

    初识函数定义与调用 def my_len(): i = 0 for k in s1: i +=1 return i #返回值 # s = 'tim' s1 = '班主任阿娇' length =my_l ...

  9. (转)iOS 属性字符串

    富文本的基本数据类型是NSAttributedString.**属性化字符串**(attributed string)是把属性设置到某些字符上的字符串.属性可以是任何键值对,但是为了实现富文本,则通常 ...

  10. JavaScript正则表达式-RegExp对象

    RegExp对象方法 exec():与String对象的match()方法功能相同. 参数为被搜索字符串.返回数组或null. test():与String对象的search()方法功能相同. 参数为 ...