Submission #477551

# Submission time Handle Problem Language Result Execution time Memory
477551 2021-10-02T15:25:41 Z 2548631 Poklon (COCI17_poklon) C++17
140 / 140
429 ms 32888 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vii> vvii;

#define fastIO ios::sync_with_stdio(false), cin.tie(NULL)
#define forw(i, l, r) for( int i = (l) ; i < (r) ; i++ )
#define forb(i, r, l) for( int i = (r) ; i >= (l) ; i-- )
#define log2i(x) (64 - __builtin_clzll(1ll * (x)) - 1)
#define numBit(x) (__builtin_popcountll(1ll * (x)))
#define getBit(x, i) ((x) >> (i) & 1)
#define Pi acos(-1.0l)
#define sz(x) int(x.size())
#define mt make_tuple
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define debug(x) cerr << #x << " = " << x << '\n';

const int N = 5e5 + 7;
int n, q;
int a[N], prv[N], T[N], ans[N], pos[N];
vi tmp;
vii event[N];

void upd(int x, int v) {
    if(x == 0) return;
    for(; x <= n; x += -x & x) T[x] += v;
}

int get(int x) {
    int res = 0;
    for(; x > 0; x -= -x & x) res += T[x];
    return res;
}

int main() {
    fastIO;
#ifndef ONLINE_JUDGE
    //freopen("test.inp", "r", stdin);
    //freopen("test.out", "w", stdout);
#endif

    cin >> n >> q;
    forw(i, 1, n + 1) cin >> a[i], tmp.pb(a[i]);
    sort(all(tmp)); tmp.resize(unique(all(tmp)) - tmp.begin());
    forw(i, 1, n + 1) a[i] = lower_bound(all(tmp), a[i]) - tmp.begin();

    forw(i, 0, q) {
        int l, r; cin >> l >> r;
        event[r].pb(mp(l, i));
    }

    forw(i, 1, n + 1) {
        prv[i] = pos[a[i]];
        pos[a[i]] = i;
        if(prv[i] != prv[prv[i]]) {
            if(prv[prv[i]] != prv[prv[prv[i]]]) {
                upd(prv[prv[prv[i]]], 1);
                upd(prv[prv[i]], -1);
            }

            upd(prv[prv[i]], -1);
            upd(prv[i], 1);
        }
        for(auto x:event[i]) {
            ans[x.se] = get(n) - get(x.fi - 1);
        }
    }

    forw(i, 0, q) cout << ans[i] << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11980 KB Output is correct
2 Correct 8 ms 12124 KB Output is correct
3 Correct 8 ms 12128 KB Output is correct
4 Correct 10 ms 12240 KB Output is correct
5 Correct 68 ms 16140 KB Output is correct
6 Correct 68 ms 16212 KB Output is correct
7 Correct 148 ms 20368 KB Output is correct
8 Correct 253 ms 24504 KB Output is correct
9 Correct 341 ms 28568 KB Output is correct
10 Correct 429 ms 32888 KB Output is correct