国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[Codeforces86D]Powerful array(莫隊)

2019-11-08 01:44:52
字體:
來源:轉載
供稿:網友

題目描述

傳送門

題解

裸莫隊啊= = 但是卡常數 塊的大小開到600 盡量用int然后強轉 然后盡量避免數組尋址

代碼

#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>#include<cmath>using namespace std;#define LL long long#define N 1000005int n,m,block,num[N],a[N],cnt[N];struct data{int l,r,id;LL ans;}q[N];LL ans;int cmp(data a,data b){ return num[a.l]<num[b.l]||(num[a.l]==num[b.l]&&num[a.r]<num[b.r]);}int main(){ scanf("%d%d",&n,&m); for (int i=1;i<=n;++i) scanf("%d",&a[i]); for (int i=1;i<=m;++i) scanf("%d%d",&q[i].l,&q[i].r),q[i].id=i; block=600; for (int i=1;i<=n;++i) num[i]=(i-1)/block+1; sort(q+1,q+m+1,cmp);ans=0LL; for (int i=q[1].l;i<=q[1].r;++i) { int x=a[i],y=cnt[x]; ans-=(LL)y*y*x; ++cnt[x]; y=cnt[x]; ans+=(LL)y*y*x; }q[q[1].id].ans=ans; for (int i=2;i<=m;++i) { if (q[i].l<q[i-1].l) { for (int j=q[i-1].l-1;j>=q[i].l;--j) { int x=a[j],y=cnt[x]; ans-=(LL)y*y*x; ++cnt[x]; y=cnt[x]; ans+=(LL)y*y*x; } } else { for (int j=q[i-1].l;j<q[i].l;++j) { int x=a[j],y=cnt[x]; ans-=(LL)y*y*x; --cnt[x]; y=cnt[x]; ans+=(LL)y*y*x; } } if (q[i].r>q[i-1].r) { for (int j=q[i-1].r+1;j<=q[i].r;++j) { int x=a[j],y=cnt[x]; ans-=(LL)y*y*x; ++cnt[x]; y=cnt[x]; ans+=(LL)y*y*x; } } else { for (int j=q[i-1].r;j>q[i].r;--j) { int x=a[j],y=cnt[x]; ans-=(LL)y*y*x; --cnt[x]; y=cnt[x]; ans+=(LL)y*y*x; } } q[q[i].id].ans=ans; } for (int i=1;i<=m;++i)
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 曲松县| 镇巴县| 华坪县| 天祝| 越西县| 宁阳县| 宝鸡市| 呼伦贝尔市| 轮台县| 黔东| 调兵山市| 开阳县| 喜德县| 嘉荫县| 松原市| 禄丰县| 吉安县| 鄂伦春自治旗| 铅山县| 南木林县| 忻城县| 瓦房店市| 汾阳市| 从化市| 镶黄旗| 和龙市| 固始县| 惠州市| 隆子县| 遂川县| 永顺县| 万安县| 黄骅市| 南开区| 呼图壁县| 黄大仙区| 江西省| 界首市| 南乐县| 罗江县| 新宾|