Submission #650185

# Submission time Handle Problem Language Result Execution time Memory
650185 2022-10-12T16:39:34 Z Elias_Obeid Klasika (COCI20_klasika) C++17
110 / 110
3557 ms 475316 KB
#include <bits/stdc++.h>
using namespace std;

const int32_t MAXN = 200'000;

int32_t timer = 0;
array<vector<pair<int32_t, int32_t>>, MAXN + 1> graph;
array<int32_t, MAXN + 1> xor_depths, time_in, time_out;

void depthFirstSearch(int32_t node, int32_t current_depth)
{
    time_in.at(node) = ++timer;
    xor_depths.at(node) = current_depth;
    for (const auto &[neighbour, weight] : graph.at(node))
    {
        depthFirstSearch(neighbour, current_depth ^ weight);
    }
    time_out.at(node) = timer;
}

const int32_t BITS = 2;
const int32_t MAXB = 30;

struct TrieNode
{
    array<int32_t, BITS> next_nodes;
    set<int32_t> available_positions;

    TrieNode()
    {
        next_nodes.fill(-1);
    }

    bool inRange(int32_t range_start, int32_t range_end)
    {
        auto it = available_positions.lower_bound(range_start);
        return (it != available_positions.end() && *it <= range_end);
    }
};

vector<TrieNode> trie;

int32_t fetchNode()
{
    trie.emplace_back(TrieNode());
    return (int32_t)trie.size() - 1;
}

const int32_t ROOT = fetchNode();

void addNode(int32_t graph_node)
{
    int32_t node = ROOT;
    int32_t value = xor_depths.at(graph_node);
    int32_t position = time_in.at(graph_node);
    for (int32_t bit = MAXB; bit >= 0; --bit)
    {
        int32_t value_bit = (value >> bit & 1);
        if (trie.at(node).next_nodes.at(value_bit) == -1)
        {
            trie.at(node).next_nodes.at(value_bit) = fetchNode();
        }
        node = trie.at(node).next_nodes.at(value_bit);
        trie.at(node).available_positions.insert(position);
    }
}

int32_t getMaximumXOR(int32_t graph_node, int32_t range_start, int32_t range_end)
{
    int32_t answer = 0;
    int32_t node = ROOT;
    int32_t value = xor_depths.at(graph_node);
    for (int32_t bit = MAXB; bit >= 0; --bit)
    {
        if (node == -1)
        {
            return answer;
        }

        int32_t value_bit = (value >> bit & 1);
        if (trie.at(node).next_nodes.at(value_bit ^ 1) != -1 &&
            trie.at(trie.at(node).next_nodes.at(value_bit ^ 1)).inRange(range_start, range_end))
        {
            answer |= (1 << bit);
            node = trie.at(node).next_nodes.at(value_bit ^ 1);
        }
        else
        {
            node = trie.at(node).next_nodes.at(value_bit);
        }
    }
    return answer;
}

int32_t current_graph_node = 1;

int32_t main()
{
    int32_t queries_count;
    cin >> queries_count;

    vector<array<int32_t, 3>> queries;
    for (int32_t query_index = 0; query_index < queries_count; ++query_index)
    {
        string operation;
        cin >> operation;

        if (operation == "Query")
        {
            int32_t starting_node, ending_subtree_node;
            cin >> starting_node >> ending_subtree_node;
            queries.push_back(array<int32_t, 3>{0, starting_node, ending_subtree_node});
        }
        else
        {
            int32_t parent_node, weight;
            cin >> parent_node >> weight;
            graph.at(parent_node).emplace_back(++current_graph_node, weight);
            queries.push_back(array<int32_t, 3>{1, current_graph_node, -1});
        }
    }
    depthFirstSearch(1, 0);

    addNode(1);
    for (int32_t query_index = 0; query_index < queries_count; ++query_index)
    {
        if (queries.at(query_index).at(0) == 1)
        {
            auto [query_type, node, _] = queries.at(query_index);
            addNode(node);
        }
        else
        {
            auto [query_type, starting_node, ending_subtree_node] = queries.at(query_index);
            int32_t range_start = time_in.at(ending_subtree_node);
            int32_t range_end = time_out.at(ending_subtree_node);
            cout << getMaximumXOR(starting_node, range_start, range_end) << "\n";
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5204 KB Output is correct
2 Correct 4 ms 5204 KB Output is correct
3 Correct 4 ms 5332 KB Output is correct
4 Correct 4 ms 5460 KB Output is correct
5 Correct 5 ms 5076 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 3 ms 5332 KB Output is correct
8 Correct 3 ms 5460 KB Output is correct
9 Correct 3 ms 5200 KB Output is correct
10 Correct 3 ms 5332 KB Output is correct
11 Correct 4 ms 5332 KB Output is correct
12 Correct 4 ms 5460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5204 KB Output is correct
2 Correct 4 ms 5204 KB Output is correct
3 Correct 4 ms 5332 KB Output is correct
4 Correct 4 ms 5460 KB Output is correct
5 Correct 5 ms 5076 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 3 ms 5332 KB Output is correct
8 Correct 3 ms 5460 KB Output is correct
9 Correct 3 ms 5200 KB Output is correct
10 Correct 3 ms 5332 KB Output is correct
11 Correct 4 ms 5332 KB Output is correct
12 Correct 4 ms 5460 KB Output is correct
13 Correct 6 ms 6512 KB Output is correct
14 Correct 9 ms 7980 KB Output is correct
15 Correct 9 ms 8360 KB Output is correct
16 Correct 12 ms 11048 KB Output is correct
17 Correct 7 ms 6384 KB Output is correct
18 Correct 9 ms 7980 KB Output is correct
19 Correct 12 ms 8232 KB Output is correct
20 Correct 12 ms 9128 KB Output is correct
21 Correct 9 ms 6480 KB Output is correct
22 Correct 9 ms 7980 KB Output is correct
23 Correct 11 ms 8232 KB Output is correct
24 Correct 11 ms 9100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 768 ms 117184 KB Output is correct
2 Correct 1338 ms 235272 KB Output is correct
3 Correct 1886 ms 289360 KB Output is correct
4 Correct 2481 ms 475196 KB Output is correct
5 Correct 867 ms 117628 KB Output is correct
6 Correct 1424 ms 230600 KB Output is correct
7 Correct 1977 ms 283204 KB Output is correct
8 Correct 2708 ms 465588 KB Output is correct
9 Correct 760 ms 117996 KB Output is correct
10 Correct 1377 ms 231636 KB Output is correct
11 Correct 1815 ms 285500 KB Output is correct
12 Correct 2483 ms 467416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5204 KB Output is correct
2 Correct 4 ms 5204 KB Output is correct
3 Correct 4 ms 5332 KB Output is correct
4 Correct 4 ms 5460 KB Output is correct
5 Correct 5 ms 5076 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 3 ms 5332 KB Output is correct
8 Correct 3 ms 5460 KB Output is correct
9 Correct 3 ms 5200 KB Output is correct
10 Correct 3 ms 5332 KB Output is correct
11 Correct 4 ms 5332 KB Output is correct
12 Correct 4 ms 5460 KB Output is correct
13 Correct 6 ms 6512 KB Output is correct
14 Correct 9 ms 7980 KB Output is correct
15 Correct 9 ms 8360 KB Output is correct
16 Correct 12 ms 11048 KB Output is correct
17 Correct 7 ms 6384 KB Output is correct
18 Correct 9 ms 7980 KB Output is correct
19 Correct 12 ms 8232 KB Output is correct
20 Correct 12 ms 9128 KB Output is correct
21 Correct 9 ms 6480 KB Output is correct
22 Correct 9 ms 7980 KB Output is correct
23 Correct 11 ms 8232 KB Output is correct
24 Correct 11 ms 9100 KB Output is correct
25 Correct 768 ms 117184 KB Output is correct
26 Correct 1338 ms 235272 KB Output is correct
27 Correct 1886 ms 289360 KB Output is correct
28 Correct 2481 ms 475196 KB Output is correct
29 Correct 867 ms 117628 KB Output is correct
30 Correct 1424 ms 230600 KB Output is correct
31 Correct 1977 ms 283204 KB Output is correct
32 Correct 2708 ms 465588 KB Output is correct
33 Correct 760 ms 117996 KB Output is correct
34 Correct 1377 ms 231636 KB Output is correct
35 Correct 1815 ms 285500 KB Output is correct
36 Correct 2483 ms 467416 KB Output is correct
37 Correct 1247 ms 120640 KB Output is correct
38 Correct 1933 ms 235756 KB Output is correct
39 Correct 2619 ms 291872 KB Output is correct
40 Correct 3557 ms 475316 KB Output is correct
41 Correct 1365 ms 118176 KB Output is correct
42 Correct 1981 ms 231008 KB Output is correct
43 Correct 2375 ms 283392 KB Output is correct
44 Correct 2811 ms 466048 KB Output is correct
45 Correct 1349 ms 118740 KB Output is correct
46 Correct 1982 ms 231920 KB Output is correct
47 Correct 2579 ms 284432 KB Output is correct
48 Correct 2951 ms 467424 KB Output is correct