BZOJ 3339 & 莫队+"所谓的暴力"
题意:
给一段数字序列,求一段区间内未出现的最小自然数.
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 & 莫队+"所谓的暴力"的更多相关文章
- bzoj 3339 莫队
题意: 求任意一个区间的SG函数. 想到线段树,但是线段树合并很麻烦. 线段树——分块. 分块的一个应用就是莫队算法. 怎么暴力递推呢? 从一个区间到另一个区间,Ans 取决于 Ans 和 加入和删除 ...
- bzoj 2038 莫队算法
莫队算法,具体的可以看10年莫涛的论文. 大题思路就是假设对于区间l,r我们有了一个答案,那么对于区间l,r+1,我们 可以暴力的转移一个答案,那么对于区间l1,r1和区间l2,r2,需要暴力处理 的 ...
- bzoj 3289 莫队 逆序对
莫队维护逆序对,区间左右增减要分类讨论. 记得离散化. /************************************************************** Problem: ...
- bzoj 3809 莫队
收获: 1.分块时顺便记录每个位置所属的块,然后一次排序就OK了. 2.要权衡在“区间移动”与“查询结果”之间的时间,莫队算法一般区间移动频率远大于查询结果,所以我们选择的辅助数据结构时就要注意了,我 ...
- bzoj 2038 莫队入门
http://www.lydsy.com/JudgeOnline/problem.php?id=2038 题意:多次询问区间内取出两个相同颜色的种类数 思路:由于不是在线更新,那么可以进行离线查询,而 ...
- BZOJ 3236 莫队+树状数组
思路: 莫队+树状数组 (据说此题卡常数) yzy写了一天(偷笑) 复杂度有点儿爆炸 O(msqrt(n)logn) //By SiriusRen #include <cmath> #in ...
- BZOJ 3339 && BZOJ 3585 莫队+权值分块
显然若一个数大于n就不可能是答案. #include <iostream> #include <cstring> #include <cstdio> #includ ...
- BZOJ 4810 莫队+bitset
思路: 看完这道题根本没有思路啊.... 然后我就膜拜了一波题解... 这神tm乱搞思路 维护两个bitset 第一个bitset代表当前区间哪些数出现过 第二个bitset是 maxp-p出现过 差 ...
- BZOJ 3809 莫队+(分块|BIT)
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> ...
随机推荐
- DB2 SQL Mixed data in character strings
Mixed character data and graphic data are always allowed for Unicode, but for EBCDIC and ASCII, the ...
- localStorage的使用
HTML5中提供了localStorage对象可以将数据长期保存在客户端,直到人为清除. localStorage提供了几个方法: 1.存储:localStorage.setItem(key,valu ...
- Android画面显示原理
转自: http://blog.csdn.net/luoshengyang/article/details/7691321/ http://blog.chinaunix.net/uid-1675954 ...
- 在PYTHON3中,使用Asyncio来管理Event loop
#!/usr/bin/env python # -*- coding: utf-8 -*- import asyncio import datetime import time def functio ...
- python threading编程中的LOCK和RLOCK(可重入锁)
找到一本PYTHON并发编辑的书, 弄弄.. #!/usr/bin/env python # -*- coding: utf-8 -*- import threading import time sh ...
- php开发(CI框架使用)
年前接了一个外包项目,要求使用PHP,琢磨来琢磨去,感叹道PHP框架实在是太多了!去知乎搜索一轮,最后决定使用CI, 相关议论如下:https://www.zhihu.com/question/216 ...
- JavaScript 简介
JavaScript是一种专为网页交互而设计的脚本语言,由下列三个不同的部分组成: ECMAScript,由ECMA-262 定义,提供核心语言功能: DOM, 提供访问和操作网页内容的方法和接口; ...
- js 控制展开折叠 div html dom
js 控制展开折叠 div html dom <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ...
- Linux gnome
一.主题风格网站:gnome-look.org.deviantart.com.Linux公社 我使用的主题是:http://gnome-look.org/content/show.php/OS+X+1 ...
- C++中单例模式
//C++单例模式:指一个类只生成一个对象 #include <iostream> using namespace std; class A{ public: static A* getA ...