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离散一下完就是裸的线段树
#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的更多相关文章
- 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 ...
- 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 ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
- 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 ...
- 【填坑向】spoj COT/bzoj2588 Count on a tree
这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...
- SPOJ bsubstr
题目大意:给你一个长度为n的字符串,求出所有不同长度的字符串出现的最大次数. n<=250000 如:abaaa 输出: 4 2 1 1 1 spoj上的时限卡的太严,必须使用O(N)的算法那才 ...
- 【SPOJ 7258】Lexicographical Substring Search
http://www.spoj.com/problems/SUBLEX/ 好难啊. 建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量.该状态能走到的所有状态的f值的和 ...
- 【SPOJ 1812】Longest Common Substring II
http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...
- 【SPOJ 8222】Substrings
http://www.spoj.com/problems/NSUBSTR/ clj课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...
随机推荐
- python一周速成学习笔记
目录 一:语法元素 1.注释,变量,空格的使用 2.输入函数,输出函数 3.分支语句,循环语句 4.保留字in,同步赋值 5.import与def以及turtle库 6.eval函数与repr函数 二 ...
- 图片,二进制,oracle数据库
图片在oracle数据库中一般以二进制存在,存储类型是blob,然而clob类型一般存储的是大于4000的字符,不能用来存储图像这样的二进制内容,下面展示一下实现图像,二进制,oracle 数据库的应 ...
- Java面试题全集(下)
这部分主要是开源Java EE框架方面的内容,包括hibernate.MyBatis.spring.Spring MVC等,由于Struts 2已经是明日黄花,在这里就不讨论Struts 2的面试题, ...
- 小白安装python软件
首先下载:anaconda3.x 下载方式:百度搜索 清华镜像anaconda https://mirrors.tuna.tsinghua.edu.cn/help/anacond ...
- Android之父Andy Rubin:被乔布斯羡慕嫉妒的天才
今年中国掀起一股“苹果热”,智能手机iPhone.平板电脑iPad遭疯抢,一度卖断货.然而,令许多人意想不到的是,在“苹果”的老家——美国市场,智能手机中卖得最火的并不是iPhone,而是Androi ...
- zust_第二周——瞎扯系列
首先来原题列表: A:Gridland http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1037 B:HangOver htt ...
- vue for循环中常见问题 之 求和(合计)
例:求后台返回数据this.dataInfo 中某个字段(item.totalSum)的和,只需添加computed,然后模板中直接可以使用totalSumAll (不需要再data中声明) comp ...
- Apache Commons Configuration的应用
Apache Commons Configuration的应用 Commons Configuration是一个java应用程序的配置管理工具.可以从properties或者xml文件中加载软件的配置 ...
- java在线聊天项目 swt可视化窗口Design 重新设计好友列表窗口 增加菜单栏
增加的菜单栏效果图如下: eclipse 中调整到 swt的design视图下 控件区域选择Menu Controls 将Menu Bar拖动到窗口标题栏 将Cascaded Menu拖动到Menu ...
- sqlite查看所有表名、判断表是否存在,字段名及字段信息
sqlite查看所有表名.判断表是否存在,字段名及字段信息 sqlite查看所有表名及字段名查询table,type 段是'table',name段是table的名字, select name f ...