/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 1.277 MiB
#2 Accepted 2ms 1.277 MiB
#3 Time Exceeded ≥2593ms ≥5.113 MiB
#4 Accepted 2ms 1.277 MiB
#5 Accepted 2ms 1.074 MiB
#6 Time Exceeded ≥2595ms ≥5.121 MiB

Code

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

const int MAXN = 200005;
int N, Q, A[MAXN];
vector<int> answer;

struct Query {
    int l, r, idx;
    bool operator<(const Query &other) const {
        int block_a = l / sqrt(N);
        int block_b = other.l / sqrt(N);
        if (block_a != block_b) return block_a < block_b;
        return (block_a & 1) ? (r < other.r) : (r > other.r);
    }
};

void processQueries(vector<Query>& queries) {
    int freq[MAXN] = {0};
    int current_l = 0, current_r = -1;
    for (auto &query : queries) {
        while (current_r < query.r) {
            current_r++;
            freq[A[current_r]]++;
        }
        while (current_r > query.r) {
            freq[A[current_r]]--;
            current_r--;
        }
        while (current_l < query.l) {
            freq[A[current_l]]--;
            current_l++;
        }
        while (current_l > query.l) {
            current_l--;
            freq[A[current_l]]++;
        }

        int threshold = (query.r - query.l + 1) / 3;
        int min_val = INT_MAX;
        for (int i = current_l; i <= current_r; i++) {
            if (freq[A[i]] > threshold) min_val = min(min_val, A[i]);
        }

        answer[query.idx] = (min_val == INT_MAX) ? -1 : min_val;
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> N >> Q;
    for (int i = 0; i < N; i++) cin >> A[i];

    vector<Query> queries(Q);
    answer.resize(Q);
    for (int i = 0; i < Q; i++) {
        int l, r;
        cin >> l >> r;
        queries[i] = {l - 1, r - 1, i};  // 0-indexed
    }

    sort(queries.begin(), queries.end());
    processQueries(queries);

    for (int i = 0; i < Q; i++) {
        cout << answer[i] << "\n";
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1103 The Secret Garden of Numbers
Language
C++17 (G++ 13.2.0)
Submit At
2024-11-05 16:54:07
Judged At
2024-11-05 16:54:07
Judged By
Score
20
Total Time
≥2595ms
Peak Memory
≥5.121 MiB