Submission #464268

# Submission time Handle Problem Language Result Execution time Memory
464268 2021-08-12T15:59:44 Z Yeboi Birthday gift (IZhO18_treearray) C++14
100 / 100
1670 ms 102016 KB
#include <bits/stdc++.h>
#define ll long long
#define mod 998244353
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define rep(i,s) for(ll i=0; i<s ; i++)
#define f(i,a,b) for(ll i(a); i<b ; i++)
const ll INF = 1000000000000000000;
const ll N = 200001;
const ll MOD = 1000000007;
const ll modi = 1000000007;
const ll MAXN = 10000000000;    
const ll rootval = 319;
using namespace std;

ll n,l;
vector<ll> adj[N];
ll timer;
vector<ll> tin, tout;
vector<vector<ll>> up;
 
void dfs(ll v, ll p)
{
    tin[v] = ++timer;
    up[v][0] = p;
    for (ll i = 1; i <= l; ++i)
        up[v][i] = up[up[v][i-1]][i-1];
 
    for (ll u : adj[v]) {
        if (u != p)
            dfs(u, v);
    }
 
    tout[v] = ++timer;
}
 
bool is_ancestor(ll u, ll v)
{
    return tin[u] <= tin[v] && tout[u] >= tout[v];
}
 
ll lca(ll u, ll v)
{
    if (is_ancestor(u, v))
        return u;
    if (is_ancestor(v, u))
        return v;
    for (ll i = l; i >= 0; --i) {
        if (!is_ancestor(up[u][i], v))
            u = up[u][i];
    }
    return up[u][0];
}
 
void preprocess(ll root) {
    tin.resize(n);
    tout.resize(n);
    timer = 0;
    l = ceil(log2(n));
    up.assign(n, vector<ll>(l + 1));
    dfs(root, root);
}

int main(){
    ll m,q;
    cin >> n >> m >> q;
    rep(i,n-1){
        ll u,v;
        cin >> u >> v;
        --u,--v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    ll arr[m];
    ll lcarr[m];
    rep(i,m){
        cin >> arr[i];
        --arr[i];
    }
    if(m == 1){
        rep(i,q){
            ll x;
            cin >> x;
            if(x == 1){
                ll pos,v;
                cin >> pos >> v;
                --pos,--v;
                arr[pos] = v;
            }
            else if(x==2){
                ll l,r,v;
                cin >> l >> r >> v;
                --l,--r,--v;
                if(arr[l] == v){
                    cout << l+1 << " " << l+1 << endl;
                    continue;
                }
                else{
                    cout << -1 << " " << -1 << endl;
                    continue;
                }
            }
        }
    }
    else{
    preprocess(0);
    set<ll> pos2[n];
    set<ll> pos1[n];
    rep(i,m){
        pos1[arr[i]].insert(i);
    }
    rep(i,m-1){
        ll lc = lca(arr[i],arr[i+1]);
        pos2[lc].insert(i);
        lcarr[i] = lc;
    }
    rep(i,q){
        ll x;
        cin >> x;
        if(x == 1){
            ll pos,v;
            cin >> pos >> v;
            --pos,--v;
            ll val = arr[pos];
            arr[pos] = v;
            pos1[val].erase(pos);
            pos1[v].insert(pos);
            if(pos == 0){
                pos2[lcarr[pos]].erase(pos);
                ll lc = lca(arr[pos],arr[pos+1]);
                lcarr[pos] = lc;
                pos2[lcarr[pos]].insert(pos);
            }
            else if(pos == m-1){
                pos2[lcarr[pos-1]].erase(pos-1);
                ll lc = lca(arr[pos],arr[pos-1]);
                lcarr[pos-1] = lc;
                pos2[lcarr[pos-1]].insert(pos-1);
            }
            else{
                pos2[lcarr[pos]].erase(pos);
                ll lc = lca(arr[pos],arr[pos+1]);
                lcarr[pos] = lc;
                pos2[lcarr[pos]].insert(pos);
                pos2[lcarr[pos-1]].erase(pos-1);
                lc = lca(arr[pos],arr[pos-1]);
                lcarr[pos-1] = lc;
                pos2[lcarr[pos-1]].insert(pos-1);
            }
        }
        else if(x==2){
            ll l,r,v;
            cin >> l >> r >> v;
            --l,--r,--v;
            ll y = 1e18;
            ll y1 = 1e18;
            if(!pos1[v].empty()){
                auto it = pos1[v].lower_bound(l);
                if(it != pos1[v].end()){
                    y=*it;
                }
            }
            if(!pos2[v].empty()){
                auto it1 = pos2[v].lower_bound(l);
                if(it1 != pos2[v].end()){
                    y1 = *it1;
                }
            }
            if(y <= r){
                cout << y+1 << " " << y+1 << endl;
                continue;
            }
            else if(y1 <= r-1){
                cout << y1+1 << " " << y1+2 << endl;
                continue;
            }
            else{
                cout << -1 << " " << -1 << endl;
                continue;
            }
        }
    }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB n=5
2 Correct 3 ms 4940 KB n=100
3 Correct 3 ms 4940 KB n=100
4 Correct 3 ms 4940 KB n=100
5 Correct 3 ms 4992 KB n=100
6 Correct 3 ms 4940 KB n=100
7 Correct 3 ms 4940 KB n=100
8 Correct 3 ms 4940 KB n=100
9 Correct 3 ms 4940 KB n=100
10 Correct 3 ms 4940 KB n=100
11 Correct 3 ms 4940 KB n=100
12 Correct 8 ms 4940 KB n=100
13 Correct 3 ms 4940 KB n=100
14 Correct 3 ms 4992 KB n=100
15 Correct 4 ms 4940 KB n=100
16 Correct 3 ms 4940 KB n=100
17 Correct 3 ms 4940 KB n=100
18 Correct 3 ms 4940 KB n=100
19 Correct 3 ms 4940 KB n=100
20 Correct 3 ms 4940 KB n=100
21 Correct 3 ms 4940 KB n=100
22 Correct 3 ms 4940 KB n=100
23 Correct 3 ms 4940 KB n=100
24 Correct 3 ms 4984 KB n=100
25 Correct 3 ms 4940 KB n=100
26 Correct 3 ms 4940 KB n=12
27 Correct 4 ms 4940 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB n=5
2 Correct 3 ms 4940 KB n=100
3 Correct 3 ms 4940 KB n=100
4 Correct 3 ms 4940 KB n=100
5 Correct 3 ms 4992 KB n=100
6 Correct 3 ms 4940 KB n=100
7 Correct 3 ms 4940 KB n=100
8 Correct 3 ms 4940 KB n=100
9 Correct 3 ms 4940 KB n=100
10 Correct 3 ms 4940 KB n=100
11 Correct 3 ms 4940 KB n=100
12 Correct 8 ms 4940 KB n=100
13 Correct 3 ms 4940 KB n=100
14 Correct 3 ms 4992 KB n=100
15 Correct 4 ms 4940 KB n=100
16 Correct 3 ms 4940 KB n=100
17 Correct 3 ms 4940 KB n=100
18 Correct 3 ms 4940 KB n=100
19 Correct 3 ms 4940 KB n=100
20 Correct 3 ms 4940 KB n=100
21 Correct 3 ms 4940 KB n=100
22 Correct 3 ms 4940 KB n=100
23 Correct 3 ms 4940 KB n=100
24 Correct 3 ms 4984 KB n=100
25 Correct 3 ms 4940 KB n=100
26 Correct 3 ms 4940 KB n=12
27 Correct 4 ms 4940 KB n=100
28 Correct 5 ms 5068 KB n=500
29 Correct 4 ms 5068 KB n=500
30 Correct 5 ms 5068 KB n=500
31 Correct 5 ms 5068 KB n=500
32 Correct 5 ms 5068 KB n=500
33 Correct 5 ms 5068 KB n=500
34 Correct 5 ms 5068 KB n=500
35 Correct 7 ms 5120 KB n=500
36 Correct 5 ms 5068 KB n=500
37 Correct 6 ms 5068 KB n=500
38 Correct 5 ms 5068 KB n=500
39 Correct 5 ms 5068 KB n=500
40 Correct 5 ms 5068 KB n=500
41 Correct 5 ms 5068 KB n=500
42 Correct 5 ms 5068 KB n=500
43 Correct 5 ms 5068 KB n=500
44 Correct 4 ms 5100 KB n=500
45 Correct 5 ms 5068 KB n=500
46 Correct 5 ms 5068 KB n=500
47 Correct 5 ms 5068 KB n=500
48 Correct 5 ms 5068 KB n=500
49 Correct 4 ms 5068 KB n=500
50 Correct 4 ms 5068 KB n=500
51 Correct 5 ms 5068 KB n=500
52 Correct 5 ms 5068 KB n=500
53 Correct 4 ms 5068 KB n=500
54 Correct 5 ms 5068 KB n=500
55 Correct 4 ms 5068 KB n=278
56 Correct 4 ms 5068 KB n=500
57 Correct 5 ms 5068 KB n=500
58 Correct 5 ms 5068 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB n=5
2 Correct 3 ms 4940 KB n=100
3 Correct 3 ms 4940 KB n=100
4 Correct 3 ms 4940 KB n=100
5 Correct 3 ms 4992 KB n=100
6 Correct 3 ms 4940 KB n=100
7 Correct 3 ms 4940 KB n=100
8 Correct 3 ms 4940 KB n=100
9 Correct 3 ms 4940 KB n=100
10 Correct 3 ms 4940 KB n=100
11 Correct 3 ms 4940 KB n=100
12 Correct 8 ms 4940 KB n=100
13 Correct 3 ms 4940 KB n=100
14 Correct 3 ms 4992 KB n=100
15 Correct 4 ms 4940 KB n=100
16 Correct 3 ms 4940 KB n=100
17 Correct 3 ms 4940 KB n=100
18 Correct 3 ms 4940 KB n=100
19 Correct 3 ms 4940 KB n=100
20 Correct 3 ms 4940 KB n=100
21 Correct 3 ms 4940 KB n=100
22 Correct 3 ms 4940 KB n=100
23 Correct 3 ms 4940 KB n=100
24 Correct 3 ms 4984 KB n=100
25 Correct 3 ms 4940 KB n=100
26 Correct 3 ms 4940 KB n=12
27 Correct 4 ms 4940 KB n=100
28 Correct 5 ms 5068 KB n=500
29 Correct 4 ms 5068 KB n=500
30 Correct 5 ms 5068 KB n=500
31 Correct 5 ms 5068 KB n=500
32 Correct 5 ms 5068 KB n=500
33 Correct 5 ms 5068 KB n=500
34 Correct 5 ms 5068 KB n=500
35 Correct 7 ms 5120 KB n=500
36 Correct 5 ms 5068 KB n=500
37 Correct 6 ms 5068 KB n=500
38 Correct 5 ms 5068 KB n=500
39 Correct 5 ms 5068 KB n=500
40 Correct 5 ms 5068 KB n=500
41 Correct 5 ms 5068 KB n=500
42 Correct 5 ms 5068 KB n=500
43 Correct 5 ms 5068 KB n=500
44 Correct 4 ms 5100 KB n=500
45 Correct 5 ms 5068 KB n=500
46 Correct 5 ms 5068 KB n=500
47 Correct 5 ms 5068 KB n=500
48 Correct 5 ms 5068 KB n=500
49 Correct 4 ms 5068 KB n=500
50 Correct 4 ms 5068 KB n=500
51 Correct 5 ms 5068 KB n=500
52 Correct 5 ms 5068 KB n=500
53 Correct 4 ms 5068 KB n=500
54 Correct 5 ms 5068 KB n=500
55 Correct 4 ms 5068 KB n=278
56 Correct 4 ms 5068 KB n=500
57 Correct 5 ms 5068 KB n=500
58 Correct 5 ms 5068 KB n=500
59 Correct 10 ms 5708 KB n=2000
60 Correct 12 ms 5692 KB n=2000
61 Correct 10 ms 5708 KB n=2000
62 Correct 11 ms 5748 KB n=2000
63 Correct 11 ms 5784 KB n=2000
64 Correct 10 ms 5772 KB n=2000
65 Correct 10 ms 5664 KB n=2000
66 Correct 10 ms 5708 KB n=2000
67 Correct 10 ms 5708 KB n=2000
68 Correct 10 ms 5708 KB n=2000
69 Correct 11 ms 5708 KB n=2000
70 Correct 12 ms 5708 KB n=2000
71 Correct 12 ms 5776 KB n=2000
72 Correct 11 ms 5708 KB n=2000
73 Correct 13 ms 5708 KB n=2000
74 Correct 9 ms 5708 KB n=1844
75 Correct 11 ms 5708 KB n=2000
76 Correct 11 ms 5708 KB n=2000
77 Correct 10 ms 5776 KB n=2000
78 Correct 10 ms 5708 KB n=2000
79 Correct 13 ms 5708 KB n=2000
80 Correct 10 ms 5708 KB n=2000
81 Correct 10 ms 5724 KB n=2000
82 Correct 11 ms 5736 KB n=2000
83 Correct 10 ms 5708 KB n=2000
84 Correct 10 ms 5708 KB n=2000
85 Correct 11 ms 5708 KB n=2000
86 Correct 10 ms 5708 KB n=2000
87 Correct 10 ms 5708 KB n=2000
88 Correct 9 ms 5708 KB n=2000
89 Correct 9 ms 5756 KB n=2000
90 Correct 10 ms 5708 KB n=2000
91 Correct 12 ms 5768 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB n=5
2 Correct 3 ms 4940 KB n=100
3 Correct 3 ms 4940 KB n=100
4 Correct 3 ms 4940 KB n=100
5 Correct 3 ms 4992 KB n=100
6 Correct 3 ms 4940 KB n=100
7 Correct 3 ms 4940 KB n=100
8 Correct 3 ms 4940 KB n=100
9 Correct 3 ms 4940 KB n=100
10 Correct 3 ms 4940 KB n=100
11 Correct 3 ms 4940 KB n=100
12 Correct 8 ms 4940 KB n=100
13 Correct 3 ms 4940 KB n=100
14 Correct 3 ms 4992 KB n=100
15 Correct 4 ms 4940 KB n=100
16 Correct 3 ms 4940 KB n=100
17 Correct 3 ms 4940 KB n=100
18 Correct 3 ms 4940 KB n=100
19 Correct 3 ms 4940 KB n=100
20 Correct 3 ms 4940 KB n=100
21 Correct 3 ms 4940 KB n=100
22 Correct 3 ms 4940 KB n=100
23 Correct 3 ms 4940 KB n=100
24 Correct 3 ms 4984 KB n=100
25 Correct 3 ms 4940 KB n=100
26 Correct 3 ms 4940 KB n=12
27 Correct 4 ms 4940 KB n=100
28 Correct 5 ms 5068 KB n=500
29 Correct 4 ms 5068 KB n=500
30 Correct 5 ms 5068 KB n=500
31 Correct 5 ms 5068 KB n=500
32 Correct 5 ms 5068 KB n=500
33 Correct 5 ms 5068 KB n=500
34 Correct 5 ms 5068 KB n=500
35 Correct 7 ms 5120 KB n=500
36 Correct 5 ms 5068 KB n=500
37 Correct 6 ms 5068 KB n=500
38 Correct 5 ms 5068 KB n=500
39 Correct 5 ms 5068 KB n=500
40 Correct 5 ms 5068 KB n=500
41 Correct 5 ms 5068 KB n=500
42 Correct 5 ms 5068 KB n=500
43 Correct 5 ms 5068 KB n=500
44 Correct 4 ms 5100 KB n=500
45 Correct 5 ms 5068 KB n=500
46 Correct 5 ms 5068 KB n=500
47 Correct 5 ms 5068 KB n=500
48 Correct 5 ms 5068 KB n=500
49 Correct 4 ms 5068 KB n=500
50 Correct 4 ms 5068 KB n=500
51 Correct 5 ms 5068 KB n=500
52 Correct 5 ms 5068 KB n=500
53 Correct 4 ms 5068 KB n=500
54 Correct 5 ms 5068 KB n=500
55 Correct 4 ms 5068 KB n=278
56 Correct 4 ms 5068 KB n=500
57 Correct 5 ms 5068 KB n=500
58 Correct 5 ms 5068 KB n=500
59 Correct 10 ms 5708 KB n=2000
60 Correct 12 ms 5692 KB n=2000
61 Correct 10 ms 5708 KB n=2000
62 Correct 11 ms 5748 KB n=2000
63 Correct 11 ms 5784 KB n=2000
64 Correct 10 ms 5772 KB n=2000
65 Correct 10 ms 5664 KB n=2000
66 Correct 10 ms 5708 KB n=2000
67 Correct 10 ms 5708 KB n=2000
68 Correct 10 ms 5708 KB n=2000
69 Correct 11 ms 5708 KB n=2000
70 Correct 12 ms 5708 KB n=2000
71 Correct 12 ms 5776 KB n=2000
72 Correct 11 ms 5708 KB n=2000
73 Correct 13 ms 5708 KB n=2000
74 Correct 9 ms 5708 KB n=1844
75 Correct 11 ms 5708 KB n=2000
76 Correct 11 ms 5708 KB n=2000
77 Correct 10 ms 5776 KB n=2000
78 Correct 10 ms 5708 KB n=2000
79 Correct 13 ms 5708 KB n=2000
80 Correct 10 ms 5708 KB n=2000
81 Correct 10 ms 5724 KB n=2000
82 Correct 11 ms 5736 KB n=2000
83 Correct 10 ms 5708 KB n=2000
84 Correct 10 ms 5708 KB n=2000
85 Correct 11 ms 5708 KB n=2000
86 Correct 10 ms 5708 KB n=2000
87 Correct 10 ms 5708 KB n=2000
88 Correct 9 ms 5708 KB n=2000
89 Correct 9 ms 5756 KB n=2000
90 Correct 10 ms 5708 KB n=2000
91 Correct 12 ms 5768 KB n=2000
92 Correct 1222 ms 100288 KB n=200000
93 Correct 1387 ms 100848 KB n=200000
94 Correct 1194 ms 100164 KB n=200000
95 Correct 1204 ms 100352 KB n=200000
96 Correct 1195 ms 100292 KB n=200000
97 Correct 1462 ms 100724 KB n=200000
98 Correct 1217 ms 100316 KB n=200000
99 Correct 1387 ms 100620 KB n=200000
100 Correct 1247 ms 100408 KB n=200000
101 Correct 1126 ms 99956 KB n=200000
102 Correct 1151 ms 101752 KB n=200000
103 Correct 1160 ms 101660 KB n=200000
104 Correct 1173 ms 101628 KB n=200000
105 Correct 1232 ms 102016 KB n=200000
106 Correct 1206 ms 102004 KB n=200000
107 Correct 1208 ms 101952 KB n=200000
108 Correct 1410 ms 100552 KB n=200000
109 Correct 1446 ms 100488 KB n=200000
110 Correct 1442 ms 100412 KB n=200000
111 Correct 1384 ms 99576 KB n=200000
112 Correct 1282 ms 100008 KB n=200000
113 Correct 1532 ms 100720 KB n=200000
114 Correct 1364 ms 99948 KB n=200000
115 Correct 1670 ms 100800 KB n=200000
116 Correct 1216 ms 100440 KB n=200000
117 Correct 1210 ms 99796 KB n=200000
118 Correct 1583 ms 100496 KB n=200000
119 Correct 1255 ms 100448 KB n=200000
120 Correct 1164 ms 98660 KB n=200000
121 Correct 1105 ms 98652 KB n=200000
122 Correct 1149 ms 98932 KB n=200000
123 Correct 1220 ms 101696 KB n=200000
124 Correct 360 ms 24864 KB n=25264