답안 #464269

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
464269 2021-08-12T16:00:27 Z Yeboi Birthday gift (IZhO18_treearray) C++14
100 / 100
1390 ms 94888 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(){
    fastio();
    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;
            }
        }
    }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB n=5
2 Correct 3 ms 4940 KB n=100
3 Correct 4 ms 5008 KB n=100
4 Correct 4 ms 4988 KB n=100
5 Correct 3 ms 4940 KB n=100
6 Correct 4 ms 4940 KB n=100
7 Correct 8 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 5 ms 4940 KB n=100
12 Correct 3 ms 4940 KB n=100
13 Correct 3 ms 4940 KB n=100
14 Correct 3 ms 4940 KB n=100
15 Correct 3 ms 4940 KB n=100
16 Correct 3 ms 4940 KB n=100
17 Correct 5 ms 4940 KB n=100
18 Correct 3 ms 4940 KB n=100
19 Correct 3 ms 4940 KB n=100
20 Correct 4 ms 4940 KB n=100
21 Correct 3 ms 4940 KB n=100
22 Correct 3 ms 4972 KB n=100
23 Correct 4 ms 4940 KB n=100
24 Correct 4 ms 4940 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
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB n=5
2 Correct 3 ms 4940 KB n=100
3 Correct 4 ms 5008 KB n=100
4 Correct 4 ms 4988 KB n=100
5 Correct 3 ms 4940 KB n=100
6 Correct 4 ms 4940 KB n=100
7 Correct 8 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 5 ms 4940 KB n=100
12 Correct 3 ms 4940 KB n=100
13 Correct 3 ms 4940 KB n=100
14 Correct 3 ms 4940 KB n=100
15 Correct 3 ms 4940 KB n=100
16 Correct 3 ms 4940 KB n=100
17 Correct 5 ms 4940 KB n=100
18 Correct 3 ms 4940 KB n=100
19 Correct 3 ms 4940 KB n=100
20 Correct 4 ms 4940 KB n=100
21 Correct 3 ms 4940 KB n=100
22 Correct 3 ms 4972 KB n=100
23 Correct 4 ms 4940 KB n=100
24 Correct 4 ms 4940 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 4 ms 5196 KB n=500
29 Correct 4 ms 5068 KB n=500
30 Correct 4 ms 5068 KB n=500
31 Correct 4 ms 5068 KB n=500
32 Correct 4 ms 5196 KB n=500
33 Correct 4 ms 5068 KB n=500
34 Correct 4 ms 5196 KB n=500
35 Correct 4 ms 5068 KB n=500
36 Correct 4 ms 5196 KB n=500
37 Correct 7 ms 5196 KB n=500
38 Correct 5 ms 5196 KB n=500
39 Correct 5 ms 5068 KB n=500
40 Correct 4 ms 5068 KB n=500
41 Correct 4 ms 5068 KB n=500
42 Correct 4 ms 5068 KB n=500
43 Correct 5 ms 5192 KB n=500
44 Correct 4 ms 5068 KB n=500
45 Correct 4 ms 5196 KB n=500
46 Correct 4 ms 5196 KB n=500
47 Correct 4 ms 5196 KB n=500
48 Correct 6 ms 5196 KB n=500
49 Correct 5 ms 5068 KB n=500
50 Correct 4 ms 5196 KB n=500
51 Correct 4 ms 5068 KB n=500
52 Correct 4 ms 5068 KB n=500
53 Correct 9 ms 5196 KB n=500
54 Correct 4 ms 5068 KB n=500
55 Correct 3 ms 5068 KB n=278
56 Correct 4 ms 5068 KB n=500
57 Correct 4 ms 5068 KB n=500
58 Correct 5 ms 5196 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB n=5
2 Correct 3 ms 4940 KB n=100
3 Correct 4 ms 5008 KB n=100
4 Correct 4 ms 4988 KB n=100
5 Correct 3 ms 4940 KB n=100
6 Correct 4 ms 4940 KB n=100
7 Correct 8 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 5 ms 4940 KB n=100
12 Correct 3 ms 4940 KB n=100
13 Correct 3 ms 4940 KB n=100
14 Correct 3 ms 4940 KB n=100
15 Correct 3 ms 4940 KB n=100
16 Correct 3 ms 4940 KB n=100
17 Correct 5 ms 4940 KB n=100
18 Correct 3 ms 4940 KB n=100
19 Correct 3 ms 4940 KB n=100
20 Correct 4 ms 4940 KB n=100
21 Correct 3 ms 4940 KB n=100
22 Correct 3 ms 4972 KB n=100
23 Correct 4 ms 4940 KB n=100
24 Correct 4 ms 4940 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 4 ms 5196 KB n=500
29 Correct 4 ms 5068 KB n=500
30 Correct 4 ms 5068 KB n=500
31 Correct 4 ms 5068 KB n=500
32 Correct 4 ms 5196 KB n=500
33 Correct 4 ms 5068 KB n=500
34 Correct 4 ms 5196 KB n=500
35 Correct 4 ms 5068 KB n=500
36 Correct 4 ms 5196 KB n=500
37 Correct 7 ms 5196 KB n=500
38 Correct 5 ms 5196 KB n=500
39 Correct 5 ms 5068 KB n=500
40 Correct 4 ms 5068 KB n=500
41 Correct 4 ms 5068 KB n=500
42 Correct 4 ms 5068 KB n=500
43 Correct 5 ms 5192 KB n=500
44 Correct 4 ms 5068 KB n=500
45 Correct 4 ms 5196 KB n=500
46 Correct 4 ms 5196 KB n=500
47 Correct 4 ms 5196 KB n=500
48 Correct 6 ms 5196 KB n=500
49 Correct 5 ms 5068 KB n=500
50 Correct 4 ms 5196 KB n=500
51 Correct 4 ms 5068 KB n=500
52 Correct 4 ms 5068 KB n=500
53 Correct 9 ms 5196 KB n=500
54 Correct 4 ms 5068 KB n=500
55 Correct 3 ms 5068 KB n=278
56 Correct 4 ms 5068 KB n=500
57 Correct 4 ms 5068 KB n=500
58 Correct 5 ms 5196 KB n=500
59 Correct 14 ms 5708 KB n=2000
60 Correct 13 ms 5708 KB n=2000
61 Correct 8 ms 5708 KB n=2000
62 Correct 8 ms 5708 KB n=2000
63 Correct 9 ms 5708 KB n=2000
64 Correct 9 ms 5708 KB n=2000
65 Correct 12 ms 5708 KB n=2000
66 Correct 8 ms 5708 KB n=2000
67 Correct 8 ms 5688 KB n=2000
68 Correct 9 ms 5708 KB n=2000
69 Correct 9 ms 5708 KB n=2000
70 Correct 9 ms 5708 KB n=2000
71 Correct 10 ms 5708 KB n=2000
72 Correct 9 ms 5708 KB n=2000
73 Correct 9 ms 5796 KB n=2000
74 Correct 7 ms 5708 KB n=1844
75 Correct 9 ms 5708 KB n=2000
76 Correct 8 ms 5708 KB n=2000
77 Correct 8 ms 5708 KB n=2000
78 Correct 8 ms 5708 KB n=2000
79 Correct 8 ms 5708 KB n=2000
80 Correct 8 ms 5708 KB n=2000
81 Correct 13 ms 5672 KB n=2000
82 Correct 8 ms 5800 KB n=2000
83 Correct 12 ms 5708 KB n=2000
84 Correct 8 ms 5768 KB n=2000
85 Correct 9 ms 5712 KB n=2000
86 Correct 8 ms 5708 KB n=2000
87 Correct 8 ms 5708 KB n=2000
88 Correct 7 ms 5708 KB n=2000
89 Correct 7 ms 5708 KB n=2000
90 Correct 8 ms 5708 KB n=2000
91 Correct 9 ms 5792 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB n=5
2 Correct 3 ms 4940 KB n=100
3 Correct 4 ms 5008 KB n=100
4 Correct 4 ms 4988 KB n=100
5 Correct 3 ms 4940 KB n=100
6 Correct 4 ms 4940 KB n=100
7 Correct 8 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 5 ms 4940 KB n=100
12 Correct 3 ms 4940 KB n=100
13 Correct 3 ms 4940 KB n=100
14 Correct 3 ms 4940 KB n=100
15 Correct 3 ms 4940 KB n=100
16 Correct 3 ms 4940 KB n=100
17 Correct 5 ms 4940 KB n=100
18 Correct 3 ms 4940 KB n=100
19 Correct 3 ms 4940 KB n=100
20 Correct 4 ms 4940 KB n=100
21 Correct 3 ms 4940 KB n=100
22 Correct 3 ms 4972 KB n=100
23 Correct 4 ms 4940 KB n=100
24 Correct 4 ms 4940 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 4 ms 5196 KB n=500
29 Correct 4 ms 5068 KB n=500
30 Correct 4 ms 5068 KB n=500
31 Correct 4 ms 5068 KB n=500
32 Correct 4 ms 5196 KB n=500
33 Correct 4 ms 5068 KB n=500
34 Correct 4 ms 5196 KB n=500
35 Correct 4 ms 5068 KB n=500
36 Correct 4 ms 5196 KB n=500
37 Correct 7 ms 5196 KB n=500
38 Correct 5 ms 5196 KB n=500
39 Correct 5 ms 5068 KB n=500
40 Correct 4 ms 5068 KB n=500
41 Correct 4 ms 5068 KB n=500
42 Correct 4 ms 5068 KB n=500
43 Correct 5 ms 5192 KB n=500
44 Correct 4 ms 5068 KB n=500
45 Correct 4 ms 5196 KB n=500
46 Correct 4 ms 5196 KB n=500
47 Correct 4 ms 5196 KB n=500
48 Correct 6 ms 5196 KB n=500
49 Correct 5 ms 5068 KB n=500
50 Correct 4 ms 5196 KB n=500
51 Correct 4 ms 5068 KB n=500
52 Correct 4 ms 5068 KB n=500
53 Correct 9 ms 5196 KB n=500
54 Correct 4 ms 5068 KB n=500
55 Correct 3 ms 5068 KB n=278
56 Correct 4 ms 5068 KB n=500
57 Correct 4 ms 5068 KB n=500
58 Correct 5 ms 5196 KB n=500
59 Correct 14 ms 5708 KB n=2000
60 Correct 13 ms 5708 KB n=2000
61 Correct 8 ms 5708 KB n=2000
62 Correct 8 ms 5708 KB n=2000
63 Correct 9 ms 5708 KB n=2000
64 Correct 9 ms 5708 KB n=2000
65 Correct 12 ms 5708 KB n=2000
66 Correct 8 ms 5708 KB n=2000
67 Correct 8 ms 5688 KB n=2000
68 Correct 9 ms 5708 KB n=2000
69 Correct 9 ms 5708 KB n=2000
70 Correct 9 ms 5708 KB n=2000
71 Correct 10 ms 5708 KB n=2000
72 Correct 9 ms 5708 KB n=2000
73 Correct 9 ms 5796 KB n=2000
74 Correct 7 ms 5708 KB n=1844
75 Correct 9 ms 5708 KB n=2000
76 Correct 8 ms 5708 KB n=2000
77 Correct 8 ms 5708 KB n=2000
78 Correct 8 ms 5708 KB n=2000
79 Correct 8 ms 5708 KB n=2000
80 Correct 8 ms 5708 KB n=2000
81 Correct 13 ms 5672 KB n=2000
82 Correct 8 ms 5800 KB n=2000
83 Correct 12 ms 5708 KB n=2000
84 Correct 8 ms 5768 KB n=2000
85 Correct 9 ms 5712 KB n=2000
86 Correct 8 ms 5708 KB n=2000
87 Correct 8 ms 5708 KB n=2000
88 Correct 7 ms 5708 KB n=2000
89 Correct 7 ms 5708 KB n=2000
90 Correct 8 ms 5708 KB n=2000
91 Correct 9 ms 5792 KB n=2000
92 Correct 1081 ms 93948 KB n=200000
93 Correct 1195 ms 93084 KB n=200000
94 Correct 953 ms 92620 KB n=200000
95 Correct 992 ms 93764 KB n=200000
96 Correct 1027 ms 93952 KB n=200000
97 Correct 1231 ms 93136 KB n=200000
98 Correct 967 ms 93740 KB n=200000
99 Correct 1187 ms 93452 KB n=200000
100 Correct 1012 ms 93768 KB n=200000
101 Correct 843 ms 92332 KB n=200000
102 Correct 909 ms 94888 KB n=200000
103 Correct 893 ms 94832 KB n=200000
104 Correct 926 ms 94884 KB n=200000
105 Correct 883 ms 94708 KB n=200000
106 Correct 912 ms 94676 KB n=200000
107 Correct 898 ms 94764 KB n=200000
108 Correct 1045 ms 93644 KB n=200000
109 Correct 1035 ms 93552 KB n=200000
110 Correct 1031 ms 93700 KB n=200000
111 Correct 1030 ms 93688 KB n=200000
112 Correct 866 ms 92356 KB n=200000
113 Correct 1147 ms 92964 KB n=200000
114 Correct 1010 ms 93620 KB n=200000
115 Correct 1390 ms 93124 KB n=200000
116 Correct 929 ms 93532 KB n=200000
117 Correct 932 ms 92100 KB n=200000
118 Correct 1307 ms 92908 KB n=200000
119 Correct 949 ms 93532 KB n=200000
120 Correct 800 ms 91592 KB n=200000
121 Correct 826 ms 91816 KB n=200000
122 Correct 798 ms 92132 KB n=200000
123 Correct 893 ms 94256 KB n=200000
124 Correct 258 ms 23040 KB n=25264