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 ...
随机推荐
- MYSQL的隐式类型转换
官方文档中是这么说的 当操作者使用不同类型的操作数,操作数类型兼容的出现使 转换.一些 发生隐式转换.例如,MySQL会自动 将数字转换为字符串的必要,反之亦然. 也可以将数字转换为字符串明确 使用( ...
- 69.Spartan-6的SelectIO资源
2.1.6 SelectIO资源 Spartan-6有丰富的I/O资源,包括SelectIO和RocketIO. Spartan-6每个I/O片(Tile)包含两个IOB.两个ILOGIC2.两个OL ...
- Centos 6.4搭建git服务器【转】
前阵子公司需要,让我搭个Git服务器,把之前用的SVN上代码迁移到git上去,所以就在阿里云主机上搭了一个,记录了下安装过程,留存文档以备查阅.本篇本章只涉及搭建部分的操作,更多git的使用可以参考文 ...
- SPOJ JZPLIT
Problem SPOJ Solution 考虑任意一个作为矩阵四个角的位置 \(r_i \oplus c_j\oplus a_{i,j}\oplus x_{i,j}=0\) \(r_i \oplus ...
- python脚本-实现自动按规则创建指定大小和指定个数的文件案例
# -*- coding: cp936 -*-#---------------------------------------------------------------------------- ...
- 教你如何修改FireFox打开新标签页(NewTab Page)的行列数
FireFox的打开新建标签页(即NewTab Page)默认只能显示3x3个网站缩略图,这9个自定义的网站,非常方便快捷,什么hao123的弱爆了,本人从未用过此类导航网站,曾经用过的也只是abou ...
- 简约而不简单的Django
本文面向:有python基础,刚接触web框架的初学者. 环境:windows7 python3.5.1 pycharm专业版 Django 1.10版 pip3 一.Django简介 百度百 ...
- 对cgic的理解——name选项
#include <stdio.h>#include <stdlib.h>#include <string.h>#include "cgic.h" ...
- 模块定义文件.def
一作用 DLL中导出函数的声明有两种方式:一种为在函数声明中加上__declspec(dllexport),这里不再举例说明:另外一种方式是采用模块定义(.def) 文件声明,.def文件为链接器提供 ...
- 连接数据库:ERROR:The server time zone value '?й???????' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration prop
本打算在maven项目中配置mybatis试试看,想到mybatis如果不是在容器中运行,那么他的事务控制实际上可以使用的是jdbc的提交和回滚,这就要在pom.xml文件中配置mysql-conne ...