码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-08-16 19:05:56      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:vector   getchar   its   return   ret   莫队算法   only   contains   mes   

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 the subarray the sum of products Ks·Ks·s for every positive integer s. The sum contains only finite number of nonzero summands as the number of different values in the array is indeed finite.

You should calculate the power of t given subarrays.

Input
First line contains two integers n and t (1?≤?n,?t?≤?200000) — the array length and the number of queries correspondingly.

Second line contains n positive integers ai (1?≤?ai?≤?106) — the elements of the array.

Next t lines contain two positive integers l, r (1?≤?l?≤?r?≤?n) each — the indices of the left and the right ends of the corresponding subarray.

Output
Output t lines, the i-th line of the output should contain single positive integer — the power of the i-th query subarray.

Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preferred to use cout stream (also you may use %I64d).

Examples
Input
3 2
1 2 1
1 2
1 3
Output
3
6
Input
8 3
1 1 2 2 1 3 1 1
2 7
1 6
2 7
Output
20
20
20
Note
Consider the following array (see the second sample) and its [2, 7] subarray (elements of the subarray are colored):

Then K1?=?3, K2?=?2, K3?=?1, so the power is equal to 32·1?+?22·2?+?12·3?=?20.

题意:
给定一个长度N的数组a

要求:询问区间1<=L<=R<=N中,每个数字出现次数的平方与当前数字的乘积和


思路:

用一个数组flag[i] 表示 数字i在当前区间出现的次数。

然后正常的莫队add,del转移即可。。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2) { ans = ans * a % MOD; } a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int *p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
pll ans[maxn];
ll Ans = 0ll;
int l = 0;
int r = 0;
struct node {
    int l, r, id;
} a[maxn];
int pos[maxn];
int n, m;
int len;
bool cmp(node aa, node bb)
{
    if (pos[aa.l] == pos[bb.l]) {
        return aa.r < bb.r;
    } else {
        return pos[aa.l] < pos[bb.l];
    }
}
int col[maxn];
int flag[maxn];
void add(int x)
{
    Ans-=1ll*col[x]*flag[col[x]]*flag[col[x]];
    flag[col[x]]++;
    Ans+=1ll*col[x]*flag[col[x]]*flag[col[x]];
}
void del(int x)
{
    Ans-=1ll*col[x]*flag[col[x]]*flag[col[x]];
    flag[col[x]]--;
    Ans+=1ll*col[x]*flag[col[x]]*flag[col[x]];
}
int main()
{
    //freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    gg(n); 
    gg(m);
    len = (int)(sqrt(n));
    repd(i, 1, n) {
        gg(col[i]);
    }
    repd(i, 1, m) {
        gg(a[i].l);
        gg(a[i].r);
        a[i].id = i;
        pos[i] = i / len;
    }
    sort(a + 1, a + 1 + m, cmp);
    repd(i, 1, m) {
        while (l > a[i].l) {
            l--;
            add(l);
        }
        while (r < a[i].r) {
            r++;
            add(r);
        }
        while (l < a[i].l) {
            del(l);
            l++;
        }
        while (r > a[i].r) {
            del(r);
            r--;
        }
        ans[a[i].id].fi = Ans;
    }
    repd(i, 1, m) {
        printf("%lld\n", ans[i].fi);
    }
    return 0;
}

inline void getInt(int *p)
{
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    } else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}



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

标签:vector   getchar   its   return   ret   莫队算法   only   contains   mes   

原文地址:https://www.cnblogs.com/qieqiemin/p/11365533.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!