P2709 Small B inquiry,
Description
Small B has a sequence, including N 1 ~ An integer between K. He has a total of M queries. Each query has a given interval [L .. r], calculate the value of Sigma (c (I) ^ 2), where the value of I ranges from 1 to K, where c (I) indicates that the number I is in [L .. the number of duplicates in R. Mr. B asks him to answer the question.
Input/Output Format
Input Format:
The first line contains three integers: N, M, and K.
The second row contains N integers, indicating the sequence of Small B.
The next M row contains two integers, L and R.
Output Format:
Row M, each row has an integer, And the integer in the row I represents the answer to the question I.
Input and Output sample
Input example #1:
6 4 31 3 2 1 1 31 42 63 55 6
Output sample #1:
6952
Description
For all data, 1 <= N, M, K <= 50000
Naked teams.
You can subtract the square and add it back'
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <algorithm> 6 using namespace std; 7 const int MAXN = 50001; 8 void read (int & n) 9 {10 char c = '+'; int x = 0; bool flag = 0; 11 while (c <'0' | c> '9') 12 {c = getchar (); if (c = '-') flag = 1 ;} 13 while (c> = '0' & c <= '9') 14 {x = x * 10 + (c-48); c = getchar ();} 15 flag = 1? N =-x: n = x; 16} 17 int n, m, k, base; 18 struct node19 {20 int l, r, id; 21} q [MAXN]; 22 int ans = 0; 23 int pos [MAXN], a [MAXN], out [MAXN]; 24 int comp (const node & a, const node & B) 25 {26 if (pos [. l] = pos [B. l]) 27 return. r <B. r; 28 else 29 return pos [. l] <pos [B. l]; 30} 31 int happen [MAXN]; // number of occurrences of each number in the record interval 32 void add (int p) 33 {34 if (p <= k) 35 {36 ans-= happen [p] * happen [p]; 37 happen [p] ++; 38 ans + = happen [p] * happen [p]; 39} 40} 41 void dele (int p) 42 {43 if (p <= k) 44 {45 ans-= happen [p] * happen [p]; 46 happen [p] --; 47 ans + = happen [p] * happen [p]; 48} 49} 50 void modui () 51 {52 int ll = 1, rr = 0; 53 for (int I = 1; I <= m; I ++) 54 {55 for (; q [I]. l <ll; ll --) 56 add (a [ll-1]); 57 for (; q [I]. l> ll; ll ++) 58 dele (a [ll]); 59 for (; q [I]. r <rr; rr --) 60 dele (a [rr]); 61 for (; q [I]. r> rr; rr ++) 62 add (a [rr + 1]); 63 out [q [I]. id] = ans; 64} 65 for (int I = 1; I <= m; I ++) 66 printf ("% d \ n", out [I]); 67} 68 int main () 69 {70 read (n); read (m); read (k); 71 for (int I = 1; I <= n; I ++) 72 read (a [I]); 73 base = sqrt (n); 74 for (int I = 1; I <= n; I ++) 75 pos [I] = (I-1)/base + 1; 76 for (int I = 1; I <= m; I ++) 77 {78 int x, y; 79 read (x); read (y); 80 q [I]. l = x; q [I]. r = y; q [I]. id = I; 81} 82 sort (q + 1, q + m + 1, comp); 83 modui (); 84 return 0; 85}