3339: Rmq Problem
Time Limit: 20 Sec Memory Limit: 128 MB
Submit: 1269 Solved: 665
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
0 2 1 0 1 3 2
1 3
2 3
1 4
3 6
2 7
Sample Output
0
3
2
4
HINT
Source
裸地莫队题目,
每次加入一个数,如果当前答案等于加入的数,就暴力向上加
每次删除一个数,如果删除的那个数比当前的答案小,那么当前的答案就赋值程这个数
因此
add和dele函数都非常好些。
但是,,,,,
我写的莫队各种RE,WA,TLE,MLE。。。。。。。。。。。。
和标称拍了一头午也没拍出错误来。。。。
也是醉了。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=;
/*inline void read(int &n)
{
char c='+';int x=0;bool flag=0;
while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;}
while(c>='0'&&c<='9')x=x*10+c-48,c=getchar();
n=flag==1?-x:x;
}*/
const int BUF=;
char Buf[BUF],*buf=Buf;int BUFflag;
inline void read(int &now)
{
for(now=; ! isdigit (*buf); ++buf);
for(; isdigit ( *buf );now = now * + (*buf-'') ,++buf);
}
struct node
{
int l,r,bh,ans;
}ask[MAXN];
int n,q;
int a[MAXN];
int pos[MAXN];
int base;
inline int comp(const node &a,const node &b)
{
if(pos[a.l]==pos[a.l]) return pos[a.r]<pos[b.r];
else return pos[a.l]<pos[b.l];
}
int happen[MAXN];
int out[MAXN];
int now=;
inline void dele(int p)
{
happen[a[p]]--;
if(happen[a[p]]==&&a[p]<now)
now=a[p];
}
inline void add(int p)
{
happen[a[p]]++;
if(now==a[p])
while(happen[now])
now++;
}
int modui()
{
int ll=,rr=;
for(int i=;i<=q;i++)
{
for(;ask[i].l<ll;ll--)
add(ll-);
for(;ask[i].l>ll;ll++)
dele(ll);
for(;ask[i].r<rr;rr--)
dele(rr);
for(;ask[i].r>rr;rr++)
add(rr+);
ask[i].ans=now;
}
for(int i=;i<=q;i++)
out[ask[i].bh]=ask[i].ans;
for(int i=;i<=q;i++)
printf("%d\n",out[i]);
}
int main()
{
//freopen("a.in","r",stdin);
// freopen("b.out","w",stdout);
fread(buf,,BUF,stdin);
read(n);read(q);
base=sqrt(n);
for(int i=;i<=n;i++)
read(a[i]),pos[i]=(i-)/base+;
for(int i=;i<=q;i++)
{
int ll,rr;read(ll);read(rr);
ask[i].l=ll;ask[i].r=rr;ask[i].bh=i;
}
sort(ask+,ask+q+,comp);
modui();
return ;
}
自己写的
#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int a[maxn],pos[maxn],c[maxn],Ans[maxn];
int ans,n,m;
struct query
{
int l,r,id;
}Q[maxn];
bool cmp(query a,query b)
{
if(pos[a.l]==pos[b.l])
return a.r<b.r;
return pos[a.l]<pos[b.l];
}
void Update(int x)
{
c[x]++;
if(ans==x)
while(c[ans])
ans++;
}
void Delete(int x)
{
c[x]--;
if(c[x]==&&x<ans)ans=x;
}
int main()
{
freopen("a.in","r",stdin);
freopen("c.out","w",stdout);
n=read(),m=read();
int sz =ceil(sqrt(1.0*n));
for(int i=;i<=n;i++)
{
a[i]=read();
pos[i]=(i-)/sz;
}
for(int i=;i<=m;i++)
{
Q[i].l=read();
Q[i].r=read();
Q[i].id = i;
}
sort(Q+,Q++m,cmp);
int L=,R=;ans=;
for(int i=;i<=m;i++)
{
int id = Q[i].id;
while(R<Q[i].r)R++,Update(a[R]);
while(L>Q[i].l)L--,Update(a[L]);
while(R>Q[i].r)Delete(a[R]),R--;
while(L<Q[i].l)Delete(a[L]),L++;
Ans[id]=ans;
}
for(int i=;i<=m;i++)
printf("%d\n",Ans[i]);
}
标称
3339: Rmq Problem的更多相关文章
- Bzoj 3339: Rmq Problem && Bzoj 3585: mex 莫队,树状数组,二分
3339: Rmq Problem Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 833 Solved: 397[Submit][Status][D ...
- BZOJ 3339: Rmq Problem 莫队算法
3339: Rmq Problem 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3339 Description n个数,m次询问l,r ...
- BZOJ 3339: Rmq Problem
3339: Rmq Problem Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 1075 Solved: 549[Submit][Status][ ...
- 【BZOJ】3339: Rmq Problem & 3585: mex(线段树+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=3585 好神的题. 但是!!!!!!!!!!!!!!我线段树现在要开8倍空间才能过!!!!!!!!!! ...
- bzoj 3339 Rmq Problem / mex
题目 我的树状数组怎么那么慢啊 就是一道水题,我们考虑一下对于一个区间\([l,r]\)什么样的数能被计算 显然需要对于一个\(j\),需要满足\(j<l\)且\(nxt_{j}>r\), ...
- bzoj 3585: mex && 3339: Rmq Problem -- 主席树
3585: mex Time Limit: 20 Sec Memory Limit: 128 MB Description 有一个长度为n的数组{a1,a2,...,an}.m次询问,每次询问一个区 ...
- BZOJ 3339 Rmq Problem(离线+线段树+mex函数)
题意: q次询问,问[l,r]子区间的mex值 思路: 对子区间[l,r],当l固定的时候,[l,r]的mex值对r单调不减 对询问按照l离线,对当前的l,都有维护一个线段树,每个叶节点保存[l,r] ...
- 【BZOJ】【3339】Rmq Problem
离线+线段树 Orz Hzwer,引用题解: 这一题在线似乎比较麻烦 至于离线.. 首先按照左端点将询问排序 然后一般可以这样考虑 首先如何得到1-i的sg值呢 这个可以一开始扫一遍完成 接着考虑l- ...
- Rmq Problem
大视野——3339: Rmq Problem Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 1192 Solved: 620[Submit][Sta ...
随机推荐
- 模式匹配的KMP 算法
常见的字符串匹配时,模式串长度为n,源串长度为m,则从头匹配,两个指针i指向源串,j指向模式串,如遇到不同则回溯使j=0,这样就要反复匹配会使效率变低. 因为在如今i之前 的模式串与匹配串的匹配是同样 ...
- nyoj--2--括号配对问题(栈函数)
括号配对问题 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 现在,有一行括号序列,请你检查这行括号是否配对. 输入 第一行输入一个数N(0<N<=100), ...
- 2019Pycharm激活方法
1.将“0.0.0.0 account.jetbrains.com”添加到hosts文件中 2.打开http://idea.lanyus.com/ 3.获取激活码,粘贴到第二个选项中 亲测可用.
- SQLServer 错误: 15404,维护计划无法执行
错误症状: D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOG下面的ERROELOG,用文本打,查看运行维维计划不成功是生成的错误日志详细信 ...
- Struts2.3.16.1+Hibernate4.3.4+Spring4.0.2 框架整合(转)
原文 http://blog.csdn.net/songanling/article/details/22454973 最新版Struts2+Hibernate+Spring整合 目前为止三 ...
- 数据库应用_innobackupex备份与恢复
1.Percona软件介绍; 2.innobackupex的备份与恢复 一, Percona软件 在学习percona软件之前,我们看一下物理备份和mysqldump备份有哪些缺陷. 物理备份的缺点: ...
- 使用js获取url中的get参数并转成json格式
写在前面的 没啥说的 上代码 思路就是先获取到?后面的参数区,然后 利用字符串转数组方法获取到各个参数 var json = {}; var url = 'https://www.baidu.com/ ...
- Django之ORM的增删改查
一.添加表记录 对于单表有两种方式 # 添加数据的两种方式 # 方式一:实例化对象就是一条表记录 Frank_obj = models.Student(name ="海东",cou ...
- 关于__str__的介绍
在python语言里,__str__一般是格式是这样的. class A: def __str__(self): return "this is in str" 事实上,__str ...
- 解决Vue引入百度地图JSSDK:BMap is undefined 问题
百度地图官网文档介绍使用JSSDK时,仅提供了2种引入方式: script引入 异步加载 解决跨域问题,实例调用百度地图 但vue项目中仅某一两个页面需要用到百度地图,所以不想在 index.html ...