
分治——莫队算法
Alex_McAvoy
想要成为渔夫的猎手
展开
-
分治 —— 莫队算法
【概述】莫队算法(mo's algorithm)是用来解决离线区间不修改询问问题,可以将复杂度优化到 O(n^1.5),除去普通的莫队算法外,还有带修改的莫队、树上莫队等等。莫队常用于维护区间答案,比如:对于一个长度为 n 的序列,给出 m 次询问,每次询问区间 [l,r] 内有多少个不同的颜色,其中 n,m<=100000.对于一个序列,当知道 [L,R] 的信息时,可在 O(...原创 2019-03-05 15:06:23 · 553 阅读 · 2 评论 -
分治 —— 莫队算法 —— 普通莫队
【思想基础】普通莫队常用于维护区间答案,比如:对于一个长度为 n 的序列,给出 m 次询问,每次询问区间 [l,r] 内有多少个不同的颜色,其中 n,m<=100000.首先考虑暴力,对于每次询问,遍历一遍 [l,r],这样的时间复杂度是 O(n*m) 的,最坏时间复杂度肯定会超时,那么考虑换一种方式进行暴力。定义 ql、qr,表示区间[ql,qr] 内有多少种颜色,再定义 c...转载 2019-03-07 19:26:19 · 1353 阅读 · 1 评论 -
分治 —— 莫队算法 —— 带修莫队
【概述】普通莫队由于强制离线是不能修改的,但对于强制在线的题,可以在普通莫队的基础上强行加上一维时间轴 time,表示这次操作的时间,即在每个询问前已经完成了多少次修改。简单来说,就是将询问 [l,r],变为 [l,r,time],那么指针也可在时间维度上移动,使得第一关键字是左端点所在的块,第二关键字是右端点所在的块,第三关键字是时间,即 [l,r,time] 多了一维可移动的方向:...原创 2019-03-07 19:28:50 · 698 阅读 · 0 评论 -
数颜色(洛谷-P1903)
题目描述墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问。墨墨会向你发布如下指令:1、 Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔。2、 R P Col 把第P支画笔替换为颜色Col。为了满足墨墨的要求,你知道你需要干什么了吗?输入输出格式输入格式:第1行两个整数N,M,分别代表初始画笔的数量以及墨墨会做的事...原创 2019-10-11 16:39:39 · 626 阅读 · 0 评论 -
Machine Learning(CF-940F)
Problem DescriptionYou come home and fell some unpleasant smell. Where is it coming from?You are given an array a. You have to answer the following queries:You are given two integers l and r. L...原创 2019-03-07 19:15:51 · 332 阅读 · 0 评论 -
Sona(NBUT-1457)
Problem DescriptionSona , Maven of the Strings . Of cause, she can play the zither.Sona can't speak but she can make fancy music. Her music can attack, heal, encourage and enchant.There're an a...原创 2019-03-06 01:55:01 · 312 阅读 · 0 评论 -
Little Elephant and Array(CF-220B)
Problem DescriptionThe Little Elephant loves playing with arrays. He has array a, consisting of n positive integers, indexed from 1 to n. Let's denote the number with index i as ai.Additionally t...原创 2019-03-06 00:48:03 · 389 阅读 · 0 评论 -
XOR and Favorite Number(CF-617E)
Problem DescriptionBob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is given by a pair li and ri and asks you to count the number of pairs of integers...原创 2019-03-06 00:29:14 · 455 阅读 · 1 评论 -
Powerful array(CF-86D)
Problem DescriptionAn array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, al + 1..., ar, where 1 ≤ l ≤ r ≤ n. For every positive integer s denote by Ks t...原创 2019-03-05 23:24:19 · 500 阅读 · 0 评论 -
Fast Queries(LightOJ-1188)
Problem DescriptionGiven an array of N integers indexed from 1 to N, and q queries, each in the form i j, you have to find the number of distinct integers from index i to j (inclusive).InputInp...原创 2019-03-05 22:15:12 · 334 阅读 · 0 评论 -
小Z的袜子(BZOJ-2038)
Problem Description作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿。终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……具体来说,小Z把这N只袜子从1到N编号,然后从编号L到R(L 尽管小Z并不在意两只袜子是不是完整的一双,甚至不在意两只袜子是否一左一右,他却很在意袜子的颜色,毕竟穿两只不同色的袜子会很尴尬。你的任务便...原创 2019-03-05 16:55:35 · 182 阅读 · 0 评论 -
Game(HDU-6610)
Problem DescriptionAgain Alice and Bob is playing a game with stones. There are N piles of stones labelled from1toN, theith pile hasaistones.First Alice will choose piles of stones with co...原创 2019-09-29 20:11:39 · 404 阅读 · 0 评论