Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, al + 1..., ar, where 1 ≤ l ≤ r ≤ n. For every positive integer s denote by Ks the number of occurrences of s into the subarray. We call the…
D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, al + 1..., ar, where 1 …
题目链接 Powerful array 给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和. $1<=n,m<=200000,   1<=s<=10^{6}$ 考虑莫队算法 把区间排序,然后让l和r分别询问即可. 根据排序的方式,l指针和r指针移动次数和大概是$n\sqrt{n}$ 级别的. #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (…
D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, al + 1..., ar, where 1 …
Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, al + 1..., ar, where 1 ≤ l ≤ r ≤ n. For every positive integer s denote by Ks the number of occurrences of s into the subarray. We call the…
简介:分块算法主要是把区间划分成sqrt(n)块,从而降低暴力的复杂度, 其实这算是一种优化的暴力吧,复杂度O(n*sqrt(n)) 题意:给定一个数列:a[i]    (1<= i <= n)    K[j]表示 在区间 [l,r]中j出现的次数. 有t个查询,每个查询l,r,对区间内所有a[i],求sigma(K[a[i]]^2*a[i]) 思路:离线+分块处理 分块和离线处理: 将n个数分成sqrt(n)块,设每块有bsize个数, 并且我们计算出每个询问的左端点所在的块号(q[i].b…
题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字i在该区间内的出现次数,求该区间内所有的cnt[i]^2*i. 解题思路:莫队模板题,改一下add,remove就好了. #include<iostream> #include<algorithm> #include<cmath> using namespace std; typedef long…
和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/b…
题意:查询的是区间内每个数出现次数的平方×该数值的和. 分析:虽然是道莫队裸体,但是姿势不对就会超时.答案可能爆int,所以要开long long 存答案.一开始的维护操作,我先在res里减掉了a[pos]*cnt[a[pos]]*cnt[a[pos]],将cnt[a[pos]]+1,再将乘积加回.其实根据数学原理,K^2和(K+1)^2差值是2K+1,那么其实每次更新时只要加上或减去a[pos]*(2*cnt[pos]+1)即可,这样更高效. #include<bits/stdc++.h>…
给定一个数列: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 (按照查询的右边界排序) 问题求解: 从上一个查询后的结果推出当前查询…