-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mo-offline.cpp
191 lines (158 loc) · 2.54 KB
/
Mo-offline.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <bits/stdc++.h>
using namespace std ;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>
// using namespace __gnu_pbds;
#define ll long long
#define fast_io() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define For(i,a,b) for (ll i = a ; i < b ; i++)
#define Forr(i,a,b) for (ll i = a ; i >= b; i--)
#define F first
#define S second
#define vl vector<ll>
#define pb push_back
#define pll pair<ll,ll>
#define mp make_pair
#define endl '\n'
// template <class T, class cmp = less<T>> using ordered_set = tree<T, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
ll modulo = (ll)1e9 + 7;
const ll N = (ll)1e5+10;
ll fpow(ll a, ll b)
{
ll ans = 1;
while (b)
{
if (b&1) ans = (ans*a)%modulo;
a = (a*a)%modulo;
b = b>>1;
}
return ans;
}
struct node
{
ll l, r;
ll index;
};
ll BLOCK = 320;
bool comparator(node a, node b)
{
ll L1 = (a.l) / BLOCK;
ll L2 = (b.l) / BLOCK;
if (L1 == L2)
{
return (a.r < b.r);
}
return (L1 < L2);
}
ll a[N];
ll FREQ[N];
map<ll,ll> ID;
ll b[N];
void solve ()
{
ll n, m;
cin >> n >> m;
memset(FREQ,0,sizeof(FREQ));
memset(b,0,sizeof(b));
For(i,1,n+1)
{
cin >> a[i];
if (ID.find(a[i]) == ID.end())
{
ID[a[i]] = i;
}
b[i] = a[i];
a[i] = ID[a[i]];
}
node query[m+1];
For(i,1,m+1)
{
cin >> query[i].l >> query[i].r;
query[i].index = i;
}
sort(query+1,query+m+1,comparator);
ll L = 1;
ll R = 1;
ll ans = 0;
ll result[m+1];
FREQ[a[1]] = 1;
if (FREQ[a[1]] == b[1])
{
ans++;
}
For(i,1,m+1)
{
node Q = query[i];
while (Q.l < L)
{
L--;
FREQ[a[L]]++;
if (FREQ[a[L]] == b[L])
{
ans++;
}
else if (FREQ[a[L]] == b[L]+1)
{
ans--;
}
}
while (R < Q.r)
{
R++;
FREQ[a[R]]++;
if (FREQ[a[R]] == b[R])
{
ans++;
}
else if (FREQ[a[R]] == b[R]+1)
{
ans--;
}
}
while (L < Q.l)
{
FREQ[a[L]]--;
if (FREQ[a[L]] == b[L])
{
ans++;
}
else if (FREQ[a[L]]+1 == b[L])
{
ans--;
}
L++;
}
while (R > Q.r)
{
FREQ[a[R]]--;
if (FREQ[a[R]] == b[R])
{
ans++;
}
else if (FREQ[a[R]]+1 == b[R])
{
ans--;
}
R--;
}
result[Q.index] = ans;
}
For(i,1,m+1)
{
cout << result[i] << ' ';
}
cout << endl;
}
signed main ()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin) ;
freopen("output.txt","w",stdout) ;
#endif
fast_io();
int t = 1 ;
// cin >> t ;
while (t--) solve() ;
return 0 ;
}