Submission #590672

# Submission time Handle Problem Language Result Execution time Memory
590672 2022-07-06T08:34:06 Z FedShat Birthday gift (IZhO18_treearray) C++17
56 / 100
4000 ms 148736 KB
#include <bits/stdc++.h>

using namespace std;
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
using ll = long long;
constexpr int INF = (numeric_limits<int>::max()) / 2;
constexpr ll INFLL = (numeric_limits<ll>::max()) / 2;

template<class T>
istream &operator>>(istream &is, vector<T> &a) {
    for (auto &i: a) {
        is >> i;
    }
    return is;
}

#ifdef __APPLE__
#include "debug.h"
#else
#define debug(...) 42
#endif

struct lca {
    vector<vector<int>> g;
    vector<int> depth;
    vector<int> tin, tout;
    vector<pair<int, int>> tour;
    static constexpr int k = 20;
    vector<vector<pair<int, int>>> st;
    vector<int> prec;

    void dfs(int v, int p) {
        if (p != -1) {
            depth[v] = depth[p] + 1;
        }
        tin[v] = tour.size();
        tour.push_back({depth[v], v});
        for (auto u: g[v]) {
            if (u != p) {
                dfs(u, v);
                tour.push_back({depth[v], v});
            }
        }
        tour.push_back({depth[v], v});
        tout[v] = tour.size();
    }

    int getmin(int l, int r) {
        int len = prec[r - l];
        return min(st[len][l], st[len][r - (1 << len)]).second;
    }

    bool anc(int u, int v) {
        return tin[u] <= tin[v] && tout[v] <= tout[u];
    }

    int LCA(int u, int v) {
        if (anc(u, v)) {
            return u;
        }
        if (anc(v, u)) {
            return v;
        }
        if (tin[u] > tout[v]) {
            swap(u, v);
        }
        return getmin(tin[u], tout[v]);
    }

    lca(vector<vector<int>> g) : g(g) {
        int n = g.size();
        depth.resize(n);
        tin.resize(n);
        tout.resize(n);
        dfs(0, -1);
        int m = tour.size();
        prec.resize(m + 1);
        for (int i = 2; i <= m; ++i) {
            prec[i] = prec[i / 2] + 1;
        }
        st.resize(k, vector<pair<int, int>>(m, {INF, -1}));
        for (int i = 0; i < m; ++i) {
            st[0][i] = tour[i];
        }
        for (int i = 1; i < k; ++i) {
            for (int j = 0; j < m; ++j) {
                st[i][j] = min(st[i - 1][j], st[i - 1][min(m - 1, j + (1 << (i - 1)))]);
            }
        }
    }
};

int main() {
    cin.tie(0)->sync_with_stdio(0);
#ifdef __APPLE__
    freopen("input.txt", "r", stdin);
#endif
    int n, m, q;
    cin >> n >> m >> q;
    vector<vector<int>> g(n);
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        --u;
        --v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    vector<int> a(m);
    cin >> a;
    for (auto &i: a) {
        --i;
    }
    vector<int> ap(m - 1);
    lca t(g);
    for (int i = 0; i + 1 < m; ++i) {
        ap[i] = t.LCA(a[i], a[i + 1]);
    }
    debug(ap);
    debug(t.tour);
    auto calc = [&](int pos) {
        if (pos >= 0 && pos + 1 < m) {
            ap[pos] = t.LCA(a[pos], a[pos + 1]);
        }
    };
    while (q--) {
        int tt;
        cin >> tt;
        if (tt == 1) {
            int pos, v;
            cin >> pos >> v;
            --pos;
            --v;
            a[pos] = v;
            // for (int i = 0; i + 1 < m; ++i) {
            //     ap[i] = t.LCA(a[i], a[i + 1]);
            // }
            calc(pos);
            calc(pos - 1);
        } else {
            int l, r, v;
            cin >> l >> r >> v;
            --l;// ap[l] is lca of (a[l], a[l + 1])
            --r;// ap[r - 1] is lca of (a[r - 2], a[r - 1])
            --v;
            bool ans = false;
            for (int i = l; i <= r; ++i) {
                if (a[i] == v) {
                    cout << i + 1 << " " << i + 1 << "\n";
                    ans = true;
                    break;
                }
            }
            for (int i = l; i < r; ++i) {
                if (ap[i] == v && !ans) {
                    cout << i + 1 << " " << i + 2 << "\n";
                    ans = true;
                    break;
                }
            }
            if (!ans) {
                cout << "-1 -1\n";
            }
        }
    }
}

Compilation message

treearray.cpp: In function 'int main()':
treearray.cpp:21:20: warning: statement has no effect [-Wunused-value]
   21 | #define debug(...) 42
      |                    ^~
treearray.cpp:120:5: note: in expansion of macro 'debug'
  120 |     debug(ap);
      |     ^~~~~
treearray.cpp:21:20: warning: statement has no effect [-Wunused-value]
   21 | #define debug(...) 42
      |                    ^~
treearray.cpp:121:5: note: in expansion of macro 'debug'
  121 |     debug(t.tour);
      |     ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB n=5
2 Correct 0 ms 340 KB n=100
3 Correct 0 ms 320 KB n=100
4 Correct 1 ms 340 KB n=100
5 Correct 1 ms 340 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 320 KB n=100
8 Correct 1 ms 340 KB n=100
9 Correct 1 ms 340 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 0 ms 340 KB n=100
12 Correct 1 ms 324 KB n=100
13 Correct 0 ms 340 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 0 ms 340 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 0 ms 340 KB n=100
18 Correct 1 ms 340 KB n=100
19 Correct 1 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 0 ms 340 KB n=100
22 Correct 1 ms 324 KB n=100
23 Correct 1 ms 328 KB n=100
24 Correct 1 ms 340 KB n=100
25 Correct 1 ms 328 KB n=100
26 Correct 0 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB n=5
2 Correct 0 ms 340 KB n=100
3 Correct 0 ms 320 KB n=100
4 Correct 1 ms 340 KB n=100
5 Correct 1 ms 340 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 320 KB n=100
8 Correct 1 ms 340 KB n=100
9 Correct 1 ms 340 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 0 ms 340 KB n=100
12 Correct 1 ms 324 KB n=100
13 Correct 0 ms 340 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 0 ms 340 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 0 ms 340 KB n=100
18 Correct 1 ms 340 KB n=100
19 Correct 1 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 0 ms 340 KB n=100
22 Correct 1 ms 324 KB n=100
23 Correct 1 ms 328 KB n=100
24 Correct 1 ms 340 KB n=100
25 Correct 1 ms 328 KB n=100
26 Correct 0 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
28 Correct 1 ms 624 KB n=500
29 Correct 1 ms 724 KB n=500
30 Correct 1 ms 716 KB n=500
31 Correct 2 ms 752 KB n=500
32 Correct 2 ms 588 KB n=500
33 Correct 2 ms 724 KB n=500
34 Correct 1 ms 596 KB n=500
35 Correct 2 ms 724 KB n=500
36 Correct 1 ms 596 KB n=500
37 Correct 1 ms 596 KB n=500
38 Correct 2 ms 592 KB n=500
39 Correct 1 ms 596 KB n=500
40 Correct 1 ms 596 KB n=500
41 Correct 1 ms 596 KB n=500
42 Correct 2 ms 596 KB n=500
43 Correct 1 ms 596 KB n=500
44 Correct 2 ms 596 KB n=500
45 Correct 2 ms 596 KB n=500
46 Correct 1 ms 724 KB n=500
47 Correct 1 ms 624 KB n=500
48 Correct 1 ms 596 KB n=500
49 Correct 1 ms 724 KB n=500
50 Correct 1 ms 588 KB n=500
51 Correct 1 ms 724 KB n=500
52 Correct 1 ms 724 KB n=500
53 Correct 1 ms 724 KB n=500
54 Correct 1 ms 724 KB n=500
55 Correct 1 ms 452 KB n=278
56 Correct 1 ms 724 KB n=500
57 Correct 1 ms 724 KB n=500
58 Correct 1 ms 596 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB n=5
2 Correct 0 ms 340 KB n=100
3 Correct 0 ms 320 KB n=100
4 Correct 1 ms 340 KB n=100
5 Correct 1 ms 340 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 320 KB n=100
8 Correct 1 ms 340 KB n=100
9 Correct 1 ms 340 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 0 ms 340 KB n=100
12 Correct 1 ms 324 KB n=100
13 Correct 0 ms 340 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 0 ms 340 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 0 ms 340 KB n=100
18 Correct 1 ms 340 KB n=100
19 Correct 1 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 0 ms 340 KB n=100
22 Correct 1 ms 324 KB n=100
23 Correct 1 ms 328 KB n=100
24 Correct 1 ms 340 KB n=100
25 Correct 1 ms 328 KB n=100
26 Correct 0 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
28 Correct 1 ms 624 KB n=500
29 Correct 1 ms 724 KB n=500
30 Correct 1 ms 716 KB n=500
31 Correct 2 ms 752 KB n=500
32 Correct 2 ms 588 KB n=500
33 Correct 2 ms 724 KB n=500
34 Correct 1 ms 596 KB n=500
35 Correct 2 ms 724 KB n=500
36 Correct 1 ms 596 KB n=500
37 Correct 1 ms 596 KB n=500
38 Correct 2 ms 592 KB n=500
39 Correct 1 ms 596 KB n=500
40 Correct 1 ms 596 KB n=500
41 Correct 1 ms 596 KB n=500
42 Correct 2 ms 596 KB n=500
43 Correct 1 ms 596 KB n=500
44 Correct 2 ms 596 KB n=500
45 Correct 2 ms 596 KB n=500
46 Correct 1 ms 724 KB n=500
47 Correct 1 ms 624 KB n=500
48 Correct 1 ms 596 KB n=500
49 Correct 1 ms 724 KB n=500
50 Correct 1 ms 588 KB n=500
51 Correct 1 ms 724 KB n=500
52 Correct 1 ms 724 KB n=500
53 Correct 1 ms 724 KB n=500
54 Correct 1 ms 724 KB n=500
55 Correct 1 ms 452 KB n=278
56 Correct 1 ms 724 KB n=500
57 Correct 1 ms 724 KB n=500
58 Correct 1 ms 596 KB n=500
59 Correct 3 ms 1876 KB n=2000
60 Correct 4 ms 2132 KB n=2000
61 Correct 3 ms 1876 KB n=2000
62 Correct 3 ms 1876 KB n=2000
63 Correct 3 ms 1748 KB n=2000
64 Correct 4 ms 1872 KB n=2000
65 Correct 3 ms 1876 KB n=2000
66 Correct 4 ms 2004 KB n=2000
67 Correct 4 ms 1744 KB n=2000
68 Correct 6 ms 1928 KB n=2000
69 Correct 8 ms 1852 KB n=2000
70 Correct 7 ms 1876 KB n=2000
71 Correct 7 ms 1748 KB n=2000
72 Correct 4 ms 1748 KB n=2000
73 Correct 4 ms 1748 KB n=2000
74 Correct 3 ms 1748 KB n=1844
75 Correct 4 ms 1748 KB n=2000
76 Correct 5 ms 1748 KB n=2000
77 Correct 5 ms 1748 KB n=2000
78 Correct 5 ms 1852 KB n=2000
79 Correct 3 ms 1748 KB n=2000
80 Correct 4 ms 2004 KB n=2000
81 Correct 3 ms 1876 KB n=2000
82 Correct 4 ms 1748 KB n=2000
83 Correct 3 ms 1876 KB n=2000
84 Correct 3 ms 1748 KB n=2000
85 Correct 4 ms 1876 KB n=2000
86 Correct 4 ms 1876 KB n=2000
87 Correct 4 ms 1748 KB n=2000
88 Correct 6 ms 2004 KB n=2000
89 Correct 6 ms 2132 KB n=2000
90 Correct 7 ms 2004 KB n=2000
91 Correct 7 ms 1872 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB n=5
2 Correct 0 ms 340 KB n=100
3 Correct 0 ms 320 KB n=100
4 Correct 1 ms 340 KB n=100
5 Correct 1 ms 340 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 320 KB n=100
8 Correct 1 ms 340 KB n=100
9 Correct 1 ms 340 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 0 ms 340 KB n=100
12 Correct 1 ms 324 KB n=100
13 Correct 0 ms 340 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 0 ms 340 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 0 ms 340 KB n=100
18 Correct 1 ms 340 KB n=100
19 Correct 1 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 0 ms 340 KB n=100
22 Correct 1 ms 324 KB n=100
23 Correct 1 ms 328 KB n=100
24 Correct 1 ms 340 KB n=100
25 Correct 1 ms 328 KB n=100
26 Correct 0 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
28 Correct 1 ms 624 KB n=500
29 Correct 1 ms 724 KB n=500
30 Correct 1 ms 716 KB n=500
31 Correct 2 ms 752 KB n=500
32 Correct 2 ms 588 KB n=500
33 Correct 2 ms 724 KB n=500
34 Correct 1 ms 596 KB n=500
35 Correct 2 ms 724 KB n=500
36 Correct 1 ms 596 KB n=500
37 Correct 1 ms 596 KB n=500
38 Correct 2 ms 592 KB n=500
39 Correct 1 ms 596 KB n=500
40 Correct 1 ms 596 KB n=500
41 Correct 1 ms 596 KB n=500
42 Correct 2 ms 596 KB n=500
43 Correct 1 ms 596 KB n=500
44 Correct 2 ms 596 KB n=500
45 Correct 2 ms 596 KB n=500
46 Correct 1 ms 724 KB n=500
47 Correct 1 ms 624 KB n=500
48 Correct 1 ms 596 KB n=500
49 Correct 1 ms 724 KB n=500
50 Correct 1 ms 588 KB n=500
51 Correct 1 ms 724 KB n=500
52 Correct 1 ms 724 KB n=500
53 Correct 1 ms 724 KB n=500
54 Correct 1 ms 724 KB n=500
55 Correct 1 ms 452 KB n=278
56 Correct 1 ms 724 KB n=500
57 Correct 1 ms 724 KB n=500
58 Correct 1 ms 596 KB n=500
59 Correct 3 ms 1876 KB n=2000
60 Correct 4 ms 2132 KB n=2000
61 Correct 3 ms 1876 KB n=2000
62 Correct 3 ms 1876 KB n=2000
63 Correct 3 ms 1748 KB n=2000
64 Correct 4 ms 1872 KB n=2000
65 Correct 3 ms 1876 KB n=2000
66 Correct 4 ms 2004 KB n=2000
67 Correct 4 ms 1744 KB n=2000
68 Correct 6 ms 1928 KB n=2000
69 Correct 8 ms 1852 KB n=2000
70 Correct 7 ms 1876 KB n=2000
71 Correct 7 ms 1748 KB n=2000
72 Correct 4 ms 1748 KB n=2000
73 Correct 4 ms 1748 KB n=2000
74 Correct 3 ms 1748 KB n=1844
75 Correct 4 ms 1748 KB n=2000
76 Correct 5 ms 1748 KB n=2000
77 Correct 5 ms 1748 KB n=2000
78 Correct 5 ms 1852 KB n=2000
79 Correct 3 ms 1748 KB n=2000
80 Correct 4 ms 2004 KB n=2000
81 Correct 3 ms 1876 KB n=2000
82 Correct 4 ms 1748 KB n=2000
83 Correct 3 ms 1876 KB n=2000
84 Correct 3 ms 1748 KB n=2000
85 Correct 4 ms 1876 KB n=2000
86 Correct 4 ms 1876 KB n=2000
87 Correct 4 ms 1748 KB n=2000
88 Correct 6 ms 2004 KB n=2000
89 Correct 6 ms 2132 KB n=2000
90 Correct 7 ms 2004 KB n=2000
91 Correct 7 ms 1872 KB n=2000
92 Execution timed out 4089 ms 148736 KB Time limit exceeded
93 Halted 0 ms 0 KB -