답안 #458254

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
458254 2021-08-08T03:12:50 Z JovanB Poklon (COCI17_poklon) C++17
140 / 140
453 ms 37312 KB
#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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 12108 KB Output is correct
2 Correct 7 ms 12108 KB Output is correct
3 Correct 8 ms 12144 KB Output is correct
4 Correct 10 ms 12236 KB Output is correct
5 Correct 72 ms 16760 KB Output is correct
6 Correct 75 ms 16780 KB Output is correct
7 Correct 183 ms 22060 KB Output is correct
8 Correct 274 ms 27164 KB Output is correct
9 Correct 340 ms 32324 KB Output is correct
10 Correct 453 ms 37312 KB Output is correct