-
Notifications
You must be signed in to change notification settings - Fork 1
/
D.cpp
104 lines (77 loc) · 1.59 KB
/
D.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
#include <bits/stdc++.h>
using namespace std;
#define block 1000
int ara[100009], ans[100009], answer = 0;
unordered_map <int, int> cnt;
struct query {
int l, r, i;
} qry[100009];
bool compare(query a, query b)
{
if(a.l / block != b.l / block) {
return a.l / block < b.l / block;
}
return a.r < b.r;
}
void del(int indx)
{
cnt[ ara[indx] ]--;
if(cnt[ ara[indx] ] == ara[indx] - 1)
answer--;
if(cnt[ ara[indx] ] == ara[indx])
answer++;
}
void add(int indx)
{
cnt[ ara[indx] ]++;
if(cnt[ ara[indx] ] == ara[indx])
answer++;
if(cnt[ ara[indx] ] == ara[indx] + 1)
answer--;
}
int main()
{
int n, q;
scanf("%d %d", &n, &q);
answer = 0;
for(int i = 0; i < n; i++)
scanf("%d", &ara[i]);
for(int i = 0; i < q; i++)
{
scanf("%d %d", &qry[i].l, &qry[i].r);
qry[i].l--;
qry[i].r--;
qry[i].i = i;
}
sort(qry, qry + q, compare);
int curl = 0, curr = 0;
for(int i = 0; i < q; i++)
{
while(curl < qry[i].l)
{
del(curl);
curl++;
}
while(curl > qry[i].l)
{
add(curl - 1);
curl--;
}
while(curr <= qry[i].r)
{
add(curr);
curr++;
}
while(curr > qry[i].r + 1)
{
del(curr - 1);
curr--;
}
ans[ qry[i].i ] = answer;
}
for(int i = 0; i < q; i++)
printf("%d\n", ans[i]);
for(int i = 0; i <= 100000; i++)
cnt[i] = 0;
return 0;
}