/ SeriousOJ /

Record Detail

Runtime Error


  
# Status Time Cost Memory Cost
#1 Wrong Answer 1ms 320.0 KiB
#2 Runtime Error terminate called after throwing an instance of 'std::length_error' what(): cannot create std::vector larger than max_size() 2ms 788.0 KiB

Code

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    while(T--) {
        int N;
        cin >> N;
        vector<int> A(N+1);
        for(int i = 1; i <= N; i++) {
            cin >> A[i];
        }

        int Q;
        cin >> Q;
        while(Q--) {
            int l, r;
            cin >> l >> r;
            int k = r - l + 1;

            vector<int> freq(k+1, 0);
            // Count valid elements in [1..k] for subarray A[l..r]
            for(int i = l; i <= r; i++) {
                int val = A[i];
                if(val >= 1 && val <= k) {
                    freq[val]++;
                }
            }

            // Count how many distinct values from 1..k
            int distinctInRange = 0;
            for(int x = 1; x <= k; x++) {
                if(freq[x] > 0) {
                    distinctInRange++;
                }
            }

            // Minimum changes = subarray length - number_of_distinct_valid_elements
            cout << (k - distinctInRange) << "\n";
        }
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1157 Roy and Tree Permutation
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-30 21:43:09
Judged At
2025-01-11 18:15:40
Judged By
Score
0
Total Time
2ms
Peak Memory
788.0 KiB