CF 86D Powerful array
离线+分块
将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*], 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]]<<)+)*a[i];
cnt[a[i]]++;
}
for (int i=L; i<x; i++) {
cnt[a[i]]--;
res -= ((cnt[a[i]]<<)+)*a[i];
}
for (int i=y+; i<=R; i++) {
cnt[a[i]]--;
res -= ((cnt[a[i]]<<)+)*a[i];
}
for (int i=R+; i<=y; i++) {
res += ((cnt[a[i]]<<)+)*a[i];
cnt[a[i]]++;
} } else {
for (int i=x; i<=y; i++) {
res += ((cnt[a[i]]<<)+)*a[i];
cnt[a[i]]++;
}
}
L = x, R = y;
}
int main() {
int n, t; scanf("%d%d", &n, &t);
for (int i=; i<=n; i++) scanf("%I64d", &a[i]);
int block_size = sqrt(n); for (int i=; 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, , sizeof(cnt)); res = ;
for (int i=; i<t; i++) {
query(q[i].x, q[i].y, i);
ans[q[i].p] = res;
} for (int i=; i<t; i++) printf("%I64d\n", ans[i]); return ;
}
CF 86D Powerful array的更多相关文章
- CF 86D Powerful array 【分块算法,n*sqrt(n)】
给定一个数列:A1, A2,……,An,定义Ks为区间(l,r)中s出现的次数. t个查询,每个查询l,r,对区间内所有a[i],求sigma(K^2*a[i]) 离线+分块 将n个数分成sqrt(n ...
- [置顶] CF 86D Powerful array 分块算法入门,n*sqrt(n)
简介:分块算法主要是把区间划分成sqrt(n)块,从而降低暴力的复杂度, 其实这算是一种优化的暴力吧,复杂度O(n*sqrt(n)) 题意:给定一个数列:a[i] (1<= i <= ...
- 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 (莫队算法)
题目链接 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
Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...
- Codeforces#86D Powerful array(分块暴力)
Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...
- Codeforces 86D Powerful array(莫队算法)
和BZOJ2038差不多..复习一下. #include<cstdio> #include<cmath> #include<algorithm> using nam ...
- Codeforces 86D - Powerful array(莫队算法)
题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字i在该区间内的出现次数,求 ...
随机推荐
- 节点属性(DOM对象)
节点属性 在文档对象模型 (DOM) 中,每个节点都是一个对象.DOM 节点有三个重要的属性 : 1. nodeName : 节点的名称 2. nodeValue :节点的值 3. nodeType ...
- 分享:mysql 随机查询数据
在mysql中查询5条不重复的数据,使用以下: 1 SELECT * FROM `table` ORDER BY RAND() LIMIT 5 就可以了.但是真正测试一下才发现这样效率非常低.一个1 ...
- WPF-控件-层级控件-TreeView
<?xml version="1.0" encoding="utf-8" ?> <Data xmlns=""> &l ...
- php微型mvc框架创建步骤
创建数据库和表结构,并且添加模拟数据: 新建models.views.controllers.utilities和include五个文件夹:models:模型层 model.php:模型基类 ...
- Java 图形编程 二:布局管理器之顺序布局
package second; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.Window ...
- Entity Framework Power Tools安装和使用
Entity Framework Power Tools是一个由EntityFramework开发小组提供的工具,它可以从现有数据库生成Fluent款式的Code First代码. 大致来说,这个工具 ...
- hornetq 入门(1)
Hornetq 版本2.4.0final 需要JDK7及以上 Hornetq官网 Hornetq2.1中文手册 step1.启动服务端 1.1准备配置文件(配置说明参考官网手册) hornetq-c ...
- 微软职位内部推荐-Principal Dev Manager
微软近期Open的职位: Title: Principal Dev Manager Location: Beijing The R&D of Shared Data Platform at S ...
- Notes of the scrum meeting(2013/10/23)
ps:本来是10月23号周三下午开的会,这几天由于各种事情忙,忘记写博客了,现在补上. 软工项目组buaa_smile开始项目第一次scrum meeting meeting time:4:00~5: ...
- OC面向对象多态笔记
面向对象的多态是建立在继承上,可以说没有继承就没有多态: 多态:父类指针指向了子类的对象: int main() { //假设已定义了Animal类和它的子类Dog,那么多态的代码体现就是 Anima ...