1. 程式人生 > >D. Powerful array 離線+莫隊算法 給定n個數,m次查詢;每次查詢[l,r]的權值; 權值計算方法:區間某個數x的個數cnt,那麽貢獻為cnt*cnt*x; 所有貢獻和即為該區間的值;

D. Powerful array 離線+莫隊算法 給定n個數,m次查詢;每次查詢[l,r]的權值; 權值計算方法:區間某個數x的個數cnt,那麽貢獻為cnt*cnt*x; 所有貢獻和即為該區間的值;

code ++ 計算方法 equal ati contains tdi ces sum

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?≤?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. /** 題目:D. Powerful array 鏈接:http://codeforces.com/problemset/problem/86/D 題意:給定n個數,m次查詢;每次查詢[l,r]的權值; 權值計算方法:區間某個數x的個數cnt,那麽貢獻為cnt*cnt*x; 所有貢獻和即為該區間的值; 思路:由於靜態區間,所以離線+莫隊算法; 然後(cnt+1)*(cnt+1)*x-cnt*cnt*x=(2*cnt+1)*x; 來優化計算;常規暴力計算超時; */ #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<cmath> #include<map> #include<set> #include<vector> #include<string> #include<queue> #include<bitset> using namespace std; typedef long long ll; const int inf = 0x3f3f3f3f; const double eps=1e-8; const int maxn = 2e5+100; const ll mod = 1e9+7; ll num[1000005], c[maxn], pos[maxn]; ll ans; int n , m; struct node { ll l, r; ll ans; int id; }t[maxn]; int cmp_id(node a,node b) { return a.id<b.id; } int cmp(node a,node b) { if(pos[a.l]==pos[b.l]) return a.r<b.r; return pos[a.l]<pos[b.l]; } void update(int place,int add) { ll v = c[place]; if(add==1){ ans += v*(2*num[v]+1); num[v]++; }else { num[v]--; ans -= v*(2*num[v]+1); } } void solve() { memset(num, 0, sizeof num); ans = 0; for(int i = t[1].l; i <= t[1].r; i++){ update(i,1); } t[1].ans = ans; for(int i = 2; i <= m; i++){ for(int j = t[i-1].l; j < t[i].l; j++) update(j,-1);//減得時候,自身開始; for(int j = t[i-1].l; j > t[i].l; j--) update(j-1,1);//增的時候,不包括自身; for(int j = t[i-1].r; j < t[i].r; j++) update(j+1,1); for(int j = t[i-1].r; j > t[i].r; j--) update(j,-1); t[i].ans = ans; } } int main() { while(scanf("%d%d",&n,&m)==2) { for(int i = 1; i <= n; i++) scanf("%I64d",&c[i]); for(int i = 1; i <= m; i++){ scanf("%I64d%I64d",&t[i].l,&t[i].r); t[i].id = i; } int N = int(sqrt(n)); for(int i = 1; i <= n; i++){ pos[i] = i/N+1; } sort(t+1,t+1+m,cmp); solve(); sort(t+1,t+1+m,cmp_id); for(int i = 1; i <= m; i++){ printf("%I64d\n",t[i].ans); } } return 0; }

D. Powerful array 離線+莫隊算法 給定n個數,m次查詢;每次查詢[l,r]的權值; 權值計算方法:區間某個數x的個數cnt,那麽貢獻為cnt*cnt*x; 所有貢獻和即為該區間的值;