题意:

  给一段数字序列,求一段区间内未出现的最小自然数.

SOL:

  框架显然用莫队.因为它兹瓷离线.

  然而在统计上我打了线段树...用&维护的结点...400w的线段树...然后二分查找...炸的妥妥的...

  然后发现所谓的"暴力"...直接开数组维护...因为指针具有一定的单调性,一次更改可以直接得到解,不用每次都查询...

  woc真是...有时候数据结构用多了忽略了那些更简单更实用的方法...

Code

  TLE的代码:

  

/*==========================================================================
# Last modified: 2016-03-22 20:48
# Filename: 3339.cpp
# Description:
==========================================================================*/
#define me AcrossTheSky
#include <cstdio>
#include <cmath>
#include <ctime>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> #include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector> #define lowbit(x) (x)&(-x)
#define FOR(i,a,b) for((i)=(a);(i)<=(b);(i)++)
#define FORP(i,a,b) for(int i=(a);i<=(b);i++)
#define FORM(i,a,b) for(int i=(a);i>=(b);i--)
#define ls(a,b) (((a)+(b)) << 1)
#define rs(a,b) (((a)+(b)) >> 1)
#define getlc(a) ch[(a)][0]
#define getrc(a) ch[(a)][1] #define maxn 3600000
#define cap 201000
#define maxm 100000
#define pi 3.1415926535898
#define _e 2.718281828459
#define INF 1070000000
using namespace std;
typedef long long ll;
typedef unsigned long long ull; template<class T> inline
void read(T& num) {
bool start=false,neg=false;
char c;
num=0;
while((c=getchar())!=EOF) {
if(c=='-') start=neg=true;
else if(c>='0' && c<='9') {
start=true;
num=num*10+c-'0';
} else if(start) break;
}
if(neg) num=-num;
}
/*==================split line==================*/
struct Query{
int l,r,id,op;
}q[cap];
int n,m;
int v[maxn],a[cap],ans[cap];
int cmp(const Query &x,const Query &y){
if (x.op==y.op) return x.r<y.r;
else return x.op<y.op;
}
void change(int node,int l,int r,int x,int d){
if (l==r) { v[node]+=d; return;}
int mid=rs(l,r),lc=ls(node,0),rc=lc|1;
if (x<=mid) change(lc,l,mid,x,d);
else change(rc,mid+1,r,x,d);
v[node]=(v[lc]?1:0)&(v[rc]?1:0);
}
int query(int node,int l,int r){
if (l==r) return l;
int mid=rs(l,r),lc=ls(node,0),rc=lc|1;
if (!v[lc]) query(lc,l,mid);
else query(rc,mid+1,r);
}
void init(){
read(n); read(m);
int sz=trunc(sqrt(n));
FORP(i,1,n) { read(a[i]); a[i]++;}
FORP(i,1,m) {
read(q[i].l); read(q[i].r); q[i].id=i;
q[i].op=q[i].l/sz+(q[i].l%sz?1:0);
}
}
int main(){
init();
sort(q+1,q+1+m,cmp);
int L=q[1].l,R=q[1].r;
FORP(i,L,R) change(1,1,cap,a[i],1);
ans[q[1].id]=query(1,1,cap);
FORP(i,2,m){
while (L<q[i].l) { change(1,1,cap,a[L],-1); L++;}
while (L>q[i].l) { L--; change(1,1,cap,a[L],1);}
while (q[i].r<R) { change(1,1,cap,a[R],-1); R--;}
while (q[i].r>R) { R++; change(1,1,cap,a[R],1);}
ans[q[i].id]=query(1,1,cap);
}
FORP(i,1,m) printf("%d\n",ans[i]-1);
}

  A掉的代码:

  

/*==========================================================================
# Last modified: 2016-03-22 20:48
# Filename: 3339.cpp
# Description:
==========================================================================*/
#define me AcrossTheSky
#include <cstdio>
#include <cmath>
#include <ctime>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> #include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector> #define lowbit(x) (x)&(-x)
#define FOR(i,a,b) for((i)=(a);(i)<=(b);(i)++)
#define FORP(i,a,b) for(int i=(a);i<=(b);i++)
#define FORM(i,a,b) for(int i=(a);i>=(b);i--)
#define ls(a,b) (((a)+(b)) << 1)
#define rs(a,b) (((a)+(b)) >> 1)
#define getlc(a) ch[(a)][0]
#define getrc(a) ch[(a)][1] #define maxn 3600000
#define cap 201000
#define maxm 100000
#define pi 3.1415926535898
#define _e 2.718281828459
#define INF 1070000000
using namespace std;
typedef long long ll;
typedef unsigned long long ull; template<class T> inline
void read(T& num) {
bool start=false,neg=false;
char c;
num=0;
while((c=getchar())!=EOF) {
if(c=='-') start=neg=true;
else if(c>='0' && c<='9') {
start=true;
num=num*10+c-'0';
} else if(start) break;
}
if(neg) num=-num;
}
/*==================split line==================*/
struct Query{
int l,r,id,op;
}q[cap];
int n,m;
int cnt[maxn],a[cap],ans[cap];
int now=0;
int cmp(const Query &x,const Query &y){
if (x.op==y.op) return x.r<y.r;
else return x.op<y.op;
}
void change(int x,int d){
cnt[x]+=d;
if (x<now){
if (cnt[x]==0) now=x;
}
else if (now==x)
while(cnt[now]) now++;
}
void init(){
read(n); read(m);
int sz=trunc(sqrt(n));
FORP(i,1,n) { read(a[i]);}
FORP(i,1,m) {
read(q[i].l); read(q[i].r); q[i].id=i;
q[i].op=q[i].l/sz+(q[i].l%sz?1:0);
}
}
int main(){
init();
sort(q+1,q+1+m,cmp);
int L=q[1].l,R=q[1].r;
FORP(i,L,R) change(a[i],1);
ans[q[1].id]=now;
FORP(i,2,m){
while (L<q[i].l) { change(a[L],-1); L++;}
while (L>q[i].l) { L--; change(a[L],1);}
while (q[i].r<R) { change(a[R],-1); R--;}
while (q[i].r>R) { R++; change(a[R],1);}
ans[q[i].id]=now;
}
FORP(i,1,m) printf("%d\n",ans[i]);
}

BZOJ 3339 & 莫队+"所谓的暴力"的更多相关文章

  1. bzoj 3339 莫队

    题意: 求任意一个区间的SG函数. 想到线段树,但是线段树合并很麻烦. 线段树——分块. 分块的一个应用就是莫队算法. 怎么暴力递推呢? 从一个区间到另一个区间,Ans 取决于 Ans 和 加入和删除 ...

  2. bzoj 2038 莫队算法

    莫队算法,具体的可以看10年莫涛的论文. 大题思路就是假设对于区间l,r我们有了一个答案,那么对于区间l,r+1,我们 可以暴力的转移一个答案,那么对于区间l1,r1和区间l2,r2,需要暴力处理 的 ...

  3. bzoj 3289 莫队 逆序对

    莫队维护逆序对,区间左右增减要分类讨论. 记得离散化. /************************************************************** Problem: ...

  4. bzoj 3809 莫队

    收获: 1.分块时顺便记录每个位置所属的块,然后一次排序就OK了. 2.要权衡在“区间移动”与“查询结果”之间的时间,莫队算法一般区间移动频率远大于查询结果,所以我们选择的辅助数据结构时就要注意了,我 ...

  5. bzoj 2038 莫队入门

    http://www.lydsy.com/JudgeOnline/problem.php?id=2038 题意:多次询问区间内取出两个相同颜色的种类数 思路:由于不是在线更新,那么可以进行离线查询,而 ...

  6. BZOJ 3236 莫队+树状数组

    思路: 莫队+树状数组 (据说此题卡常数) yzy写了一天(偷笑) 复杂度有点儿爆炸 O(msqrt(n)logn) //By SiriusRen #include <cmath> #in ...

  7. BZOJ 3339 && BZOJ 3585 莫队+权值分块

    显然若一个数大于n就不可能是答案. #include <iostream> #include <cstring> #include <cstdio> #includ ...

  8. BZOJ 4810 莫队+bitset

    思路: 看完这道题根本没有思路啊.... 然后我就膜拜了一波题解... 这神tm乱搞思路 维护两个bitset 第一个bitset代表当前区间哪些数出现过 第二个bitset是 maxp-p出现过 差 ...

  9. BZOJ 3809 莫队+(分块|BIT)

    #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> ...

随机推荐

  1. HTML+CSS页面滚动效果处理

    HTML+CSS代码如下: <!doctype html> <html> <head> <meta charset="utf-8"> ...

  2. 【翻译二十二】java-并发之集合与原子变量

    Concurrent Collections The java.util.concurrent package includes a number of additions to the Java C ...

  3. 【翻译五】java-中断机制

    Interrupts An interrupt is an indication to a thread that it should stop what it is doing and do som ...

  4. log4j设置日志格式为UTF-8

    想要log4j输出的日志文件的编码格式为UTF-8.正常情况下只需要添加下述的代码即可: log4j.appender.appender_name.Encoding=UTF-8 但是在spring与l ...

  5. nodejs2

    jade@1.11.0 严格注意缩进 extends layout block content h1= title p Welcome to #{title} - var a='abc'; p his ...

  6. [Linux][Hadoop] 运行WordCount例子

    紧接上篇,完成Hadoop的安装并跑起来之后,是该运行相关例子的时候了,而最简单最直接的例子就是HelloWorld式的WordCount例子.   参照博客进行运行:http://xiejiangl ...

  7. 用Feature的方式删除SharePoint2010的Page中重复的WebPart

    用Feature的方式删除SharePoint2010的Page中重复的WebPart. 代码如下所示: public class SupportCenterDuplicatedWebpartRemo ...

  8. oc中定时器的基本使用

    // 时间间隔 调用的对象  调用的方法 用户信息 是否循环 [NSTimer scheduledTimerWithTimeInterval: target:self selector:@select ...

  9. 德飞莱STM32单片机学习(一)——下载环境搭建

    一.下载驱动安装. 1.手动打开CH341 文件夹(驱动程序文件夹内) ,双击安装驱动 2. 尼莫M3S 开发硬件设置 硬件需要做到以下2 点:1. USB插入USB1(COM),打开电源开关J14( ...

  10. 安卓图表引擎AChartEngine(三) - 示例源码折线图、饼图和柱状图

    折线图: package org.achartengine.chartdemo.demo.chart; import java.util.ArrayList; import java.util.Lis ...