Submission #925129

# Submission time Handle Problem Language Result Execution time Memory
925129 2024-02-10T19:52:53 Z Camillus Floppy (RMI20_floppy) C++17
100 / 100
92 ms 24060 KB
#include "bits/stdc++.h"
using namespace std;

#ifndef LOCAL
#include "floppy.h"
#else
void read_array(int subtask_id, const vector<int> &v);
void save_to_floppy(const string &bits);
vector<int> solve_queries(int subtask_id, int N, const string &bits, const vector<int> &a, const vector<int> &b);
#endif


struct segment_tree {
    int n;
    vector<int> tree;

    segment_tree(int n) : n(n) {
        tree.resize(4 * n);
    }

    void set(int i, int v, int x = 0, int lx = 0, int rx = -1) {
        if (rx == -1) {
            rx = n;
        }

        if (rx - lx == 1) {
            tree[x] = v;
            return;
        }

        int mx = (lx + rx) / 2;

        if (i < mx) {
            set(i, v, x * 2 + 1, lx, mx);
        } else {
            set(i, v, x * 2 + 2, mx, rx);
        }

        tree[x] = max(tree[x * 2 + 1], tree[x * 2 + 2]);
    }

    int get(int l, int r, int x = 0, int lx = 0, int rx = -1) const {
        if (rx == -1) {
            rx = n;
        }

        if (l <= lx && rx <= r) {
            return tree[x];
        }

        if (l >= rx || lx >= r) {
            return INT32_MIN;
        }

        return max(
                get(l, r, x * 2 + 1, lx, (lx + rx) / 2),
                get(l, r, x * 2 + 2, (lx + rx) / 2, rx)
        );
    }
};


void read_array(int subtask_id, const vector<int> &v) {
    int n = (int)v.size();

    segment_tree st(n);
    map<int, int> pos;
    for (int i = 0; i < n; i++) {
        st.set(i, v[i]);
        pos[v[i]] = i;
    }

    string s;

    auto dfs = [&](auto &&dfs, int l, int r) -> void {
        int i = pos[st.get(l, r)];

        if (l < i) {
            s += "1";
            dfs(dfs, l, i);
        } else {
            s += "0";
        }

        if (i + 1 < r) {
            s += "1";
            dfs(dfs, i + 1, r);
        } else {
            s += "0";
        }
    };

    dfs(dfs, 0, n);

    save_to_floppy(s);
}

static constexpr int maxn = 100500;

int tin[maxn];
int tout[maxn];
int ord[maxn];
int uord[maxn];
int up[20][maxn];

bool is_parent(int u, int v) {
    return tin[u] <= tin[v] && tout[v] <= tout[u];
}

int lca(int u, int v) {
    if (is_parent(u, v)) {
        return u;
    }

    if (is_parent(v, u)) {
        return v;
    }

    for (int i = 19; i >= 0; i--) {
        if (!is_parent(up[i][u], v)) {
            u = up[i][u];
        }
    }

    return up[0][u];
}

vector<int> solve_queries(int subtask_id, int N, const string &bits, const vector<int> &a, const vector<int> &b) {
    int m = (int)a.size();
    int n = N;

    int timer = 0;
    int __ord = 0;

    string s = bits;
    reverse(s.begin(), s.end());

    auto get_last = [&]() -> char {
        char x = s.back();
        s.pop_back();
        return x;
    };

    auto dfs = [&](auto &&dfs, int p) -> void {
        int u = timer++;
        tin[u] = u;

        if (p == -1) {
            up[0][u] = u;
        } else {
            up[0][u] = p;
        }

        if (get_last() == '1') {
            dfs(dfs, u);
        }

        ord[u] = __ord++;
        uord[ord[u]] = u;

        if (get_last() == '1') {
            dfs(dfs, u);
        }

        tout[u] = timer;
    };

    dfs(dfs, -1);

    for (int u = 0; u < n; u++) {
        for (int i = 1; i < 20; i++) {
            up[i][u] = up[i - 1][up[i - 1][u]];
        }
    }

    auto r = a;

    for (int i = 0; i < m; i++) {
        int l = lca(uord[a[i]], uord[b[i]]);
        r[i] = ord[l];
    }

    return r;
}













#ifdef LOCAL
#define NMAX 100000
#define MMAX 100000

int subtask_id, N, M;
vector<int> v, sorted_v;
vector<int> a, b;
vector<int> correct_answers;

// Print score to stdout and exit.
void score_and_exit(const double pts, const char *verdict) {
    fprintf(stderr, "%s", verdict);
    fprintf(stdout, "%f", pts);
    exit(0);
}

// Contestant sent too many bits.
void too_many_bits() {
    score_and_exit(0, "Too many stored bits!");
}

// Contestant did not send any bits.
void misformatted_stored_bits() {
    score_and_exit(0, "Misformatted stored bits or save_to_floppy not called!");
}

// Contestant did not call the answer function.
void answer_not_provided() {
    score_and_exit(0, "Answer not provided!");
}

// Contestant sent a wrong answer.
void wrong_answer() {
    score_and_exit(0, "Wrong answer to query!");
}

// Contestant sent a correct answer.
void correct_answer() {
    score_and_exit(1, "OK!");
}

void read_test() {
    assert(scanf("%d", &subtask_id) == 1);
    assert(scanf("%d%d", &N, &M) == 2);
    assert(1 <= N && N <= NMAX);
    assert(0 <= M && M <= MMAX);
    v.resize(N);
    for (int i = 0; i < N; ++i) {
        assert(scanf("%d", &v[i]) == 1);
    }

    // Check all values are distinct.
    sorted_v.resize(N);
    map<int, int> pos;
    segment_tree st(N);
    for (int i = 0; i < N; ++i) {
        sorted_v[i] = v[i];
        st.set(i, v[i]);
        pos[v[i]] = i;
    }
    sort(sorted_v.begin(), sorted_v.end());
    for (int i = 0; i + 1 < N; ++i) {
        assert(sorted_v[i] < sorted_v[i + 1]);
    }

    a.resize(M);
    b.resize(M);
    correct_answers.resize(M);
    for (int i = 0; i < M; i++) {
        correct_answers[i] = pos[st.get(a[i], b[i] + 1)];
    }
    for (int i = 0; i < M; ++i) {
        assert(0 <= a[i] && a[i] <= correct_answers[i] &&
               correct_answers[i] <= b[i] && b[i] < N);
    }
}

void save_to_floppy(const string &bits) {
    vector<int> contestant_answers = solve_queries(subtask_id, N, bits, a, b);
    for (int i = 0; i < M; ++i) {
        if (contestant_answers[i] != correct_answers[i]) {
            wrong_answer();
        }
    }
    correct_answer();
    exit(0);
}

int main(int argc, char **argv) {
    // Read input data.
    read_test();

    // Send subtask_id, v.
    read_array(subtask_id, v);

    answer_not_provided();
    return 0;
}
#endif

Compilation message

stub.cpp: In function 'void run2()':
stub.cpp:101:30: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  101 |     if (query_answers.size() != M) {
      |         ~~~~~~~~~~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9000 KB Output is correct
2 Correct 2 ms 9000 KB Output is correct
3 Correct 2 ms 9008 KB Output is correct
4 Correct 2 ms 9008 KB Output is correct
5 Correct 2 ms 9012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 11584 KB Output is correct
2 Correct 23 ms 12220 KB Output is correct
3 Correct 20 ms 12472 KB Output is correct
4 Correct 20 ms 12732 KB Output is correct
5 Correct 25 ms 12212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 92 ms 19108 KB Output is correct
2 Correct 90 ms 22596 KB Output is correct
3 Correct 80 ms 23984 KB Output is correct
4 Correct 84 ms 24060 KB Output is correct
5 Correct 91 ms 22676 KB Output is correct