-
Notifications
You must be signed in to change notification settings - Fork 1
/
ADANUM - Ada and Numbering.cc
70 lines (65 loc) · 1.66 KB
/
ADANUM - Ada and Numbering.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int id = 1;
map<int, int> getID;
map<int, int>::iterator it;
int bs;
int passed[200010];
int counter[200010];
int amount[200010];
int A[200010];
LL res[200010];
struct Q {
int l, r, id;
bool operator < (const Q& other) const {
return (l / bs < other.l / bs || (l / bs == other.l / bs && r < other.r));
}//operator <
} q[200010];
int main() {
int N, Q, a, tot, curr, num;
int L = 0, R = -1;
LL sum = 0;
scanf("%d %d", &N, &Q);
bs = sqrt(N);
for (int i = 0; i < N; i++) {
scanf("%d", &a);
A[i] = ((it = getID.find(a)) != getID.end()) ? it->second : (getID[a] = id++);
}//for
for (int i = 0; i < Q; i++) {
scanf("%d %d", &q[i].l, &q[i].r);
q[i].l--; q[i].r--;
q[i].id = i;
}//for
sort(q, q + Q);
for (int i = 0; i < Q; i++) {
while (R < q[i].r) {
counter[amount[A[++R]]]--;
passed[++amount[A[R]]]++;
counter[amount[A[R]]]++;
sum += passed[amount[A[R]]];
}//while
while (R > q[i].r) {
sum -= passed[amount[A[R]]]--;
counter[amount[A[R]]]--;
counter[--amount[A[R]]]++;
R--;
}//while
while (L < q[i].l) {
sum -= passed[amount[A[L]]]--;
counter[amount[A[L]]]--;
counter[--amount[A[L]]]++;
L++;
}//while
while (L > q[i].l) {
counter[amount[A[--L]]]--;
passed[++amount[A[L]]]++;
counter[amount[A[L]]]++;
sum += passed[amount[A[L]]];
}//while
res[q[i].id] = sum;
}//for
for (int i = 0; i < Q; i++)
printf("%lld\n", res[i]);
return 0;
}//main