Submission #379337

# Submission time Handle Problem Language Result Execution time Memory
379337 2021-03-18T02:25:36 Z balbit Birthday gift (IZhO18_treearray) C++14
100 / 100
1026 ms 70124 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define y1 zck_is_king
#define pii pair<int, int>
#define ull unsigned ll
#define f first
#define s second
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define SQ(x) (x)*(x)
#define MN(a,b) a = min(a,(__typeof__(a))(b))
#define MX(a,b) a = max(a,(__typeof__(a))(b))
#define pb push_back
#define REP(i,n) for (int i = 0; i<n; ++i)
#define RREP(i,n) for (int i = n-1; i>=0; --i)
#define REP1(i,n) for (int i = 1; i<=n; ++i)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#ifdef BALBIT
#define IOS()
#define bug(...) fprintf(stderr,"#%d (%s) = ",__LINE__,#__VA_ARGS__),_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define bug(...)
#endif

const int iinf = 1e9+10;
const ll inf = 1ll<<60;
const ll mod = 1e9+7 ;


void GG(){cout<<"0\n"; exit(0);}

ll mpow(ll a, ll n, ll mo = mod){ // a^n % mod
    ll re=1;
    while (n>0){
        if (n&1) re = re*a %mo;
        a = a*a %mo;
        n>>=1;
    }
    return re;
}

ll inv (ll b, ll mo = mod){
    if (b==1) return b;
    return (mo-mo/b) * inv(mo%b,mo) % mo;
}

const int maxn = 2e5+5;

int par[maxn], skip[maxn], dep[maxn];

vector<int> g[maxn];

void dfs(int v, int p) {
    if (p != -1) {
        dep[v] = dep[p] + 1;
        par[v] = p;
        if (dep[skip[skip[p]]] - dep[skip[p]] == dep[skip[p]] - dep[p]) {
            skip[v] = skip[skip[p]];
        }else{
            skip[v] = p;
        }
    }
    for (int u : g[v]) {
        if (u!=p) dfs(u,v);
    }
}

int lca(int a, int b) {
    if (dep[a] < dep[b]) {
        swap(a,b);
    }
    while (dep[a] > dep[b]) {
        if (dep[skip[a]]>=dep[b]) a = skip[a];
        else a = par[a];
    }
    if (a == b) return a;
    while (par[a] != par[b]) {
        if (skip[a] != skip[b]) {
            a=skip[a]; b = skip[b];
        }else{
            a=par[a]; b = par[b];
        }
    }
    return par[a];
}

int a[maxn];

set<int> where2[maxn], where1[maxn];



signed main(){
    IOS();
    int n,m,q; cin>>n>>m>>q;
    REP(i,n-1) {
        int a,b; cin>>a>>b; --a; --b;
        g[a].pb(b); g[b].pb(a);
    }
    dfs(0,-1);
//    REP(i,q){
//        int a,b; cin>>a>>b;
//        cout<<lca(a-1,b-1)+1<<endl;
//    }
    REP(i,m) {
        cin>>a[i]; --a[i];
        where1[a[i]].insert(i);
        if (i)
            where2[lca(a[i], a[i-1])].insert(i-1);
    }
    REP(x,q) {
        int tp; cin>>tp;
        if (tp == 1) {
            int i,v; cin>>i>>v; --v; --i;
            where1[a[i]].erase(i);
            if (i-1>=0)
                where2[lca(a[i], a[i-1])].erase(i-1);
            if (i+1<m)
                where2[lca(a[i], a[i+1])].erase(i);
            a[i] = v;
            where1[a[i]].insert(i);
            if (i-1>=0)
                where2[lca(a[i], a[i-1])].insert(i-1);
            if (i+1<m)
                where2[lca(a[i], a[i+1])].insert(i);
        }else{
            int l,r,v; cin>>l>>r>>v; --v;
            auto it = where1[v].lower_bound(l-1);
            if (it!=where1[v].end() && *it <= r-1) {
                cout<<*it+1<<' '<<*it+1<<endl; continue;
            }
            it = where2[v].lower_bound(l-1);
            bug(l-1, *it);
            if (it!=where2[v].end() && *it < r-1) {
                cout<<*it+1<<' '<<*it+2<<endl; continue;
            }
            cout<<-1<<' '<<-1<<endl;
        }
    }

}

/*
5 4 4
1 2
3 1
3 4
5 3
4 5 2 3
2 1 3 1
1 3 5
2 3 4 5
2 1 3 1
*/
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23788 KB n=5
2 Correct 19 ms 23788 KB n=100
3 Correct 17 ms 23788 KB n=100
4 Correct 18 ms 23788 KB n=100
5 Correct 17 ms 23788 KB n=100
6 Correct 16 ms 23788 KB n=100
7 Correct 16 ms 23788 KB n=100
8 Correct 16 ms 23788 KB n=100
9 Correct 16 ms 23788 KB n=100
10 Correct 17 ms 23788 KB n=100
11 Correct 16 ms 23788 KB n=100
12 Correct 16 ms 23788 KB n=100
13 Correct 17 ms 23788 KB n=100
14 Correct 17 ms 23788 KB n=100
15 Correct 17 ms 23788 KB n=100
16 Correct 18 ms 23808 KB n=100
17 Correct 16 ms 23788 KB n=100
18 Correct 18 ms 23788 KB n=100
19 Correct 18 ms 23788 KB n=100
20 Correct 18 ms 23788 KB n=100
21 Correct 17 ms 23788 KB n=100
22 Correct 16 ms 23788 KB n=100
23 Correct 18 ms 23788 KB n=100
24 Correct 18 ms 23788 KB n=100
25 Correct 16 ms 23788 KB n=100
26 Correct 16 ms 23788 KB n=12
27 Correct 16 ms 23788 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23788 KB n=5
2 Correct 19 ms 23788 KB n=100
3 Correct 17 ms 23788 KB n=100
4 Correct 18 ms 23788 KB n=100
5 Correct 17 ms 23788 KB n=100
6 Correct 16 ms 23788 KB n=100
7 Correct 16 ms 23788 KB n=100
8 Correct 16 ms 23788 KB n=100
9 Correct 16 ms 23788 KB n=100
10 Correct 17 ms 23788 KB n=100
11 Correct 16 ms 23788 KB n=100
12 Correct 16 ms 23788 KB n=100
13 Correct 17 ms 23788 KB n=100
14 Correct 17 ms 23788 KB n=100
15 Correct 17 ms 23788 KB n=100
16 Correct 18 ms 23808 KB n=100
17 Correct 16 ms 23788 KB n=100
18 Correct 18 ms 23788 KB n=100
19 Correct 18 ms 23788 KB n=100
20 Correct 18 ms 23788 KB n=100
21 Correct 17 ms 23788 KB n=100
22 Correct 16 ms 23788 KB n=100
23 Correct 18 ms 23788 KB n=100
24 Correct 18 ms 23788 KB n=100
25 Correct 16 ms 23788 KB n=100
26 Correct 16 ms 23788 KB n=12
27 Correct 16 ms 23788 KB n=100
28 Correct 17 ms 23916 KB n=500
29 Correct 17 ms 23916 KB n=500
30 Correct 17 ms 23916 KB n=500
31 Correct 17 ms 23916 KB n=500
32 Correct 17 ms 23916 KB n=500
33 Correct 17 ms 23916 KB n=500
34 Correct 17 ms 23916 KB n=500
35 Correct 18 ms 23916 KB n=500
36 Correct 17 ms 23916 KB n=500
37 Correct 17 ms 23916 KB n=500
38 Correct 17 ms 23916 KB n=500
39 Correct 16 ms 23916 KB n=500
40 Correct 18 ms 23916 KB n=500
41 Correct 17 ms 23916 KB n=500
42 Correct 18 ms 23916 KB n=500
43 Correct 17 ms 23916 KB n=500
44 Correct 17 ms 23916 KB n=500
45 Correct 17 ms 23916 KB n=500
46 Correct 17 ms 23916 KB n=500
47 Correct 17 ms 23916 KB n=500
48 Correct 17 ms 23916 KB n=500
49 Correct 17 ms 23916 KB n=500
50 Correct 18 ms 23916 KB n=500
51 Correct 17 ms 23916 KB n=500
52 Correct 18 ms 23916 KB n=500
53 Correct 17 ms 23916 KB n=500
54 Correct 17 ms 23916 KB n=500
55 Correct 17 ms 23916 KB n=278
56 Correct 17 ms 23916 KB n=500
57 Correct 18 ms 23916 KB n=500
58 Correct 17 ms 23916 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23788 KB n=5
2 Correct 19 ms 23788 KB n=100
3 Correct 17 ms 23788 KB n=100
4 Correct 18 ms 23788 KB n=100
5 Correct 17 ms 23788 KB n=100
6 Correct 16 ms 23788 KB n=100
7 Correct 16 ms 23788 KB n=100
8 Correct 16 ms 23788 KB n=100
9 Correct 16 ms 23788 KB n=100
10 Correct 17 ms 23788 KB n=100
11 Correct 16 ms 23788 KB n=100
12 Correct 16 ms 23788 KB n=100
13 Correct 17 ms 23788 KB n=100
14 Correct 17 ms 23788 KB n=100
15 Correct 17 ms 23788 KB n=100
16 Correct 18 ms 23808 KB n=100
17 Correct 16 ms 23788 KB n=100
18 Correct 18 ms 23788 KB n=100
19 Correct 18 ms 23788 KB n=100
20 Correct 18 ms 23788 KB n=100
21 Correct 17 ms 23788 KB n=100
22 Correct 16 ms 23788 KB n=100
23 Correct 18 ms 23788 KB n=100
24 Correct 18 ms 23788 KB n=100
25 Correct 16 ms 23788 KB n=100
26 Correct 16 ms 23788 KB n=12
27 Correct 16 ms 23788 KB n=100
28 Correct 17 ms 23916 KB n=500
29 Correct 17 ms 23916 KB n=500
30 Correct 17 ms 23916 KB n=500
31 Correct 17 ms 23916 KB n=500
32 Correct 17 ms 23916 KB n=500
33 Correct 17 ms 23916 KB n=500
34 Correct 17 ms 23916 KB n=500
35 Correct 18 ms 23916 KB n=500
36 Correct 17 ms 23916 KB n=500
37 Correct 17 ms 23916 KB n=500
38 Correct 17 ms 23916 KB n=500
39 Correct 16 ms 23916 KB n=500
40 Correct 18 ms 23916 KB n=500
41 Correct 17 ms 23916 KB n=500
42 Correct 18 ms 23916 KB n=500
43 Correct 17 ms 23916 KB n=500
44 Correct 17 ms 23916 KB n=500
45 Correct 17 ms 23916 KB n=500
46 Correct 17 ms 23916 KB n=500
47 Correct 17 ms 23916 KB n=500
48 Correct 17 ms 23916 KB n=500
49 Correct 17 ms 23916 KB n=500
50 Correct 18 ms 23916 KB n=500
51 Correct 17 ms 23916 KB n=500
52 Correct 18 ms 23916 KB n=500
53 Correct 17 ms 23916 KB n=500
54 Correct 17 ms 23916 KB n=500
55 Correct 17 ms 23916 KB n=278
56 Correct 17 ms 23916 KB n=500
57 Correct 18 ms 23916 KB n=500
58 Correct 17 ms 23916 KB n=500
59 Correct 20 ms 24172 KB n=2000
60 Correct 20 ms 24172 KB n=2000
61 Correct 21 ms 24172 KB n=2000
62 Correct 23 ms 24172 KB n=2000
63 Correct 19 ms 24172 KB n=2000
64 Correct 21 ms 24172 KB n=2000
65 Correct 20 ms 24172 KB n=2000
66 Correct 20 ms 24172 KB n=2000
67 Correct 20 ms 24172 KB n=2000
68 Correct 20 ms 24172 KB n=2000
69 Correct 19 ms 24172 KB n=2000
70 Correct 19 ms 24172 KB n=2000
71 Correct 20 ms 24172 KB n=2000
72 Correct 19 ms 24172 KB n=2000
73 Correct 19 ms 24172 KB n=2000
74 Correct 20 ms 24172 KB n=1844
75 Correct 19 ms 24172 KB n=2000
76 Correct 20 ms 24172 KB n=2000
77 Correct 22 ms 24172 KB n=2000
78 Correct 19 ms 24172 KB n=2000
79 Correct 20 ms 24172 KB n=2000
80 Correct 20 ms 24172 KB n=2000
81 Correct 20 ms 24172 KB n=2000
82 Correct 20 ms 24196 KB n=2000
83 Correct 20 ms 24172 KB n=2000
84 Correct 20 ms 24192 KB n=2000
85 Correct 20 ms 24220 KB n=2000
86 Correct 20 ms 24172 KB n=2000
87 Correct 21 ms 24172 KB n=2000
88 Correct 20 ms 24172 KB n=2000
89 Correct 21 ms 24172 KB n=2000
90 Correct 21 ms 24172 KB n=2000
91 Correct 19 ms 24172 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23788 KB n=5
2 Correct 19 ms 23788 KB n=100
3 Correct 17 ms 23788 KB n=100
4 Correct 18 ms 23788 KB n=100
5 Correct 17 ms 23788 KB n=100
6 Correct 16 ms 23788 KB n=100
7 Correct 16 ms 23788 KB n=100
8 Correct 16 ms 23788 KB n=100
9 Correct 16 ms 23788 KB n=100
10 Correct 17 ms 23788 KB n=100
11 Correct 16 ms 23788 KB n=100
12 Correct 16 ms 23788 KB n=100
13 Correct 17 ms 23788 KB n=100
14 Correct 17 ms 23788 KB n=100
15 Correct 17 ms 23788 KB n=100
16 Correct 18 ms 23808 KB n=100
17 Correct 16 ms 23788 KB n=100
18 Correct 18 ms 23788 KB n=100
19 Correct 18 ms 23788 KB n=100
20 Correct 18 ms 23788 KB n=100
21 Correct 17 ms 23788 KB n=100
22 Correct 16 ms 23788 KB n=100
23 Correct 18 ms 23788 KB n=100
24 Correct 18 ms 23788 KB n=100
25 Correct 16 ms 23788 KB n=100
26 Correct 16 ms 23788 KB n=12
27 Correct 16 ms 23788 KB n=100
28 Correct 17 ms 23916 KB n=500
29 Correct 17 ms 23916 KB n=500
30 Correct 17 ms 23916 KB n=500
31 Correct 17 ms 23916 KB n=500
32 Correct 17 ms 23916 KB n=500
33 Correct 17 ms 23916 KB n=500
34 Correct 17 ms 23916 KB n=500
35 Correct 18 ms 23916 KB n=500
36 Correct 17 ms 23916 KB n=500
37 Correct 17 ms 23916 KB n=500
38 Correct 17 ms 23916 KB n=500
39 Correct 16 ms 23916 KB n=500
40 Correct 18 ms 23916 KB n=500
41 Correct 17 ms 23916 KB n=500
42 Correct 18 ms 23916 KB n=500
43 Correct 17 ms 23916 KB n=500
44 Correct 17 ms 23916 KB n=500
45 Correct 17 ms 23916 KB n=500
46 Correct 17 ms 23916 KB n=500
47 Correct 17 ms 23916 KB n=500
48 Correct 17 ms 23916 KB n=500
49 Correct 17 ms 23916 KB n=500
50 Correct 18 ms 23916 KB n=500
51 Correct 17 ms 23916 KB n=500
52 Correct 18 ms 23916 KB n=500
53 Correct 17 ms 23916 KB n=500
54 Correct 17 ms 23916 KB n=500
55 Correct 17 ms 23916 KB n=278
56 Correct 17 ms 23916 KB n=500
57 Correct 18 ms 23916 KB n=500
58 Correct 17 ms 23916 KB n=500
59 Correct 20 ms 24172 KB n=2000
60 Correct 20 ms 24172 KB n=2000
61 Correct 21 ms 24172 KB n=2000
62 Correct 23 ms 24172 KB n=2000
63 Correct 19 ms 24172 KB n=2000
64 Correct 21 ms 24172 KB n=2000
65 Correct 20 ms 24172 KB n=2000
66 Correct 20 ms 24172 KB n=2000
67 Correct 20 ms 24172 KB n=2000
68 Correct 20 ms 24172 KB n=2000
69 Correct 19 ms 24172 KB n=2000
70 Correct 19 ms 24172 KB n=2000
71 Correct 20 ms 24172 KB n=2000
72 Correct 19 ms 24172 KB n=2000
73 Correct 19 ms 24172 KB n=2000
74 Correct 20 ms 24172 KB n=1844
75 Correct 19 ms 24172 KB n=2000
76 Correct 20 ms 24172 KB n=2000
77 Correct 22 ms 24172 KB n=2000
78 Correct 19 ms 24172 KB n=2000
79 Correct 20 ms 24172 KB n=2000
80 Correct 20 ms 24172 KB n=2000
81 Correct 20 ms 24172 KB n=2000
82 Correct 20 ms 24196 KB n=2000
83 Correct 20 ms 24172 KB n=2000
84 Correct 20 ms 24192 KB n=2000
85 Correct 20 ms 24220 KB n=2000
86 Correct 20 ms 24172 KB n=2000
87 Correct 21 ms 24172 KB n=2000
88 Correct 20 ms 24172 KB n=2000
89 Correct 21 ms 24172 KB n=2000
90 Correct 21 ms 24172 KB n=2000
91 Correct 19 ms 24172 KB n=2000
92 Correct 718 ms 60480 KB n=200000
93 Correct 885 ms 65456 KB n=200000
94 Correct 874 ms 68752 KB n=200000
95 Correct 725 ms 60256 KB n=200000
96 Correct 688 ms 60384 KB n=200000
97 Correct 943 ms 64528 KB n=200000
98 Correct 748 ms 60416 KB n=200000
99 Correct 907 ms 60480 KB n=200000
100 Correct 685 ms 60384 KB n=200000
101 Correct 815 ms 70124 KB n=200000
102 Correct 455 ms 61676 KB n=200000
103 Correct 447 ms 61548 KB n=200000
104 Correct 475 ms 61580 KB n=200000
105 Correct 460 ms 61932 KB n=200000
106 Correct 451 ms 61932 KB n=200000
107 Correct 489 ms 61932 KB n=200000
108 Correct 832 ms 60508 KB n=200000
109 Correct 874 ms 60496 KB n=200000
110 Correct 807 ms 60396 KB n=200000
111 Correct 713 ms 60004 KB n=200000
112 Correct 839 ms 69316 KB n=200000
113 Correct 889 ms 64620 KB n=200000
114 Correct 713 ms 59872 KB n=200000
115 Correct 1026 ms 62700 KB n=200000
116 Correct 649 ms 60512 KB n=200000
117 Correct 839 ms 69444 KB n=200000
118 Correct 905 ms 63340 KB n=200000
119 Correct 634 ms 60512 KB n=200000
120 Correct 798 ms 69048 KB n=200000
121 Correct 793 ms 68932 KB n=200000
122 Correct 779 ms 69380 KB n=200000
123 Correct 457 ms 61676 KB n=200000
124 Correct 230 ms 36972 KB n=25264