CodeForces 86D Powerful array(莫队算法)

BZOJ2038差不多。。复习一下。

 1 #include<cstdio>
 2 #include<cmath>
 3 #include<algorithm>
 4 using namespace std;
 5 int block;
 6 struct Query{
 7     int i,l,r;
 8     bool operator<(const Query &q)const{
 9         if(l/block==q.l/block) return r<q.r;
10         return l/block<q.l/block;
11     }
12 }query[220000];
13 int cnt[1111111];
14 long long res,ans[220000];
15 void insert(long long x){
16     res-=x*cnt[x]*cnt[x];
17     ++cnt[x];
18     res+=x*cnt[x]*cnt[x];
19 }
20 void remove(long long x){
21     res-=x*cnt[x]*cnt[x];
22     --cnt[x];
23     res+=x*cnt[x]*cnt[x];
24 }
25 int a[220000];
26 int main(){
27     int n,t;
28     scanf("%d%d",&n,&t);
29     for(int i=1; i<=n; ++i) scanf("%d",a+i);
30     block=(int)sqrt(n);
31     for(int i=0; i<t; ++i){
32         query[i].i=i;
33         scanf("%d%d",&query[i].l,&query[i].r);
34     }
35     sort(query,query+t);
36     int l=1,r=1; cnt[a[1]]=1; res=a[1];
37     for(int i=0; i<t; ++i){
38         while(l<query[i].l) remove(a[l++]);
39         while(l>query[i].l) insert(a[--l]);
40         while(r>query[i].r) remove(a[r--]);
41         while(r<query[i].r) insert(a[++r]);
42         ans[query[i].i]=res;
43     }
44     for(int i=0; i<t; ++i){
45         printf("%I64d\n",ans[i]);
46     }
47     return 0;
48 }
时间: 2024-08-04 05:17:12

CodeForces 86D Powerful array(莫队算法)的相关文章

CodeForces - 86D Powerful array (莫队)

题意:查询的是区间内每个数出现次数的平方×该数值的和. 分析:虽然是道莫队裸体,但是姿势不对就会超时.答案可能爆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>

D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力

莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array 典型的莫队算法题 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include

codeforces 86D D. Powerful array(莫队算法)

题目链接: 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, wh

Codeforces 86D Powerful array(莫队)

题目链接:http://codeforces.com/problemset/problem/86/D 题目: 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 occu

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

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

【分块】Codeforces 86d Powerful array

通道 题意:询问[l,r]区间的权和,权定义为sum(k^2*a[i]),k表示a[i]出现的次数 思路:区间每增加一个a[i],增量是(2*x+1)*a[i],因为(x+1)^2*a[i] = (x^2 +2*x + 1)*a[i] 分块排序即可,块内按r排序 代码: #include <cstdio> #include <algorithm> #include <cmath> using namespace std; #define N 200100 typedef

(莫队算法)两题莫队算法统计数量的入门题

因为这两题差不多,而且比较简单,就放一起,做了这题,这种题目就是巨水的题了.随便写都行. CodeForces - 86D  Powerful array 题意: 多次查询数列中从L到R每个数字出现次数的平方乘这个数字的和. 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <cmath> 5 #include <map> 6 #includ

D. Powerful array 离线+莫队算法 给定n个数,m次查询;每次查询[l,r]的权值; 权值计算方法:区间某个数x的个数cnt,那么贡献为cnt*cnt*x; 所有贡献和即为该区间的值;

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?

CodeForces - 86D 莫队算法

http://codeforces.com/problemset/problem/86/D 莫队算法就是调整查询的顺序,然后暴力求解. 每回可以通过现有区间解ans(l,r)得到区间(l+1,r),(l-1,r),(l,r+1),(l,r-1)的区间解. 调整方式http://blog.csdn.net/bossup/article/details/39236275 这题比那个还要简单,查询的是K^2*Z,很清楚就是莫队算法,然而做的时候没有学过,回来补题补到 关键是我一直没明白为什么重载小于号