1
- #include < bits/stdc++.h>
1
+ #include < bits/stdc++.h>
2
+
2
3
using namespace std ;
3
- #define FOR (i,a,b ) for (int i=(a); i<=(b); ++i)
4
- #define FORD (i,a,b ) for (int i=(a); i>=(b); --i)
5
- #define TRAV (x,T ) for (auto & (x): (T))
6
- #define ALL (x ) x.begin(), x.end()
7
- #define TAB (x,n ) (x)+1 , (x)+((n)+1 )
8
- #define sz (x ) (int )(x).size()
9
- #define pb push_back
10
- #define pf push_front
11
- #define st first
12
- #define nd second
13
- typedef long long ll;
14
- typedef long double ld;
15
- typedef pair<int , int > pii;
16
- typedef pair<ll, ll> pll;
17
- typedef vector<int > vi;
4
+
18
5
const int N = 1e6 ;
19
6
20
7
int A[N + 2 ], ILE[N + 2 ], ANS[N + 2 ];
21
- struct zap { int x, y, id; } Q[N + 2 ];
8
+ struct zap {
9
+ int x, y, id;
10
+ } Q[N + 2 ];
22
11
23
12
int BL, ans = 0 ;
24
13
int blok (int x) { return x / BL; }
25
14
26
15
void add (int x) {
27
- if (ILE[x] == 0 ) ++ans;
28
- ILE[x]++;
16
+ if (ILE[x] == 0 )
17
+ ++ans;
18
+ ILE[x]++;
29
19
}
30
20
31
21
void remove (int x) {
32
- ILE[x]--;
33
- if (ILE[x] == 0 ) --ans;
22
+ ILE[x]--;
23
+ if (ILE[x] == 0 )
24
+ --ans;
34
25
}
35
26
36
27
int L = 0 , R = 0 ;
37
28
int query (int l, int r) {
38
- while (R < r) {
39
- ++R;
40
- add (A[R]);
41
- }
42
- while (R > r) {
43
- remove (A[R]);
44
- --R;
45
- }
46
- while (L < l - 1 ) {
47
- ++L;
48
- remove (A[L]);
49
- }
50
- while (L >= l) {
51
- add (A[L]);
52
- --L;
53
- }
54
- return ans;
55
- }
56
-
57
- bool cmp (zap a, zap b) {
58
- if (blok (a.x ) != blok (b.x )) return a.x < b.x ;
59
- return a.y < b.y ;
29
+ while (R < r) {
30
+ ++R;
31
+ add (A[R]);
32
+ }
33
+ while (R > r) {
34
+ remove (A[R]);
35
+ --R;
36
+ }
37
+ while (L < l - 1 ) {
38
+ ++L;
39
+ remove (A[L]);
40
+ }
41
+ while (L >= l) {
42
+ add (A[L]);
43
+ --L;
44
+ }
45
+ return ans;
60
46
}
61
47
62
48
signed main () {
63
- ios_base::sync_with_stdio (0 );
64
- cin.tie (0 );
49
+ ios_base::sync_with_stdio (0 );
50
+ cin.tie (0 );
51
+
52
+ // wprowadzanie danych
53
+ int n, m;
54
+ cin >> n >> m;
55
+ for (int i = 1 ; i <= n; i++)
56
+ cin >> A[i];
57
+ for (int i = 1 ; i <= n; i++) {
58
+ cin >> Q[i].x >> Q[i].y ;
59
+ Q[i].id = i;
60
+ }
65
61
66
- // wprowadzanie danych
67
- int n, m;
68
- cin>>n>>m;
69
- FOR (i, 1 , n) cin>>A[i];
70
- FOR (i, 1 , m) {
71
- cin>>Q[i].x >>Q[i].y ;
72
- Q[i].id = i;
73
- }
74
-
75
- // wyliczenie pierwiastka
76
- BL = sqrt (n);
62
+ // wyliczenie pierwiastka
63
+ BL = sqrt (n);
77
64
78
- // sortowanie zapytan po blokach
79
- sort (TAB (Q, m), cmp);
65
+ // sortowanie zapytan po blokach
66
+ sort (Q + 1 , Q + (m + 1 ), [](zap a, zap b) {
67
+ if (blok (a.x ) != blok (b.x ))
68
+ return a.x < b.x ;
69
+ return a.y < b.y ;
70
+ });
80
71
81
- FOR (i, 1 , m) ANS[Q[i].id ] = query (Q[i].x , Q[i].y );
82
- FOR (i, 1 , m) cout<<ANS[i]<<" " ; cout<<" \n " ;
83
- return 0 ;
84
- }
72
+ for (int i = 1 ; i <= m; i++)
73
+ ANS[Q[i].id ] = query (Q[i].x , Q[i].y );
74
+ for (int i = 1 ; i <= m; i++)
75
+ cout << ANS[i] << " " ;
76
+ cout << " \n " ;
77
+ return 0 ;
78
+ }
0 commit comments