Submission #854886

# Submission time Handle Problem Language Result Execution time Memory
854886 2023-09-29T09:16:46 Z tibinyte Floppy (RMI20_floppy) C++14
100 / 100
110 ms 27788 KB
#include <bits/stdc++.h>

using namespace std;

const int lgmax = 18;

struct aint
{
    vector<int> a;
    void resize(int n)
    {
        a = vector<int>(4 * n);
    }
    void update(int node, int left, int right, int pos, int val)
    {
        if (left == right)
        {
            a[node] = val;
            return;
        }
        int mid = (left + right) / 2;
        if (pos <= mid)
        {
            update(2 * node, left, mid, pos, val);
        }
        else
        {
            update(2 * node + 1, mid + 1, right, pos, val);
        }
        a[node] = max(a[2 * node], a[2 * node + 1]);
    }
    int query(int node, int left, int right, int st, int dr)
    {
        if (right < st || left > dr)
        {
            return 0;
        }
        if (st <= left && dr >= right)
        {
            return a[node];
        }
        int mid = (left + right) / 2;
        return max(query(2 * node, left, mid, st, dr), query(2 * node + 1, mid + 1, right, st, dr));
    }
};

#include "floppy.h"

void read_array(int subtask_id, const std::vector<int> &v)
{

    int n = (int)v.size();

    map<int, int> who;
    vector<int> lying = v;
    sort(lying.begin(), lying.end());
    for (auto i : lying)
    {
        if (who.find(i) == who.end())
        {
            int p = (int)who.size();
            who[i] = p;
        }
    }

    vector<int> a(n + 1);

    vector<int> pos(n + 1);

    for (int i = 0; i < n; ++i)
    {
        a[i + 1] = who[v[i]];
        pos[a[i + 1]] = i + 1;
    }

    aint tree;
    tree.resize(n);
    for (int i = 1; i <= n; ++i)
    {
        tree.update(1, 1, n, i, a[i]);
    }

    string bits;

    function<void(int, int)> dfs = [&](int st, int dr)
    {
        if (st > dr)
        {
            return;
        }
        int split = pos[tree.query(1, 1, n, st, dr)];

        if (split != st)
        {
            bits += '1';
        }
        else
        {
            bits += '0';
        }
        if (split != dr)
        {
            bits += '1';
        }
        else
        {
            bits += '0';
        }
        dfs(st, split - 1);
        dfs(split + 1, dr);
    };

    dfs(1, n);

    save_to_floppy(bits);
}

std::vector<int> solve_queries(int subtask_id, int n,
                               const std::string &bits,
                               const std::vector<int> &a, const std::vector<int> &b)
{
    vector<vector<int>> g(n + 1);
    vector<pair<int,int>> children(n+1);
    int p = 1;
    function<void(int)> dfs = [&](int node){
        bool leftchild = bits[2*(node-1)] == '1';
        bool rightchild = bits[2*(node-1)+1]=='1';
        if(leftchild){
            p++;
            children[node].first = p;
            g[node].push_back(p);
            g[p].push_back(node);
            dfs(p);
        }
        if(rightchild){
            p++;
            children[node].second = p;
            g[node].push_back(p);
            g[p].push_back(node);
            dfs(p);
        }
    };
    dfs(1);
    vector<int> label(n+1);
    int cur_label = 0;
    function<void(int)> dfs_label = [&](int node){
        if(children[node].first){
            dfs_label(children[node].first);
        }
        label[node] = ++cur_label;
        if(children[node].second){
            dfs_label(children[node].second);
        }
    };
    dfs_label(1);
    vector<int> qui(n+1);
    for(int i= 1; i <= n; ++i){
        qui[label[i]] = i;
    }

    vector<vector<int>> jump(n+1,vector<int>(lgmax+1));
    vector<int> tin(n+1);
    vector<int> tout(n+1);

    p = 1;

    function<void(int,int)> init_lca = [&](int node, int parent){
        tin[node] = p++;
        jump[node][0] = parent;
        for(int i= 1; i <= lgmax; ++i){
            jump[node][i] = jump[jump[node][i-1]][i-1];
        }
        for(auto i : g[node]){
            if(i!=parent){
                init_lca(i,node);
            }
        }
        tout[node] = p++;
    };

    init_lca(1,1);

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

    function<int(int,int)> lca = [&](int u, int v){
        if(isanc(u,v)){
            return u;
        }
        if(isanc(v,u)){
            return v;
        }
        for(int i= lgmax; i >= 0; --i){
            if(!isanc(jump[u][i],v)){
                u = jump[u][i];
            }
        }
        return jump[u][0];
    };

    vector<int> answers;
    for (int i = 0; i < (int)a.size(); ++i)
    {
        int st = a[i];
        int dr = b[i];
        st++;
        dr++;
        //cout<<st<<' ' << dr << ' ' << label[lca(qui[st],qui[dr])]<<'\n';
        answers.push_back(label[lca(qui[st],qui[dr])] - 1);
    }
    return answers;
}

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 2 ms 828 KB Output is correct
2 Correct 2 ms 820 KB Output is correct
3 Correct 2 ms 820 KB Output is correct
4 Correct 2 ms 824 KB Output is correct
5 Correct 2 ms 828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 5976 KB Output is correct
2 Correct 25 ms 5948 KB Output is correct
3 Correct 24 ms 7592 KB Output is correct
4 Correct 25 ms 6840 KB Output is correct
5 Correct 24 ms 6084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 105 ms 22548 KB Output is correct
2 Correct 110 ms 22616 KB Output is correct
3 Correct 98 ms 27788 KB Output is correct
4 Correct 106 ms 25128 KB Output is correct
5 Correct 104 ms 22596 KB Output is correct