pastebin

Paste #TVD -- näytä pelkkänä tekstinä -- uusi tämän pohjalta

Värjäys: Tyyli: ensimmäinen rivinumero: Tabin korvaus:

 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
#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>

using namespace std;

#define A 500

int n, q;
int a[505050];
unordered_map<int,int> z;
int c;
int h[505050];
int u = 0;
vector<pair<pair<int,int>,pair<int,int>>> f;
int g[505050];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> q;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        if (z[a[i]] == 0) z[a[i]] = ++c;
    }
    for (int i = 1; i <= n; i++) a[i] = z[a[i]];
    h[a[1]] = 1;
    for (int i = 1; i <= q; i++) {
        int l, r;
        cin >> l >> r;
        f.push_back(make_pair(make_pair(l/A,r),make_pair(l,i)));
    }
    int x = 1, y = 1;
    sort(f.begin(),f.end());
    for (int i = 0; i < f.size(); i++) {
        int l = f[i].second.first;
        int r = f[i].first.second;
        int k = f[i].second.second;
        while (x > l) {
            x--;
            if (h[a[x]] == 2) u--;
            h[a[x]]++;
            if (h[a[x]] == 2) u++;
        }
        while (y < r) {
            y++;
            if (h[a[y]] == 2) u--;
            h[a[y]]++;
            if (h[a[y]] == 2) u++;
        }
        while (x < l) {
            if (h[a[x]] == 2) u--;
            h[a[x]]--;
            if (h[a[x]] == 2) u++;
            x++;
        }
        while (y > r) {
            if (h[a[y]] == 2) u--;
            h[a[y]]--;
            if (h[a[y]] == 2) u++;
            y--;
        }
        g[k] = u;
    }
    for (int i = 1; i <= q; i++) cout << g[i] << "\n";
}