给定一个数列:A1, A2,……,An,定义Ks为区间(l,r)中s出现的次数。

t个查询,每个查询l,r,对区间内所有a[i],求sigma(K^2*a[i])

离线+分块

将n个数分成sqrt(n)块。

对所有询问进行排序,排序标准:

1. Q[i].left /block_size < Q[j].left / block_size (块号优先排序)

2. 如果1相同,则 Q[i].right < Q[j].right (按照查询的右边界排序)

问题求解:

从上一个查询后的结果推出当前查询的结果。(这个看程序中query的部分)

如果一个数已经出现了x次,那么需要累加(2*x+1)*a[i],因为(x+1)^2*a[i] = (x^2 +2*x + 1)*a[i],x^2*a[i]是出现x次的结果,(x+1)^2 * a[i]是出现x+1次的结果。

时间复杂度分析:

排完序后,对于相邻的两个查询,left值之间的差最大为sqrt(n),则相邻两个查询左端点移动的次数<=sqrt(n),总共有t个查询,则复杂度为O(t*sqrt(n))。

又对于相同块内的查询,right端点单调上升,每一块所有操作,右端点最多移动O(n)次,总块数位sqrt(n),则复杂度为O(sqrt(n)*n)。

right和left的复杂度是独立的,因此总的时间复杂度为O(t*sqrt(n)  +  n*sqrt(n))。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
#define N 200100
typedef long long ll;
ll a[N], cnt[N*5], ans[N], res;
int L, R; struct node {
int x, y, l, p;
} q[N];
bool cmp(const node &x, const node &y) {
if (x.l == y.l) return x.y < y.y;
return x.l < y.l;
}
void query(int x, int y, int flag) {
if (flag) {
for (int i=x; i<L; i++) {
res += ((cnt[a[i]]<<1)+1)*a[i];
cnt[a[i]]++;
}
for (int i=L; i<x; i++) {
cnt[a[i]]--;
res -= ((cnt[a[i]]<<1)+1)*a[i];
}
for (int i=y+1; i<=R; i++) {
cnt[a[i]]--;
res -= ((cnt[a[i]]<<1)+1)*a[i];
}
for (int i=R+1; i<=y; i++) {
res += ((cnt[a[i]]<<1)+1)*a[i];
cnt[a[i]]++;
} } else {
for (int i=x; i<=y; i++) {
res += ((cnt[a[i]]<<1)+1)*a[i];
cnt[a[i]]++;
}
}
L = x, R = y;
}
int main() {
int n, t; scanf("%d%d", &n, &t);
for (int i=1; i<=n; i++) scanf("%I64d", &a[i]);
int block_size = sqrt(n); for (int i=0; i<t; i++) {
scanf("%d%d", &q[i].x, &q[i].y);
q[i].l = q[i].x / block_size;
q[i].p = i;
} sort(q, q+t, cmp); memset(cnt, 0, sizeof(cnt)); res = 0;
for (int i=0; i<t; i++) {
query(q[i].x, q[i].y, i);
ans[q[i].p] = res;
} for (int i=0; i<t; i++) printf("%I64d\n", ans[i]); return 0;
}

CF 86D Powerful array 【分块算法,n*sqrt(n)】的更多相关文章

  1. [置顶] CF 86D Powerful array 分块算法入门,n*sqrt(n)

    简介:分块算法主要是把区间划分成sqrt(n)块,从而降低暴力的复杂度, 其实这算是一种优化的暴力吧,复杂度O(n*sqrt(n)) 题意:给定一个数列:a[i]    (1<= i <= ...

  2. CF 86D Powerful array

    离线+分块 将n个数分成sqrt(n)块. 对所有询问进行排序,排序标准:       1. Q[i].left /block_size < Q[j].left / block_size (块号 ...

  3. Codeforces 86D Powerful array (莫队算法)

    题目链接 Powerful array 给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和. $1<=n,m<=200000,   1<=s< ...

  4. CodeForces 86D Powerful array(莫队+优化)

    D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...

  5. Codeforces 86D Powerful array (莫队)

    D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...

  6. Codeforces#86D Powerful array(分块暴力)

    Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...

  7. Codeforces 86D Powerful array(莫队算法)

    和BZOJ2038差不多..复习一下. #include<cstdio> #include<cmath> #include<algorithm> using nam ...

  8. Codeforces 86D - Powerful array(莫队算法)

    题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字i在该区间内的出现次数,求 ...

  9. codeforces 86D : Powerful array

    Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...

随机推荐

  1. 添加navbar以及上面的左右按钮代码

    UINavigationBar *navBar = [[UINavigationBaralloc] initWithFrame:CGRectMake(0, 0, 824, 44)]; navBar.b ...

  2. jquery事件学习笔记(转载)

    一.页面载入1.ready(fn)当DOM载入就绪可以查询及操纵时绑定一个要执行的函数.这是事件模块中最重要的一个函数,因为它可以极大地提高web应用程序的响应速度. 简单地说,这个方法纯粹是对向wi ...

  3. Linux共享内存(一)

    inux系统编程我一直看 <GNU/LINUX编程指南>,只是讲的太简单了,通常是书和网络上的资料结合着来掌握才比较全面 .在掌握了书上的内容后,再来都其他资料 . 原文链接 http:/ ...

  4. Effective java笔记5--通用程序设计

    一.将局部变量的作用域最小化      本条目与前面(使类和成员的可访问能力最小化)本质上是类似的.将局部变量的作用域最小化,可以增加代码的可读性和可维护性,并降低出错的可能性. 使一个局部变量的作用 ...

  5. YII Framework学习教程-YII的异常处理

    异常无处不在,作为程序员,活着就是为了创造这些异常,然后修复这些异常而存在的.YII框架封装了PHP的异常,让异常处理起来更简单. 使用 YII处理错误和异常的配置方法: 你可以在入口文件中定义YII ...

  6. [转]Linux的chattr与lsattr命令详解

    转自:http://www.cnblogs.com/yangxia-test/archive/2013/05/24/3096410.html 这两个命令是用来查看和改变文件.目录属性的,与chmod这 ...

  7. PHP 转义详解

    php中数据的魔法引用函数 magic_quotes_gpc  或 magic_quotes_runtime 设置为on时,为我们引用的数据碰到 单引号' 和 双引号" 以及 反斜线\ 时自 ...

  8. Ubuntu14.04LTS安装记录(办公室联想台式机)

    一.用UltraISO制作U盘启动器,被安装的电脑要设置成从U盘启动. 二.傻瓜式安装简体中文版 三.安装更新 sudo apt-get update sudo apt-get upgrade 四.安 ...

  9. VS2010安装EntityFramework5.0

    EntityFramework 当前最新版本是6.0,2012年10月份发布,不过是alpha1版本,稳定版本是8月份发布的5.0版本,对于初学者来说,还是别在测试版上折腾了,先学习稳定的5.0版本, ...

  10. FlatBuffers要点

    FlatBuffers发布出来一周多,周末便抽时间先研究下它的使用方法.Flatbuffers的idl的语法主要参考[http://google.github.io/flatbuffers/md__s ...