답안 #516421

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
516421 2022-01-21T09:51:10 Z KoD Index (COCI21_index) C++17
60 / 110
2500 ms 11812 KB
#include <bits/stdc++.h>

using std::vector;
using std::array;
using std::pair;
using std::tuple;

struct Fenwick {
    int size;
    vector<int> data;
    Fenwick(const int n) : size(n), data(n + 1) {}
    void add(int i, const int x) {
        i += 1;
        while (i <= size) {
            data[i] += x;
            i += i & -i;
        }
    }
    int pref(int i) const {
        int ret = 0;
        while (i > 0) {
            ret += data[i];
            i -= i & -i;
        }
        return ret;
    }
    int fold(int l, int r) const {
        return pref(r) - pref(l);
    }
    void clear() {
        std::fill(data.begin(), data.end(), 0);
    }
};

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int N, Q;
    std::cin >> N >> Q;
    vector<int> A(N);
    for (auto& x : A) {
        std::cin >> x;
    }
    vector<int> ord(N);
    std::iota(ord.begin(), ord.end(), 0);
    std::sort(ord.begin(), ord.end(), [&](const int i, const int j) {
        return A[i] > A[j];
    });
    vector<int> L(Q), R(Q);
    for (int i = 0; i < Q; ++i) {
        std::cin >> L[i] >> R[i];
        L[i] -= 1;
    }
    Fenwick fen(N);
    int idx = 0;
    const auto prepare = [&](const int x) {
        if (idx > 0 and A[ord[idx - 1]] < x) {
            idx = 0;
            fen.clear();
        }
        while (idx < N and A[ord[idx]] >= x) {
            fen.add(ord[idx], 1);
            idx += 1;
        }
    };
    std::queue<tuple<int, int, vector<int>>> que;
    vector<int> all(Q);
    std::iota(all.begin(), all.end(), 0);
    que.emplace(0, N + 1, std::move(all));
    vector<int> ans(Q);
    while (!que.empty()) {
        const auto [l, r, vec] = std::move(que.front());
        que.pop();
        if (r - l == 1) {
            for (const int i : vec) {
                ans[i] = l;
            }
            continue;
        }
        const int m = (l + r) / 2;
        prepare(m);
        vector<int> low, high;
        for (const int i : vec) {
            (fen.fold(L[i], R[i]) >= m ? high : low).push_back(i);
        }
        if (!low.empty()) {
            que.emplace(l, m, std::move(low));
        }
        if (!high.empty()) {
            que.emplace(m, r, std::move(high));
        }
    }
    for (const int x : ans) {
        std::cout << x << '\n';
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 1 ms 312 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 1 ms 312 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1489 ms 3120 KB Output is correct
12 Correct 1474 ms 3068 KB Output is correct
13 Correct 1525 ms 3072 KB Output is correct
14 Correct 1500 ms 3160 KB Output is correct
15 Correct 1514 ms 3168 KB Output is correct
16 Correct 1507 ms 3088 KB Output is correct
17 Correct 1517 ms 3064 KB Output is correct
18 Correct 1544 ms 3056 KB Output is correct
19 Correct 1559 ms 3124 KB Output is correct
20 Correct 1523 ms 3068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 1 ms 312 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1489 ms 3120 KB Output is correct
12 Correct 1474 ms 3068 KB Output is correct
13 Correct 1525 ms 3072 KB Output is correct
14 Correct 1500 ms 3160 KB Output is correct
15 Correct 1514 ms 3168 KB Output is correct
16 Correct 1507 ms 3088 KB Output is correct
17 Correct 1517 ms 3064 KB Output is correct
18 Correct 1544 ms 3056 KB Output is correct
19 Correct 1559 ms 3124 KB Output is correct
20 Correct 1523 ms 3068 KB Output is correct
21 Execution timed out 2575 ms 11812 KB Time limit exceeded
22 Halted 0 ms 0 KB -