Submission #699648

#TimeUsernameProblemLanguageResultExecution timeMemory
699648finn__Ball Machine (BOI13_ballmachine)C++17
66.29 / 100
183 ms26812 KiB
#include <bits/stdc++.h>
using namespace std;

#define N 100000

vector<unsigned> g[N], anc[N];
unsigned min_child[N], postorder[N];
bitset<N> has_ball;

unsigned calc_min_child(unsigned u)
{
    min_child[u] = u;
    for (unsigned const &v : g[u])
        min_child[u] = min(min_child[u], calc_min_child(v));
    return min_child[u];
}

unsigned traverse(unsigned u, vector<unsigned> &path, unsigned i = 0)
{
    for (size_t j = 1; j <= path.size(); j <<= 1)
        anc[u].push_back(path[path.size() - j]);
    path.push_back(u);

    for (unsigned const &v : g[u])
        i = traverse(v, path, i);

    path.pop_back();
    postorder[u] = i++;
    return i;
}

bool postorder_compare(unsigned a, unsigned b) { return postorder[a] > postorder[b]; }

int main()
{
    size_t n, q;
    scanf("%zu %zu", &n, &q);
    unsigned r;

    for (size_t i = 0; i < n; i++)
    {
        unsigned p;
        scanf("%u", &p);
        if (!p)
            r = i;
        else
            g[p - 1].push_back(i);
    }

    calc_min_child(r);
    for (size_t i = 0; i < n; i++)
        sort(g[i].begin(), g[i].end(), [](unsigned a, unsigned b)
             { return min_child[a] < min_child[b]; });

    vector<unsigned> path;
    traverse(r, path);

    priority_queue<unsigned, vector<unsigned>, decltype(&postorder_compare)> qu(&postorder_compare);
    for (unsigned i = 0; i < n; i++)
        qu.push(i);

    while (q--)
    {
        unsigned t, x;
        scanf("%u %u", &t, &x);

        if (t == 1)
        {
            while (--x)
                has_ball[qu.top()] = 1, qu.pop();
            printf("%u\n", qu.top() + 1);
            has_ball[qu.top()] = 1;
            qu.pop();
        }
        else
        {
            x--;
            unsigned h = 0;

            for (size_t i = anc[x].size() - 1; i < anc[x].size(); i--)
                if (has_ball[anc[x][i]])
                    x = anc[x][i], h += 1U << i;

            has_ball[x] = 0;
            qu.push(x);
            printf("%u\n", h);
        }
    }
}

Compilation message (stderr)

ballmachine.cpp: In function 'int main()':
ballmachine.cpp:37:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |     scanf("%zu %zu", &n, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~
ballmachine.cpp:43:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   43 |         scanf("%u", &p);
      |         ~~~~~^~~~~~~~~~
ballmachine.cpp:65:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |         scanf("%u %u", &t, &x);
      |         ~~~~~^~~~~~~~~~~~~~~~~
ballmachine.cpp:56:13: warning: 'r' may be used uninitialized in this function [-Wmaybe-uninitialized]
   56 |     traverse(r, path);
      |     ~~~~~~~~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...