简介:分块算法主要是把区间划分成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…
给定一个数列: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 (按照查询的右边界排序) 问题求解: 从上一个查询后的结果推出当前查询…
离线+分块 将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] =…
题目链接 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 …
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 …
题意:求满足a^x=b(mod n)的最小的整数x. 分析:很多地方写到n是素数的时候可以用Baby step,Giant step, 其实研究过Baby step,Giant step算法以后,你会发现  它能解决    “n与a互质”的情况,而并不是单纯的n是素数的情况.如果a与n不是互质的,那么我们需要处理一下原方程,让a与n互质,然后再用Baby step,Giant step解出x即可. Baby step,Giant step算法思想:对于a与n互质,那么则有a^phi(n)=1(m…
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…
和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…
题目链接: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…