답안 #494908

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
494908 2021-12-17T11:40:07 Z Ronin13 Birthday gift (IZhO18_treearray) C++14
100 / 100
1406 ms 77728 KB
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
#define inf 1e9+1
#define linf 1e18+1
using namespace std;
const int NMAX=2e5+1;
void dfs(int v,int e=-1);
vector<vector<int> >g(NMAX);
vector<int>dp(NMAX,0);
int p[NMAX][20];
void dfs(int v,int e){
    p[v][0]=e;
    for(int to:g[v]){
        if(to!=e)dp[to]=dp[v]+1,dfs(to,v);
    }
}
int n;
void prepare(){
    dfs(1);
    for(int i=1;i<=19;i++){
        for(int j=1;j<=n;j++)p[j][i]=-1;
    }
    for(int j=1;j<20;j++){
        for(int i=1;i<=n;i++){
            if(p[i][j-1]!=-1)p[i][j]=p[p[i][j-1]][j-1];
        }
    }
}

int binary_lift(int v,int k){
    for(int j=0;j<20;j++){
        if(k&(1<<j))v=p[v][j];
        if(v==-1)break;
    }
    return v;
}

int lca(int v,int u){
    if(dp[v]>dp[u])swap(u,v);
    int x=dp[u]-dp[v];
    u=binary_lift(u,x);
    if(u==v)return v;
    for(int j=19;j>=0;j--){
        if(p[u][j]!=p[v][j]){
            v=p[v][j];
            u=p[u][j];
        }
    }
    return p[u][0];
}
int a[NMAX];
void solve(){
    cin>>n;
    int m;cin>>m;
    int q;cin>>q;
    for(int i=1;i<n;i++){
        int u,v;cin>>u>>v;
        g[u].pb(v);g[v].pb(u);
    }
    for(int i=1;i<=m;i++)cin>>a[i];
    set<int>st[n+1],st1[n+1];
    prepare();

    for(int i=1;i<m;i++){
        int x=lca(a[i+1],a[i]);
        st[x].insert(i);
    }
    for(int i=1;i<=m;i++){
        st1[a[i]].insert(i);
    }
    while(q--){
        int ind;cin>>ind;
        if(ind==1){
            int pos;cin>>pos;
            int val;cin>>val;
            if(pos>1){
                int x=lca(a[pos],a[pos-1]);
                st[x].erase(pos-1);
            }
            if(pos<m){
                int x=lca(a[pos],a[pos+1]);
                st[x].erase(pos);
            }
            st1[a[pos]].erase(pos);
            a[pos]=val;
            if(pos>1){
                int x=lca(a[pos],a[pos-1]);
                st[x].insert(pos-1);
            }
            if(pos<m){
                int x=lca(a[pos],a[pos+1]);
                st[x].insert(pos);
            }
            st1[val].insert(pos);
        }
        else{
            int l,r,v;cin>>l>>r>>v;
            if(st[v].lower_bound(l)!=st[v].lower_bound(r)){
                int x=*st[v].lower_bound(l);
                cout<<x<<' '<<x+1<<"\n";
                continue;
            }
            if(st1[v].lower_bound(l)!=st1[v].upper_bound(r)){
                int x=*st1[v].lower_bound(l);
                cout<<x<<' '<<x<<"\n";
                continue;
            }
            cout<<-1<<' '<<-1<<"\n";
        }
    }
}

int main(){
   // ios_base::sync_with_stdio(false);cin.tie(0);
    int test=1;//cin>>test;
    while(test--)solve();
}
/*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*/
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5776 KB n=5
2 Correct 5 ms 5836 KB n=100
3 Correct 4 ms 5836 KB n=100
4 Correct 4 ms 5836 KB n=100
5 Correct 4 ms 5796 KB n=100
6 Correct 3 ms 5796 KB n=100
7 Correct 3 ms 5836 KB n=100
8 Correct 4 ms 5836 KB n=100
9 Correct 5 ms 5800 KB n=100
10 Correct 4 ms 5836 KB n=100
11 Correct 3 ms 5836 KB n=100
12 Correct 4 ms 5836 KB n=100
13 Correct 3 ms 5836 KB n=100
14 Correct 3 ms 5836 KB n=100
15 Correct 4 ms 5800 KB n=100
16 Correct 4 ms 5836 KB n=100
17 Correct 3 ms 5836 KB n=100
18 Correct 4 ms 5792 KB n=100
19 Correct 4 ms 5800 KB n=100
20 Correct 3 ms 5796 KB n=100
21 Correct 4 ms 5708 KB n=100
22 Correct 3 ms 5708 KB n=100
23 Correct 3 ms 5800 KB n=100
24 Correct 3 ms 5836 KB n=100
25 Correct 4 ms 5756 KB n=100
26 Correct 3 ms 5708 KB n=12
27 Correct 4 ms 5836 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5776 KB n=5
2 Correct 5 ms 5836 KB n=100
3 Correct 4 ms 5836 KB n=100
4 Correct 4 ms 5836 KB n=100
5 Correct 4 ms 5796 KB n=100
6 Correct 3 ms 5796 KB n=100
7 Correct 3 ms 5836 KB n=100
8 Correct 4 ms 5836 KB n=100
9 Correct 5 ms 5800 KB n=100
10 Correct 4 ms 5836 KB n=100
11 Correct 3 ms 5836 KB n=100
12 Correct 4 ms 5836 KB n=100
13 Correct 3 ms 5836 KB n=100
14 Correct 3 ms 5836 KB n=100
15 Correct 4 ms 5800 KB n=100
16 Correct 4 ms 5836 KB n=100
17 Correct 3 ms 5836 KB n=100
18 Correct 4 ms 5792 KB n=100
19 Correct 4 ms 5800 KB n=100
20 Correct 3 ms 5796 KB n=100
21 Correct 4 ms 5708 KB n=100
22 Correct 3 ms 5708 KB n=100
23 Correct 3 ms 5800 KB n=100
24 Correct 3 ms 5836 KB n=100
25 Correct 4 ms 5756 KB n=100
26 Correct 3 ms 5708 KB n=12
27 Correct 4 ms 5836 KB n=100
28 Correct 5 ms 5964 KB n=500
29 Correct 5 ms 5940 KB n=500
30 Correct 5 ms 5964 KB n=500
31 Correct 5 ms 5964 KB n=500
32 Correct 4 ms 5964 KB n=500
33 Correct 5 ms 5964 KB n=500
34 Correct 4 ms 5964 KB n=500
35 Correct 5 ms 5964 KB n=500
36 Correct 4 ms 5964 KB n=500
37 Correct 5 ms 5964 KB n=500
38 Correct 5 ms 5964 KB n=500
39 Correct 5 ms 5964 KB n=500
40 Correct 5 ms 5964 KB n=500
41 Correct 5 ms 5964 KB n=500
42 Correct 5 ms 5964 KB n=500
43 Correct 5 ms 5928 KB n=500
44 Correct 5 ms 5964 KB n=500
45 Correct 4 ms 5936 KB n=500
46 Correct 5 ms 5964 KB n=500
47 Correct 5 ms 5964 KB n=500
48 Correct 5 ms 5964 KB n=500
49 Correct 5 ms 5964 KB n=500
50 Correct 5 ms 5936 KB n=500
51 Correct 5 ms 5936 KB n=500
52 Correct 5 ms 5964 KB n=500
53 Correct 4 ms 5940 KB n=500
54 Correct 5 ms 5964 KB n=500
55 Correct 4 ms 5836 KB n=278
56 Correct 4 ms 5940 KB n=500
57 Correct 5 ms 5936 KB n=500
58 Correct 4 ms 5964 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5776 KB n=5
2 Correct 5 ms 5836 KB n=100
3 Correct 4 ms 5836 KB n=100
4 Correct 4 ms 5836 KB n=100
5 Correct 4 ms 5796 KB n=100
6 Correct 3 ms 5796 KB n=100
7 Correct 3 ms 5836 KB n=100
8 Correct 4 ms 5836 KB n=100
9 Correct 5 ms 5800 KB n=100
10 Correct 4 ms 5836 KB n=100
11 Correct 3 ms 5836 KB n=100
12 Correct 4 ms 5836 KB n=100
13 Correct 3 ms 5836 KB n=100
14 Correct 3 ms 5836 KB n=100
15 Correct 4 ms 5800 KB n=100
16 Correct 4 ms 5836 KB n=100
17 Correct 3 ms 5836 KB n=100
18 Correct 4 ms 5792 KB n=100
19 Correct 4 ms 5800 KB n=100
20 Correct 3 ms 5796 KB n=100
21 Correct 4 ms 5708 KB n=100
22 Correct 3 ms 5708 KB n=100
23 Correct 3 ms 5800 KB n=100
24 Correct 3 ms 5836 KB n=100
25 Correct 4 ms 5756 KB n=100
26 Correct 3 ms 5708 KB n=12
27 Correct 4 ms 5836 KB n=100
28 Correct 5 ms 5964 KB n=500
29 Correct 5 ms 5940 KB n=500
30 Correct 5 ms 5964 KB n=500
31 Correct 5 ms 5964 KB n=500
32 Correct 4 ms 5964 KB n=500
33 Correct 5 ms 5964 KB n=500
34 Correct 4 ms 5964 KB n=500
35 Correct 5 ms 5964 KB n=500
36 Correct 4 ms 5964 KB n=500
37 Correct 5 ms 5964 KB n=500
38 Correct 5 ms 5964 KB n=500
39 Correct 5 ms 5964 KB n=500
40 Correct 5 ms 5964 KB n=500
41 Correct 5 ms 5964 KB n=500
42 Correct 5 ms 5964 KB n=500
43 Correct 5 ms 5928 KB n=500
44 Correct 5 ms 5964 KB n=500
45 Correct 4 ms 5936 KB n=500
46 Correct 5 ms 5964 KB n=500
47 Correct 5 ms 5964 KB n=500
48 Correct 5 ms 5964 KB n=500
49 Correct 5 ms 5964 KB n=500
50 Correct 5 ms 5936 KB n=500
51 Correct 5 ms 5936 KB n=500
52 Correct 5 ms 5964 KB n=500
53 Correct 4 ms 5940 KB n=500
54 Correct 5 ms 5964 KB n=500
55 Correct 4 ms 5836 KB n=278
56 Correct 4 ms 5940 KB n=500
57 Correct 5 ms 5936 KB n=500
58 Correct 4 ms 5964 KB n=500
59 Correct 9 ms 6476 KB n=2000
60 Correct 9 ms 6476 KB n=2000
61 Correct 10 ms 6456 KB n=2000
62 Correct 11 ms 6476 KB n=2000
63 Correct 9 ms 6348 KB n=2000
64 Correct 11 ms 6616 KB n=2000
65 Correct 12 ms 6452 KB n=2000
66 Correct 13 ms 6476 KB n=2000
67 Correct 10 ms 6452 KB n=2000
68 Correct 9 ms 6512 KB n=2000
69 Correct 10 ms 6388 KB n=2000
70 Correct 11 ms 6408 KB n=2000
71 Correct 9 ms 6476 KB n=2000
72 Correct 10 ms 6420 KB n=2000
73 Correct 10 ms 6452 KB n=2000
74 Correct 10 ms 6476 KB n=1844
75 Correct 11 ms 6576 KB n=2000
76 Correct 9 ms 6472 KB n=2000
77 Correct 9 ms 6448 KB n=2000
78 Correct 9 ms 6476 KB n=2000
79 Correct 9 ms 6476 KB n=2000
80 Correct 9 ms 6544 KB n=2000
81 Correct 10 ms 6476 KB n=2000
82 Correct 9 ms 6452 KB n=2000
83 Correct 9 ms 6448 KB n=2000
84 Correct 9 ms 6448 KB n=2000
85 Correct 9 ms 6476 KB n=2000
86 Correct 9 ms 6476 KB n=2000
87 Correct 9 ms 6452 KB n=2000
88 Correct 10 ms 6556 KB n=2000
89 Correct 9 ms 6456 KB n=2000
90 Correct 11 ms 6476 KB n=2000
91 Correct 10 ms 6452 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5776 KB n=5
2 Correct 5 ms 5836 KB n=100
3 Correct 4 ms 5836 KB n=100
4 Correct 4 ms 5836 KB n=100
5 Correct 4 ms 5796 KB n=100
6 Correct 3 ms 5796 KB n=100
7 Correct 3 ms 5836 KB n=100
8 Correct 4 ms 5836 KB n=100
9 Correct 5 ms 5800 KB n=100
10 Correct 4 ms 5836 KB n=100
11 Correct 3 ms 5836 KB n=100
12 Correct 4 ms 5836 KB n=100
13 Correct 3 ms 5836 KB n=100
14 Correct 3 ms 5836 KB n=100
15 Correct 4 ms 5800 KB n=100
16 Correct 4 ms 5836 KB n=100
17 Correct 3 ms 5836 KB n=100
18 Correct 4 ms 5792 KB n=100
19 Correct 4 ms 5800 KB n=100
20 Correct 3 ms 5796 KB n=100
21 Correct 4 ms 5708 KB n=100
22 Correct 3 ms 5708 KB n=100
23 Correct 3 ms 5800 KB n=100
24 Correct 3 ms 5836 KB n=100
25 Correct 4 ms 5756 KB n=100
26 Correct 3 ms 5708 KB n=12
27 Correct 4 ms 5836 KB n=100
28 Correct 5 ms 5964 KB n=500
29 Correct 5 ms 5940 KB n=500
30 Correct 5 ms 5964 KB n=500
31 Correct 5 ms 5964 KB n=500
32 Correct 4 ms 5964 KB n=500
33 Correct 5 ms 5964 KB n=500
34 Correct 4 ms 5964 KB n=500
35 Correct 5 ms 5964 KB n=500
36 Correct 4 ms 5964 KB n=500
37 Correct 5 ms 5964 KB n=500
38 Correct 5 ms 5964 KB n=500
39 Correct 5 ms 5964 KB n=500
40 Correct 5 ms 5964 KB n=500
41 Correct 5 ms 5964 KB n=500
42 Correct 5 ms 5964 KB n=500
43 Correct 5 ms 5928 KB n=500
44 Correct 5 ms 5964 KB n=500
45 Correct 4 ms 5936 KB n=500
46 Correct 5 ms 5964 KB n=500
47 Correct 5 ms 5964 KB n=500
48 Correct 5 ms 5964 KB n=500
49 Correct 5 ms 5964 KB n=500
50 Correct 5 ms 5936 KB n=500
51 Correct 5 ms 5936 KB n=500
52 Correct 5 ms 5964 KB n=500
53 Correct 4 ms 5940 KB n=500
54 Correct 5 ms 5964 KB n=500
55 Correct 4 ms 5836 KB n=278
56 Correct 4 ms 5940 KB n=500
57 Correct 5 ms 5936 KB n=500
58 Correct 4 ms 5964 KB n=500
59 Correct 9 ms 6476 KB n=2000
60 Correct 9 ms 6476 KB n=2000
61 Correct 10 ms 6456 KB n=2000
62 Correct 11 ms 6476 KB n=2000
63 Correct 9 ms 6348 KB n=2000
64 Correct 11 ms 6616 KB n=2000
65 Correct 12 ms 6452 KB n=2000
66 Correct 13 ms 6476 KB n=2000
67 Correct 10 ms 6452 KB n=2000
68 Correct 9 ms 6512 KB n=2000
69 Correct 10 ms 6388 KB n=2000
70 Correct 11 ms 6408 KB n=2000
71 Correct 9 ms 6476 KB n=2000
72 Correct 10 ms 6420 KB n=2000
73 Correct 10 ms 6452 KB n=2000
74 Correct 10 ms 6476 KB n=1844
75 Correct 11 ms 6576 KB n=2000
76 Correct 9 ms 6472 KB n=2000
77 Correct 9 ms 6448 KB n=2000
78 Correct 9 ms 6476 KB n=2000
79 Correct 9 ms 6476 KB n=2000
80 Correct 9 ms 6544 KB n=2000
81 Correct 10 ms 6476 KB n=2000
82 Correct 9 ms 6452 KB n=2000
83 Correct 9 ms 6448 KB n=2000
84 Correct 9 ms 6448 KB n=2000
85 Correct 9 ms 6476 KB n=2000
86 Correct 9 ms 6476 KB n=2000
87 Correct 9 ms 6452 KB n=2000
88 Correct 10 ms 6556 KB n=2000
89 Correct 9 ms 6456 KB n=2000
90 Correct 11 ms 6476 KB n=2000
91 Correct 10 ms 6452 KB n=2000
92 Correct 1134 ms 69364 KB n=200000
93 Correct 1357 ms 73060 KB n=200000
94 Correct 1259 ms 76496 KB n=200000
95 Correct 1102 ms 69332 KB n=200000
96 Correct 1076 ms 69160 KB n=200000
97 Correct 1365 ms 72344 KB n=200000
98 Correct 1147 ms 69084 KB n=200000
99 Correct 1298 ms 68664 KB n=200000
100 Correct 1156 ms 69288 KB n=200000
101 Correct 1303 ms 77728 KB n=200000
102 Correct 884 ms 69800 KB n=200000
103 Correct 895 ms 69852 KB n=200000
104 Correct 882 ms 69888 KB n=200000
105 Correct 913 ms 69696 KB n=200000
106 Correct 896 ms 69776 KB n=200000
107 Correct 919 ms 69772 KB n=200000
108 Correct 1170 ms 68484 KB n=200000
109 Correct 1161 ms 68468 KB n=200000
110 Correct 1200 ms 68800 KB n=200000
111 Correct 1106 ms 68868 KB n=200000
112 Correct 1260 ms 76448 KB n=200000
113 Correct 1406 ms 72032 KB n=200000
114 Correct 1069 ms 68932 KB n=200000
115 Correct 1396 ms 69784 KB n=200000
116 Correct 1020 ms 68564 KB n=200000
117 Correct 1242 ms 76364 KB n=200000
118 Correct 1328 ms 70384 KB n=200000
119 Correct 980 ms 68400 KB n=200000
120 Correct 1174 ms 76696 KB n=200000
121 Correct 1172 ms 76620 KB n=200000
122 Correct 1187 ms 77020 KB n=200000
123 Correct 933 ms 68728 KB n=200000
124 Correct 286 ms 21956 KB n=25264