제출 #458254

#제출 시각아이디문제언어결과실행 시간메모리
458254JovanBPoklon (COCI17_poklon)C++17
140 / 140
453 ms37312 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;

const int N = 500001;

int last[N+5];
int bit[N+5];

void bit_add(int x, int val){
    while(x <= N){
        bit[x] += val;
        x += x & -x;
    }
}

int bit_get(int x){
    int res = 0;
    while(x){
        res += bit[x];
        x -= x & -x;
    }
    return res;
}

vector <pair <int, int>> vec[N+5];
map <int, int> pos;
int sol[N+5];

int main(){
    ios_base::sync_with_stdio(false), cin.tie(0);
    cout.precision(10);
    cout << fixed;

    int n, qrs;
    cin >> n >> qrs;
    for(int i=1; i<=n; i++){
        int x;
        cin >> x;
        last[i] = pos[x];
        pos[x] = i;
    }
    for(int i=1; i<=qrs; i++){
        int l, r;
        cin >> l >> r;
        vec[r].push_back({l, i});
    }
    for(int i=1; i<=n; i++){
        if(last[i]){
            int j = last[i];
            bit_add(j, 1);
            if(last[j]){
                int k = last[j];
                bit_add(k, -2);
                if(last[k]){
                    bit_add(last[k], 1);
                }
            }
        }
        for(auto qu : vec[i]){
            int l = qu.first;
            int ind = qu.second;
            sol[ind] = bit_get(i) - bit_get(l - 1);
        }
    }
    for(int i=1; i<=qrs; i++) cout << sol[i] << "\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...