当前位置:网站首页>CF86D Powerful array
CF86D Powerful array
2022-08-04 17:11:00 【汤键.】
Powerful array(过程在注释)
题面翻译
- 给定长度为 n n n 的序列 a a a,有 q q q 次询问,每次询问给出两个数 l , r l,r l,r。
- 对于每次询问,设 c n t i cnt_i cnti 表示 i i i 在 a l , a l + 1 , ⋯ , a r a_l,a_{l+1},\cdots,a_r al,al+1,⋯,ar 出现的次数,您需要求出 ∑ i c n t i 2 ⋅ i \displaystyle\sum_i cnt_i^2\cdot i i∑cnti2⋅i。
- 1 ≤ n , q ≤ 2 × 1 0 5 1\le n,q\le 2\times 10^5 1≤n,q≤2×105, 1 ≤ a i ≤ 1 0 6 1\le a_i\le 10^6 1≤ai≤106, 1 ≤ l ≤ r ≤ n 1\le l\le r\le n 1≤l≤r≤n。
题目描述
An array of positive integers a 1 , a 2 , . . . , a n a_{1},a_{2},...,a_{n} a1,a2,...,an is given.
Let us consider its arbitrary subarray a l , a l + 1 . . . , a r a_{l},a_{l+1}...,a_{r} al,al+1...,ar , where 1 < = l < = r < = n 1<=l<=r<=n 1<=l<=r<=n .
For every positive integer s s s denote by K s K_{s} Ks the number of occurrences of s s s into the subarray.
We call the power of the subarray the sum of products K s ⋅ K s ⋅ s K_{s}·K_{s}·s Ks⋅Ks⋅s for every positive integer s s 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 t t given subarrays.
输入格式
First line contains two integers n n n and t t t ( 1 < = n , t < = 200000 1<=n,t<=200000 1<=n,t<=200000 ) — the array length and the number of queries correspondingly.
Second line contains n n n positive integers a i a_{i} ai ( 1 < = a i < = 1 0 6 1<=a_{i}<=10^{6} 1<=ai<=106 ) — the elements of the array.
Next t t t lines contain two positive integers l l l , r r r ( 1 < = l < = r < = n 1<=l<=r<=n 1<=l<=r<=n ) each — the indices of the left and the right ends of the corresponding subarray.
输出格式
Output t t t lines, the i i i -th line of the output should contain single positive integer — the power of the i i 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).
样例 #1
样例输入 #1
3 2
1 2 1
1 2
1 3
样例输出 #1
3
6
样例 #2
样例输入 #2
8 3
1 1 2 2 1 3 1 1
2 7
1 6
2 7
样例输出 #2
20
20
20
提示
Consider the following array (see the second sample) and its [2, 7] subarray (elements of the subarray are colored):
Then K 1 = 3 K_{1}=3 K1=3 , K 2 = 2 K_{2}=2 K2=2 , K 3 = 1 K_{3}=1 K3=1 , so the power is equal to 3 2 ⋅ 1 + 2 2 ⋅ 2 + 1 2 ⋅ 3 = 20 3^{2}·1+2^{2}·2+1^{2}·3=20 32⋅1+22⋅2+12⋅3=20 .
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
typedef long long ll;
struct modui{
int l,r,id;
}s[N];
ll a[N],c[N],ans[N],sss=0;
int L=1,R=0,pos[N],n,t;
void add(int x){
c[x]++;//因为要求Ks*Ks*s,所以每次增加一个需要在原有结果上加(Ks*Ks*x - (Ks-1)*(Ks-1)*x)
sss+=x*(c[x]*c[x]-(c[x]-1)*(c[x]-1));
}
void del(int x){
c[x]--;//,删除的话也是要相应的减去
sss-=x*((c[x]+1)*(c[x]+1)-(c[x])*(c[x]));
}
bool cmp(modui x,modui y){
//按左端点分块,然后在同一个块当中区间右端点要递增
if (pos[x.l]==pos[y.l]) return x.r<y.r;//如果左在同一块当中区间右端点要递增
return pos[x.l]<pos[y.l];
}
void query(int l,int r){
while (L<l) del(a[L++]);//左指针往右移删除
while (L>l) add(a[--L]);//左指针往左移加入
while (R<r) add(a[++R]);//右指针往右移加入
while (R>r) del(a[R--]);//右指针往左移删除
return;
}
int main(){
cin>>n>>t;
int dis=sqrt(n);//块大小
for(int i=1;i<=n;i++) scanf("%lld",&a[i]),pos[i]=i/dis;//pos标记每个数所属的分块
for(int i=1;i<=t;i++){
scanf("%d %d",&s[i].l,&s[i].r);
s[i].id=i;
}
sort(s+1,s+t+1,cmp);//排序的时候按照左端点所在的块为第一关键字,右端点为第二关键字排序
for(int i=1;i<=t;i++){
query(s[i].l,s[i].r);
ans[s[i].id]=sss;//记录答案
}
for(int i=1;i<=t;i++) printf("%lld\n",ans[i]);
return 0;
}
边栏推荐
猜你喜欢
开发一套高容错分布式系统
适配器模式
LeetCode Question of the Day - 1403. Minimum Subsequence in Non-Increasing Order
力拓信创生态,博睿数据多款产品获得东方通与达梦数据库产品兼容互认证明
学习探索-网站中引入百度统计
LeetCode 每日一题——1403. 非递增顺序的最小子序列
88.(cesium之家)cesium聚合图
Copycat CNN: Stealing Knowledge by Persuading Confession with Random Non-Labeled Data阅读心得
Mobile BesTV_R3300-L_S905L_8189_wire brush firmware package
shell脚本详解 --------循环语句之for循环
随机推荐
Hubei Mobile ZTE B860AV2.1_S905L_ flash firmware package
跨域传递数据(iframe)
移动平台助力推进智慧型科研院所信息化建设
水能自发变成“消毒水”,83岁斯坦福教授:揭示冬天容易得流感的部分原因...
两个对象相同数据赋值
Boost库学习笔记(一)安装与配置
安装win11提示开启安全模式如何解决
hi, 请问下这是什么问题, 我看官网的example就是mysql的, 咋提示不支持?
Taurus.MVC WebAPI 入门开发教程2:添加控制器输出Hello World。
R语言使用cov函数计算矩阵或者dataframe数据变量之间的协方差、cor函数计算相关性、cor函数通过method参数指定相关性、相关性计算方法Pearson,Spearman, Kendall
并发编程原理学习-reentrantlock源码分析
麒麟信安石勇博士荣获openEuler社区年度开源贡献之星
IDEA以多端口启动同一个服务项目
【LeetCode Daily Question】——374. Guess the size of the number
开发一套高容错分布式系统
正则过滤字符串中 script 标签
dotnet core 隐藏控制台
开一个羽毛球馆大概需要多少钱?大约15万左右可以搞定!
机器学习(十):朴素贝叶斯
SAP 电商云 Spartacus UI 页面布局的设计原理