当前位置:网站首页>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;
}
边栏推荐
- Hubei Telecom Tianyi TY1608_S905L3B_MT7668_ card brush firmware package
- 机器学习(十八):随机搜索和XGBoost
- 域名哪家便宜?怎么买便宜域名?
- 谷歌开发者社区推荐:《Jetpack Compose 从入门到实战》新书上架,带你踏上 Compose 开发之旅~
- 【小程序】实现发动态功能
- Copycat CNN: Stealing Knowledge by Persuading Confession with Random Non-Labeled Data阅读心得
- 两个对象相同数据赋值
- 跨域传递数据(iframe)
- 设置表头颜色
- el-date-picker 设置时间范围
猜你喜欢
随机推荐
适配器模式
不需要服务器,教你仅用30行代码搞定实时健康码识别
力拓信创生态,博睿数据多款产品获得东方通与达梦数据库产品兼容互认证明
AtCoder Beginner Contest 262 部分题解
机器人示教编程与离线编程的优缺点对比
response的contentType 几种类型
Catering Supply Chain Management System
RecyclerView 缓存与复用机制
jMeter Transaction Controller 学习笔记
基于clipboard.js对复制组件的封装
RTL8762DK 远端设备配对
LeetCode 每日一题——1403. 非递增顺序的最小子序列
【论文阅读】Decision Transformer: Reinforcement Learning via Sequence Modeling
全世界国家和地区国家顶级域名对照表
Mobile BesTV_R3300-L_S905L_8189_wire brush firmware package
码蹄集 - MT3029 - 新月轩就餐
机器学习(十九):梯度提升回归(GBR)
ctfshow 萌新web1-21
码蹄集 - MT2165 - 小码哥的抽卡之旅1
JVM内存和垃圾回收-08.方法区