codeforce 86D Powerful array 莫对算法
来源:互联网 发布:react.js ui 编辑:程序博客网 时间:2024/05/17 06:19
简单莫队算法题,对于她给的公式来说怎么,每一次转移区间的是候用一个数组来位数每个数值的数的个数即可。
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<cmath>using namespace std;const int maxn=200005;typedef long long LL;int a[1000006],b[maxn];struct pi{ int l,r; int x; int id;}pp[maxn];LL s1[maxn];int cmp(pi a,pi b){ if(a.x!=b.x) return a.x<b.x; return a.r<b.r;}int main(){ int i,j,n,m,p; cin>>n>>m; for(i=1;i<=n;i++) scanf("%d",&b[i]); LL s=0; p=sqrt(n); for(i=0;i<m;i++){ scanf("%d%d",&pp[i].l,&pp[i].r); pp[i].id=i; pp[i].x=(pp[i].l-1)/p+1; } sort(pp,pp+m,cmp); int l=0,r=0; for(i=0;i<m;i++){ if(i==0){ for(j=pp[i].l;j<=pp[i].r;j++){ if(a[b[j]]>=0){ s-=(LL)a[b[j]]*a[b[j]]*b[j]; s+=(LL)(a[b[j]]+1)*(a[b[j]]+1)*b[j]; } a[b[j]]++; } } else{ if(l<pp[i].l){ for(j=l;j<pp[i].l;j++){ if(a[b[j]]>0){ s-=(LL)a[b[j]]*a[b[j]]*b[j]; s+=(LL)(a[b[j]]-1)*(a[b[j]]-1)*b[j]; } a[b[j]]--; } } else { for(j=l-1;j>=pp[i].l;j--){ if(a[b[j]]>=0){ s-=(LL)a[b[j]]*a[b[j]]*b[j]; s+=(LL)(a[b[j]]+1)*(a[b[j]]+1)*b[j]; } a[b[j]]++; } } if(r<pp[i].r){ for(j=r+1;j<=pp[i].r;j++){ if(a[b[j]]>=0){ s-=(LL)a[b[j]]*a[b[j]]*b[j]; s+=(LL)(a[b[j]]+1)*(a[b[j]]+1)*b[j]; } a[b[j]]++; } } else{ for(j=r;j>pp[i].r;j--){ if(a[b[j]]>0){ s-=(LL)a[b[j]]*a[b[j]]*b[j]; s+=(LL)(a[b[j]]-1)*(a[b[j]]-1)*b[j]; } a[b[j]]--; } } } s1[pp[i].id]=s; l=pp[i].l; r=pp[i].r; } for(i=0;i<m;i++) printf("%I64d\n",s1[i]);
0 0
- codeforce 86D Powerful array 莫对算法
- [CF 86D] Powerful array · 莫队算法
- Codeforces 86D Powerful array(莫队算法)
- CodeForces 86D Powerful array 莫队算法
- codeforces 86D. Powerful array(莫队算法)
- Codeforces 86D Powerful Array [莫队算法]
- CodeForces 86D Powerful array 莫队算法
- Codeforces 86D Powerful array 莫队算法 分块
- Codeforces-86D:Powerful array(莫队算法)
- 86D - Powerful array
- CF 86D Powerful array
- Codeforces 86D. Powerful array
- Codeforces 86D:Powerful array
- codeforces 86 D Powerful array
- 【CF 86D】Powerful array
- CF 86D Powerful array 【分块算法,n*sqrt(n)】
- CF 86D Powerful array 【分块算法,n*sqrt(n)】
- codeforces 86D D. Powerful array
- 凸优化:ADMM(Alternating Direction Method of Multipliers)交替方向乘子算法系列之八: Distributed Model Fitting
- 260 数数小木块
- poj 2724 Purifying Machine (最小边覆盖)
- 【Python 学习手册笔记】介绍Python对象类型
- logback实用配置示例
- codeforce 86D Powerful array 莫对算法
- HttpClient 与 HtmlParser 简介
- [转]榨干 PHP,不得不转的一篇PHP使用技巧!
- Flatten Binary Tree to Linked List
- 凸优化:ADMM(Alternating Direction Method of Multipliers)交替方向乘子算法系列之九: Nonconvex Problems
- [转]开发大型高负载类网站应用的几个要点
- leetcode 014 —— Longest Common Prefix
- sizeof和strlen
- 实现基于Memcache存储的Session类