CF 86D 莫队(卡常数)
CF 86D 莫队(卡常数)
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 the subarray the sum of products Ks·Ks·s for every positive integer s. The sum contains only finite number of nonzero summands as the number of different values in the array is indeed finite.
You should calculate the power of t given subarrays.
First line contains two integers n and t (1 ≤ n, t ≤ 200000) — the array length and the number of queries correspondingly.
Second line contains n positive integers ai (1 ≤ ai ≤ 106) — the elements of the array.
Next t lines contain two positive integers l, r (1 ≤ l ≤ r ≤ n) each — the indices of the left and the right ends of the corresponding subarray.
Output t lines, the i-th line of the output should contain single positive integer — the power of the i-th query subarray.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preferred to use cout stream (also you may use %I64d).
3 2
1 2 1
1 2
1 3
3
6
8 3
1 1 2 2 1 3 1 1
2 7
1 6
2 7
20
20
20
Consider the following array (see the second sample) and its [2, 7] subarray (elements of the subarray are colored):

题意:
一个数列,问[L,R]区间内(每个数字的个数的平方*数字的大小)的和。
思路:
莫队模板。
离线+分块
将n个数分成sqrt(n)块。
对所有询问进行排序,排序标准:
1. Q[i].left /block_size < Q[j].left / block_size (块号优先排序)
2. 如果1相同,则 Q[i].right < Q[j].right (按照查询的右边界排序)
问题求解:
从上一个查询后的结果推出当前查询的结果。(这个看程序中query的部分)
如果一个数已经出现了x次,那么需要累加(2*x+1)*a[i],因为(x+1)^2*a[i] = (x^2 +2*x + 1)*a[i],x^2*a[i]是出现x次的结果,(x+1)^2 * a[i]是出现x+1次的结果。
时间复杂度分析:
排完序后,对于相邻的两个查询,left值之间的差最大为sqrt(n),则相邻两个查询左端点移动的次数<=sqrt(n),总共有t个查询,则复杂度为O(t*sqrt(n))。
又对于相同块内的查询,right端点单调上升,每一块所有操作,右端点最多移动O(n)次,总块数位sqrt(n),则复杂度为O(sqrt(n)*n)。
right和left的复杂度是独立的,因此总的时间复杂度为O(t*sqrt(n) + n*sqrt(n))。
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <cstring> 5 #include <cmath> 6 using namespace std; 7 #define N 200100 8 typedef long long ll; 9 ll a[N], cnt[N*5], ans[N], res; 10 int L, R; 11 12 struct node { 13 int x, y, l, p; 14 } q[N]; 15 bool cmp(const node &x, const node &y) { 16 if (x.l == y.l) return x.y < y.y; 17 return x.l < y.l; 18 } 19 void query(int x, int y, int flag) { 20 if (flag) { 21 for (int i=x; i<L; i++) { 22 res += ((cnt[a[i]]<<1)+1)*a[i]; 23 cnt[a[i]]++; 24 } 25 for (int i=L; i<x; i++) { 26 cnt[a[i]]--; 27 res -= ((cnt[a[i]]<<1)+1)*a[i]; 28 } 29 for (int i=y+1; i<=R; i++) { 30 cnt[a[i]]--; 31 res -= ((cnt[a[i]]<<1)+1)*a[i]; 32 } 33 for (int i=R+1; i<=y; i++) { 34 res += ((cnt[a[i]]<<1)+1)*a[i]; 35 cnt[a[i]]++; 36 } 37 38 } else { 39 for (int i=x; i<=y; i++) { 40 res += ((cnt[a[i]]<<1)+1)*a[i]; 41 cnt[a[i]]++; 42 } 43 } 44 L = x, R = y; 45 } 46 int main() { 47 int n, t; 48 49 scanf("%d%d", &n, &t); 50 for (int i=1; i<=n; i++) scanf("%I64d", &a[i]); 51 int block_size = sqrt(n); 52 53 for (int i=0; i<t; i++) { 54 scanf("%d%d", &q[i].x, &q[i].y); 55 q[i].l = q[i].x / block_size; 56 q[i].p = i; 57 } 58 59 sort(q, q+t, cmp); 60 61 62 memset(cnt, 0, sizeof(cnt)); 63 64 res = 0; 65 for (int i=0; i<t; i++) { 66 query(q[i].x, q[i].y, i); 67 ans[q[i].p] = res; 68 } 69 70 for (int i=0; i<t; i++) printf("%I64d\n", ans[i]); 71 72 return 0; 73 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!