SPOJ BGSHOOT - Shoot and kill (线段树 区间修改 区间查询)
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 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]上会出现一只猎物,有这样的n个区间。这个人只有一支箭,所以只能射一次,由于所有猎物都在一条直线上,
所以在同一时刻出现的猎物可以同时被射死。然后给你q次询问,每次 询问给出一个区间,问在此区间内这个人用一支箭最多可以射死多少只猎物。
这就是个区间覆盖问题,求某一整数点最多覆盖多少区间。这就可以用线段树来做了,几乎模板题。记得离散化。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 100000000
#define met(a,b) memset(a,b,sizeof a)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
using namespace std;
const int N = 4e5+;
const int M = 4e5+;
int n,sum[*N],m;
int lazy[*N];
int num[N*];
int ql[N*],qr[N*];
map<int,int>mp;
struct man{
int l,r;
}a[N*];
void pushup(int pos){
sum[pos]=max(sum[pos*],sum[pos*+]);
}
void pushdown(int pos){
if(lazy[pos]){
lazy[pos*]+=lazy[pos];lazy[pos*+]+=lazy[pos];
sum[pos*]+=lazy[pos];
sum[pos*+]+=lazy[pos];
lazy[pos]=;
}return;
} void update(int L,int R,int val,int l,int r,int pos) {
if(l>=L&&r<=R) {
lazy[pos]+=val;
sum[pos]+=val;
return;
}
int mid=(l+r)>>;
pushdown(pos);
if(L<=mid) update(L,R,val,l,mid,pos<<);
if(mid<R)update(L,R,val,mid+,r,pos<<|);
pushup(pos);
}
int query(int L,int R,int l,int r,int pos) {
if(l>=L&&r<=R){
return sum[pos];
}
int mid=(l+r)>>;
pushdown(pos);
int ans=;
if(L<=mid) ans=max(query(L,R,l,mid,pos<<),ans);
if(R>mid) ans=max(query(L,R,mid+,r,pos<<|),ans);
return ans;
}
int main() {
int ll,rr,cnt=;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d%d",&a[i].l,&a[i].r);
num[cnt++]=a[i].l;
num[cnt++]=a[i].r;
}
scanf("%d",&m);
for(int i=;i<m;i++){
scanf("%d%d",&ql[i],&qr[i]);
num[cnt++]=ql[i];
num[cnt++]=qr[i];
}
sort(num,num+cnt);
int all=;
for(int i=;i<cnt;i++)if(!mp[num[i]])mp[num[i]]=++all;
for(int i=;i<=n;i++)update(mp[a[i].l],mp[a[i].r],,,all,);
for(int i=;i<m;i++){
printf("%d\n",query(mp[ql[i]],mp[qr[i]],,all,));
}
return ;
}
SPOJ BGSHOOT - Shoot and kill (线段树 区间修改 区间查询)的更多相关文章
- [线段树]区间修改&区间查询问题
区间修改&区间查询问题 [引言]信息学奥赛中常见有区间操作问题,这种类型的题目一般数据规模极大,无法用简单的模拟通过,因此本篇论文将讨论关于可以实现区间修改和区间查询的一部分算法的优越与否. ...
- SPOJ GSS2 - Can you answer these queries II(线段树 区间修改+区间查询)(后缀和)
GSS2 - Can you answer these queries II #tree Being a completist and a simplist, kid Yang Zhe cannot ...
- Hdu 1698(线段树 区间修改 区间查询)
In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...
- A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询
//add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #includ ...
- Codeforces Round #442 (Div. 2) E Danil and a Part-time Job (dfs序加上一个线段树区间修改查询)
题意: 给出一个具有N个点的树,现在给出两种操作: 1.get x,表示询问以x作为根的子树中,1的个数. 2.pow x,表示将以x作为根的子树全部翻转(0变1,1变0). 思路:dfs序加上一个线 ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- codevs 1690 开关灯 线段树区间更新 区间查询Lazy
题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的,六点之后,会有M(2<=m<=100000)个人 ...
- 题解报告:hdu 1698 Just a Hook(线段树区间修改+lazy懒标记的运用)
Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...
随机推荐
- sqlserver数据库迁移
本篇我们将利用DMA一步一步实现SQL Server 的迁移.帮助大家理解现在的SQL Server与新版本的融合问题,同时需要我们做哪些操作来实现新版本的升级或者迁移. SQL Server 迁移 ...
- Expect使用小记
By francis_hao May 31,2017 本文翻译了部分Expect的man手册,只选取了个人常用的功能,因此并不完善. Expect是一个可以和交互式程序对话的程序 概述 ...
- ar用法小记
ar是用来创建.修改或者从档案文件中提取的GNU程序,它被认为是一个二进制的工具,因为它最大的应用就是将一些子程序归档为库文件. 用法概述 ar [-]p[mod [relpos] [count]] ...
- recycleview的基础Adapter
.封装了一个基础的adapter.,用于recycleview的快捷使用有BaseAdapter,BaseViewHolder,PAdapter,MainActivity public abstrac ...
- 封装安卓的okhttp
1.封装了get方法,handler更新主线程,回调的onsuccess,onfailure,onerror等方法 2.配置文件 api 'com.android.support:recyclervi ...
- bzoj 2304 [Apio2011]寻路 Dij+模拟+恶心建图
[Apio2011]寻路 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 624 Solved: 193[Submit][Status][Discus ...
- vue-transition-fade
<!Doctype> <html> <head> <meta charset="utf-8"> <meta name=&quo ...
- Spring 中 AbstractExcelView 支持根据模板生成Excel文件. 通过设置 view 的 URL 属性指定模板的路径
注意:1. 模板需放在 WEB-INF 目录下2. 指定模板路径时不需要添加扩展名, Spring将自动添加 .xls 到URL 属性中.3. 在指定URL前需先设置 view 的 Applicat ...
- 【洛谷 P1337】[JSOI2004]平衡点 / 吊打XXX (模拟退火)
题目链接 正解就算了吧,谁叫我理生化 语数外 政史地都菜呢 模拟退火真玄学,不知道发生了什么就跑出答案了,原理就算了吧,能用(pianfen)就好. 当重物平衡时,势能一定是最小的,于是当我随机出一个 ...
- bzoj 1042 DP+容斥原理
我们可以先DP预处理出W[I]代表买I的东西,每种钞票的个数 不做限制的方案数,那么对于每一组数据的限制,我们可以知道 W[S-C[I]*(D[I]+1)]C为面值,D为数量,这个代表第I种钞票一定 ...