Submission #246099

# Submission time Handle Problem Language Result Execution time Memory
246099 2020-07-08T07:27:58 Z Vladikus004 Poklon (COCI17_poklon) C++14
140 / 140
1948 ms 13304 KB
#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].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 time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 6 ms 384 KB Output is correct
4 Correct 12 ms 512 KB Output is correct
5 Correct 192 ms 2880 KB Output is correct
6 Correct 195 ms 2936 KB Output is correct
7 Correct 497 ms 5496 KB Output is correct
8 Correct 892 ms 8312 KB Output is correct
9 Correct 1374 ms 10616 KB Output is correct
10 Correct 1948 ms 13304 KB Output is correct