This repository has been archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP1494.cpp
executable file
·105 lines (104 loc) · 1.74 KB
/
P1494.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
// Fear cuts deeper than swords.
#include <algorithm>
#include <cmath>
#include <iostream>
#define int long long
using namespace std;
int n, m, bs, chance;
int gcd(int x, int y)
{
if (y == 0)
{
return x;
}
return gcd(y, x % y);
}
struct query
{
int l;
int r;
int id;
bool operator<(const query &b) const
{
if (l / bs != b.l / bs)
{
return l < b.l;
}
return ((l / bs) & 1) ? r < b.r : r > b.r;
}
} q[50007];
struct ans
{
int y;
int x;
void s()
{
if (x != 0)
{
int u = gcd(x, y);
x = x / u;
y = y / u;
return;
}
else
{
return;
}
}
} a[50007];
int c[50007], v[50007];
void widen(int x)
{
chance += v[x];
v[x]++;
}
void narrow(int x)
{
v[x]--;
chance -= v[x];
}
signed main()
{
cin >> n >> m;
bs = sqrt(n);
for (int i = 1; i <= n; i++)
{
cin >> c[i];
}
for (int i = 1; i <= m; i++)
{
cin >> q[i].l >> q[i].r;
q[i].id = i;
}
sort(q + 1, q + m + 1);
for (int i = 1, l = 1, r = 0; i <= m; i++)
{
if (q[i].l == q[i].r)
{
a[q[i].id] = ans{0, 1};
continue;
}
while (l > q[i].l)
{
widen(c[--l]);
}
while (r < q[i].r)
{
widen(c[++r]);
}
while (l < q[i].l)
{
narrow(c[l++]);
}
while (r > q[i].r)
{
narrow(c[r--]);
}
a[q[i].id] = ans{chance, ((r - l + 1) * (r - l)) / 2};
}
for (int i = 1; i <= m; i++)
{
a[i].s();
cout << a[i].y << '/' << a[i].x << endl;
}
}