Time Limit: 20 Sec  Memory Limit: 128 MB
Submit: 1269  Solved: 665
[Submit][Status][Discuss]

Description

Input

Output

Sample Input

7 5
0 2 1 0 1 3 2
1 3
2 3
1 4
3 6
2 7

Sample Output

3
0
3
2
4

HINT

Source

By X

裸地莫队题目,

每次加入一个数,如果当前答案等于加入的数,就暴力向上加

每次删除一个数,如果删除的那个数比当前的答案小,那么当前的答案就赋值程这个数

因此

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的更多相关文章

  1. Bzoj 3339: Rmq Problem && Bzoj 3585: mex 莫队,树状数组,二分

    3339: Rmq Problem Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 833  Solved: 397[Submit][Status][D ...

  2. BZOJ 3339: Rmq Problem 莫队算法

    3339: Rmq Problem 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3339 Description n个数,m次询问l,r ...

  3. BZOJ 3339: Rmq Problem

    3339: Rmq Problem Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 1075  Solved: 549[Submit][Status][ ...

  4. 【BZOJ】3339: Rmq Problem & 3585: mex(线段树+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3585 好神的题. 但是!!!!!!!!!!!!!!我线段树现在要开8倍空间才能过!!!!!!!!!! ...

  5. bzoj 3339 Rmq Problem / mex

    题目 我的树状数组怎么那么慢啊 就是一道水题,我们考虑一下对于一个区间\([l,r]\)什么样的数能被计算 显然需要对于一个\(j\),需要满足\(j<l\)且\(nxt_{j}>r\), ...

  6. bzoj 3585: mex && 3339: Rmq Problem -- 主席树

    3585: mex Time Limit: 20 Sec  Memory Limit: 128 MB Description 有一个长度为n的数组{a1,a2,...,an}.m次询问,每次询问一个区 ...

  7. BZOJ 3339 Rmq Problem(离线+线段树+mex函数)

    题意: q次询问,问[l,r]子区间的mex值 思路: 对子区间[l,r],当l固定的时候,[l,r]的mex值对r单调不减 对询问按照l离线,对当前的l,都有维护一个线段树,每个叶节点保存[l,r] ...

  8. 【BZOJ】【3339】Rmq Problem

    离线+线段树 Orz Hzwer,引用题解: 这一题在线似乎比较麻烦 至于离线.. 首先按照左端点将询问排序 然后一般可以这样考虑 首先如何得到1-i的sg值呢 这个可以一开始扫一遍完成 接着考虑l- ...

  9. Rmq Problem

    大视野——3339: Rmq Problem Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 1192  Solved: 620[Submit][Sta ...

随机推荐

  1. log4j.propertie配置具体解释

    1.log4j.rootCategory=INFO, stdout , R 此句为将等级为INFO的日志信息输出到stdout和R这两个目的地,stdout和R的定义在以下的代码,能够随意起名.等级可 ...

  2. silverlight wpf Command提交时输入验证

    silverlight 或WPF在MVVM模式中使用INotifyDataErrorInfo接口对输入进行验证时 控件lostFocus时会触发验证,但在提交动作(例如button的Command)时 ...

  3. Nashorn——在JDK 8中融合Java与JavaScript之力--转

    原文地址:http://www.infoq.com/cn/articles/nashorn 从JDK 6开始,Java就已经捆绑了JavaScript引擎,该引擎基于Mozilla的Rhino.该特性 ...

  4. MyBatis数据持久化(九)动态sql

    本文摘自:mybatis参考文档中文版 MyBatis的一个强大的特性之一通常是它的动态SQL能力.如果你有使用JDBC或其他相似框架的经验,你就明白条件地串联SQL字符串在一起是多么的痛苦,确保不能 ...

  5. C# 运算符 ?、??、?: 、?. 、 各种问号的用法和说明

    1. 可空类型修饰符(?):引用类型可以使用空引用表示一个不存在的值,而值类型通常不能表示为空.例如:string str=null; 是正确的,int i=null; 编译器就会报错.为了使值类型也 ...

  6. STM8S103之时钟设置

    最大时钟(指的是system clock):外部晶振24MHz,内部高速RC16MHz 三个时钟源:外部晶振.内部高速RC(上电默认) +内部低速RC 几个时钟:master clock(即sytem ...

  7. HDU 1556 Color the ball【树状数组】

    题意:给出n个区间,每次给这个区间里面的数加1,询问单点的值 一维的区间更新,单点查询,还是那篇论文里面讲了的 #include<iostream> #include<cstdio& ...

  8. windows连接投影仪后桌面画面和白板画面不一致

    windows连接投影仪后桌面画面和白板画面不一致: 一. windows 搜索: 投影仪 选择相应的效果:

  9. 第三方-FastDFS分布式文件系统

    1.什么是FastDFS? FastDFS 是用 c 语言编写的一款开源的分布式文件系统.FastDFS 为互联网量身定制, 充分考虑了冗余备份.负载均衡.线性扩容等机制,并注重高可用.高性能等指标, ...

  10. SASS 使用(安装)

    一.安装SASS 1.sass基于Ruby语言开发而成,因此安装sass前需要安装Ruby.(注:mac下自带Ruby无需在安装Ruby!) 2.安装过程中请注意勾选Add Ruby executab ...