Mo's Algorithm
来源:互联网 发布:淘宝一键复制怎么用 编辑:程序博客网 时间:2024/05/05 14:15
This algorithm can solve some difficult data structure problems. Like this: Give an array a[] of size N, we query Q times. Each time we want to get the mode number(the value that appears most often in a set of data) of subsequence a[l], a[l + 1] .. a[r].
Maybe you want to solve it by segment tree or some other data structures. However, if you know the answer of [l, middle], [middle + 1, r], it's difficult to get the answer of [l, r].
Here is an offline algorithm which can solve this problem in O(N * sqrt(N) * log(N) + Q log Q).
First we need a data structure that can support: insert a number. delete a number. get the mode number. It can be solved easily by binary heap / balanced binary search tree. We call this data structure DS.
Secondly, if we have all the numbers in [l, r] organized in DS, we can transform to [l', r'] in O((|l-l'| + |r-r'|) * log N). We need a good tranform order.
S = the max integer number which is less than sqrt(N);
bool cmp(Query A, Query B)
{
if (A.l / S != B.l / S) return A.l / S < B.l / S;
return A.r > B.r
}
We sort all the queries by this comparator, and it can be proofed easily that the total transform costs is N sqrt(N) log N.
- Mo's Algorithm
- Codeforces 617E ( 莫队算法 Mo's algorithm )
- 【CF 617E】 XOR and Favorite Number (Mo's algorithm)
- Codeforces 86D. Powerful array (MO's Algorithm(区间查询的离线优化))
- Codeforces 633H. Fibonacci-ish II (Mo's Algorithm(莫队算法) + 线段树 + 离散化)
- Codeforces Round #340 (Div. 2)E - XOR and Favorite Number(Mo's algorithm)
- [BZOJ 2038]小Z的袜子 莫队(Mo's Algorithm)模板题
- Algorithm: Euclid's Algorithm
- 【Algorithm】Manacher's Algorithm
- mo
- Algorithm: Extended Euclid's Algorithm
- Nagle's algorithm
- euclid's algorithm
- Dekker's algorithm
- Marzullo's algorithm
- Manacher's ALGORITHM
- Dijkstra\'s Algorithm
- Knuth's breaking algorithm
- sqlserver2008迁移到oracle11gR2
- 关于Spark 1.5 版本中Spark自己管理内存而不是由Java管理内存的解释
- bitDelete函数的编写
- XUtils===XUtils3框架的基本使用方法(二)
- 【oracle调优】表扫描还是索引扫描
- Mo's Algorithm
- Apache Spark Jobs 性能调优(二)
- PCIE 和SATA 接口
- ZTE中兴实习结束感想
- 【mongodb】 log4j输出信息到mongodb
- javascript 对象类型
- Struts2中Result类型配置详解
- 过河卒
- Android权限 uses-permission