[BZOJ-2038]小Z的袜子

Description

读入n个数,询问区间[L,R]之间取两个数相同的概率为多少。

Solution

这是一道[国家集训队2009]的题。
使用莫队即可。

Notice

注意要开long long。

Code

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
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define Rep(i, a, b) for (int i = (a); i < (b); i++)
#define travel(i, u) for (int i = head[u]; ~i; i = edge[i].next)

const ll INF = 1e9, Mo = 998244353;
const int N = 50000;
const double eps = 1e-6;
namespace slow_IO
{
ll read()
{
ll x = 0; int zf = 1; char ch = getchar();
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * zf;
}
void write(ll y)
{
if (y < 0) putchar('-'), y = -y;
if (y > 9) write(y / 10);
putchar(y % 10 + '0');
}
}
using namespace slow_IO;

struct node
{
int l, r, id;
}Q[N + 5];
int Belong[N + 5], X[N + 5], T[N + 5];
ll Ansx[N + 5], Ansy[N + 5];
ll ans = 0;
int cmp(node X, node Y)
{
return Belong[X.l] == Belong[Y.l] ? X.r < Y.r : Belong[X.l] < Belong[Y.l];
}
ll gcd(ll a, ll b)
{
return b ? gcd(b, a % b) : a;
}

void Insert(int pos)
{
ans += T[X[pos]];
T[X[pos]]++;
}
void Delete(int pos)
{
T[X[pos]]--;
ans -= T[X[pos]];
}

int sqz()
{
int n = read(), m = read(), Size = sqrt(n);
rep(i, 1, n) X[i] = read(), Belong[i] = (i - 1) / Size + 1;
rep(i, 1, m) Q[i].l = read(), Q[i].r = read(), Q[i].id = i;
sort(Q + 1, Q + m + 1, cmp);
int L = 1, R = 0;
rep(i, 1, m)
{
while (L < Q[i].l) Delete(L++);
while (L > Q[i].l) Insert(--L);
while (R > Q[i].r) Delete(R--);
while (R < Q[i].r) Insert(++R);
Ansx[Q[i].id] = ans, Ansy[Q[i].id] = (ll)(Q[i].r - Q[i].l + 1) * (Q[i].r - Q[i].l) / 2;
}
rep(i, 1, m)
{
ll g = gcd(Ansx[i], Ansy[i]);
printf("%lld/%lld\n", Ansx[i] / g, Ansy[i] / g);
}
return 0;
}
文章目录
  1. 1. Description
  2. 2. Solution
  3. 3. Notice
  4. 4. Code