Submission #708668

# Submission time Handle Problem Language Result Execution time Memory
708668 2023-03-12T06:59:18 Z becaido Prize (CEOI22_prize) C++17
0 / 100
1733 ms 318180 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;

#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout (T t, U...u) {cout << t << (sizeof... (u) ? ", " : ""), dout (u...);}
#else
#define debug(...) 7122
#endif

#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second

const int SIZE = 1e6 + 5;
const int H = __lg(SIZE) + 1;

int n, k, q, t;
int dep[SIZE], ans[SIZE];

struct Tree {
    vector<int> adj[SIZE];
    int root, to[SIZE][H + 1], h[SIZE];
    void init() {
        FOR (i, 1, n) {
            int pa;
            cin >> pa;
            if (pa != -1) adj[pa].pb(i);
            else root = i;
        }
    }
    void dfs(int pos) {
        for (int np : adj[pos]) {
            h[np] = h[pos] + 1;
            to[np][0] = pos;
            dfs(np);
        }
    }
    void build() {
        h[root] = 1;
        dfs(root);
        FOR (j, 1, H) FOR (i, 1, n) to[i][j] = to[to[i][j - 1]][j - 1];
    }
    int jump(int pos, int k) {
        int cnt = 0;
        while (k) {
            if (k & 1) pos = to[pos][cnt];
            cnt++;
            k >>= 1;
        }
        return pos;
    }
    int lca(int a, int b) {
        if (h[a] < h[b]) swap(a, b);
        a = jump(a, h[a] - h[b]);
        if (a == b) return a;
        for (int i = H; i >= 0; i--) if (to[a][i] != to[b][i]) {
            a = to[a][i];
            b = to[b][i];
        }
        return to[a][0];
    }
} tree1, tree2;

void solve() {
    cin >> n >> k >> q >> t;
    tree1.init();
    tree2.init();
    tree1.build();
    tree2.build();

    vector<int> V;
    auto dfs = [&](auto dfs, int pos)->void {
        if (V.size() == k) return;
        V.pb(pos);
        for (int np : tree1.adj[pos]) dfs(dfs, np);
    };
    dfs(dfs, tree1.root);
    for (int x : V) {
        cout << x;
        if (x == V.back()) cout << endl;
        else cout << ' ';
    }
    FOR (i, 1, k - 1) cout << "? " << tree1.root << ' ' << V[i] << endl;
    cout << "!" << endl;
    FOR (i, 1, k - 1) {
        int foo;
        cin >> foo >> dep[V[i]] >> foo >> foo;
    }

    FOR (i, 1, t) {
        int a, b;
        cin >> a >> b;
        int c = tree1.lca(a, b);
        ans[i] = dep[a] - dep[c] + dep[b] - dep[c];
    }
    FOR (i, 1, t) cout << ans[i] << endl;
}

int main() {
    Waimai;
    solve();
}

Compilation message

Main.cpp: In instantiation of 'solve()::<lambda(auto:23, int)> [with auto:23 = solve()::<lambda(auto:23, int)>]':
Main.cpp:85:24:   required from here
Main.cpp:81:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   81 |         if (V.size() == k) return;
      |             ~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Runtime error 941 ms 183140 KB Execution killed with signal 13
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 905 ms 183328 KB Execution killed with signal 13
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 746 ms 181120 KB Execution killed with signal 13
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1644 ms 316328 KB Execution killed with signal 13
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1733 ms 318180 KB Execution killed with signal 13
2 Halted 0 ms 0 KB -