国产探花免费观看_亚洲丰满少妇自慰呻吟_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)
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 舟曲县| 厦门市| 高州市| 怀柔区| 饶平县| 扶余县| 当雄县| 怀柔区| 铜山县| 西丰县| 嵊泗县| 景德镇市| 宁阳县| 班玛县| 永州市| 新民市| 门源| 监利县| 延庆县| 重庆市| 恩施市| 台中县| 营口市| 公安县| 宜宾市| 咸阳市| 离岛区| 阿图什市| 通江县| 黑水县| 合山市| 新郑市| 慈利县| 年辖:市辖区| 措勤县| 晴隆县| 松滋市| 南召县| 兴安县| 故城县| 奉化市|