답안 #1022423

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1022423 2024-07-13T13:26:53 Z hasan2006 Birthday gift (IZhO18_treearray) C++17
100 / 100
720 ms 147536 KB
#include <bits/stdc++.h>

using namespace std;

#define TL ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define rall(s) s.rbegin(),s.rend()
#define all(s) s.begin(),s.end()
#define pb push_back
#define se second
#define fi first
#define ll long long
#define ld long double
#define YES cout<<"YES\n"
#define Yes cout<<"Yes\n"
#define yes cout<<"yes\n"
#define NO cout<<"NO\n"
#define No cout<<"No\n"
#define no cout<<"no\n"


const int N = 5e5 + 9 , mod = 1e9 + 7;
ll a[N] , b[N] , dp[N] , c[N] , d[N] , p[N][25]  , us[N] , us1[N] , timer = 0;
vector<int>v[N];
void dfs(int n , int par = 0){
    p[n][0] = par;
    us[n] = ++timer;
    for(int i = 1; i <= 20; i++)
        p[n][i] = p[p[n][i - 1]][i - 1];
    for(auto to : v[n])
        if(to != par)
            dfs(to , n);
    us1[n] = ++timer;
}
bool ok(int x , int y){
    return (us[x] <= us[y] && us1[x] >= us1[y]);
}
ll lca(ll x , ll y){
    if(ok(x , y))
        return x;
    if(ok(y , x))
        return y;
    for(int i = 20; i >= 0; i--)
        if(p[x][i] && !ok(p[x][i] , y))
            x = p[x][i];
    return p[x][0];
}
set<int>st[N] , ss[N];
void solve()
{
    ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
    cin>>n>>m>>q;
    for(i = 1; i < n; i++){
        cin>>l>>r;
        v[l].pb(r);
        v[r].pb(l);
    }
    dfs(1);
    for(i = 1; i <= m; i++){
        cin>>a[i];
        ss[a[i]].insert(i);
        if(i != 1)
            b[i] = lca(a[i] , a[i - 1]) , st[b[i]].insert(i);
    }
    while(q--){
        cin>>k>>x>>y;
        if(k == 1){
            ss[a[x]].erase(x);
            a[x] = y;
            ss[a[x]].insert(x);
            if(x != m){
                st[b[x + 1]].erase(x + 1);
                b[x + 1] = lca(a[x + 1], a[x]);
                st[b[x + 1]].insert(x + 1);
            }
            if(x != 1){
                st[b[x]].erase(x);
                b[x] = lca(a[x - 1], a[x]);
                st[b[x]].insert(x);
            }
        }else {
            cin>>k;
            auto it = st[k].upper_bound(x);
            if(it != st[k].end() && *it <= y){
                cout<<*it - 1 << " "<<*it<<"\n";
                continue;
            }
            auto it1 = ss[k].upper_bound(x - 1);
            if(it1 != ss[k].end() && *it1 <= y){
                cout<<*it1 << " "<<*it1<<"\n";
                continue;
            }
            cout<<-1<<" "<<-1<<"\n";
        }
    }
}

int main(){
    TL;

    /*#ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif*/
    int t = 1;
   // cin>>t;
    while(t--)
     {
        solve();
     }
}
// Author : حسن

Compilation message

treearray.cpp: In function 'void solve()':
treearray.cpp:50:20: warning: unused variable 'j' [-Wunused-variable]
   50 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                    ^
treearray.cpp:50:38: warning: unused variable 's' [-Wunused-variable]
   50 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                                      ^
treearray.cpp:50:46: warning: unused variable 'f' [-Wunused-variable]
   50 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                                              ^
treearray.cpp:50:58: warning: unused variable 'mn' [-Wunused-variable]
   50 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                                                          ^~
treearray.cpp:50:69: warning: unused variable 'mx' [-Wunused-variable]
   50 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                                                                     ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 59216 KB n=5
2 Correct 28 ms 59224 KB n=100
3 Correct 28 ms 59224 KB n=100
4 Correct 30 ms 59228 KB n=100
5 Correct 29 ms 59228 KB n=100
6 Correct 27 ms 59064 KB n=100
7 Correct 31 ms 59156 KB n=100
8 Correct 29 ms 59184 KB n=100
9 Correct 28 ms 59216 KB n=100
10 Correct 32 ms 59156 KB n=100
11 Correct 28 ms 59216 KB n=100
12 Correct 27 ms 59220 KB n=100
13 Correct 28 ms 59216 KB n=100
14 Correct 30 ms 59220 KB n=100
15 Correct 28 ms 58968 KB n=100
16 Correct 28 ms 59228 KB n=100
17 Correct 28 ms 59228 KB n=100
18 Correct 29 ms 59228 KB n=100
19 Correct 28 ms 59228 KB n=100
20 Correct 27 ms 59216 KB n=100
21 Correct 28 ms 59228 KB n=100
22 Correct 28 ms 59228 KB n=100
23 Correct 27 ms 59216 KB n=100
24 Correct 29 ms 59216 KB n=100
25 Correct 27 ms 59228 KB n=100
26 Correct 28 ms 58972 KB n=12
27 Correct 29 ms 59220 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 59216 KB n=5
2 Correct 28 ms 59224 KB n=100
3 Correct 28 ms 59224 KB n=100
4 Correct 30 ms 59228 KB n=100
5 Correct 29 ms 59228 KB n=100
6 Correct 27 ms 59064 KB n=100
7 Correct 31 ms 59156 KB n=100
8 Correct 29 ms 59184 KB n=100
9 Correct 28 ms 59216 KB n=100
10 Correct 32 ms 59156 KB n=100
11 Correct 28 ms 59216 KB n=100
12 Correct 27 ms 59220 KB n=100
13 Correct 28 ms 59216 KB n=100
14 Correct 30 ms 59220 KB n=100
15 Correct 28 ms 58968 KB n=100
16 Correct 28 ms 59228 KB n=100
17 Correct 28 ms 59228 KB n=100
18 Correct 29 ms 59228 KB n=100
19 Correct 28 ms 59228 KB n=100
20 Correct 27 ms 59216 KB n=100
21 Correct 28 ms 59228 KB n=100
22 Correct 28 ms 59228 KB n=100
23 Correct 27 ms 59216 KB n=100
24 Correct 29 ms 59216 KB n=100
25 Correct 27 ms 59228 KB n=100
26 Correct 28 ms 58972 KB n=12
27 Correct 29 ms 59220 KB n=100
28 Correct 28 ms 59228 KB n=500
29 Correct 32 ms 59228 KB n=500
30 Correct 29 ms 59224 KB n=500
31 Correct 27 ms 59216 KB n=500
32 Correct 28 ms 59228 KB n=500
33 Correct 32 ms 59224 KB n=500
34 Correct 33 ms 59220 KB n=500
35 Correct 28 ms 59312 KB n=500
36 Correct 30 ms 59216 KB n=500
37 Correct 29 ms 59220 KB n=500
38 Correct 29 ms 59228 KB n=500
39 Correct 30 ms 59220 KB n=500
40 Correct 29 ms 59224 KB n=500
41 Correct 29 ms 59184 KB n=500
42 Correct 29 ms 59228 KB n=500
43 Correct 32 ms 59216 KB n=500
44 Correct 31 ms 59228 KB n=500
45 Correct 28 ms 59224 KB n=500
46 Correct 30 ms 59216 KB n=500
47 Correct 28 ms 59224 KB n=500
48 Correct 25 ms 59228 KB n=500
49 Correct 31 ms 59228 KB n=500
50 Correct 30 ms 59184 KB n=500
51 Correct 36 ms 59224 KB n=500
52 Correct 28 ms 59220 KB n=500
53 Correct 30 ms 59228 KB n=500
54 Correct 29 ms 59252 KB n=500
55 Correct 30 ms 59228 KB n=278
56 Correct 29 ms 59228 KB n=500
57 Correct 28 ms 59228 KB n=500
58 Correct 30 ms 59228 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 59216 KB n=5
2 Correct 28 ms 59224 KB n=100
3 Correct 28 ms 59224 KB n=100
4 Correct 30 ms 59228 KB n=100
5 Correct 29 ms 59228 KB n=100
6 Correct 27 ms 59064 KB n=100
7 Correct 31 ms 59156 KB n=100
8 Correct 29 ms 59184 KB n=100
9 Correct 28 ms 59216 KB n=100
10 Correct 32 ms 59156 KB n=100
11 Correct 28 ms 59216 KB n=100
12 Correct 27 ms 59220 KB n=100
13 Correct 28 ms 59216 KB n=100
14 Correct 30 ms 59220 KB n=100
15 Correct 28 ms 58968 KB n=100
16 Correct 28 ms 59228 KB n=100
17 Correct 28 ms 59228 KB n=100
18 Correct 29 ms 59228 KB n=100
19 Correct 28 ms 59228 KB n=100
20 Correct 27 ms 59216 KB n=100
21 Correct 28 ms 59228 KB n=100
22 Correct 28 ms 59228 KB n=100
23 Correct 27 ms 59216 KB n=100
24 Correct 29 ms 59216 KB n=100
25 Correct 27 ms 59228 KB n=100
26 Correct 28 ms 58972 KB n=12
27 Correct 29 ms 59220 KB n=100
28 Correct 28 ms 59228 KB n=500
29 Correct 32 ms 59228 KB n=500
30 Correct 29 ms 59224 KB n=500
31 Correct 27 ms 59216 KB n=500
32 Correct 28 ms 59228 KB n=500
33 Correct 32 ms 59224 KB n=500
34 Correct 33 ms 59220 KB n=500
35 Correct 28 ms 59312 KB n=500
36 Correct 30 ms 59216 KB n=500
37 Correct 29 ms 59220 KB n=500
38 Correct 29 ms 59228 KB n=500
39 Correct 30 ms 59220 KB n=500
40 Correct 29 ms 59224 KB n=500
41 Correct 29 ms 59184 KB n=500
42 Correct 29 ms 59228 KB n=500
43 Correct 32 ms 59216 KB n=500
44 Correct 31 ms 59228 KB n=500
45 Correct 28 ms 59224 KB n=500
46 Correct 30 ms 59216 KB n=500
47 Correct 28 ms 59224 KB n=500
48 Correct 25 ms 59228 KB n=500
49 Correct 31 ms 59228 KB n=500
50 Correct 30 ms 59184 KB n=500
51 Correct 36 ms 59224 KB n=500
52 Correct 28 ms 59220 KB n=500
53 Correct 30 ms 59228 KB n=500
54 Correct 29 ms 59252 KB n=500
55 Correct 30 ms 59228 KB n=278
56 Correct 29 ms 59228 KB n=500
57 Correct 28 ms 59228 KB n=500
58 Correct 30 ms 59228 KB n=500
59 Correct 38 ms 59728 KB n=2000
60 Correct 30 ms 59996 KB n=2000
61 Correct 35 ms 60200 KB n=2000
62 Correct 30 ms 59940 KB n=2000
63 Correct 31 ms 59732 KB n=2000
64 Correct 29 ms 59984 KB n=2000
65 Correct 31 ms 59728 KB n=2000
66 Correct 32 ms 59996 KB n=2000
67 Correct 32 ms 59708 KB n=2000
68 Correct 30 ms 59984 KB n=2000
69 Correct 30 ms 59912 KB n=2000
70 Correct 31 ms 59740 KB n=2000
71 Correct 31 ms 59728 KB n=2000
72 Correct 30 ms 59740 KB n=2000
73 Correct 31 ms 59736 KB n=2000
74 Correct 30 ms 59728 KB n=1844
75 Correct 33 ms 59732 KB n=2000
76 Correct 30 ms 59912 KB n=2000
77 Correct 31 ms 59740 KB n=2000
78 Correct 30 ms 59992 KB n=2000
79 Correct 30 ms 59740 KB n=2000
80 Correct 30 ms 59988 KB n=2000
81 Correct 33 ms 60240 KB n=2000
82 Correct 36 ms 60048 KB n=2000
83 Correct 31 ms 59992 KB n=2000
84 Correct 30 ms 59724 KB n=2000
85 Correct 30 ms 59996 KB n=2000
86 Correct 31 ms 59992 KB n=2000
87 Correct 31 ms 59740 KB n=2000
88 Correct 29 ms 59984 KB n=2000
89 Correct 30 ms 59984 KB n=2000
90 Correct 30 ms 59996 KB n=2000
91 Correct 38 ms 59884 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 59216 KB n=5
2 Correct 28 ms 59224 KB n=100
3 Correct 28 ms 59224 KB n=100
4 Correct 30 ms 59228 KB n=100
5 Correct 29 ms 59228 KB n=100
6 Correct 27 ms 59064 KB n=100
7 Correct 31 ms 59156 KB n=100
8 Correct 29 ms 59184 KB n=100
9 Correct 28 ms 59216 KB n=100
10 Correct 32 ms 59156 KB n=100
11 Correct 28 ms 59216 KB n=100
12 Correct 27 ms 59220 KB n=100
13 Correct 28 ms 59216 KB n=100
14 Correct 30 ms 59220 KB n=100
15 Correct 28 ms 58968 KB n=100
16 Correct 28 ms 59228 KB n=100
17 Correct 28 ms 59228 KB n=100
18 Correct 29 ms 59228 KB n=100
19 Correct 28 ms 59228 KB n=100
20 Correct 27 ms 59216 KB n=100
21 Correct 28 ms 59228 KB n=100
22 Correct 28 ms 59228 KB n=100
23 Correct 27 ms 59216 KB n=100
24 Correct 29 ms 59216 KB n=100
25 Correct 27 ms 59228 KB n=100
26 Correct 28 ms 58972 KB n=12
27 Correct 29 ms 59220 KB n=100
28 Correct 28 ms 59228 KB n=500
29 Correct 32 ms 59228 KB n=500
30 Correct 29 ms 59224 KB n=500
31 Correct 27 ms 59216 KB n=500
32 Correct 28 ms 59228 KB n=500
33 Correct 32 ms 59224 KB n=500
34 Correct 33 ms 59220 KB n=500
35 Correct 28 ms 59312 KB n=500
36 Correct 30 ms 59216 KB n=500
37 Correct 29 ms 59220 KB n=500
38 Correct 29 ms 59228 KB n=500
39 Correct 30 ms 59220 KB n=500
40 Correct 29 ms 59224 KB n=500
41 Correct 29 ms 59184 KB n=500
42 Correct 29 ms 59228 KB n=500
43 Correct 32 ms 59216 KB n=500
44 Correct 31 ms 59228 KB n=500
45 Correct 28 ms 59224 KB n=500
46 Correct 30 ms 59216 KB n=500
47 Correct 28 ms 59224 KB n=500
48 Correct 25 ms 59228 KB n=500
49 Correct 31 ms 59228 KB n=500
50 Correct 30 ms 59184 KB n=500
51 Correct 36 ms 59224 KB n=500
52 Correct 28 ms 59220 KB n=500
53 Correct 30 ms 59228 KB n=500
54 Correct 29 ms 59252 KB n=500
55 Correct 30 ms 59228 KB n=278
56 Correct 29 ms 59228 KB n=500
57 Correct 28 ms 59228 KB n=500
58 Correct 30 ms 59228 KB n=500
59 Correct 38 ms 59728 KB n=2000
60 Correct 30 ms 59996 KB n=2000
61 Correct 35 ms 60200 KB n=2000
62 Correct 30 ms 59940 KB n=2000
63 Correct 31 ms 59732 KB n=2000
64 Correct 29 ms 59984 KB n=2000
65 Correct 31 ms 59728 KB n=2000
66 Correct 32 ms 59996 KB n=2000
67 Correct 32 ms 59708 KB n=2000
68 Correct 30 ms 59984 KB n=2000
69 Correct 30 ms 59912 KB n=2000
70 Correct 31 ms 59740 KB n=2000
71 Correct 31 ms 59728 KB n=2000
72 Correct 30 ms 59740 KB n=2000
73 Correct 31 ms 59736 KB n=2000
74 Correct 30 ms 59728 KB n=1844
75 Correct 33 ms 59732 KB n=2000
76 Correct 30 ms 59912 KB n=2000
77 Correct 31 ms 59740 KB n=2000
78 Correct 30 ms 59992 KB n=2000
79 Correct 30 ms 59740 KB n=2000
80 Correct 30 ms 59988 KB n=2000
81 Correct 33 ms 60240 KB n=2000
82 Correct 36 ms 60048 KB n=2000
83 Correct 31 ms 59992 KB n=2000
84 Correct 30 ms 59724 KB n=2000
85 Correct 30 ms 59996 KB n=2000
86 Correct 31 ms 59992 KB n=2000
87 Correct 31 ms 59740 KB n=2000
88 Correct 29 ms 59984 KB n=2000
89 Correct 30 ms 59984 KB n=2000
90 Correct 30 ms 59996 KB n=2000
91 Correct 38 ms 59884 KB n=2000
92 Correct 519 ms 137972 KB n=200000
93 Correct 585 ms 142956 KB n=200000
94 Correct 428 ms 146260 KB n=200000
95 Correct 502 ms 137876 KB n=200000
96 Correct 499 ms 137924 KB n=200000
97 Correct 656 ms 142116 KB n=200000
98 Correct 489 ms 137924 KB n=200000
99 Correct 511 ms 138108 KB n=200000
100 Correct 469 ms 137932 KB n=200000
101 Correct 392 ms 147536 KB n=200000
102 Correct 324 ms 139088 KB n=200000
103 Correct 315 ms 139144 KB n=200000
104 Correct 325 ms 139092 KB n=200000
105 Correct 360 ms 139524 KB n=200000
106 Correct 329 ms 139344 KB n=200000
107 Correct 339 ms 139344 KB n=200000
108 Correct 573 ms 137876 KB n=200000
109 Correct 558 ms 137956 KB n=200000
110 Correct 499 ms 137972 KB n=200000
111 Correct 450 ms 137292 KB n=200000
112 Correct 365 ms 146516 KB n=200000
113 Correct 532 ms 141908 KB n=200000
114 Correct 448 ms 137412 KB n=200000
115 Correct 720 ms 139996 KB n=200000
116 Correct 488 ms 137920 KB n=200000
117 Correct 365 ms 146772 KB n=200000
118 Correct 613 ms 140844 KB n=200000
119 Correct 419 ms 137924 KB n=200000
120 Correct 304 ms 146764 KB n=200000
121 Correct 296 ms 146512 KB n=200000
122 Correct 305 ms 146772 KB n=200000
123 Correct 311 ms 139260 KB n=200000
124 Correct 103 ms 78164 KB n=25264