Submission #894339

# Submission time Handle Problem Language Result Execution time Memory
894339 2023-12-28T06:44:10 Z zeta7532 Birthday gift (IZhO18_treearray) C++17
56 / 100
4000 ms 51868 KB
#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
const ll mod = 998244353;
#define fi first
#define se second
#define rep(i,n) for(ll i=0;i<n;i++)
#define all(x) x.begin(),x.end()
#define faster ios::sync_with_stdio(false);cin.tie(nullptr)

using Graph = vector<vector<ll>>;

struct LCA{
    vector<vector<ll>> par;
    vector<ll> dis;
    LCA(const Graph &G,ll root=0){init (G,root);}
    void init(const Graph &G,ll root=0){
        ll v=G.size();
        ll k=1;
        while((1<<k)<v) k++;
        par.assign(k,vector<ll>(v,-1));
        dis.assign(v,-1);
        dfs(G,root,-1,0);
        for(ll i=0;i+1<k;i++){
            for(ll j=0;j<v;j++){
                if(par[i][j]<0) par[i+1][j]=-1;
                else par[i+1][j]=par[i][par[i][j]];
            }
        }
    }
    void dfs(const Graph &G,ll v,ll p,ll d){
        par[0][v]=p;
        dis[v]=d;
        for(ll nv:G[v]){
            if(nv!=p) dfs(G,nv,v,d+1);
        }
    }
    ll query(ll u,ll v){
        if(dis[u]<dis[v]) swap(u,v);
        ll K=par.size();
        for(ll k=0;k<K;k++){
            if((dis[u]-dis[v])>>k&1){
                u=par[k][u];
            }
        }
        if(u==v) return u;
        for(ll k=K-1;k>=0;k--){
            if(par[k][u]!=par[k][v]){
                u=par[k][u];
                v=par[k][v];
            }
        }
        return par[0][u];
    }
    ll get_dis(ll u,ll v){return dis[u]+dis[v]-2*dis[query(u,v)];}
};

int main() {
    ll N,M,Q;
    cin >> N >> M >> Q;
    Graph G(N);
    rep(i,N-1){
        ll u,v;
        cin >> u >> v;
        u--,v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    auto lca=LCA(G);
    vector<ll> A(M);
    rep(i,M) cin >> A[i],A[i]--;
    while(Q--){
        ll q;
        cin >> q;
        if(q==1){
            ll pos,v;
            cin >> pos >> v;
            pos--,v--;
            A[pos]=v;
        }
        if(q==2){
            ll l,r,v;
            cin >> l >> r >> v;
            l--,r--,v--;
            bool ok=false;
            ll nowl=l;
            while(1){
                bool loop=false;
                if(nowl>r) break;
                ll nowv=A[nowl];
                for(ll nowr=nowl;nowr<=r;nowr++){
                    nowv=lca.query(nowv,A[nowr]);
                    if(lca.query(nowv,v)!=v){
                        nowl=nowr+1;
                        break;
                    }
                    if(nowv==v){
                        cout << nowl+1 << " " << nowr+1 << endl;
                        loop=true;
                        ok=true;
                        break;
                    }
                    if(nowr==r) loop=true;
                }
                if(loop) break;
            }
            if(ok) continue;
            cout << -1 << " " << -1 << endl; 
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 432 KB n=5
2 Correct 1 ms 348 KB n=100
3 Correct 1 ms 344 KB n=100
4 Correct 1 ms 348 KB n=100
5 Correct 1 ms 348 KB n=100
6 Correct 1 ms 344 KB n=100
7 Correct 1 ms 600 KB n=100
8 Correct 1 ms 348 KB n=100
9 Correct 1 ms 348 KB n=100
10 Correct 1 ms 348 KB n=100
11 Correct 1 ms 348 KB n=100
12 Correct 1 ms 348 KB n=100
13 Correct 0 ms 348 KB n=100
14 Correct 0 ms 348 KB n=100
15 Correct 1 ms 348 KB n=100
16 Correct 1 ms 348 KB n=100
17 Correct 1 ms 344 KB n=100
18 Correct 1 ms 348 KB n=100
19 Correct 1 ms 348 KB n=100
20 Correct 1 ms 348 KB n=100
21 Correct 0 ms 344 KB n=100
22 Correct 0 ms 348 KB n=100
23 Correct 1 ms 348 KB n=100
24 Correct 1 ms 348 KB n=100
25 Correct 1 ms 348 KB n=100
26 Correct 1 ms 348 KB n=12
27 Correct 1 ms 348 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 1 ms 432 KB n=5
2 Correct 1 ms 348 KB n=100
3 Correct 1 ms 344 KB n=100
4 Correct 1 ms 348 KB n=100
5 Correct 1 ms 348 KB n=100
6 Correct 1 ms 344 KB n=100
7 Correct 1 ms 600 KB n=100
8 Correct 1 ms 348 KB n=100
9 Correct 1 ms 348 KB n=100
10 Correct 1 ms 348 KB n=100
11 Correct 1 ms 348 KB n=100
12 Correct 1 ms 348 KB n=100
13 Correct 0 ms 348 KB n=100
14 Correct 0 ms 348 KB n=100
15 Correct 1 ms 348 KB n=100
16 Correct 1 ms 348 KB n=100
17 Correct 1 ms 344 KB n=100
18 Correct 1 ms 348 KB n=100
19 Correct 1 ms 348 KB n=100
20 Correct 1 ms 348 KB n=100
21 Correct 0 ms 344 KB n=100
22 Correct 0 ms 348 KB n=100
23 Correct 1 ms 348 KB n=100
24 Correct 1 ms 348 KB n=100
25 Correct 1 ms 348 KB n=100
26 Correct 1 ms 348 KB n=12
27 Correct 1 ms 348 KB n=100
28 Correct 1 ms 348 KB n=500
29 Correct 3 ms 344 KB n=500
30 Correct 3 ms 348 KB n=500
31 Correct 3 ms 348 KB n=500
32 Correct 1 ms 348 KB n=500
33 Correct 2 ms 344 KB n=500
34 Correct 1 ms 348 KB n=500
35 Correct 3 ms 452 KB n=500
36 Correct 5 ms 348 KB n=500
37 Correct 5 ms 348 KB n=500
38 Correct 5 ms 348 KB n=500
39 Correct 3 ms 348 KB n=500
40 Correct 3 ms 348 KB n=500
41 Correct 4 ms 348 KB n=500
42 Correct 3 ms 348 KB n=500
43 Correct 3 ms 348 KB n=500
44 Correct 3 ms 348 KB n=500
45 Correct 1 ms 348 KB n=500
46 Correct 3 ms 348 KB n=500
47 Correct 3 ms 600 KB n=500
48 Correct 1 ms 348 KB n=500
49 Correct 3 ms 348 KB n=500
50 Correct 2 ms 348 KB n=500
51 Correct 4 ms 348 KB n=500
52 Correct 5 ms 348 KB n=500
53 Correct 3 ms 348 KB n=500
54 Correct 3 ms 348 KB n=500
55 Correct 1 ms 348 KB n=278
56 Correct 4 ms 348 KB n=500
57 Correct 4 ms 440 KB n=500
58 Correct 4 ms 348 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 1 ms 432 KB n=5
2 Correct 1 ms 348 KB n=100
3 Correct 1 ms 344 KB n=100
4 Correct 1 ms 348 KB n=100
5 Correct 1 ms 348 KB n=100
6 Correct 1 ms 344 KB n=100
7 Correct 1 ms 600 KB n=100
8 Correct 1 ms 348 KB n=100
9 Correct 1 ms 348 KB n=100
10 Correct 1 ms 348 KB n=100
11 Correct 1 ms 348 KB n=100
12 Correct 1 ms 348 KB n=100
13 Correct 0 ms 348 KB n=100
14 Correct 0 ms 348 KB n=100
15 Correct 1 ms 348 KB n=100
16 Correct 1 ms 348 KB n=100
17 Correct 1 ms 344 KB n=100
18 Correct 1 ms 348 KB n=100
19 Correct 1 ms 348 KB n=100
20 Correct 1 ms 348 KB n=100
21 Correct 0 ms 344 KB n=100
22 Correct 0 ms 348 KB n=100
23 Correct 1 ms 348 KB n=100
24 Correct 1 ms 348 KB n=100
25 Correct 1 ms 348 KB n=100
26 Correct 1 ms 348 KB n=12
27 Correct 1 ms 348 KB n=100
28 Correct 1 ms 348 KB n=500
29 Correct 3 ms 344 KB n=500
30 Correct 3 ms 348 KB n=500
31 Correct 3 ms 348 KB n=500
32 Correct 1 ms 348 KB n=500
33 Correct 2 ms 344 KB n=500
34 Correct 1 ms 348 KB n=500
35 Correct 3 ms 452 KB n=500
36 Correct 5 ms 348 KB n=500
37 Correct 5 ms 348 KB n=500
38 Correct 5 ms 348 KB n=500
39 Correct 3 ms 348 KB n=500
40 Correct 3 ms 348 KB n=500
41 Correct 4 ms 348 KB n=500
42 Correct 3 ms 348 KB n=500
43 Correct 3 ms 348 KB n=500
44 Correct 3 ms 348 KB n=500
45 Correct 1 ms 348 KB n=500
46 Correct 3 ms 348 KB n=500
47 Correct 3 ms 600 KB n=500
48 Correct 1 ms 348 KB n=500
49 Correct 3 ms 348 KB n=500
50 Correct 2 ms 348 KB n=500
51 Correct 4 ms 348 KB n=500
52 Correct 5 ms 348 KB n=500
53 Correct 3 ms 348 KB n=500
54 Correct 3 ms 348 KB n=500
55 Correct 1 ms 348 KB n=278
56 Correct 4 ms 348 KB n=500
57 Correct 4 ms 440 KB n=500
58 Correct 4 ms 348 KB n=500
59 Correct 4 ms 604 KB n=2000
60 Correct 46 ms 860 KB n=2000
61 Correct 36 ms 600 KB n=2000
62 Correct 24 ms 600 KB n=2000
63 Correct 4 ms 604 KB n=2000
64 Correct 31 ms 604 KB n=2000
65 Correct 4 ms 604 KB n=2000
66 Correct 40 ms 812 KB n=2000
67 Correct 5 ms 604 KB n=2000
68 Correct 33 ms 848 KB n=2000
69 Correct 96 ms 796 KB n=2000
70 Correct 80 ms 600 KB n=2000
71 Correct 82 ms 604 KB n=2000
72 Correct 43 ms 604 KB n=2000
73 Correct 43 ms 604 KB n=2000
74 Correct 3 ms 604 KB n=1844
75 Correct 45 ms 604 KB n=2000
76 Correct 44 ms 600 KB n=2000
77 Correct 44 ms 812 KB n=2000
78 Correct 45 ms 1056 KB n=2000
79 Correct 4 ms 604 KB n=2000
80 Correct 34 ms 860 KB n=2000
81 Correct 29 ms 604 KB n=2000
82 Correct 4 ms 600 KB n=2000
83 Correct 39 ms 600 KB n=2000
84 Correct 24 ms 604 KB n=2000
85 Correct 50 ms 600 KB n=2000
86 Correct 51 ms 856 KB n=2000
87 Correct 25 ms 600 KB n=2000
88 Correct 142 ms 860 KB n=2000
89 Correct 141 ms 856 KB n=2000
90 Correct 55 ms 856 KB n=2000
91 Correct 76 ms 808 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 1 ms 432 KB n=5
2 Correct 1 ms 348 KB n=100
3 Correct 1 ms 344 KB n=100
4 Correct 1 ms 348 KB n=100
5 Correct 1 ms 348 KB n=100
6 Correct 1 ms 344 KB n=100
7 Correct 1 ms 600 KB n=100
8 Correct 1 ms 348 KB n=100
9 Correct 1 ms 348 KB n=100
10 Correct 1 ms 348 KB n=100
11 Correct 1 ms 348 KB n=100
12 Correct 1 ms 348 KB n=100
13 Correct 0 ms 348 KB n=100
14 Correct 0 ms 348 KB n=100
15 Correct 1 ms 348 KB n=100
16 Correct 1 ms 348 KB n=100
17 Correct 1 ms 344 KB n=100
18 Correct 1 ms 348 KB n=100
19 Correct 1 ms 348 KB n=100
20 Correct 1 ms 348 KB n=100
21 Correct 0 ms 344 KB n=100
22 Correct 0 ms 348 KB n=100
23 Correct 1 ms 348 KB n=100
24 Correct 1 ms 348 KB n=100
25 Correct 1 ms 348 KB n=100
26 Correct 1 ms 348 KB n=12
27 Correct 1 ms 348 KB n=100
28 Correct 1 ms 348 KB n=500
29 Correct 3 ms 344 KB n=500
30 Correct 3 ms 348 KB n=500
31 Correct 3 ms 348 KB n=500
32 Correct 1 ms 348 KB n=500
33 Correct 2 ms 344 KB n=500
34 Correct 1 ms 348 KB n=500
35 Correct 3 ms 452 KB n=500
36 Correct 5 ms 348 KB n=500
37 Correct 5 ms 348 KB n=500
38 Correct 5 ms 348 KB n=500
39 Correct 3 ms 348 KB n=500
40 Correct 3 ms 348 KB n=500
41 Correct 4 ms 348 KB n=500
42 Correct 3 ms 348 KB n=500
43 Correct 3 ms 348 KB n=500
44 Correct 3 ms 348 KB n=500
45 Correct 1 ms 348 KB n=500
46 Correct 3 ms 348 KB n=500
47 Correct 3 ms 600 KB n=500
48 Correct 1 ms 348 KB n=500
49 Correct 3 ms 348 KB n=500
50 Correct 2 ms 348 KB n=500
51 Correct 4 ms 348 KB n=500
52 Correct 5 ms 348 KB n=500
53 Correct 3 ms 348 KB n=500
54 Correct 3 ms 348 KB n=500
55 Correct 1 ms 348 KB n=278
56 Correct 4 ms 348 KB n=500
57 Correct 4 ms 440 KB n=500
58 Correct 4 ms 348 KB n=500
59 Correct 4 ms 604 KB n=2000
60 Correct 46 ms 860 KB n=2000
61 Correct 36 ms 600 KB n=2000
62 Correct 24 ms 600 KB n=2000
63 Correct 4 ms 604 KB n=2000
64 Correct 31 ms 604 KB n=2000
65 Correct 4 ms 604 KB n=2000
66 Correct 40 ms 812 KB n=2000
67 Correct 5 ms 604 KB n=2000
68 Correct 33 ms 848 KB n=2000
69 Correct 96 ms 796 KB n=2000
70 Correct 80 ms 600 KB n=2000
71 Correct 82 ms 604 KB n=2000
72 Correct 43 ms 604 KB n=2000
73 Correct 43 ms 604 KB n=2000
74 Correct 3 ms 604 KB n=1844
75 Correct 45 ms 604 KB n=2000
76 Correct 44 ms 600 KB n=2000
77 Correct 44 ms 812 KB n=2000
78 Correct 45 ms 1056 KB n=2000
79 Correct 4 ms 604 KB n=2000
80 Correct 34 ms 860 KB n=2000
81 Correct 29 ms 604 KB n=2000
82 Correct 4 ms 600 KB n=2000
83 Correct 39 ms 600 KB n=2000
84 Correct 24 ms 604 KB n=2000
85 Correct 50 ms 600 KB n=2000
86 Correct 51 ms 856 KB n=2000
87 Correct 25 ms 600 KB n=2000
88 Correct 142 ms 860 KB n=2000
89 Correct 141 ms 856 KB n=2000
90 Correct 55 ms 856 KB n=2000
91 Correct 76 ms 808 KB n=2000
92 Correct 837 ms 51868 KB n=200000
93 Execution timed out 4051 ms 51028 KB Time limit exceeded
94 Halted 0 ms 0 KB -