답안 #525926

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
525926 2022-02-13T08:24:04 Z Dilshod_Imomov Birthday gift (IZhO18_treearray) C++17
100 / 100
960 ms 85192 KB
# include <bits/stdc++.h>
# define speed ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
// # define int long long
# define fi first
# define se second
 
using namespace std;
 
const int N = 2e5 + 7;
const int mod = 1e9 + 7;
// const int INF = 1e18;

vector < int > adj[N];
set < int > pos[N], pos2[N];
int a[N], tin[N], tout[N], timer = 1, up[N][30], lg;

void dfs( int v, int pr ) {
   tin[v] = timer++;
   up[v][0] = pr;
   for ( int i = 1; i <= lg; i++ ) {
      up[v][i] = up[ up[v][i - 1] ][i - 1];
   }
   for ( auto u: adj[v] ) {
      if ( u != pr ) {
         dfs(u, v);
      }
   }
   tout[v] = timer;
}

bool is_ancestor( int v, int u ) {
   return tin[v] <= tin[u] && tout[v] >= tout[u];
}

int lca( int v, int u ) {
   if ( is_ancestor(v, u) ) {
      return v;
   }
   if ( is_ancestor(u, v) ) {
      return u;
   }
   for ( int i = lg; i >= 0; i-- ) {
      if ( !is_ancestor(up[v][i], u) ) {
         v = up[v][i];
      }
   }
   return up[v][0];
}

int32_t main() {
   speed;
   int n, m, q;
   cin >> n >> m >> q;
   lg = log2(n) + 1;
   for ( int i = 1; i < n; i++ ) {
      int u, v;
      cin >> u >> v;
      adj[u].push_back(v);
      adj[v].push_back(u);
   }
   dfs(1, 1);
   for ( int i = 1; i <= m; i++ ) {
      cin >> a[i];
   }
   for ( int i = 1; i < m; i++ ) {
      pos[ a[i] ].insert(i);
      pos2[ lca( a[i], a[i + 1] ) ].insert(i);
   }
   pos[ a[m] ].insert(m);
   for ( int i = 1; i <= q; i++ ) {
      int t;
      cin >> t;
      if ( t == 1 ) {
         int p, v;
         cin >> p >> v;
         pos[ a[p] ].erase(p);
         if ( p > 1 ) {
            pos2[ lca( a[p - 1], a[p] ) ].erase( p - 1 );
         }
         if ( p != m ) {
            pos2[ lca( a[p], a[p + 1] ) ].erase( p );
         }
         a[p] = v;
         pos[ a[p] ].insert(p);
         if ( p > 1 ) {
            pos2[ lca( a[p - 1], a[p] ) ].insert(p - 1);
         }
         if ( p != m ) {
            pos2[ lca( a[p], a[p + 1] ) ].insert(p);
         }
      }
      else {
         int l, r, v;
         cin >> l >> r >> v;
         if ( !pos[v].empty() ) {
            auto it = pos[v].lower_bound(l);
            if ( it != pos[v].end() ) {
               int j = *it;
               if ( j <= r ) {
                  cout << j << ' ' << j << "\n";
                  continue;
               }
            }
         }
         if ( !pos2[v].empty() ) {
            auto it = pos2[v].lower_bound(l);
            if ( it != pos2[v].end() ) {
               int j = *it;
               if ( j < r ) {
                  cout << j << ' ' << j + 1 << "\n";
                  continue;
               }
            }
         }
         cout << "-1 -1\n";
      }
   }
}
/*
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
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23756 KB n=5
2 Correct 12 ms 23752 KB n=100
3 Correct 12 ms 23796 KB n=100
4 Correct 12 ms 23760 KB n=100
5 Correct 12 ms 23756 KB n=100
6 Correct 12 ms 23756 KB n=100
7 Correct 12 ms 23840 KB n=100
8 Correct 12 ms 23756 KB n=100
9 Correct 12 ms 23756 KB n=100
10 Correct 12 ms 23748 KB n=100
11 Correct 12 ms 23756 KB n=100
12 Correct 12 ms 23756 KB n=100
13 Correct 12 ms 23804 KB n=100
14 Correct 12 ms 23740 KB n=100
15 Correct 12 ms 23756 KB n=100
16 Correct 12 ms 23756 KB n=100
17 Correct 12 ms 23724 KB n=100
18 Correct 12 ms 23768 KB n=100
19 Correct 12 ms 23844 KB n=100
20 Correct 12 ms 23756 KB n=100
21 Correct 12 ms 23812 KB n=100
22 Correct 12 ms 23828 KB n=100
23 Correct 12 ms 23772 KB n=100
24 Correct 12 ms 23812 KB n=100
25 Correct 12 ms 23756 KB n=100
26 Correct 12 ms 23748 KB n=12
27 Correct 12 ms 23804 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23756 KB n=5
2 Correct 12 ms 23752 KB n=100
3 Correct 12 ms 23796 KB n=100
4 Correct 12 ms 23760 KB n=100
5 Correct 12 ms 23756 KB n=100
6 Correct 12 ms 23756 KB n=100
7 Correct 12 ms 23840 KB n=100
8 Correct 12 ms 23756 KB n=100
9 Correct 12 ms 23756 KB n=100
10 Correct 12 ms 23748 KB n=100
11 Correct 12 ms 23756 KB n=100
12 Correct 12 ms 23756 KB n=100
13 Correct 12 ms 23804 KB n=100
14 Correct 12 ms 23740 KB n=100
15 Correct 12 ms 23756 KB n=100
16 Correct 12 ms 23756 KB n=100
17 Correct 12 ms 23724 KB n=100
18 Correct 12 ms 23768 KB n=100
19 Correct 12 ms 23844 KB n=100
20 Correct 12 ms 23756 KB n=100
21 Correct 12 ms 23812 KB n=100
22 Correct 12 ms 23828 KB n=100
23 Correct 12 ms 23772 KB n=100
24 Correct 12 ms 23812 KB n=100
25 Correct 12 ms 23756 KB n=100
26 Correct 12 ms 23748 KB n=12
27 Correct 12 ms 23804 KB n=100
28 Correct 12 ms 23884 KB n=500
29 Correct 12 ms 23968 KB n=500
30 Correct 14 ms 23876 KB n=500
31 Correct 13 ms 23872 KB n=500
32 Correct 12 ms 23932 KB n=500
33 Correct 13 ms 23888 KB n=500
34 Correct 14 ms 23884 KB n=500
35 Correct 13 ms 23968 KB n=500
36 Correct 13 ms 23884 KB n=500
37 Correct 13 ms 23884 KB n=500
38 Correct 14 ms 23916 KB n=500
39 Correct 13 ms 23952 KB n=500
40 Correct 13 ms 23884 KB n=500
41 Correct 16 ms 23956 KB n=500
42 Correct 16 ms 23884 KB n=500
43 Correct 13 ms 23884 KB n=500
44 Correct 12 ms 23884 KB n=500
45 Correct 13 ms 23876 KB n=500
46 Correct 12 ms 23936 KB n=500
47 Correct 12 ms 23884 KB n=500
48 Correct 13 ms 23936 KB n=500
49 Correct 13 ms 23924 KB n=500
50 Correct 13 ms 23896 KB n=500
51 Correct 12 ms 23884 KB n=500
52 Correct 13 ms 23848 KB n=500
53 Correct 13 ms 23940 KB n=500
54 Correct 14 ms 23884 KB n=500
55 Correct 13 ms 23808 KB n=278
56 Correct 13 ms 23884 KB n=500
57 Correct 13 ms 23976 KB n=500
58 Correct 13 ms 23884 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23756 KB n=5
2 Correct 12 ms 23752 KB n=100
3 Correct 12 ms 23796 KB n=100
4 Correct 12 ms 23760 KB n=100
5 Correct 12 ms 23756 KB n=100
6 Correct 12 ms 23756 KB n=100
7 Correct 12 ms 23840 KB n=100
8 Correct 12 ms 23756 KB n=100
9 Correct 12 ms 23756 KB n=100
10 Correct 12 ms 23748 KB n=100
11 Correct 12 ms 23756 KB n=100
12 Correct 12 ms 23756 KB n=100
13 Correct 12 ms 23804 KB n=100
14 Correct 12 ms 23740 KB n=100
15 Correct 12 ms 23756 KB n=100
16 Correct 12 ms 23756 KB n=100
17 Correct 12 ms 23724 KB n=100
18 Correct 12 ms 23768 KB n=100
19 Correct 12 ms 23844 KB n=100
20 Correct 12 ms 23756 KB n=100
21 Correct 12 ms 23812 KB n=100
22 Correct 12 ms 23828 KB n=100
23 Correct 12 ms 23772 KB n=100
24 Correct 12 ms 23812 KB n=100
25 Correct 12 ms 23756 KB n=100
26 Correct 12 ms 23748 KB n=12
27 Correct 12 ms 23804 KB n=100
28 Correct 12 ms 23884 KB n=500
29 Correct 12 ms 23968 KB n=500
30 Correct 14 ms 23876 KB n=500
31 Correct 13 ms 23872 KB n=500
32 Correct 12 ms 23932 KB n=500
33 Correct 13 ms 23888 KB n=500
34 Correct 14 ms 23884 KB n=500
35 Correct 13 ms 23968 KB n=500
36 Correct 13 ms 23884 KB n=500
37 Correct 13 ms 23884 KB n=500
38 Correct 14 ms 23916 KB n=500
39 Correct 13 ms 23952 KB n=500
40 Correct 13 ms 23884 KB n=500
41 Correct 16 ms 23956 KB n=500
42 Correct 16 ms 23884 KB n=500
43 Correct 13 ms 23884 KB n=500
44 Correct 12 ms 23884 KB n=500
45 Correct 13 ms 23876 KB n=500
46 Correct 12 ms 23936 KB n=500
47 Correct 12 ms 23884 KB n=500
48 Correct 13 ms 23936 KB n=500
49 Correct 13 ms 23924 KB n=500
50 Correct 13 ms 23896 KB n=500
51 Correct 12 ms 23884 KB n=500
52 Correct 13 ms 23848 KB n=500
53 Correct 13 ms 23940 KB n=500
54 Correct 14 ms 23884 KB n=500
55 Correct 13 ms 23808 KB n=278
56 Correct 13 ms 23884 KB n=500
57 Correct 13 ms 23976 KB n=500
58 Correct 13 ms 23884 KB n=500
59 Correct 14 ms 24268 KB n=2000
60 Correct 14 ms 24312 KB n=2000
61 Correct 15 ms 24292 KB n=2000
62 Correct 15 ms 24244 KB n=2000
63 Correct 15 ms 24268 KB n=2000
64 Correct 14 ms 24268 KB n=2000
65 Correct 15 ms 24268 KB n=2000
66 Correct 18 ms 24484 KB n=2000
67 Correct 15 ms 24332 KB n=2000
68 Correct 14 ms 24268 KB n=2000
69 Correct 14 ms 24312 KB n=2000
70 Correct 14 ms 24268 KB n=2000
71 Correct 14 ms 24336 KB n=2000
72 Correct 13 ms 24268 KB n=2000
73 Correct 14 ms 24340 KB n=2000
74 Correct 15 ms 24292 KB n=1844
75 Correct 14 ms 24268 KB n=2000
76 Correct 15 ms 24288 KB n=2000
77 Correct 16 ms 24268 KB n=2000
78 Correct 15 ms 24328 KB n=2000
79 Correct 14 ms 24268 KB n=2000
80 Correct 14 ms 24336 KB n=2000
81 Correct 17 ms 24312 KB n=2000
82 Correct 15 ms 24348 KB n=2000
83 Correct 17 ms 24388 KB n=2000
84 Correct 15 ms 24268 KB n=2000
85 Correct 15 ms 24268 KB n=2000
86 Correct 15 ms 24356 KB n=2000
87 Correct 17 ms 24328 KB n=2000
88 Correct 15 ms 24396 KB n=2000
89 Correct 13 ms 24376 KB n=2000
90 Correct 14 ms 24396 KB n=2000
91 Correct 14 ms 24268 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23756 KB n=5
2 Correct 12 ms 23752 KB n=100
3 Correct 12 ms 23796 KB n=100
4 Correct 12 ms 23760 KB n=100
5 Correct 12 ms 23756 KB n=100
6 Correct 12 ms 23756 KB n=100
7 Correct 12 ms 23840 KB n=100
8 Correct 12 ms 23756 KB n=100
9 Correct 12 ms 23756 KB n=100
10 Correct 12 ms 23748 KB n=100
11 Correct 12 ms 23756 KB n=100
12 Correct 12 ms 23756 KB n=100
13 Correct 12 ms 23804 KB n=100
14 Correct 12 ms 23740 KB n=100
15 Correct 12 ms 23756 KB n=100
16 Correct 12 ms 23756 KB n=100
17 Correct 12 ms 23724 KB n=100
18 Correct 12 ms 23768 KB n=100
19 Correct 12 ms 23844 KB n=100
20 Correct 12 ms 23756 KB n=100
21 Correct 12 ms 23812 KB n=100
22 Correct 12 ms 23828 KB n=100
23 Correct 12 ms 23772 KB n=100
24 Correct 12 ms 23812 KB n=100
25 Correct 12 ms 23756 KB n=100
26 Correct 12 ms 23748 KB n=12
27 Correct 12 ms 23804 KB n=100
28 Correct 12 ms 23884 KB n=500
29 Correct 12 ms 23968 KB n=500
30 Correct 14 ms 23876 KB n=500
31 Correct 13 ms 23872 KB n=500
32 Correct 12 ms 23932 KB n=500
33 Correct 13 ms 23888 KB n=500
34 Correct 14 ms 23884 KB n=500
35 Correct 13 ms 23968 KB n=500
36 Correct 13 ms 23884 KB n=500
37 Correct 13 ms 23884 KB n=500
38 Correct 14 ms 23916 KB n=500
39 Correct 13 ms 23952 KB n=500
40 Correct 13 ms 23884 KB n=500
41 Correct 16 ms 23956 KB n=500
42 Correct 16 ms 23884 KB n=500
43 Correct 13 ms 23884 KB n=500
44 Correct 12 ms 23884 KB n=500
45 Correct 13 ms 23876 KB n=500
46 Correct 12 ms 23936 KB n=500
47 Correct 12 ms 23884 KB n=500
48 Correct 13 ms 23936 KB n=500
49 Correct 13 ms 23924 KB n=500
50 Correct 13 ms 23896 KB n=500
51 Correct 12 ms 23884 KB n=500
52 Correct 13 ms 23848 KB n=500
53 Correct 13 ms 23940 KB n=500
54 Correct 14 ms 23884 KB n=500
55 Correct 13 ms 23808 KB n=278
56 Correct 13 ms 23884 KB n=500
57 Correct 13 ms 23976 KB n=500
58 Correct 13 ms 23884 KB n=500
59 Correct 14 ms 24268 KB n=2000
60 Correct 14 ms 24312 KB n=2000
61 Correct 15 ms 24292 KB n=2000
62 Correct 15 ms 24244 KB n=2000
63 Correct 15 ms 24268 KB n=2000
64 Correct 14 ms 24268 KB n=2000
65 Correct 15 ms 24268 KB n=2000
66 Correct 18 ms 24484 KB n=2000
67 Correct 15 ms 24332 KB n=2000
68 Correct 14 ms 24268 KB n=2000
69 Correct 14 ms 24312 KB n=2000
70 Correct 14 ms 24268 KB n=2000
71 Correct 14 ms 24336 KB n=2000
72 Correct 13 ms 24268 KB n=2000
73 Correct 14 ms 24340 KB n=2000
74 Correct 15 ms 24292 KB n=1844
75 Correct 14 ms 24268 KB n=2000
76 Correct 15 ms 24288 KB n=2000
77 Correct 16 ms 24268 KB n=2000
78 Correct 15 ms 24328 KB n=2000
79 Correct 14 ms 24268 KB n=2000
80 Correct 14 ms 24336 KB n=2000
81 Correct 17 ms 24312 KB n=2000
82 Correct 15 ms 24348 KB n=2000
83 Correct 17 ms 24388 KB n=2000
84 Correct 15 ms 24268 KB n=2000
85 Correct 15 ms 24268 KB n=2000
86 Correct 15 ms 24356 KB n=2000
87 Correct 17 ms 24328 KB n=2000
88 Correct 15 ms 24396 KB n=2000
89 Correct 13 ms 24376 KB n=2000
90 Correct 14 ms 24396 KB n=2000
91 Correct 14 ms 24268 KB n=2000
92 Correct 664 ms 76660 KB n=200000
93 Correct 713 ms 80352 KB n=200000
94 Correct 508 ms 83644 KB n=200000
95 Correct 654 ms 76500 KB n=200000
96 Correct 623 ms 76568 KB n=200000
97 Correct 778 ms 79564 KB n=200000
98 Correct 642 ms 76508 KB n=200000
99 Correct 786 ms 76012 KB n=200000
100 Correct 639 ms 76572 KB n=200000
101 Correct 452 ms 84932 KB n=200000
102 Correct 389 ms 77440 KB n=200000
103 Correct 391 ms 77456 KB n=200000
104 Correct 395 ms 77460 KB n=200000
105 Correct 397 ms 77260 KB n=200000
106 Correct 405 ms 77380 KB n=200000
107 Correct 396 ms 77300 KB n=200000
108 Correct 703 ms 76160 KB n=200000
109 Correct 696 ms 76148 KB n=200000
110 Correct 684 ms 76100 KB n=200000
111 Correct 688 ms 76416 KB n=200000
112 Correct 534 ms 83896 KB n=200000
113 Correct 755 ms 79404 KB n=200000
114 Correct 634 ms 76472 KB n=200000
115 Correct 960 ms 77252 KB n=200000
116 Correct 612 ms 76212 KB n=200000
117 Correct 475 ms 84216 KB n=200000
118 Correct 831 ms 78404 KB n=200000
119 Correct 624 ms 76340 KB n=200000
120 Correct 407 ms 84584 KB n=200000
121 Correct 422 ms 84636 KB n=200000
122 Correct 412 ms 85192 KB n=200000
123 Correct 408 ms 76948 KB n=200000
124 Correct 143 ms 37828 KB n=25264