Codeforces 86D Powerful array(莫队算法)
和BZOJ2038差不多。。复习一下。
- #include<cstdio>
- #include<cmath>
- #include<algorithm>
- using namespace std;
- int block;
- struct Query{
- int i,l,r;
- bool operator<(const Query &q)const{
- if(l/block==q.l/block) return r<q.r;
- return l/block<q.l/block;
- }
- }query[];
- int cnt[];
- long long res,ans[];
- void insert(long long x){
- res-=x*cnt[x]*cnt[x];
- ++cnt[x];
- res+=x*cnt[x]*cnt[x];
- }
- void remove(long long x){
- res-=x*cnt[x]*cnt[x];
- --cnt[x];
- res+=x*cnt[x]*cnt[x];
- }
- int a[];
- int main(){
- int n,t;
- scanf("%d%d",&n,&t);
- for(int i=; i<=n; ++i) scanf("%d",a+i);
- block=(int)sqrt(n);
- for(int i=; i<t; ++i){
- query[i].i=i;
- scanf("%d%d",&query[i].l,&query[i].r);
- }
- sort(query,query+t);
- int l=,r=; cnt[a[]]=; res=a[];
- for(int i=; i<t; ++i){
- while(l<query[i].l) remove(a[l++]);
- while(l>query[i].l) insert(a[--l]);
- while(r>query[i].r) remove(a[r--]);
- while(r<query[i].r) insert(a[++r]);
- ans[query[i].i]=res;
- }
- for(int i=; i<t; ++i){
- printf("%I64d\n",ans[i]);
- }
- return ;
- }
Codeforces 86D Powerful array(莫队算法)的更多相关文章
- Codeforces 86D - Powerful array(莫队算法)
题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字i在该区间内的出现次数,求 ...
- CodeForces - 86D Powerful array (莫队)
题意:查询的是区间内每个数出现次数的平方×该数值的和. 分析:虽然是道莫队裸体,但是姿势不对就会超时.答案可能爆int,所以要开long long 存答案.一开始的维护操作,我先在res里减掉了a[p ...
- D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力
莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array ...
- [Codeforces86D]Powerful array(莫队算法)
题意:定义K[x]为元素x在区间[l,r]内出现的次数,那么它的贡献为K[x]*K[x]*x 给定一个序列,以及一些区间询问,求每个区间的贡献 算是莫队算法膜版题,不带修改的 Code #includ ...
- CodeForces - 86D D. Powerful array —— 莫队算法
题目链接:http://codeforces.com/problemset/problem/86/D D. Powerful array time limit per test 5 seconds m ...
- codeforces 86D D. Powerful array(莫队算法)
题目链接: D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input stan ...
- Codeforces 86D Powerful array (莫队算法)
题目链接 Powerful array 给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和. $1<=n,m<=200000, 1<=s< ...
- Codeforces 86D Powerful array (莫队)
D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...
- codeforces 86D,Powerful array 莫队
传送门:https://codeforces.com/contest/86/problem/D 题意: 给你n个数,m次询问,每次询问问你在区间l,r内每个数字出现的次数的平方于当前这个数的乘积的和 ...
- codeforces 86D : Powerful array
Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...
随机推荐
- WinAPI: ExtCreateRegion - 区域变换
转载:http://www.cnblogs.com/del/archive/2008/06/03/1212534.html 相似函数: SetWorldTransform 本例效果图: 代码文件: u ...
- js 中数组或者对象的深拷贝和浅拷贝
浅拷贝 : 就是两个js 对象指向同一块内存地址,所以当obj1 ,obj2指向obj3的时候,一旦其中一个改变,其他的便会改变! 深拷贝:就是重新复制一块内存,这样就不会互相影响. 有些时候我们定义 ...
- lz4,pigz,gzip 3者比较
一.压缩(1.1)使用gzip进行打包:# time tar -zcf tar1.tar binlog*real 0m48.497suser 0m38.371ssys 0m2.571s (1.2)使用 ...
- 创建一个最简单的Linux随机启动服务
转自: http://xiaoxia.org/2011/11/15/create-a-simple-linux-daemon/
- 获取Ad用户信息
private]; } dt.Rows.Add(dr); } return dt; ...
- Simple Chroma Key 0.1.16 图片抠像(vs2003) 无任何插件
学习扣像的最基础代码 (只支持 BMP TGA) simplechromakey.rar
- codeforces 374A Inna and Pink Pony 解题报告
题目链接:http://codeforces.com/problemset/problem/374/A 题目意思:给出一个 n 行 m 列 的棋盘,要将放置在坐标点为(i, j)的 candy 移动 ...
- rdesktop remember
整个地球都知道rdesktop,有了它,我们可以从Solaris或者Linux使用Windows,当然Windows要开启Windows Terminal Service.虽然也有基于GTK+的tsc ...
- 决策树之C4.5算法
决策树之C4.5算法 一.C4.5算法概述 C4.5算法是最常用的决策树算法,因为它继承了ID3算法的所有优点并对ID3算法进行了改进和补充. 改进有如下几个要点: 用信息增益率来选择属性,克服了ID ...
- 拷贝Java项目报错
经常需要将一个项目,导出,然后发给同事,或者是自己用另一个Eclipse工具打开. 这时,导入项目后,就会出现各种各样的问题.大牛笔记:www.weixuehao.com 代码相同,环境不同,主要是修 ...