bzoj 3809 莫队
收获:
1、分块时顺便记录每个位置所属的块,然后一次排序就OK了。
2、要权衡在“区间移动”与“查询结果”之间的时间,莫队算法一般区间移动频率远大于查询结果,所以我们选择的辅助数据结构时就要注意了,我最开始写的是值域线段树,自己生成的极限数据要1m8s,改成树状数组后要24s,还是过不了,hzwer只要13s,细看hzwer的代码,发现Ta用的是分块,O(1)修改O(n0.5)查询,改成分块后的确快多了。
3、块的大小理论最优值是O(n*m-0.5),最开始设成这个交上去35卡过,改成hzwer的n/2后29s,所以做题时要自己试一下。
/**************************************************************
Problem: 3809
User: idy002
Language: C++
Result: Accepted
Time:29336 ms
Memory:26988 kb
****************************************************************/ #include <cstdio>
#include <cctype>
#include <cstring>
#include <cmath>
#include <algorithm>
#define maxn 100010
#define maxm 1000010
#define lowbit(i) ((i)&(-(i)))
using namespace std; int n, m;
int lx[maxn], rx[maxn], mccno[maxn], stot;
int w[maxn];
int ans[maxm];
int cnt[maxn], siz[maxn], cmc[maxn]; struct Qu {
int l, r, a, b, id;
bool operator<( const Qu & c ) const {
return mccno[l]<mccno[c.l] || ( mccno[l]==mccno[c.l] && r<c.r );
}
};
Qu qu[maxm]; void add( int pos ) {
siz[pos]++;
if( siz[pos]== ) cmc[mccno[pos]]++;
}
void del( int pos ) {
siz[pos]--;
if( siz[pos]== ) cmc[mccno[pos]]--;
}
int query( int l, int r ) {
int lm = mccno[l], rm = mccno[r];
int rt = ;
if( lm==rm ) {
for( int j=l; j<=r; j++ )
if( siz[j] ) rt++;
return rt;
}
for( int b=lm+; b<rm; b++ )
rt += cmc[b];
for( int j=l; j<=rx[lm]; j++ )
if( siz[j] ) rt++;
for( int j=lx[rm]; j<=r; j++ )
if( siz[j] ) rt++;
return rt;
}
void partition() {
int len = (int)ceil(sqrt(n/)+);
stot = n/len;
rx[] = ;
for( int i=; i<=stot; i++ ) {
lx[i] = rx[i-]+;
rx[i] = rx[i-]+len;
}
if( rx[stot]!=n ) {
stot++;
lx[stot] = rx[stot-]+;
rx[stot] = n;
}
for( int i=; i<=stot; i++ )
for( int j=lx[i]; j<=rx[i]; j++ )
mccno[j] = i;
}
void work() {
sort( qu+, qu++m); int lf = qu[].l;
int rg = qu[].r;
for( int j=lf; j<=rg; j++ )
add(w[j]);
ans[qu[].id] = query( qu[].a, qu[].b );
for( int q=; q<=m; q++ ) {
while( qu[q].l<lf ) add(w[--lf]);
while( qu[q].l>lf ) del(w[lf++]);
while( qu[q].r>rg ) add(w[++rg]);
while( qu[q].r<rg ) del(w[rg--]);
ans[qu[q].id] = query( qu[q].a, qu[q].b );
}
}
int main() {
scanf( "%d%d", &n, &m );
for( int i=; i<=n; i++ )
scanf( "%d", w+i );
for( int i=; i<=m; i++ ) {
scanf( "%d%d%d%d", &qu[i].l, &qu[i].r, &qu[i].a, &qu[i].b );
qu[i].id = i;
}
partition();
work();
for( int i=; i<=m; i++ )
printf( "%d\n", ans[i] );
}
bzoj 3809 莫队的更多相关文章
- BZOJ 3809 莫队+(分块|BIT)
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> ...
- BZOJ 3339 & 莫队+"所谓的暴力"
题意: 给一段数字序列,求一段区间内未出现的最小自然数. SOL: 框架显然用莫队.因为它兹瓷离线. 然而在统计上我打了线段树...用&维护的结点...400w的线段树...然后二分查找... ...
- bzoj 2038 莫队算法
莫队算法,具体的可以看10年莫涛的论文. 大题思路就是假设对于区间l,r我们有了一个答案,那么对于区间l,r+1,我们 可以暴力的转移一个答案,那么对于区间l1,r1和区间l2,r2,需要暴力处理 的 ...
- bzoj 3289 莫队 逆序对
莫队维护逆序对,区间左右增减要分类讨论. 记得离散化. /************************************************************** Problem: ...
- bzoj 2038 莫队入门
http://www.lydsy.com/JudgeOnline/problem.php?id=2038 题意:多次询问区间内取出两个相同颜色的种类数 思路:由于不是在线更新,那么可以进行离线查询,而 ...
- bzoj 3339 莫队
题意: 求任意一个区间的SG函数. 想到线段树,但是线段树合并很麻烦. 线段树——分块. 分块的一个应用就是莫队算法. 怎么暴力递推呢? 从一个区间到另一个区间,Ans 取决于 Ans 和 加入和删除 ...
- 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 2308 莫队入门经典
题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=2038 参考博客 https://www.cnblogs.com/Paul-Guderi ...
随机推荐
- gitlab使用 —— 多人协同工作(重要技能)
gitlab使用 —— 多人协同工作(重要技能) 学习链接: http://herry2013git.blog.163.com/blog/static/219568011201341111240751 ...
- awk正则匹配nginx日志【原创】
查看网页访问代码不为200和30x所有行的内容 awk '{if($9!~/200|30*/) print $0}' /app/logs/http_access.log 或 awk '$9!~/200 ...
- 修改 firefox accesskey 的快捷键
Chrome中,如果设置了 accesskey 的话,可以通过 Alt + 快捷键 来之直接跳转的.但在Firefox 中,可能是为了防止于菜单的快捷键冲突,所以设置了 Shift + Alt + 快 ...
- 设计模式之笔记--代理模式(Proxy)
代理模式(Proxy) 定义 代理模式(Proxy),为其他对象提供一种代理以控制对这个对象的访问. 类图 描述 Subject,定义了ConcreteSubject和Proxy的共用接口,这样就可以 ...
- 匿名函数、lambda表达式
匿名函数 func = lambda x: y #x是形参,y是返回值 键字lambda表示匿名函数,冒号前面的x表示函数参数,冒号后面的y表示匿名函数的返回值. 例1:返回列表中长度大于等于3的元素 ...
- [ python ] 匿名函数和高阶函数
匿名函数 描述: 关键字 lambda 定义的函数 语法: 函数名 = lambda 参数:返回值 返回值: 函数返回结果值 实例: 一个参数的匿名函数: func = lambda ...
- s3cmd : Add a config parameter to enable path-style bucket access 当ceph rgw使用域名时,需要支持 path-style bucket特性
s3cmd 要是1.6.1 之后的版本 增加配置项: vi .s3cfg use_path_mode = True 源码参考: cat /usr/local/lib/python2.7/dist- ...
- 所有依赖的jar将提取到lib目录
1.在pom.xml添加如下内容: <build> <plugins> <plugin> <artifactId>maven-dependency-pl ...
- 【LOJ】#2131. 「NOI2015」寿司晚宴
题解 怎么NOI2015D1--全是一眼秒的sb题--然后我代码全都写跪一遍= = 要是NOI2015是IOI赛制我就可以AK啦(大雾) 代码能力直线下降,NOI2018滚粗预定了啊TAT 我是不是要 ...
- Maven下载安装步骤
Maven下载安装步骤 1.下载maven 进入Maven官网的下载页面:http://maven.apache.org/download.cgi,如下图所示: 选择当前最新版本:"apac ...