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>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long LL;
const int maxn =2e5+5;
const int maxv = 1e6+5;
int block;
int a[maxn];
int cnt[maxv];
int pos[maxn];
LL ans[maxn],res;
struct Query{
    int L,R,id;
    bool operator <(const Query &p) {
        if(pos[L]==pos[p.L]) return R<p.R;
        return pos[L]<pos[p.L];
    }
}Q[maxn];

void add(int pos)
{
    res+= (LL)(cnt[a[pos]]*2+1)*a[pos];
    cnt[a[pos]]++;
}

void del(int pos)
{
    cnt[a[pos]]--;
    res-= (LL)(cnt[a[pos]]*2+1)*a[pos];
}

//#define LOCAL
int main()
{
    #ifdef LOCAL
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    int N,M,K,u,v,k;
    while(scanf("%d%d",&N,&M)==2){
        block = ceil(sqrt(1.0*N));
        memset(cnt,0,sizeof(cnt));
        for(int i=1;i<=N;++i){
            scanf("%d",&a[i]);
            pos[i]= i /block;
        }
        for(int i=1;i<=M;++i){
            scanf("%d%d",&Q[i].L,&Q[i].R);
            Q[i].id = i;
        }
        sort(Q+1,Q+M+1);
        res=0;
        int curL=0,curR=0;
        for(int i=1;i<=M;++i){
            while(curL>Q[i].L)  add(--curL);
            while(curR<Q[i].R)  add(++curR);
            while(curL<Q[i].L)  del(curL++);
            while(curR>Q[i].R)  del(curR--);
            ans[Q[i].id]= res;
        }
        for(int i=1;i<=M;++i)
            printf("%lld\n",ans[i]);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/xiuwenli/p/9343342.html

时间: 2024-11-05 16:33:48

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

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

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 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(莫队算法)

和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 re

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

86D - Powerful array

莫队. 统计ai[i]的出现次数,每一次先还原贡献,再加上或减去当前的贡献即可. #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<cstdlib> #include<climits> #include<stack> #include<vector> #include<queue> #i

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

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

Powerful array CodeForces - 86D (莫队算法)

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 power of th