-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathMo Algorithm Example.cpp
115 lines (91 loc) · 1.81 KB
/
Mo Algorithm Example.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
struct info
{
int l, r, id;
info(){}
info(int l, int r, int id) : l(l), r(r), id(id){}
};
int n, t, a[2*MAX];
info Q[2*MAX];
int Block, cnt[1000004];
ll ans=0;
ll Ans[2*MAX];
// always constant
// Farther improvement: When dividing the elements into blocks, we may sort the first block in the
// ascending order of right borders, the second — in descending, the third — in ascending order again, and so on.
// inline bool comp(const info &a, const info &b)
// {
// if(a.l/blockSZ!=b.l/blockSZ)
// return a.l<b.l;
// if((a.l/blockSZ)&1)
// return a.r<b.r;
// return a.r>b.r;
// }
inline bool comp(const info &a, const info &b)
{
if(a.l/Block==b.l/Block) return a.r<b.r;
return a.l<b.l;
}
inline void Add(int idx)
{
ans+=(2*cnt[a[idx]]+1)*a[idx];
cnt[a[idx]]++;
/* Actual meaning of the above code
ans-=cnt[a[idx]]*cnt[a[idx]]*a[idx];
cnt[a[idx]]++;
ans+=cnt[a[idx]]*cnt[a[idx]]*a[idx];
*/
}
inline void Remove(int idx)
{
ans-=(2*cnt[a[idx]]-1)*a[idx];
cnt[a[idx]]--;
/* Actual meaning of the above code
ans-=cnt[a[idx]]*cnt[a[idx]]*a[idx];
cnt[a[idx]]--;
ans+=cnt[a[idx]]*cnt[a[idx]]*a[idx];
*/
}
int main()
{
// ios_base::sync_with_stdio(0);
// cin.tie(NULL); cout.tie(NULL);
// freopen("in.txt","r",stdin);
// Problem: For each query, find the value cnt[a[i]]*cnt[a[i]]*a[i]
scanf("%d%d", &n, &t);
Block=sqrt(n);
FOR(i,1,n+1) a[i]=getnum();
FOR(i,0,t)
{
Q[i].l=getnum();
Q[i].r=getnum();
Q[i].id=i;
}
sort(Q,Q+t,comp);
int Left=0, Right=-1;
FOR(i,0,t)
{
while(Left<Q[i].l)
{
Remove(Left);
Left++;
}
while(Left>Q[i].l)
{
Left--;
Add(Left);
}
while(Right<Q[i].r)
{
Right++;
Add(Right);
}
while(Right>Q[i].r)
{
Remove(Right);
Right--;
}
Ans[Q[i].id]=ans;
}
FOR(i,0,t) printf("%lld\n", Ans[i]);
return 0;
}