Submission #879144

# Submission time Handle Problem Language Result Execution time Memory
879144 2023-11-26T12:56:04 Z sysia Birthday gift (IZhO18_treearray) C++17
100 / 100
1965 ms 108884 KB
//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;

void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 20;
const int mod = 998244353;

void solve(){
    int n, m, q; cin >> n >> m >> q;
    vector<vector<int>>g(n+1);
    for (int i = 1; i<n; i++){
        int a, b; cin >> a >> b;
        g[a].emplace_back(b);
        g[b].emplace_back(a);
    }
    vector<int>depth(n+1);
    vector up(n+1, vector<int>(K));
    function<void(int, int)>dfs = [&](int v, int pa){
        up[v][0] = pa;
        for (int i = 1; i<K; i++) up[v][i] = up[up[v][i-1]][i-1];
        for (auto x: g[v]){
            if (x == pa) continue;
            depth[x] = depth[v]+1;
            dfs(x, v);
        }
    };
    dfs(1, 1);
    auto lca = [&](int a, int b){
        if (depth[a] < depth[b]) swap(a, b);
        for (int i = K-1; i>=0; i--){
            if (depth[a] - (1<<i) >= depth[b]){
                a = up[a][i];
            }
        }
        if (a == b) return a;
        for (int i = K-1; i>=0; i--){
            if (up[a][i] != up[b][i]){
                a = up[a][i];
                b = up[b][i];
            }
        }
        return up[a][0];
    };
    vector<int>a(m+1);
    vector<set<T>>s(2);

    for (int i = 1; i<=m; i++){
        cin >> a[i];
        s[0].insert({a[i], i});
        if (i > 1) s[1].insert({lca(a[i], a[i-1]), i-1}); 
    }
    while (q--){
        int t; cin >> t;
        if (t == 2){
            int l, r, v; cin >> l >> r >> v;
            bool f = 0;
            for (int rep = 0; rep < 2; rep++){
                auto it = s[rep].lower_bound({v, l});
                if (it != s[rep].end() && it->first == v && it->second+rep <= r){
                    f = 1;
                    cout << it->second << " " << it->second+rep << "\n";
                    break;
                }
            }
            if (!f) {
                cout << "-1 -1\n";
            }
        } else {
            int i, val; cin >> i >> val;
            s[0].erase({a[i], i});
            if (i > 1) s[1].erase({lca(a[i-1], a[i]), i-1});
            if (i+1 <= m) s[1].erase({lca(a[i], a[i+1]), i});

            a[i] = val;

            s[0].insert({a[i], i});
            if (i > 1) s[1].insert({lca(a[i-1], a[i]), i-1});
            if (i+1 <= m) s[1].insert({lca(a[i], a[i+1]), i});
        }
    }
}

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

    int t = 1;
    //cin >> t;
    while (t--) solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB n=5
2 Correct 1 ms 348 KB n=100
3 Correct 1 ms 348 KB n=100
4 Correct 1 ms 348 KB n=100
5 Correct 0 ms 348 KB n=100
6 Correct 0 ms 348 KB n=100
7 Correct 0 ms 348 KB n=100
8 Correct 0 ms 500 KB n=100
9 Correct 0 ms 348 KB n=100
10 Correct 1 ms 348 KB n=100
11 Correct 0 ms 344 KB n=100
12 Correct 0 ms 348 KB n=100
13 Correct 0 ms 344 KB n=100
14 Correct 0 ms 348 KB n=100
15 Correct 0 ms 348 KB n=100
16 Correct 1 ms 348 KB n=100
17 Correct 0 ms 348 KB n=100
18 Correct 1 ms 348 KB n=100
19 Correct 1 ms 600 KB n=100
20 Correct 1 ms 348 KB n=100
21 Correct 0 ms 348 KB n=100
22 Correct 1 ms 348 KB n=100
23 Correct 0 ms 348 KB n=100
24 Correct 0 ms 348 KB n=100
25 Correct 0 ms 348 KB n=100
26 Correct 0 ms 348 KB n=12
27 Correct 1 ms 348 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB n=5
2 Correct 1 ms 348 KB n=100
3 Correct 1 ms 348 KB n=100
4 Correct 1 ms 348 KB n=100
5 Correct 0 ms 348 KB n=100
6 Correct 0 ms 348 KB n=100
7 Correct 0 ms 348 KB n=100
8 Correct 0 ms 500 KB n=100
9 Correct 0 ms 348 KB n=100
10 Correct 1 ms 348 KB n=100
11 Correct 0 ms 344 KB n=100
12 Correct 0 ms 348 KB n=100
13 Correct 0 ms 344 KB n=100
14 Correct 0 ms 348 KB n=100
15 Correct 0 ms 348 KB n=100
16 Correct 1 ms 348 KB n=100
17 Correct 0 ms 348 KB n=100
18 Correct 1 ms 348 KB n=100
19 Correct 1 ms 600 KB n=100
20 Correct 1 ms 348 KB n=100
21 Correct 0 ms 348 KB n=100
22 Correct 1 ms 348 KB n=100
23 Correct 0 ms 348 KB n=100
24 Correct 0 ms 348 KB n=100
25 Correct 0 ms 348 KB n=100
26 Correct 0 ms 348 KB n=12
27 Correct 1 ms 348 KB n=100
28 Correct 1 ms 604 KB n=500
29 Correct 1 ms 472 KB n=500
30 Correct 1 ms 600 KB n=500
31 Correct 2 ms 800 KB n=500
32 Correct 1 ms 604 KB n=500
33 Correct 1 ms 600 KB n=500
34 Correct 1 ms 604 KB n=500
35 Correct 1 ms 604 KB n=500
36 Correct 1 ms 604 KB n=500
37 Correct 1 ms 604 KB n=500
38 Correct 1 ms 604 KB n=500
39 Correct 1 ms 604 KB n=500
40 Correct 1 ms 604 KB n=500
41 Correct 1 ms 604 KB n=500
42 Correct 1 ms 604 KB n=500
43 Correct 1 ms 460 KB n=500
44 Correct 1 ms 604 KB n=500
45 Correct 1 ms 604 KB n=500
46 Correct 1 ms 600 KB n=500
47 Correct 1 ms 604 KB n=500
48 Correct 1 ms 604 KB n=500
49 Correct 1 ms 604 KB n=500
50 Correct 1 ms 604 KB n=500
51 Correct 1 ms 856 KB n=500
52 Correct 1 ms 600 KB n=500
53 Correct 1 ms 604 KB n=500
54 Correct 1 ms 604 KB n=500
55 Correct 1 ms 604 KB n=278
56 Correct 1 ms 604 KB n=500
57 Correct 1 ms 604 KB n=500
58 Correct 1 ms 604 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB n=5
2 Correct 1 ms 348 KB n=100
3 Correct 1 ms 348 KB n=100
4 Correct 1 ms 348 KB n=100
5 Correct 0 ms 348 KB n=100
6 Correct 0 ms 348 KB n=100
7 Correct 0 ms 348 KB n=100
8 Correct 0 ms 500 KB n=100
9 Correct 0 ms 348 KB n=100
10 Correct 1 ms 348 KB n=100
11 Correct 0 ms 344 KB n=100
12 Correct 0 ms 348 KB n=100
13 Correct 0 ms 344 KB n=100
14 Correct 0 ms 348 KB n=100
15 Correct 0 ms 348 KB n=100
16 Correct 1 ms 348 KB n=100
17 Correct 0 ms 348 KB n=100
18 Correct 1 ms 348 KB n=100
19 Correct 1 ms 600 KB n=100
20 Correct 1 ms 348 KB n=100
21 Correct 0 ms 348 KB n=100
22 Correct 1 ms 348 KB n=100
23 Correct 0 ms 348 KB n=100
24 Correct 0 ms 348 KB n=100
25 Correct 0 ms 348 KB n=100
26 Correct 0 ms 348 KB n=12
27 Correct 1 ms 348 KB n=100
28 Correct 1 ms 604 KB n=500
29 Correct 1 ms 472 KB n=500
30 Correct 1 ms 600 KB n=500
31 Correct 2 ms 800 KB n=500
32 Correct 1 ms 604 KB n=500
33 Correct 1 ms 600 KB n=500
34 Correct 1 ms 604 KB n=500
35 Correct 1 ms 604 KB n=500
36 Correct 1 ms 604 KB n=500
37 Correct 1 ms 604 KB n=500
38 Correct 1 ms 604 KB n=500
39 Correct 1 ms 604 KB n=500
40 Correct 1 ms 604 KB n=500
41 Correct 1 ms 604 KB n=500
42 Correct 1 ms 604 KB n=500
43 Correct 1 ms 460 KB n=500
44 Correct 1 ms 604 KB n=500
45 Correct 1 ms 604 KB n=500
46 Correct 1 ms 600 KB n=500
47 Correct 1 ms 604 KB n=500
48 Correct 1 ms 604 KB n=500
49 Correct 1 ms 604 KB n=500
50 Correct 1 ms 604 KB n=500
51 Correct 1 ms 856 KB n=500
52 Correct 1 ms 600 KB n=500
53 Correct 1 ms 604 KB n=500
54 Correct 1 ms 604 KB n=500
55 Correct 1 ms 604 KB n=278
56 Correct 1 ms 604 KB n=500
57 Correct 1 ms 604 KB n=500
58 Correct 1 ms 604 KB n=500
59 Correct 3 ms 1112 KB n=2000
60 Correct 4 ms 1372 KB n=2000
61 Correct 4 ms 1372 KB n=2000
62 Correct 4 ms 1372 KB n=2000
63 Correct 4 ms 1116 KB n=2000
64 Correct 4 ms 1364 KB n=2000
65 Correct 3 ms 1116 KB n=2000
66 Correct 4 ms 1368 KB n=2000
67 Correct 4 ms 1112 KB n=2000
68 Correct 4 ms 1372 KB n=2000
69 Correct 4 ms 1368 KB n=2000
70 Correct 3 ms 1116 KB n=2000
71 Correct 3 ms 1116 KB n=2000
72 Correct 3 ms 1116 KB n=2000
73 Correct 2 ms 1112 KB n=2000
74 Correct 3 ms 1116 KB n=1844
75 Correct 2 ms 1116 KB n=2000
76 Correct 4 ms 1116 KB n=2000
77 Correct 5 ms 1368 KB n=2000
78 Correct 3 ms 1112 KB n=2000
79 Correct 3 ms 1116 KB n=2000
80 Correct 4 ms 1324 KB n=2000
81 Correct 4 ms 1372 KB n=2000
82 Correct 3 ms 1104 KB n=2000
83 Correct 4 ms 1372 KB n=2000
84 Correct 3 ms 1316 KB n=2000
85 Correct 4 ms 1372 KB n=2000
86 Correct 4 ms 1372 KB n=2000
87 Correct 4 ms 1116 KB n=2000
88 Correct 4 ms 1368 KB n=2000
89 Correct 4 ms 1372 KB n=2000
90 Correct 4 ms 1508 KB n=2000
91 Correct 3 ms 1116 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB n=5
2 Correct 1 ms 348 KB n=100
3 Correct 1 ms 348 KB n=100
4 Correct 1 ms 348 KB n=100
5 Correct 0 ms 348 KB n=100
6 Correct 0 ms 348 KB n=100
7 Correct 0 ms 348 KB n=100
8 Correct 0 ms 500 KB n=100
9 Correct 0 ms 348 KB n=100
10 Correct 1 ms 348 KB n=100
11 Correct 0 ms 344 KB n=100
12 Correct 0 ms 348 KB n=100
13 Correct 0 ms 344 KB n=100
14 Correct 0 ms 348 KB n=100
15 Correct 0 ms 348 KB n=100
16 Correct 1 ms 348 KB n=100
17 Correct 0 ms 348 KB n=100
18 Correct 1 ms 348 KB n=100
19 Correct 1 ms 600 KB n=100
20 Correct 1 ms 348 KB n=100
21 Correct 0 ms 348 KB n=100
22 Correct 1 ms 348 KB n=100
23 Correct 0 ms 348 KB n=100
24 Correct 0 ms 348 KB n=100
25 Correct 0 ms 348 KB n=100
26 Correct 0 ms 348 KB n=12
27 Correct 1 ms 348 KB n=100
28 Correct 1 ms 604 KB n=500
29 Correct 1 ms 472 KB n=500
30 Correct 1 ms 600 KB n=500
31 Correct 2 ms 800 KB n=500
32 Correct 1 ms 604 KB n=500
33 Correct 1 ms 600 KB n=500
34 Correct 1 ms 604 KB n=500
35 Correct 1 ms 604 KB n=500
36 Correct 1 ms 604 KB n=500
37 Correct 1 ms 604 KB n=500
38 Correct 1 ms 604 KB n=500
39 Correct 1 ms 604 KB n=500
40 Correct 1 ms 604 KB n=500
41 Correct 1 ms 604 KB n=500
42 Correct 1 ms 604 KB n=500
43 Correct 1 ms 460 KB n=500
44 Correct 1 ms 604 KB n=500
45 Correct 1 ms 604 KB n=500
46 Correct 1 ms 600 KB n=500
47 Correct 1 ms 604 KB n=500
48 Correct 1 ms 604 KB n=500
49 Correct 1 ms 604 KB n=500
50 Correct 1 ms 604 KB n=500
51 Correct 1 ms 856 KB n=500
52 Correct 1 ms 600 KB n=500
53 Correct 1 ms 604 KB n=500
54 Correct 1 ms 604 KB n=500
55 Correct 1 ms 604 KB n=278
56 Correct 1 ms 604 KB n=500
57 Correct 1 ms 604 KB n=500
58 Correct 1 ms 604 KB n=500
59 Correct 3 ms 1112 KB n=2000
60 Correct 4 ms 1372 KB n=2000
61 Correct 4 ms 1372 KB n=2000
62 Correct 4 ms 1372 KB n=2000
63 Correct 4 ms 1116 KB n=2000
64 Correct 4 ms 1364 KB n=2000
65 Correct 3 ms 1116 KB n=2000
66 Correct 4 ms 1368 KB n=2000
67 Correct 4 ms 1112 KB n=2000
68 Correct 4 ms 1372 KB n=2000
69 Correct 4 ms 1368 KB n=2000
70 Correct 3 ms 1116 KB n=2000
71 Correct 3 ms 1116 KB n=2000
72 Correct 3 ms 1116 KB n=2000
73 Correct 2 ms 1112 KB n=2000
74 Correct 3 ms 1116 KB n=1844
75 Correct 2 ms 1116 KB n=2000
76 Correct 4 ms 1116 KB n=2000
77 Correct 5 ms 1368 KB n=2000
78 Correct 3 ms 1112 KB n=2000
79 Correct 3 ms 1116 KB n=2000
80 Correct 4 ms 1324 KB n=2000
81 Correct 4 ms 1372 KB n=2000
82 Correct 3 ms 1104 KB n=2000
83 Correct 4 ms 1372 KB n=2000
84 Correct 3 ms 1316 KB n=2000
85 Correct 4 ms 1372 KB n=2000
86 Correct 4 ms 1372 KB n=2000
87 Correct 4 ms 1116 KB n=2000
88 Correct 4 ms 1368 KB n=2000
89 Correct 4 ms 1372 KB n=2000
90 Correct 4 ms 1508 KB n=2000
91 Correct 3 ms 1116 KB n=2000
92 Correct 944 ms 87812 KB n=200000
93 Correct 1849 ms 99092 KB n=200000
94 Correct 1870 ms 106716 KB n=200000
95 Correct 898 ms 87760 KB n=200000
96 Correct 864 ms 88072 KB n=200000
97 Correct 1813 ms 96968 KB n=200000
98 Correct 861 ms 87748 KB n=200000
99 Correct 1164 ms 88036 KB n=200000
100 Correct 941 ms 87784 KB n=200000
101 Correct 1965 ms 108884 KB n=200000
102 Correct 553 ms 89248 KB n=200000
103 Correct 579 ms 89248 KB n=200000
104 Correct 530 ms 89184 KB n=200000
105 Correct 556 ms 89644 KB n=200000
106 Correct 519 ms 89548 KB n=200000
107 Correct 515 ms 89812 KB n=200000
108 Correct 1088 ms 88060 KB n=200000
109 Correct 1111 ms 88144 KB n=200000
110 Correct 1103 ms 88124 KB n=200000
111 Correct 789 ms 87232 KB n=200000
112 Correct 1835 ms 107216 KB n=200000
113 Correct 1810 ms 97020 KB n=200000
114 Correct 827 ms 87348 KB n=200000
115 Correct 1871 ms 92176 KB n=200000
116 Correct 889 ms 87844 KB n=200000
117 Correct 1951 ms 107996 KB n=200000
118 Correct 1954 ms 94612 KB n=200000
119 Correct 852 ms 87964 KB n=200000
120 Correct 1810 ms 108152 KB n=200000
121 Correct 1745 ms 108072 KB n=200000
122 Correct 1770 ms 108504 KB n=200000
123 Correct 515 ms 89428 KB n=200000
124 Correct 266 ms 23384 KB n=25264