Submission #246096

#TimeUsernameProblemLanguageResultExecution timeMemory
246096Vladikus004Poklon (COCI17_poklon)C++14
0 / 140
1999 ms13944 KiB
#include <bits/stdc++.h>
#define inf 2e9
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;

const int N = 500000 + 3, sz = 707;
int n, nq, a[N], cnt[N], k2, ans[N], b[N];
pair <pii, int> q[N];
unordered_map <int, int> mp;

bool cmp(pair <pii, int> a, pair <pii, int> b){
    return (a.first.first / sz) < (b.first.first / sz) ||
    ((a.first.first / sz) == (b.first.first / sz) && a.first.second < b.first.second);
}

void add(int x){
    if (cnt[x] == 2) k2--;
    cnt[x]++;
    if (cnt[x] == 2) k2++;
}

void err(int x){
    if (cnt[x] == 2) k2--;
    cnt[x]--;
    if (cnt[x] == 2) k2++;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
    #endif // LOCAL
    cin >> n >> nq;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        b[i] = a[i];
    }
    for (int i = 0; i < nq; i++) {
        cin >> q[i].first.first >> q[i].first.second;
        q[i].second = i;
    }
    sort(q, q + nq, cmp);
    sort(b, b + n);
    int b_sz = unique(b, b + n) - b;
    for (int i = 0; i < b_sz; i++)
        mp[b[i]] = i;
    for (int i = 0; i < n; i++) a[i] = mp[a[i]];
    int l = 0, r = -1;
    for (int i = 0; i < nq; i++){
        while (r < q[i].first.second) add(a[++r]);
        while (r > q[i].first.second) err(a[r--]);
        while (l < q[i].first.first) err(a[l++]);
        while (l > q[i].first.first) add(a[--l]);
        ans[q[i].second] = k2;
    }
    for (int i = 0; i < nq; i++) cout << ans[i] << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...