收获:

  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 莫队的更多相关文章

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

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

  2. BZOJ 3339 & 莫队+"所谓的暴力"

    题意: 给一段数字序列,求一段区间内未出现的最小自然数. SOL: 框架显然用莫队.因为它兹瓷离线. 然而在统计上我打了线段树...用&维护的结点...400w的线段树...然后二分查找... ...

  3. bzoj 2038 莫队算法

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

  4. bzoj 3289 莫队 逆序对

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

  5. bzoj 2038 莫队入门

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

  6. bzoj 3339 莫队

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

  7. BZOJ 3236 莫队+树状数组

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

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

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

  9. BZOJ 2308 莫队入门经典

    题目链接  https://www.lydsy.com/JudgeOnline/problem.php?id=2038 参考博客 https://www.cnblogs.com/Paul-Guderi ...

随机推荐

  1. Hash破解神器:Hashcat的简单使用

    Hash破解神器:Hashcat的简单使用 2014-06-10 21:02:42|  分类: 离线密码破解 |  标签:密码字典  rar密码破解  zip密码破解  密码破解  |举报|字号 订阅 ...

  2. python OS 模块 文件目录操作

    Python OS 模块 文件目录操作 os模块中包含了一系列文件操作的函数,这里介绍的是一些在Linux平台上应用的文件操作函数.由于Linux是C写的,低层的libc库和系统调用的接口都是C AP ...

  3. Linux端口占用

    1.netstat netstat -anp | grep 23232 Sample: [root@BICServer 0825]# netstat -anp | grep 23232 tcp 0 0 ...

  4. 当while read line 遇到 ssh

    问题:while read line 中使用ssh只能读取一行? #!/bin/sh while read line do echo $line ssh root@$line "echo 1 ...

  5. redis常见数据操作

    redis中有5种常见的数据类型,针对这5种数据类型有着相应的数据操作. 1.String(键值对为String - String) set k1 v1 get k1 getset k1 v1 - h ...

  6. 深度学习方法(八):自然语言处理中的Encoder-Decoder模型,基本Sequence to Sequence模型

    欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld.技术交流QQ群:433250724,欢迎对算法.技术感兴趣的同学加入. Encoder-Decoder(编码- ...

  7. Ntp时间服务器与定时任务Crontab

    一 NTP时间服务器 1 局域网内的NTP同步配置 注意 所有配置操作必须是root用户 ,局域网内node21作为NTP Server,node22,node23作为NTP Client与服务器进行 ...

  8. python开发学习-day03(set集合、collection系列 、深浅拷贝、函数)

    s12-20160116-day03 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...

  9. C# 6.0 新特性 (二)

    自动属性初始化表达式 有过正确实现结构经验的所有 .NET 开发人员无疑都为一个问题所困扰:需要使用多少语法才能使类型固定不变(为 .NET 标准建议的类型).此问题实际上是只读属性存在的问题: 定义 ...

  10. Java 8 对 List<List<String>> 排序

    Java 8 对 List<List> 排序 import java.util.ArrayList; import java.util.List; import java.util.str ...