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. [Hibernate]Access to DialectResolutionInfo cannot be null when &#39;hibernate.dialect&#39; not set

    使用Hibernate官方文档上的下面代码进行測试时报出这个异常. org.hibernate.HibernateException: Access to DialectResolutionInfo ...

  2. 【struts2】struts2中的流接收与流发送

    [前言]在我们的struts2后端中,实现流的接收和发送.就能够实现向server传视频流以及下载图片. [流接收] 如今举一个传公钥的样例.struts2用一个action接收Key,而Key就是用 ...

  3. NHibernate3剖析:Query篇之NHibernate.Linq增强查询

    系列引入 NHibernate3.0剖析系列分别从Configuration篇.Mapping篇.Query篇.Session策略篇.应用篇等方面全面揭示NHibernate3.0新特性和应用及其各种 ...

  4. cocos2d-x 移植到android中编译的一些问题:fatal error: Box2D/Box2D.h: No such file or directory&quot;

    1.fatal error: Box2D/Box2D.h: No such file or directory" 须要加入box2d库的支持,改动android.mk文件,例如以下: 查看文 ...

  5. 【LeetCode-面试算法经典-Java实现】【168-Excel Sheet Column Title(Excell列标题)】

    [168-Excel Sheet Column Title(Excell列标题)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a positive in ...

  6. 源泉书签,助您管理海量收藏。www.yuanquanshuqian.com 今日更新:支持了导入url为js代码的书签

    源泉书签,助您管理海量收藏.www.yuanquanshuqian.com 今日更新:支持了导入url为js代码的书签

  7. angular4(1)angular脚手架

    angular2之后有了类似于vue-cli的脚手架工具,很方便的帮助我们搭建项目: 1.安装angular命令行工具:npm install @angular/cli -g 2.检测angular- ...

  8. CentOS7系统安装完MySQL后启动MySQL提示无服务

    重新安装MariaDB数据库即可解决,MySQL所有命令可通用 MariaDB MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,开发这个分支的原因之一是:甲骨文公司收购了My ...

  9. 17.广度优先遍历bfs

    #include <iostream> #include <boost/config.hpp> //图(矩阵实现) #include <boost/graph/adjac ...

  10. Android 去掉TabLayout下的阴影,AppBarLayout下的阴影

    开始还以为是TabLayout在高版本系统上的特殊表现呢,没有在意,UI提出说感觉不好看就查了一下,原来是在TabLayout放在AppBarLayout里面才有这样的效果,只需要对AppBarLay ...