Submission #590684

# Submission time Handle Problem Language Result Execution time Memory
590684 2022-07-06T08:40:54 Z FedShat Birthday gift (IZhO18_treearray) C++17
100 / 100
1046 ms 195080 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);
    vector<set<int>> inda(n);
    for (int i = 0; i < m; ++i) {
        inda[a[i]].insert(i);
    }
    vector<set<int>> indap(n);
    for (int i = 0; i < m - 1; ++i) {
        indap[ap[i]].insert(i);
    }
    auto calc = [&](int pos) {
        if (pos >= 0 && pos + 1 < m) {
            indap[ap[pos]].erase(pos);
            ap[pos] = t.LCA(a[pos], a[pos + 1]);
            indap[ap[pos]].insert(pos);
        }
    };
    while (q--) {
        int tt;
        cin >> tt;
        if (tt == 1) {
            int pos, v;
            cin >> pos >> v;
            --pos;
            --v;
            inda[a[pos]].erase(pos);
            a[pos] = v;
            inda[a[pos]].insert(pos);
            // 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;
            {
                auto it = inda[v].lower_bound(l);
                debug(inda[v]);
                if (it != inda[v].end() && *it <= r) {
                    cout << *it + 1 << " " << *it + 1 << "\n";
                    ans = true;
                }
            }
            if (!ans) {
                auto it = indap[v].lower_bound(l);
                if (it != indap[v].end() && *it < r) {
                    cout << *it + 1 << " " << *it + 2 << "\n";
                    ans = true;
                }
            }
            // 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);
      |     ^~~~~
treearray.cpp:21:20: warning: statement has no effect [-Wunused-value]
   21 | #define debug(...) 42
      |                    ^~
treearray.cpp:162:17: note: in expansion of macro 'debug'
  162 |                 debug(inda[v]);
      |                 ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB n=5
2 Correct 1 ms 340 KB n=100
3 Correct 1 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 340 KB n=100
8 Correct 1 ms 320 KB n=100
9 Correct 1 ms 340 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 1 ms 340 KB n=100
12 Correct 1 ms 316 KB n=100
13 Correct 1 ms 340 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 1 ms 340 KB n=100
18 Correct 1 ms 324 KB n=100
19 Correct 1 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 1 ms 340 KB n=100
22 Correct 1 ms 340 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 1 ms 340 KB n=100
25 Correct 1 ms 340 KB n=100
26 Correct 1 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB n=5
2 Correct 1 ms 340 KB n=100
3 Correct 1 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 340 KB n=100
8 Correct 1 ms 320 KB n=100
9 Correct 1 ms 340 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 1 ms 340 KB n=100
12 Correct 1 ms 316 KB n=100
13 Correct 1 ms 340 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 1 ms 340 KB n=100
18 Correct 1 ms 324 KB n=100
19 Correct 1 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 1 ms 340 KB n=100
22 Correct 1 ms 340 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 1 ms 340 KB n=100
25 Correct 1 ms 340 KB n=100
26 Correct 1 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
28 Correct 1 ms 724 KB n=500
29 Correct 2 ms 724 KB n=500
30 Correct 2 ms 724 KB n=500
31 Correct 2 ms 724 KB n=500
32 Correct 1 ms 724 KB n=500
33 Correct 2 ms 724 KB n=500
34 Correct 2 ms 724 KB n=500
35 Correct 1 ms 724 KB n=500
36 Correct 1 ms 724 KB n=500
37 Correct 1 ms 724 KB n=500
38 Correct 1 ms 724 KB n=500
39 Correct 1 ms 768 KB n=500
40 Correct 2 ms 724 KB n=500
41 Correct 2 ms 716 KB n=500
42 Correct 1 ms 724 KB n=500
43 Correct 1 ms 724 KB n=500
44 Correct 3 ms 724 KB n=500
45 Correct 1 ms 724 KB n=500
46 Correct 2 ms 724 KB n=500
47 Correct 2 ms 712 KB n=500
48 Correct 1 ms 712 KB n=500
49 Correct 1 ms 724 KB n=500
50 Correct 2 ms 724 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 2 ms 724 KB n=500
55 Correct 2 ms 468 KB n=278
56 Correct 1 ms 712 KB n=500
57 Correct 2 ms 724 KB n=500
58 Correct 1 ms 724 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB n=5
2 Correct 1 ms 340 KB n=100
3 Correct 1 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 340 KB n=100
8 Correct 1 ms 320 KB n=100
9 Correct 1 ms 340 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 1 ms 340 KB n=100
12 Correct 1 ms 316 KB n=100
13 Correct 1 ms 340 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 1 ms 340 KB n=100
18 Correct 1 ms 324 KB n=100
19 Correct 1 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 1 ms 340 KB n=100
22 Correct 1 ms 340 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 1 ms 340 KB n=100
25 Correct 1 ms 340 KB n=100
26 Correct 1 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
28 Correct 1 ms 724 KB n=500
29 Correct 2 ms 724 KB n=500
30 Correct 2 ms 724 KB n=500
31 Correct 2 ms 724 KB n=500
32 Correct 1 ms 724 KB n=500
33 Correct 2 ms 724 KB n=500
34 Correct 2 ms 724 KB n=500
35 Correct 1 ms 724 KB n=500
36 Correct 1 ms 724 KB n=500
37 Correct 1 ms 724 KB n=500
38 Correct 1 ms 724 KB n=500
39 Correct 1 ms 768 KB n=500
40 Correct 2 ms 724 KB n=500
41 Correct 2 ms 716 KB n=500
42 Correct 1 ms 724 KB n=500
43 Correct 1 ms 724 KB n=500
44 Correct 3 ms 724 KB n=500
45 Correct 1 ms 724 KB n=500
46 Correct 2 ms 724 KB n=500
47 Correct 2 ms 712 KB n=500
48 Correct 1 ms 712 KB n=500
49 Correct 1 ms 724 KB n=500
50 Correct 2 ms 724 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 2 ms 724 KB n=500
55 Correct 2 ms 468 KB n=278
56 Correct 1 ms 712 KB n=500
57 Correct 2 ms 724 KB n=500
58 Correct 1 ms 724 KB n=500
59 Correct 4 ms 2004 KB n=2000
60 Correct 4 ms 2124 KB n=2000
61 Correct 4 ms 2160 KB n=2000
62 Correct 5 ms 2004 KB n=2000
63 Correct 6 ms 2004 KB n=2000
64 Correct 4 ms 2132 KB n=2000
65 Correct 4 ms 1996 KB n=2000
66 Correct 4 ms 2132 KB n=2000
67 Correct 7 ms 1996 KB n=2000
68 Correct 4 ms 2132 KB n=2000
69 Correct 4 ms 2004 KB n=2000
70 Correct 4 ms 2004 KB n=2000
71 Correct 3 ms 1992 KB n=2000
72 Correct 3 ms 2004 KB n=2000
73 Correct 4 ms 2004 KB n=2000
74 Correct 4 ms 1876 KB n=1844
75 Correct 4 ms 2004 KB n=2000
76 Correct 4 ms 2008 KB n=2000
77 Correct 4 ms 2008 KB n=2000
78 Correct 5 ms 2004 KB n=2000
79 Correct 4 ms 2004 KB n=2000
80 Correct 4 ms 2132 KB n=2000
81 Correct 7 ms 2124 KB n=2000
82 Correct 4 ms 2004 KB n=2000
83 Correct 3 ms 2060 KB n=2000
84 Correct 4 ms 2004 KB n=2000
85 Correct 4 ms 2000 KB n=2000
86 Correct 4 ms 2004 KB n=2000
87 Correct 4 ms 2064 KB n=2000
88 Correct 3 ms 2132 KB n=2000
89 Correct 3 ms 2132 KB n=2000
90 Correct 4 ms 2132 KB n=2000
91 Correct 5 ms 2004 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB n=5
2 Correct 1 ms 340 KB n=100
3 Correct 1 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 340 KB n=100
8 Correct 1 ms 320 KB n=100
9 Correct 1 ms 340 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 1 ms 340 KB n=100
12 Correct 1 ms 316 KB n=100
13 Correct 1 ms 340 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 1 ms 340 KB n=100
18 Correct 1 ms 324 KB n=100
19 Correct 1 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 1 ms 340 KB n=100
22 Correct 1 ms 340 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 1 ms 340 KB n=100
25 Correct 1 ms 340 KB n=100
26 Correct 1 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
28 Correct 1 ms 724 KB n=500
29 Correct 2 ms 724 KB n=500
30 Correct 2 ms 724 KB n=500
31 Correct 2 ms 724 KB n=500
32 Correct 1 ms 724 KB n=500
33 Correct 2 ms 724 KB n=500
34 Correct 2 ms 724 KB n=500
35 Correct 1 ms 724 KB n=500
36 Correct 1 ms 724 KB n=500
37 Correct 1 ms 724 KB n=500
38 Correct 1 ms 724 KB n=500
39 Correct 1 ms 768 KB n=500
40 Correct 2 ms 724 KB n=500
41 Correct 2 ms 716 KB n=500
42 Correct 1 ms 724 KB n=500
43 Correct 1 ms 724 KB n=500
44 Correct 3 ms 724 KB n=500
45 Correct 1 ms 724 KB n=500
46 Correct 2 ms 724 KB n=500
47 Correct 2 ms 712 KB n=500
48 Correct 1 ms 712 KB n=500
49 Correct 1 ms 724 KB n=500
50 Correct 2 ms 724 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 2 ms 724 KB n=500
55 Correct 2 ms 468 KB n=278
56 Correct 1 ms 712 KB n=500
57 Correct 2 ms 724 KB n=500
58 Correct 1 ms 724 KB n=500
59 Correct 4 ms 2004 KB n=2000
60 Correct 4 ms 2124 KB n=2000
61 Correct 4 ms 2160 KB n=2000
62 Correct 5 ms 2004 KB n=2000
63 Correct 6 ms 2004 KB n=2000
64 Correct 4 ms 2132 KB n=2000
65 Correct 4 ms 1996 KB n=2000
66 Correct 4 ms 2132 KB n=2000
67 Correct 7 ms 1996 KB n=2000
68 Correct 4 ms 2132 KB n=2000
69 Correct 4 ms 2004 KB n=2000
70 Correct 4 ms 2004 KB n=2000
71 Correct 3 ms 1992 KB n=2000
72 Correct 3 ms 2004 KB n=2000
73 Correct 4 ms 2004 KB n=2000
74 Correct 4 ms 1876 KB n=1844
75 Correct 4 ms 2004 KB n=2000
76 Correct 4 ms 2008 KB n=2000
77 Correct 4 ms 2008 KB n=2000
78 Correct 5 ms 2004 KB n=2000
79 Correct 4 ms 2004 KB n=2000
80 Correct 4 ms 2132 KB n=2000
81 Correct 7 ms 2124 KB n=2000
82 Correct 4 ms 2004 KB n=2000
83 Correct 3 ms 2060 KB n=2000
84 Correct 4 ms 2004 KB n=2000
85 Correct 4 ms 2000 KB n=2000
86 Correct 4 ms 2004 KB n=2000
87 Correct 4 ms 2064 KB n=2000
88 Correct 3 ms 2132 KB n=2000
89 Correct 3 ms 2132 KB n=2000
90 Correct 4 ms 2132 KB n=2000
91 Correct 5 ms 2004 KB n=2000
92 Correct 737 ms 169192 KB n=200000
93 Correct 619 ms 184308 KB n=200000
94 Correct 677 ms 192072 KB n=200000
95 Correct 799 ms 173704 KB n=200000
96 Correct 710 ms 173836 KB n=200000
97 Correct 704 ms 182084 KB n=200000
98 Correct 760 ms 173820 KB n=200000
99 Correct 778 ms 173332 KB n=200000
100 Correct 661 ms 173872 KB n=200000
101 Correct 605 ms 195080 KB n=200000
102 Correct 426 ms 174328 KB n=200000
103 Correct 433 ms 174264 KB n=200000
104 Correct 413 ms 174212 KB n=200000
105 Correct 443 ms 174668 KB n=200000
106 Correct 462 ms 174740 KB n=200000
107 Correct 548 ms 174752 KB n=200000
108 Correct 722 ms 173180 KB n=200000
109 Correct 745 ms 173364 KB n=200000
110 Correct 744 ms 173180 KB n=200000
111 Correct 679 ms 173280 KB n=200000
112 Correct 566 ms 192672 KB n=200000
113 Correct 633 ms 182112 KB n=200000
114 Correct 680 ms 173316 KB n=200000
115 Correct 754 ms 177180 KB n=200000
116 Correct 695 ms 174104 KB n=200000
117 Correct 633 ms 193780 KB n=200000
118 Correct 954 ms 179672 KB n=200000
119 Correct 1046 ms 173884 KB n=200000
120 Correct 599 ms 194228 KB n=200000
121 Correct 581 ms 194200 KB n=200000
122 Correct 695 ms 194452 KB n=200000
123 Correct 572 ms 174524 KB n=200000
124 Correct 165 ms 32308 KB n=25264