Submission #516437

# Submission time Handle Problem Language Result Execution time Memory
516437 2022-01-21T10:05:24 Z KoD Index (COCI21_index) C++17
110 / 110
303 ms 12716 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 (!high.empty()) {
            que.emplace(m, r, std::move(high));
        }
        if (!low.empty()) {
            que.emplace(l, m, std::move(low));
        }
    }
    for (const int x : ans) {
        std::cout << x << '\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 1 ms 320 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 1 ms 320 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 2 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 58 ms 2612 KB Output is correct
12 Correct 71 ms 2604 KB Output is correct
13 Correct 60 ms 2596 KB Output is correct
14 Correct 61 ms 2716 KB Output is correct
15 Correct 58 ms 2668 KB Output is correct
16 Correct 63 ms 2640 KB Output is correct
17 Correct 58 ms 2656 KB Output is correct
18 Correct 58 ms 2628 KB Output is correct
19 Correct 61 ms 2784 KB Output is correct
20 Correct 63 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 1 ms 320 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 2 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 58 ms 2612 KB Output is correct
12 Correct 71 ms 2604 KB Output is correct
13 Correct 60 ms 2596 KB Output is correct
14 Correct 61 ms 2716 KB Output is correct
15 Correct 58 ms 2668 KB Output is correct
16 Correct 63 ms 2640 KB Output is correct
17 Correct 58 ms 2656 KB Output is correct
18 Correct 58 ms 2628 KB Output is correct
19 Correct 61 ms 2784 KB Output is correct
20 Correct 63 ms 2652 KB Output is correct
21 Correct 284 ms 8504 KB Output is correct
22 Correct 303 ms 11840 KB Output is correct
23 Correct 289 ms 11852 KB Output is correct
24 Correct 286 ms 12668 KB Output is correct
25 Correct 278 ms 11856 KB Output is correct
26 Correct 288 ms 12716 KB Output is correct
27 Correct 301 ms 11844 KB Output is correct
28 Correct 296 ms 11972 KB Output is correct
29 Correct 289 ms 12664 KB Output is correct
30 Correct 284 ms 11832 KB Output is correct