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 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';}} }轉載于:https://www.cnblogs.com/qieqiemin/p/11365533.html
總結
以上是生活随笔為你收集整理的Powerful array CodeForces - 86D (莫队算法)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: D-query SPOJ - DQUER
- 下一篇: 设计模式 之 创建者