vjudge上莫队专题 真的是要吐槽自己(自己的莫队手残写了2个bug) s=sqrt(n) 是元素的个数而不是询问的个数(之所以是sqrt(n)使得左端点每个块左端点的范围嘴都是sqrt(n)) 在重载<是将q[i].l/s<q2[i].l/s 写成q1[i].l<s<q2[i].l<s 导致一下午都在调bug疯了 Sona 比小Z的袜子简单,直接维护区间频度的^3 需要离散化(离散化化的标号应提前准备好,如果用时在二分查找会增加复杂度) 还有比较坑的一点是多case,但样…
The Little Elephant loves playing with arrays. He has array a, consisting of npositive integers, indexed from 1 to n. Let's denote the number with index i as ai. Additionally the Little Elephant has m queries to the array, each query is characterised…
Description An 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 the number of occurrences of s into the subarray. We call the…
D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, al + 1..., ar, where 1 …
[1457] Sona 时间限制: 5000 ms 内存限制: 65535 K 问题描述 Sona, 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 ancient score(乐谱). But because i…
莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array 典型的莫队算法题 #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <string&g…
codefroce D. Powerful array[初识块状数组] 由于是初始所以,仅仅能先用别人的分析.囧... 题目: 给定一个数列:A1, A2,--,An,定义Ks为区间(l,r)中s出现的次数. t个查询,每一个查询l,r,对区间内全部a[i],求sigma(K^2*a[i]) 离线+分块 将n个数分成sqrt(n)块. 对全部询问进行排序,排序标准: 1. Q[i].left /block_size < Q[j].left / block_size (块号优先排序) 2. 假设1…
D. Powerful array 题意 给定一个数列:a[i] (1<= i <= n) K[j]表示 在区间 [l,r]中j出现的次数.有t个查询,每个查询l,r,对区间内所有a[i],求sigma(K[a[i]] * K[a[i]] * a[i]). 分析 首先将区间分块,每块sqrt(n)个,先读入所有的查询,对查询进行排序:先按块序号从小到大,再按右端点的值从小大到排序. 按顺序读入查询,动态变更区间,以及区间的值.如果某个点出现了 x 次,出现 x + 1 次时,区间内增加(2 *…
1,VS2013 错误 1 error C2556: “const int &Array<int>::operator [](int)”: 重载函数与“int &Array<int>::operator [](int)”只是在返回类型上不同 出错代码: 出错原因: 在 C++ 中,两个只有返回类型不同的函数不可以实现重载,重载只是参数类型不同才可以重载.想知道这种情况下如何实现重载,可点击这里. 错误解决: 把第二个改为:const T& operator[…
题意翻译 题意:给出一个n个数组成的数列a,有t次询问,每次询问为一个[l,r]的区间,求区间内每种数字出现次数的平方×数字的值 的和. 输入:第一行2个正整数n,t. 接下来一行n个正整数,表示数列a1​~an的值. 接下来t行,每行两个正整数l,r,为一次询问. 输出:t行,分别为每次询问的答案. 数据范围:1≤n,t≤2∗105,1≤ai≤106,1≤l,r≤n 题目描述 An array of positive integers a1,a2,...,an a_{1},a_{2},...,…
题目链接:点击传送 D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, al + 1..., ar…
总结 1.插入数据 db.inventory.insertMany( [ { item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15 } ] }, { item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] }, { item: "…
D. Powerful array time limit per test seconds memory limit per test megabytes input standard input output standard output An array of positive integers a1, a2, ..., an ..., ar,  ≤ l ≤ r ≤ n. For every positive integer s denote by Ks the number of occ…
题目链接: D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, al + 1..., ar, wh…
题目链接:http://codeforces.com/problemset/problem/86/D D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output An array of positive integers a1, a2, ..., an is given. Let us consider…
题目链接 Powerful array 给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和. $1<=n,m<=200000,   1<=s<=10^{6}$ 考虑莫队算法 把区间排序,然后让l和r分别询问即可. 根据排序的方式,l指针和r指针移动次数和大概是$n\sqrt{n}$ 级别的. #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (…
Powerful array 题意:求区间[l, r] 内的数的出现次数的平方 * 该数字. 题解:莫队离线操作, 然后加减位置的时候直接修改答案就好了. 这个题目中发现了一个很神奇的事情,本来数组开1e6大小就直接过了4100+ms, 想测试一下inline,顺手把空间砍成了刚好够用,然后跑的更慢了 4700+ms,删了inline之后T了,到现在也不知道发生了啥. 如果有大牛路过希望帮我看下,  CF run id  38822420(4100+) 38822607(4700+)388226…
题目描述: Problem Description An 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 the number of occurrences of s into the subarra…
An 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 the number of occurrences of s into the subarray. We call the power of th…
D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, al + 1..., ar, where 1 …
An 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 the number of occurrences of s into the subarray. We call the power of th…
菜鸡wwb因为想不出口胡题所以来写题解了 A. chess 昨天晚上考试,有点困 开考先花五分钟扫了一边题,好开始肝$T1$ 看了一眼$m$的范围很大,第一反应矩阵快速幂?? $n$很小,那么可以打$n^4$的DP, $10min$过去了,好像就是一个$DP$啊,随便乘个组合数就好了, 最后距离考试$20min$时,因为瞎取模,把自己的$AC$覆盖了kukukuku 正解的话,首先对于第一列而言,第$1+n$列的放的$C$的个数与他相同 但是因为只知道数目我们乘上组合数就好 $f_{ij}$表示…
给一段长为n的序列和m个关于区间的询问,求出每个询问的区间中有多少种数字是 该种数字出现的次数等于该数字 的. #include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <set> #include <vector> #include <stack> #include &…
题意:N个数,M个查询,求[Li,Ri]区间内出现次数等于其数值大小的数的个数. 分析:用莫队处理离线问题是一种解决方案.但ai的范围可达到1e9,所以需要离散化预处理.每次区间向外扩的更新的过程中,检查该位置的数ai的出现次数是否已经达到ai或ai+1,以判断是否要更新结果.同理,区间收缩的时候判断ai出现次数是否达到ai或ai-1. 另一种更高效的方法是使用树状数组离线处理查询.用一个vector数组维护每个ai以此出现的位置.显然ai>N的数不会对结果做出贡献,所以数组开1e5就足够了.树…
题意: m次查询.每次查询范围[L,R]中出现次数等于该数字的数字个数. 题解: 由于分块,在每次询问中,同一块时l至多移动根号n,从一块到另一块也是最多2倍根号n.对于r,每个块中因为同一块是按y排序,所以最多移动根号n:一共根号n个块.注意l和r要分开考虑. 要注意的是这道题需要离散一下数据. #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #incl…
传送门:https://codeforces.com/problemset/problem/220/B 题意: 给你n个数,m次询问,每次询问问你在区间l,r内有多少个数满足其值为其出现的次数 题解: 莫队算法 思路:每次区间向外扩的更新的过程中,检查该位置的数ai的出现次数是否已经达到ai或ai+1,以判断是否要更新结果.同理,区间收缩的时候判断ai出现次数是否达到ai或ai-1. 因为数据范围为1e9,所以需要离散化,方便记录每种数出现的次数 代码: /** * ┏┓ ┏┓ * ┏┛┗━━━…
这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1增加了有重复数值的限制条件. 153.154都需要考虑是否旋转成和原数组一样的情况,特别的,154题还需要考虑10111和11101这种特殊情况无法使用二分查找. 33.81则不用考虑这些情况. 找最小值的题主要是利用最小值小于他前一个位置的数,同时也小于后一个位置的数 153. Find Mini…
后面3个题都是限制在1-n的,所有可以不先排序,可以利用巧方法做.最后两个题几乎一模一样. 217. Contains Duplicate class Solution { public: bool containsDuplicate(vector<int>& nums) { int length = nums.size(); ) return false; sort(nums.begin(),nums.end()); ;i < length;i++){ ]) return tr…
Description An 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 the number of occurrences of s into the subarray. We call the…
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this array. Could you do it without extra space and in O(n) runtime?…