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> ...
随机推荐
- Rotate partitions in DB2 on z
Rotating partitions You can use the ALTER TABLE statement to rotate any logical partition to becom ...
- SpringMVC @RequestBody问题:Unrecognized field , not marked as ignorable
http://blog.csdn.net/isea533/article/details/33397735
- 与你相遇好幸运,Sails.js自定义数据库名
在/api/models/下,自定义的.js文件内容 module.exports = { tableName: '自定义的数据库名', autoCreatedAt: false, //关闭 au ...
- 【Java EE 学习 21 下】【使用java实现邮件发送、邮件验证】
一.邮件发送 1.邮件发送使用SMTP协议或者IMAP协议,这里使用SMTP协议演示. SMTP协议使用的端口号:25 rfc821详细记载了该协议的相关信息 (1)使用telnet发送邮件(使用12 ...
- 【Java EE 学习 20】【使用过滤器实现登陆验证、权限认证】【观察者模式和监听器(使用监听器实现统计在线IP、登录IP 、踢人功能)】
一.使用过滤器实现登录验证.权限认证 1.创建5张表 /*使用过滤器实现权限过滤功能*/ /**创建数据库*/ DROP DATABASE day20; CREATE DATABASE day20; ...
- SOLR+LUCENE错误
java.lang.NoClassDefFoundError: org/apache/lucene/analysis/synonym/SynonymFilter 该错误发生在自定义SOLR服务器时,原 ...
- ASP.NET WebApi Document Helper
本项目实现了ASP.NET WebApi 接口文档的自动生成功能. 微软出的ASP.NET WebApi Help Page固然好用,但是我们项目基于Owin 平台的纯WebApi 项目,不想引入MV ...
- phpcms 完美实现 导航栏当前栏目高亮
我们在用phpcms做网站的时候,经常碰到导航栏高亮功能,或者侧栏高亮,这个会涉及到几个问题: .栏目列表页子栏目高亮判断,如果当前页面为子栏目,他的顶级栏目如果在导航栏也要高亮. .内容页高亮,这个 ...
- mysql_multi启动数据库
1.初始化数据库 在$mysql_base目录下,新增加存放data的文件夹,用mysql_install_db命令执行初始化 [root@ora11g scripts]# ./mysql_insta ...
- Windows Server 2008 R2 实现多用户同时登陆
Windows Server 2008 R2远程用户数设置 在windows server 2008 R2里面,默认的远程桌面连接数为1.这对我们的服务器管理带来了很大的不便,那么怎样来修改2008 ...