收获:

  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. 天梯赛L2-008 最长对称子串 (字符串处理)

    对给定的字符串,本题要求你输出最长对称子串的长度.例如,给定"Is PAT&TAP symmetric?",最长对称子串为"s PAT&TAP s&quo ...

  2. 56、isinstance作用以及应用场景?

    isinstance作用:来判断一个对象是否是一个已知的类型: 其第一个参数(object)为对象,第二个参数为类型名(int...)或类型名的一个列表((int,list,float)是一个列表). ...

  3. 生产环境手把手部署ERC20智能合约

    工具 rimex http://remix.ethereum.org/ metamask https://metamask.io/ ERC20 代码 https://github.com/OpenZe ...

  4. perl6正则 1: ~~ , //, m//, rx//

    ~~ perl6 中, 要匹配一个正则, 使用 ~~ 智能匹配符. > so 'abcde' ~~ /a.c/ True > so 'abcde' ~~ /a.d/ False > ...

  5. 33 - 并发编程-线程同步-Event-lock

    目录 1 线程同步 1.1 Event 1.1.1 什么是Flag? 1.1.2 Event原理 1.1.3 吃包子 1.2 Lock 1.2.1 lock方法 1.2.2 计数器 1.2.3 非阻塞 ...

  6. 一个TCP报文段的数据部分最多为多少个字节,为什么

    IP数据报的最大长度=2^16-1=65535(字节)TCP报文段的数据部分=IP数据报的最大长度-IP数据报的首部-TCP报文段的首部=65535-20-20=65495(字节) 一个tcp报文段的 ...

  7. 关于text-decoration无法清除继承的问题

    因为text-decoration的值可以叠加,所以即使设置了none,浏览器也是看成是叠加,而不是清除的意思.

  8. UVA题解二

    UVA题解二 UVA 110 题目描述:输出一个Pascal程序,该程序能读入不多于\(8\)个数,并输出从小到大排好序后的数.注意:该程序只能用读入语句,输出语句,if语句. solution 模仿 ...

  9. <mvc:annotation-driven/>都做了那些事情

    mvc:annotation-driven是一种简写的配置方式,那么mvc:annotation-driven到底做了哪些工作呢?如何替换掉mvc:annotation-driven呢? <mv ...

  10. C/C++——[05] 函数

    函数是 C/C++语言中的一种程序组件单位.一个函数通常代表了一种数据处理的功能,由函数体和函数原型两部分组成.函数原型为这个数据处理功能指定一个标识符号(函数的名称).说明被处理数据的组成及其类型. ...