-
Notifications
You must be signed in to change notification settings - Fork 1
/
MO's Algo (Offline Query).cpp
131 lines (108 loc) · 2.23 KB
/
MO's Algo (Offline Query).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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// LightOJ 1188
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair <ll, ll> pll;
const int Max = 1e6 + 10;
const int Mod = 1e9 + 7;
const ll Inf = 1LL << 62;
int mp[Max];
set<int>st;
int BLOCK_SIZE;
int ans[Max];
int ar[Max];
struct Node // template's Part
{
ll l, r, id;
Node() {};
Node(int l_, int r_, int id_)
{
l = l_;
r = r_;
id = id_;
}
bool operator<(const Node &b) const
{
if(l / BLOCK_SIZE < b.l / BLOCK_SIZE)
{
return 1;
}
if(l / BLOCK_SIZE > b.l / BLOCK_SIZE)
{
return 0;
}
return ((l / BLOCK_SIZE) & 1) ? r < b.r : r > b.r;
}
} qr[Max];
int cnt;
void add(int x) // change according to statement
{
mp[x]++;
if(mp[x]==1)
cnt++;
}
void del(int x) // change according to statement
{
mp[x]--;
if(mp[x]==0)
cnt--;
}
int main()
{
int n, q, l, r, t;
scanf("%d",&t);
for(int cs=1; cs<=t; cs++)
{
//st.clear();
cnt=0;
memset(mp,0,sizeof(mp));
memset(ans,0,sizeof(ans));
memset(ar,0,sizeof(ar));
memset(qr,0,sizeof(qr));
scanf("%d %d", &n, &q);
BLOCK_SIZE = 1000;
for(int i = 1; i <= n; i++)
{
scanf("%d", &ar[i]);
}
for(int i = 1; i <= q; i++)
{
scanf("%d %d", &l, &r);
qr[i] = Node(l, r, i);
}
sort(qr + 1, qr + q + 1);
ll L = 1, R = 0;
for(int i = 1; i <= q; i++) // template's part
{
ll l = qr[i].l;
ll r = qr[i].r;
while(R < r)
{
R++;
add(ar[R]);
}
while(R > r)
{
del(ar[R]);
R--;
}
while(L < l)
{
del(ar[L]);
L++;
}
while(L > l)
{
L--;
add(ar[L]);
}
ans[ qr[i].id ] = cnt;
}
printf("Case %d:\n",cs);
for(int i = 1; i <= q; i++)
{
printf("%d\n", ans[i]);
}
}
return 0;
}