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 ...
随机推荐
- CCControlExtension/CCControlPotentiometer
#ifndef __CCCONTROLPOTENTIOMETER_H__ #define __CCCONTROLPOTENTIOMETER_H__ #include "CCControl.h ...
- uva_644暴力加字典树解法
暴力 #include<iostream> #include<string.h> #include<cstdio> using namespace std; int ...
- JAVA设计模式之【迭代器模式】
迭代器模式 聚合类,聚合对象 电视机遥控器,迭代器,遍历局和对象中的成员 灵活新增遍历方法 将负责遍历数据的方法提取出来,封装到专门的类中,实现数据存储和数据遍历分离.这就是迭代器模式. 灵活性,可扩 ...
- Effective Java(一)—— 创建和销毁对象
在客户端(调用端)获取自身实例的方法: 公有的构造器: 类的静态工厂方法: 1. 使用静态工厂方法代替构造器 Boolean 是对基本类型 boolean 的包装类: public final cla ...
- [jzoj NOIP2018模拟10.29]
OI生涯的最高分,来了纪中这么多天,在经历了这么多场“NOIP难度”的模拟赛之后,终于看到了真正的NOIP 今天考场上效率很高,很快码完了全部的题目,留下了足够的时间对拍和...发呆.不得不说看着电脑 ...
- mybatis使用注解替代xml配置,动态生成Sql
mybatis使用注解替代xml配置时,遇到判断条件是否为null或者为空时,@Select很难搞定,不知道怎么办? mybatis3中增加了使用注解来配置Mapper的新特性,使用 SelectPr ...
- 爬取xml数据之R
生物信息很多时候要爬数据.最近也看了一些这些方面的. url<-"要爬取的网址" url.html<-htmlParse(url,encoding="UTF- ...
- Enable .Net 4.5 in IIS on Windows 8.1
Setting up a new development box for myself I had forgotten all about the necessity to use theaspnet ...
- php查询字符串是否存在 strpos
/*** 查询字符是否存在于某字符串** @param $haystack 字符串* @param $needle 要查找的字符* @return bool*/function str_exists( ...
- SpringCloud学习笔记(16)----Spring Cloud Netflix之Hystrix Dashboard+Turbine集群监控
前言: 上一节中,我们使用Hystrix Dashboard,只能看到单个应用内的服务信息.在生产环境中,我们经常是集群状态,所以我们需要用到Turbine这一应用. 作用:汇总系统内的多个服务的数据 ...