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课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...
随机推荐
- XPath基本使用
一.简介 1.什么是XPath 1)XPath是W3C的一个标准 2)XPath 是一门在 XML 文档中查找信息的语言. 3)XPath 用于在 XML 文档中通过元素和属性进行导航. 4)XPa ...
- SnowKiting
原文 Let's go fly a kite...in the snow Reach into your closet,find that dusty kite and clean it off - ...
- SpringMVC、Spring和Struts的区别
http://www.cnblogs.com/hhx626/p/6010293.html 导读:近期做到的项目中,用到的框架师SSM(SpringMVC+Spring+Mybatis),那么在这之前用 ...
- JDBC连接数据库详解
JDBC连接数据库 •创建一个以JDBC连接数据库的程序,包含7个步骤: 1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机),这通过java.la ...
- POI 读取 Excel 文件
import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.Out ...
- Bootstrap 网格系统(Grid System)实例3
Bootstrap 网格系统(Grid System)实例:堆叠水平 <!DOCTYPE html><html><head><meta http-equiv= ...
- js获取主机名实现页面跳转
<script language="javascript" type="text/javascript"> var hostname ...
- redhat7.3建两个网桥导致mac地址不一致解决办法
将网卡配置文件中加入下面一行参数 NM_CONTROLLED=no
- 【计算机网络】DNS的作用以及修改DNS的方法
1.DNS的作用及修改DNS的方法 1.1.DNS的作用 DNS就是将域名映射成ip的分布式数据库服务器,它的作用如下图: 1.2.修改DNS的方法 常用的DNS服务器 1.114.114.114.1 ...
- Python9-模块1-day19
在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.defaultdict.namedtuple和Ord ...