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. 怎样在Nginxserver中启用Gzip压缩

    原文链接: Enable GZIP Compression on nginx Servers原文日期: 2014年7月16日翻译日期: 2014年7月19日翻译人员: 铁锚 速度决定一切,没有什么比一 ...

  2. JDBC-连接数据库代码

    package com.zxc.connection; import java.sql.Connection; import java.sql.DriverManager; public class ...

  3. ubuntu16.04安装opencl

    参考链接:https://www.jianshu.com/p/ad808584ce26 安装OpenCL OpenCL是一系列库和头文件,需要根据硬件安装对应的SDK,也就是说,如果希望使用Intel ...

  4. iOS开发之UIAlertController的适配

    在iOS8中,只能用UIAlertController.而原来的UIAlertView及UIActionSheet已经被抛弃掉了.但是如果一台iOS 7 的手机运行到有UIAlertControlle ...

  5. thinkphp项目上传到github,为什么缺少很多文件

    thinkphp项目上传到github,为什么缺少很多文件 问题: 把tp5项目push到码云(类似github)上,为什么没有thinkphp这个核心库? 然后我看了下码云和github上,官方的t ...

  6. [Codeforces 1051F] The Shortest Statement 解题报告(树+最短路)

    题目链接: https://codeforces.com/contest/1051/problem/F 题目大意: 给出一张$n$个点,$m$条边的带权无向图,多次询问,每次给出$u,v$,要求输出$ ...

  7. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现

    最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...

  8. sicily 1137 河床 (二分分治)

    <计算机算法设计与分析>啃书中... 有点看不进书,就来刷个水题吧,刚开始看错题了还. 注意:是所有测量点相差均不大于di而不是相邻两点... //1137.河床 #include < ...

  9. TYVJ 1541 八数码

    Orz双向搜索的cy大神 我用的是hash 也蛮快的 //By SiriusRen #include <queue> #include <cstdio> using names ...

  10. Wordcount 和 shuffle的流程