Submission #691784

# Submission time Handle Problem Language Result Execution time Memory
691784 2023-01-31T14:29:31 Z piOOE Prize (CEOI22_prize) C++17
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>

using namespace std;

#define int int64_t

void massert(bool f) {
    while (!f) {
        cout << "hell nah" << endl;
    }
}

constexpr int N = 1e6 + 1;

struct Tree {
    static int tin[N], tout[N], par[N];
    static int jump[20][N];
    int logn;

    void init(vector<vector<int>> adj, int root) {
        int n = adj.size();
        int T = 0;

        function<void(int)> dfs = [&](int v) {
            tin[v] = T++;
            for (int to : adj[v]) {
                par[to] = v;
                dfs(to);
            }
            tout[v] = T;
        };

        dfs(root);

        par[root] = root;

        logn = __lg(n) + 1;

        memcpy(jump[0], par, sizeof(par));

        for (int j = 1; j < logn; ++j) {
            for (int i = 0; i < n; ++i) {
                jump[j][i] = jump[j - 1][jump[j - 1][i]];
            }
        }
    }

    bool isp(int a, int b) {
        return tin[a] <= tin[b] && tout[a] >= tout[b];
    }

    int lca(int a, int b) {
        if (isp(a, b)) {
            return a;
        }

        for (int i = logn - 1; i >= 0; --i) {
            if (!isp(jump[i][a], b)) {
                a = jump[i][a];
            }
        }

        return jump[0][a];
    }
} t1, t2;

vector<int> normalize(int n, int root, vector<vector<pair<int, int>>> g) {
    vector<int> dist(n, -1);
    dist[root] = 0;

    function<void(int)> dfs = [&](int v) {
        for (auto [to, w] : g[v]) {
            if (dist[to] == -1) {
                dist[to] = dist[v] + w;
                massert(dist[to] != -1);
                dfs(to);
            }
        }
    };

    dfs(root);

    return dist;
}

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, k, q, t;
    cin >> n >> k >> q >> t;

    vector<int> p1(n), p2(n);

    for (int i = 0; i < n; ++i) {
        cin >> p1[i];
        p1[i] = max(int(-1), p1[i] - 1);
    }

    for (int i = 0; i < n; ++i) {
        cin >> p2[i];
        p2[i] = max(int(-1), p2[i] - 1);
    }

    for (int i = 1; i <= k; ++i) {
        cout << i << " ";
    }
    cout << endl;

    int root1 = find(p1.begin(), p1.end(), -1) - p1.begin();
    int root2 = find(p2.begin(), p2.end(), -1) - p2.begin();

    vector<vector<int>> adj(n);

    for (int i = 0; i < n; ++i) {
        if (p1[i] != -1) {
            adj[p1[i]].push_back(i);
        }
    }

    t1.init(adj, root1);

    for (int i = 0; i < n; ++i) {
        adj[i].clear();
    }

    for (int i = 0; i < n; ++i) {
        if (p2[i] != -1) {
            adj[p2[i]].push_back(i);
        }
    }

    t2.init(adj, root2);

    vector<pair<int, int>> questions;

    vector<int> any(n, -1);
    vector<bool> used(n);

    function<void(int)> dfs = [&](int v) {
        if (v < k) {
            any[v] = v;
        }
        used[v] = true;

        vector<int> alive;

        for (int to : adj[v]) {
            massert(!used[to]);
            dfs(to);

            if (any[to] != -1) {
                alive.push_back(to);
            }
        }

        if (alive.empty()) {
            return;
        }

        any[v] = any[alive[0]];

        if (alive.size() == 1) {
            if (v < k) {
                questions.emplace_back(any[alive[0]], v);
            }
        } else {
            for (int i = 1; i < alive.size(); ++i) {
                questions.emplace_back(any[alive[0]], any[alive[i]]);
            }
        }
    };

    dfs(root2);

    for (auto [x, y] : questions) {
        cout << "? " << x + 1 << " " << y + 1 << endl;
    }
    cout << "!" << endl;

    vector<vector<pair<int, int>>> g1(n), g2(n);

    int h1 = 0, h2 = 0;

    auto addEdge = [&](int t, int x, int y, int w) {
        (t == 0 ? g1[x] : g2[x]).emplace_back(y, w);
        (t == 0 ? g1[y] : g2[y]).emplace_back(x, -w);
    };

    for (auto [x, y] : questions) {
        int a, b, c, d;
        cin >> a >> b >> c >> d;

        int c1 = t1.lca(x, y);
        int c2 = t2.lca(x, y);

        if (t1.isp(c1, h1)) {
            h1 = c1;
        }
        if (t2.isp(c2, h2)) {
            h2 = c2;
        }

        addEdge(0, c1, x, a);
        addEdge(0, c1, y, b);
        addEdge(1, c2, x, c);
        addEdge(1, c2, y, d);
    }

    auto first = normalize(n, h1, g1);
    auto second = normalize(n, h2, g2);

    vector<pair<int, int>> queries(t);
    for (auto &[a, b] : queries) {
        cin >> a >> b;
        a -= 1, b -= 1;
    }

    for (auto [a, b] : queries) {
        int dist1 = first[a] + first[b] - 2 * first[t1.lca(a, b)];
        int dist2 = second[a] + second[b] - 2 * second[t2.lca(a, b)];

        cout << dist1 << " " << dist2 << '\n';
    }

    return 0;
}

Compilation message

Main.cpp: In lambda function:
Main.cpp:168:31: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  168 |             for (int i = 1; i < alive.size(); ++i) {
      |                             ~~^~~~~~~~~~~~~~
/usr/bin/ld: /tmp/cc940PNb.o: in function `std::_Function_handler<void (long), Tree::init(std::vector<std::vector<long, std::allocator<long> >, std::allocator<std::vector<long, std::allocator<long> > > >, long)::{lambda(long)#1}>::_M_invoke(std::_Any_data const&, long&&)':
Main.cpp:(.text._ZNSt17_Function_handlerIFvlEZN4Tree4initESt6vectorIS2_IlSaIlEESaIS4_EElEUllE_E9_M_invokeERKSt9_Any_dataOl[_ZNSt17_Function_handlerIFvlEZN4Tree4initESt6vectorIS2_IlSaIlEESaIS4_EElEUllE_E9_M_invokeERKSt9_Any_dataOl]+0x39): undefined reference to `Tree::tin'
/usr/bin/ld: Main.cpp:(.text._ZNSt17_Function_handlerIFvlEZN4Tree4initESt6vectorIS2_IlSaIlEESaIS4_EElEUllE_E9_M_invokeERKSt9_Any_dataOl[_ZNSt17_Function_handlerIFvlEZN4Tree4initESt6vectorIS2_IlSaIlEESaIS4_EElEUllE_E9_M_invokeERKSt9_Any_dataOl]+0x61): undefined reference to `Tree::par'
/usr/bin/ld: Main.cpp:(.text._ZNSt17_Function_handlerIFvlEZN4Tree4initESt6vectorIS2_IlSaIlEESaIS4_EElEUllE_E9_M_invokeERKSt9_Any_dataOl[_ZNSt17_Function_handlerIFvlEZN4Tree4initESt6vectorIS2_IlSaIlEESaIS4_EElEUllE_E9_M_invokeERKSt9_Any_dataOl]+0xa3): undefined reference to `Tree::tout'
/usr/bin/ld: /tmp/cc940PNb.o: in function `Tree::init(std::vector<std::vector<long, std::allocator<long> >, std::allocator<std::vector<long, std::allocator<long> > > >, long)':
Main.cpp:(.text._ZN4Tree4initESt6vectorIS0_IlSaIlEESaIS2_EEl[_ZN4Tree4initESt6vectorIS0_IlSaIlEESaIS2_EEl]+0xae): undefined reference to `Tree::par'
/usr/bin/ld: Main.cpp:(.text._ZN4Tree4initESt6vectorIS0_IlSaIlEESaIS2_EEl[_ZN4Tree4initESt6vectorIS0_IlSaIlEESaIS2_EEl]+0xbd): undefined reference to `Tree::jump'
/usr/bin/ld: /tmp/cc940PNb.o: in function `main':
Main.cpp:(.text.startup+0x76e): undefined reference to `Tree::tin'
/usr/bin/ld: Main.cpp:(.text.startup+0x775): undefined reference to `Tree::jump'
/usr/bin/ld: Main.cpp:(.text.startup+0x785): undefined reference to `Tree::tout'
/usr/bin/ld: Main.cpp:(.text.startup+0xe45): undefined reference to `Tree::tin'
/usr/bin/ld: Main.cpp:(.text.startup+0xe51): undefined reference to `Tree::jump'
/usr/bin/ld: Main.cpp:(.text.startup+0xe58): undefined reference to `Tree::tout'
collect2: error: ld returned 1 exit status