Submission #726984

# Submission time Handle Problem Language Result Execution time Memory
726984 2023-04-19T18:41:33 Z finn__ Inside information (BOI21_servers) C++17
100 / 100
650 ms 36924 KB
#include <bits/stdc++.h>
using namespace std;

template <typename T, int64_t N>
struct FenwickTree
{
    T t[N];

    void update(int64_t i, T x)
    {
        ++i;
        while (i <= N)
            t[i - 1] += x, i += i & -i;
    }

    T prefix_sum(int64_t i)
    {
        ++i;
        T x = 0;
        while (i)
            x += t[i - 1], i -= i & -i;
        return x;
    }
};

constexpr size_t N = 120021;

vector<pair<unsigned, unsigned>> g[N], q1[N], q2[N];
unsigned subtree_size[N], min_time[N], max_time[N], subtree[N];
vector<pair<unsigned, string>> ans;
uint8_t monotonicity[N];
bitset<N> removed;
FenwickTree<int32_t, 2 * N> tree;

unsigned find_centroid(unsigned u, unsigned p, unsigned n)
{
    subtree_size[u] = 1;
    for (auto const &[v, t] : g[u])
        if (!removed[v] && v != p)
        {
            unsigned x = find_centroid(v, u, n);
            if (x != UINT_MAX)
                return x;
            subtree_size[u] += subtree_size[v];
        }
    return subtree_size[u] > n / 2 ? u : UINT_MAX;
}

void trav(unsigned u, unsigned p, unsigned t0, unsigned min_t, unsigned max_t, uint8_t m, unsigned s)
{
    subtree_size[u] = 1;
    monotonicity[u] = m;
    min_time[u] = min_t;
    max_time[u] = max_t;
    subtree[u] = s;
    for (auto const &[v, t] : g[u])
        if (!removed[v] && v != p)
        {
            trav(v, u, t, min(min_t, t), max(max_t, t), m & (t < t0 ? 1 : 2), s);
            subtree_size[u] += subtree_size[v];
        }
}

void answer_queries(unsigned u, unsigned p)
{
    for (size_t i = 0; i < q1[u].size(); ++i)
    {
        auto [v, t] = q1[u][i];
        if (subtree[v] != subtree[u])
        {
            ans.emplace_back(t, ((monotonicity[u] & 2) && (monotonicity[v] & 1) &&
                                 max_time[v] < min_time[u] && t > max_time[u] &&
                                 t > max_time[v])
                                    ? "yes"
                                    : "no");
            swap(q1[u][i], q1[u].back());
            q1[u].pop_back();
            --i;
        }
    }
    for (auto const &[v, t] : g[u])
        if (!removed[v] && v != p)
            answer_queries(v, u);
}

void collect_nodes_queries(unsigned u, unsigned p, vector<pair<unsigned, pair<unsigned, unsigned> *>> &queries,
                           vector<pair<unsigned, unsigned>> &node_times)
{
    if (monotonicity[u] & 2)
        node_times.emplace_back(min_time[u], max_time[u]);
    if (monotonicity[u] & 1)
        for (size_t i = 0; i < q2[u].size(); ++i)
            if (q2[u][i].first > max_time[u])
                queries.emplace_back(max_time[u], &q2[u][i]);
    for (auto const &[v, t] : g[u])
        if (v != p && !removed[v])
            collect_nodes_queries(v, u, queries, node_times);
}

void decompose(unsigned u, unsigned n)
{
    unsigned const c = find_centroid(u, UINT_MAX, n);
    removed[c] = 1;
    monotonicity[c] = 3;
    max_time[c] = 0;
    min_time[c] = UINT_MAX;
    subtree[c] = UINT_MAX;
    vector<pair<unsigned, pair<unsigned, unsigned> *>> queries;
    vector<pair<unsigned, unsigned>> node_times;

    for (auto const &[v, t] : g[c])
        if (!removed[v])
        {
            trav(v, c, t, t, t, 3, v);
            size_t olen1 = queries.size(), olen2 = node_times.size();
            collect_nodes_queries(v, c, queries, node_times);
            sort(node_times.begin() + olen2, node_times.end(), [](auto const &a, auto const &b)
                 { return a.first > b.first; });
            sort(queries.begin() + olen1, queries.end(), [](auto const &a, auto const &b)
                 { return a.first > b.first; });
            size_t j = olen2;
            for (size_t i = olen1; i < queries.size(); ++i)
            {
                while (j < node_times.size() && node_times[j].first > queries[i].first)
                    tree.update(node_times[j].second, 1), ++j;
                queries[i].second->second -= tree.prefix_sum(queries[i].second->first);
            }
            for (size_t i = olen2; i < j; ++i)
                tree.update(node_times[i].second, -1);
        }

    node_times.emplace_back(UINT_MAX, 0);
    for (size_t i = 0; i < q2[c].size(); ++i)
        queries.emplace_back(0, &q2[c][i]);
    sort(node_times.begin(), node_times.end(), [](auto const &a, auto const &b)
         { return a.first > b.first; });
    sort(queries.begin(), queries.end(), [](auto const &a, auto const &b)
         { return a.first > b.first; });

    size_t j = 0;
    for (size_t i = 0; i < queries.size(); ++i)
    {
        while (j < node_times.size() && node_times[j].first > queries[i].first)
            tree.update(node_times[j].second, 1), ++j;
        queries[i].second->second += tree.prefix_sum(queries[i].second->first);
    }
    for (size_t i = 0; i < j; ++i)
        tree.update(node_times[i].second, -1);

    answer_queries(c, UINT_MAX);
    for (auto const &[v, t] : g[c])
        if (!removed[v])
            decompose(v, subtree_size[v]);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    size_t n, k;
    cin >> n >> k;
    for (size_t i = 1; i <= n + k - 1; ++i)
    {
        char c;
        cin >> c;
        if (c == 'S')
        {
            unsigned u, v;
            cin >> u >> v;
            g[u].emplace_back(v, i);
            g[v].emplace_back(u, i);
        }
        else if (c == 'Q')
        {
            unsigned u, v;
            cin >> u >> v;
            if (u == v)
                ans.emplace_back(i, "yes");
            else
                q1[u].emplace_back(v, i);
        }
        else
        {
            unsigned d;
            cin >> d;
            q2[d].emplace_back(i, 0);
        }
    }
    decompose(1, n);
    for (size_t i = 1; i <= n; ++i)
        for (size_t j = 0; j < q2[i].size(); ++j)
            ans.emplace_back(q2[i][j].first, to_string(q2[i][j].second));
    sort(ans.begin(), ans.end());
    assert(ans.size() == k);
    for (auto const &[t, a] : ans)
        cout << a << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 42 ms 15516 KB Output is correct
2 Correct 54 ms 15924 KB Output is correct
3 Correct 49 ms 15588 KB Output is correct
4 Correct 58 ms 15948 KB Output is correct
5 Correct 81 ms 16088 KB Output is correct
6 Correct 53 ms 15892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 15516 KB Output is correct
2 Correct 54 ms 15924 KB Output is correct
3 Correct 49 ms 15588 KB Output is correct
4 Correct 58 ms 15948 KB Output is correct
5 Correct 81 ms 16088 KB Output is correct
6 Correct 53 ms 15892 KB Output is correct
7 Correct 42 ms 15804 KB Output is correct
8 Correct 62 ms 17580 KB Output is correct
9 Correct 56 ms 17732 KB Output is correct
10 Correct 72 ms 17728 KB Output is correct
11 Correct 67 ms 17444 KB Output is correct
12 Correct 54 ms 17552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 15588 KB Output is correct
2 Correct 179 ms 24444 KB Output is correct
3 Correct 173 ms 24372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 15588 KB Output is correct
2 Correct 179 ms 24444 KB Output is correct
3 Correct 173 ms 24372 KB Output is correct
4 Correct 41 ms 16088 KB Output is correct
5 Correct 172 ms 28404 KB Output is correct
6 Correct 135 ms 26960 KB Output is correct
7 Correct 135 ms 27216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 15516 KB Output is correct
2 Correct 402 ms 30936 KB Output is correct
3 Correct 402 ms 30904 KB Output is correct
4 Correct 244 ms 32172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 15516 KB Output is correct
2 Correct 402 ms 30936 KB Output is correct
3 Correct 402 ms 30904 KB Output is correct
4 Correct 244 ms 32172 KB Output is correct
5 Correct 51 ms 15812 KB Output is correct
6 Correct 389 ms 35520 KB Output is correct
7 Correct 311 ms 36784 KB Output is correct
8 Correct 427 ms 35492 KB Output is correct
9 Correct 471 ms 35264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 15604 KB Output is correct
2 Correct 218 ms 23940 KB Output is correct
3 Correct 286 ms 22444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 15604 KB Output is correct
2 Correct 218 ms 23940 KB Output is correct
3 Correct 286 ms 22444 KB Output is correct
4 Correct 45 ms 15836 KB Output is correct
5 Correct 278 ms 28116 KB Output is correct
6 Correct 427 ms 27036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 15588 KB Output is correct
2 Correct 400 ms 30912 KB Output is correct
3 Correct 397 ms 31048 KB Output is correct
4 Correct 248 ms 32240 KB Output is correct
5 Correct 44 ms 15588 KB Output is correct
6 Correct 214 ms 23844 KB Output is correct
7 Correct 315 ms 22408 KB Output is correct
8 Correct 357 ms 23936 KB Output is correct
9 Correct 380 ms 23428 KB Output is correct
10 Correct 473 ms 27352 KB Output is correct
11 Correct 349 ms 27096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 15588 KB Output is correct
2 Correct 400 ms 30912 KB Output is correct
3 Correct 397 ms 31048 KB Output is correct
4 Correct 248 ms 32240 KB Output is correct
5 Correct 44 ms 15588 KB Output is correct
6 Correct 214 ms 23844 KB Output is correct
7 Correct 315 ms 22408 KB Output is correct
8 Correct 357 ms 23936 KB Output is correct
9 Correct 380 ms 23428 KB Output is correct
10 Correct 473 ms 27352 KB Output is correct
11 Correct 349 ms 27096 KB Output is correct
12 Correct 44 ms 15888 KB Output is correct
13 Correct 451 ms 35572 KB Output is correct
14 Correct 305 ms 36900 KB Output is correct
15 Correct 412 ms 35340 KB Output is correct
16 Correct 441 ms 35308 KB Output is correct
17 Correct 51 ms 16740 KB Output is correct
18 Correct 315 ms 28088 KB Output is correct
19 Correct 342 ms 26944 KB Output is correct
20 Correct 394 ms 27892 KB Output is correct
21 Correct 400 ms 28156 KB Output is correct
22 Correct 423 ms 32140 KB Output is correct
23 Correct 598 ms 32208 KB Output is correct
24 Correct 509 ms 31188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 15468 KB Output is correct
2 Correct 56 ms 15908 KB Output is correct
3 Correct 50 ms 15588 KB Output is correct
4 Correct 57 ms 15992 KB Output is correct
5 Correct 57 ms 16104 KB Output is correct
6 Correct 53 ms 15912 KB Output is correct
7 Correct 39 ms 15588 KB Output is correct
8 Correct 201 ms 24516 KB Output is correct
9 Correct 161 ms 24400 KB Output is correct
10 Correct 48 ms 15556 KB Output is correct
11 Correct 361 ms 30920 KB Output is correct
12 Correct 347 ms 30936 KB Output is correct
13 Correct 220 ms 32180 KB Output is correct
14 Correct 40 ms 15588 KB Output is correct
15 Correct 194 ms 23864 KB Output is correct
16 Correct 310 ms 22544 KB Output is correct
17 Correct 300 ms 23856 KB Output is correct
18 Correct 419 ms 23376 KB Output is correct
19 Correct 332 ms 27264 KB Output is correct
20 Correct 355 ms 27080 KB Output is correct
21 Correct 185 ms 24240 KB Output is correct
22 Correct 193 ms 24184 KB Output is correct
23 Correct 246 ms 23496 KB Output is correct
24 Correct 352 ms 23568 KB Output is correct
25 Correct 411 ms 30808 KB Output is correct
26 Correct 298 ms 21604 KB Output is correct
27 Correct 301 ms 21344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 15468 KB Output is correct
2 Correct 56 ms 15908 KB Output is correct
3 Correct 50 ms 15588 KB Output is correct
4 Correct 57 ms 15992 KB Output is correct
5 Correct 57 ms 16104 KB Output is correct
6 Correct 53 ms 15912 KB Output is correct
7 Correct 39 ms 15588 KB Output is correct
8 Correct 201 ms 24516 KB Output is correct
9 Correct 161 ms 24400 KB Output is correct
10 Correct 48 ms 15556 KB Output is correct
11 Correct 361 ms 30920 KB Output is correct
12 Correct 347 ms 30936 KB Output is correct
13 Correct 220 ms 32180 KB Output is correct
14 Correct 40 ms 15588 KB Output is correct
15 Correct 194 ms 23864 KB Output is correct
16 Correct 310 ms 22544 KB Output is correct
17 Correct 300 ms 23856 KB Output is correct
18 Correct 419 ms 23376 KB Output is correct
19 Correct 332 ms 27264 KB Output is correct
20 Correct 355 ms 27080 KB Output is correct
21 Correct 185 ms 24240 KB Output is correct
22 Correct 193 ms 24184 KB Output is correct
23 Correct 246 ms 23496 KB Output is correct
24 Correct 352 ms 23568 KB Output is correct
25 Correct 411 ms 30808 KB Output is correct
26 Correct 298 ms 21604 KB Output is correct
27 Correct 301 ms 21344 KB Output is correct
28 Correct 43 ms 15920 KB Output is correct
29 Correct 63 ms 17556 KB Output is correct
30 Correct 54 ms 17740 KB Output is correct
31 Correct 65 ms 17688 KB Output is correct
32 Correct 64 ms 17508 KB Output is correct
33 Correct 61 ms 17536 KB Output is correct
34 Correct 42 ms 16848 KB Output is correct
35 Correct 155 ms 28328 KB Output is correct
36 Correct 109 ms 26980 KB Output is correct
37 Correct 147 ms 27176 KB Output is correct
38 Correct 53 ms 16700 KB Output is correct
39 Correct 445 ms 35444 KB Output is correct
40 Correct 339 ms 36924 KB Output is correct
41 Correct 513 ms 35244 KB Output is correct
42 Correct 425 ms 35188 KB Output is correct
43 Correct 44 ms 16752 KB Output is correct
44 Correct 299 ms 28220 KB Output is correct
45 Correct 306 ms 26960 KB Output is correct
46 Correct 403 ms 27908 KB Output is correct
47 Correct 358 ms 28036 KB Output is correct
48 Correct 409 ms 32140 KB Output is correct
49 Correct 650 ms 32248 KB Output is correct
50 Correct 564 ms 31432 KB Output is correct
51 Correct 162 ms 28340 KB Output is correct
52 Correct 179 ms 27932 KB Output is correct
53 Correct 164 ms 26264 KB Output is correct
54 Correct 112 ms 28204 KB Output is correct
55 Correct 120 ms 27904 KB Output is correct
56 Correct 277 ms 28136 KB Output is correct
57 Correct 411 ms 33564 KB Output is correct
58 Correct 463 ms 27856 KB Output is correct
59 Correct 296 ms 26284 KB Output is correct