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课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...
随机推荐
- selenium-Python之鼠标事件
通过click()来模拟鼠标的单击操作,鼠标还具有鼠标右击,双击,悬停甚至鼠标拖动等功能.在webdriver中,将这些鼠标操作方法封装在ActionChains类提供. ActionChains类提 ...
- mac系统下android studio创建手机模拟器
打开android studio,点击右上角的模拟器图标,打开“Android Virtual Device Manager” 窗口,如下图 点击“Create Virtual Device”,在 ...
- css3中animation属性animation-timing-function知识点以及其属性值steps()
在animation中最重要的其实就是时间函数(animation-timing-function)这个属性,他决定了你的动画将以什么样的速度执行,所以最关键的属性值也就是cubic-bezier(n ...
- url地址数据参数转化JSON对象(js三种方法实现)
当我们用get方法提交表单时,在url上会显示出请求的参数组成的字符串,例如:http://localhost:3000/index.html?phone=12345678901&pwd=12 ...
- javascript变量名命名规则
1. js变量名可以包含数字,字母,$及_,不能以数字开头. 2. js变量可以使用中文,但是最好不要这么命名,以避免不必要的麻烦.
- c++ 将输入存储到数组,然后反转数组,最后输出
// 输入一个包含多个double元素的数组,先打印结果,然后反转出头和尾元素之外的所有元素,最后再打印结果 #include <iostream> using namespace std ...
- URAL1765 Error 404
题目描述: vjudge 题解: STO ljx OTZ 下面这个算法是这位贡献的. 不妨将删去改为加入. 那么对于$n=p^k$,即只有一个质因子的$n$来说,若$i$已选,那么$i+n/p$.$i ...
- python数据类型、字符编码、文件处理-练习
练习-字符串 # 写代码,有如下变量,请按照要求实现每个功能 (共6分,每小题各0.5分) name = " aleX" # ) 移除 name 变量对应的值两边的空格,并输出处理 ...
- 总线(bus);设备(devices);驱动(drivers)
Linux Cross Reference Free Electrons Embedded Linux Experts • Source Navigation • Diff Markup • Id ...
- 在myeclipse中使用查找功能
1.全局搜索(快捷键:ctrl+H) 在弹出对话框中选File Search选项,然后在第一个文本框中粘贴(我一般用粘贴)或自已手动录入(容易写错)要查找的字符串(可以是英文字符也可以是汉字),在第二 ...