Submission #525923

# Submission time Handle Problem Language Result Execution time Memory
525923 2022-02-13T08:21:18 Z Dilshod_Imomov Birthday gift (IZhO18_treearray) C++17
100 / 100
1058 ms 118476 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
*/
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23756 KB n=5
2 Correct 13 ms 23756 KB n=100
3 Correct 13 ms 23772 KB n=100
4 Correct 15 ms 23844 KB n=100
5 Correct 13 ms 23816 KB n=100
6 Correct 12 ms 23872 KB n=100
7 Correct 15 ms 23832 KB n=100
8 Correct 12 ms 23868 KB n=100
9 Correct 12 ms 23756 KB n=100
10 Correct 13 ms 23756 KB n=100
11 Correct 13 ms 23756 KB n=100
12 Correct 12 ms 23884 KB n=100
13 Correct 12 ms 23756 KB n=100
14 Correct 12 ms 23752 KB n=100
15 Correct 14 ms 23864 KB n=100
16 Correct 15 ms 23856 KB n=100
17 Correct 12 ms 23756 KB n=100
18 Correct 12 ms 23756 KB n=100
19 Correct 12 ms 23756 KB n=100
20 Correct 14 ms 23864 KB n=100
21 Correct 12 ms 23800 KB n=100
22 Correct 14 ms 23772 KB n=100
23 Correct 12 ms 23768 KB n=100
24 Correct 12 ms 23756 KB n=100
25 Correct 14 ms 23756 KB n=100
26 Correct 14 ms 23756 KB n=12
27 Correct 13 ms 23780 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23756 KB n=5
2 Correct 13 ms 23756 KB n=100
3 Correct 13 ms 23772 KB n=100
4 Correct 15 ms 23844 KB n=100
5 Correct 13 ms 23816 KB n=100
6 Correct 12 ms 23872 KB n=100
7 Correct 15 ms 23832 KB n=100
8 Correct 12 ms 23868 KB n=100
9 Correct 12 ms 23756 KB n=100
10 Correct 13 ms 23756 KB n=100
11 Correct 13 ms 23756 KB n=100
12 Correct 12 ms 23884 KB n=100
13 Correct 12 ms 23756 KB n=100
14 Correct 12 ms 23752 KB n=100
15 Correct 14 ms 23864 KB n=100
16 Correct 15 ms 23856 KB n=100
17 Correct 12 ms 23756 KB n=100
18 Correct 12 ms 23756 KB n=100
19 Correct 12 ms 23756 KB n=100
20 Correct 14 ms 23864 KB n=100
21 Correct 12 ms 23800 KB n=100
22 Correct 14 ms 23772 KB n=100
23 Correct 12 ms 23768 KB n=100
24 Correct 12 ms 23756 KB n=100
25 Correct 14 ms 23756 KB n=100
26 Correct 14 ms 23756 KB n=12
27 Correct 13 ms 23780 KB n=100
28 Correct 13 ms 24024 KB n=500
29 Correct 12 ms 24012 KB n=500
30 Correct 13 ms 24036 KB n=500
31 Correct 13 ms 23944 KB n=500
32 Correct 13 ms 23916 KB n=500
33 Correct 13 ms 24012 KB n=500
34 Correct 13 ms 24012 KB n=500
35 Correct 14 ms 24028 KB n=500
36 Correct 14 ms 23928 KB n=500
37 Correct 13 ms 24012 KB n=500
38 Correct 13 ms 24012 KB n=500
39 Correct 18 ms 23936 KB n=500
40 Correct 13 ms 23952 KB n=500
41 Correct 16 ms 23956 KB n=500
42 Correct 13 ms 23928 KB n=500
43 Correct 13 ms 23956 KB n=500
44 Correct 13 ms 23944 KB n=500
45 Correct 16 ms 23952 KB n=500
46 Correct 14 ms 24016 KB n=500
47 Correct 14 ms 23936 KB n=500
48 Correct 14 ms 23940 KB n=500
49 Correct 13 ms 24012 KB n=500
50 Correct 13 ms 23940 KB n=500
51 Correct 13 ms 23944 KB n=500
52 Correct 13 ms 23976 KB n=500
53 Correct 13 ms 24028 KB n=500
54 Correct 13 ms 24012 KB n=500
55 Correct 13 ms 23884 KB n=278
56 Correct 12 ms 24008 KB n=500
57 Correct 17 ms 24012 KB n=500
58 Correct 13 ms 24012 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23756 KB n=5
2 Correct 13 ms 23756 KB n=100
3 Correct 13 ms 23772 KB n=100
4 Correct 15 ms 23844 KB n=100
5 Correct 13 ms 23816 KB n=100
6 Correct 12 ms 23872 KB n=100
7 Correct 15 ms 23832 KB n=100
8 Correct 12 ms 23868 KB n=100
9 Correct 12 ms 23756 KB n=100
10 Correct 13 ms 23756 KB n=100
11 Correct 13 ms 23756 KB n=100
12 Correct 12 ms 23884 KB n=100
13 Correct 12 ms 23756 KB n=100
14 Correct 12 ms 23752 KB n=100
15 Correct 14 ms 23864 KB n=100
16 Correct 15 ms 23856 KB n=100
17 Correct 12 ms 23756 KB n=100
18 Correct 12 ms 23756 KB n=100
19 Correct 12 ms 23756 KB n=100
20 Correct 14 ms 23864 KB n=100
21 Correct 12 ms 23800 KB n=100
22 Correct 14 ms 23772 KB n=100
23 Correct 12 ms 23768 KB n=100
24 Correct 12 ms 23756 KB n=100
25 Correct 14 ms 23756 KB n=100
26 Correct 14 ms 23756 KB n=12
27 Correct 13 ms 23780 KB n=100
28 Correct 13 ms 24024 KB n=500
29 Correct 12 ms 24012 KB n=500
30 Correct 13 ms 24036 KB n=500
31 Correct 13 ms 23944 KB n=500
32 Correct 13 ms 23916 KB n=500
33 Correct 13 ms 24012 KB n=500
34 Correct 13 ms 24012 KB n=500
35 Correct 14 ms 24028 KB n=500
36 Correct 14 ms 23928 KB n=500
37 Correct 13 ms 24012 KB n=500
38 Correct 13 ms 24012 KB n=500
39 Correct 18 ms 23936 KB n=500
40 Correct 13 ms 23952 KB n=500
41 Correct 16 ms 23956 KB n=500
42 Correct 13 ms 23928 KB n=500
43 Correct 13 ms 23956 KB n=500
44 Correct 13 ms 23944 KB n=500
45 Correct 16 ms 23952 KB n=500
46 Correct 14 ms 24016 KB n=500
47 Correct 14 ms 23936 KB n=500
48 Correct 14 ms 23940 KB n=500
49 Correct 13 ms 24012 KB n=500
50 Correct 13 ms 23940 KB n=500
51 Correct 13 ms 23944 KB n=500
52 Correct 13 ms 23976 KB n=500
53 Correct 13 ms 24028 KB n=500
54 Correct 13 ms 24012 KB n=500
55 Correct 13 ms 23884 KB n=278
56 Correct 12 ms 24008 KB n=500
57 Correct 17 ms 24012 KB n=500
58 Correct 13 ms 24012 KB n=500
59 Correct 16 ms 24644 KB n=2000
60 Correct 16 ms 24652 KB n=2000
61 Correct 18 ms 24616 KB n=2000
62 Correct 17 ms 24612 KB n=2000
63 Correct 16 ms 24652 KB n=2000
64 Correct 16 ms 24708 KB n=2000
65 Correct 15 ms 24652 KB n=2000
66 Correct 15 ms 24652 KB n=2000
67 Correct 15 ms 24584 KB n=2000
68 Correct 15 ms 24708 KB n=2000
69 Correct 15 ms 24652 KB n=2000
70 Correct 16 ms 24576 KB n=2000
71 Correct 15 ms 24652 KB n=2000
72 Correct 14 ms 24652 KB n=2000
73 Correct 15 ms 24652 KB n=2000
74 Correct 20 ms 24512 KB n=1844
75 Correct 15 ms 24668 KB n=2000
76 Correct 16 ms 24644 KB n=2000
77 Correct 16 ms 24652 KB n=2000
78 Correct 17 ms 24652 KB n=2000
79 Correct 19 ms 24652 KB n=2000
80 Correct 17 ms 24652 KB n=2000
81 Correct 20 ms 24580 KB n=2000
82 Correct 15 ms 24584 KB n=2000
83 Correct 15 ms 24652 KB n=2000
84 Correct 15 ms 24592 KB n=2000
85 Correct 17 ms 24584 KB n=2000
86 Correct 16 ms 24588 KB n=2000
87 Correct 15 ms 24652 KB n=2000
88 Correct 14 ms 24652 KB n=2000
89 Correct 19 ms 24712 KB n=2000
90 Correct 17 ms 24712 KB n=2000
91 Correct 15 ms 24640 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23756 KB n=5
2 Correct 13 ms 23756 KB n=100
3 Correct 13 ms 23772 KB n=100
4 Correct 15 ms 23844 KB n=100
5 Correct 13 ms 23816 KB n=100
6 Correct 12 ms 23872 KB n=100
7 Correct 15 ms 23832 KB n=100
8 Correct 12 ms 23868 KB n=100
9 Correct 12 ms 23756 KB n=100
10 Correct 13 ms 23756 KB n=100
11 Correct 13 ms 23756 KB n=100
12 Correct 12 ms 23884 KB n=100
13 Correct 12 ms 23756 KB n=100
14 Correct 12 ms 23752 KB n=100
15 Correct 14 ms 23864 KB n=100
16 Correct 15 ms 23856 KB n=100
17 Correct 12 ms 23756 KB n=100
18 Correct 12 ms 23756 KB n=100
19 Correct 12 ms 23756 KB n=100
20 Correct 14 ms 23864 KB n=100
21 Correct 12 ms 23800 KB n=100
22 Correct 14 ms 23772 KB n=100
23 Correct 12 ms 23768 KB n=100
24 Correct 12 ms 23756 KB n=100
25 Correct 14 ms 23756 KB n=100
26 Correct 14 ms 23756 KB n=12
27 Correct 13 ms 23780 KB n=100
28 Correct 13 ms 24024 KB n=500
29 Correct 12 ms 24012 KB n=500
30 Correct 13 ms 24036 KB n=500
31 Correct 13 ms 23944 KB n=500
32 Correct 13 ms 23916 KB n=500
33 Correct 13 ms 24012 KB n=500
34 Correct 13 ms 24012 KB n=500
35 Correct 14 ms 24028 KB n=500
36 Correct 14 ms 23928 KB n=500
37 Correct 13 ms 24012 KB n=500
38 Correct 13 ms 24012 KB n=500
39 Correct 18 ms 23936 KB n=500
40 Correct 13 ms 23952 KB n=500
41 Correct 16 ms 23956 KB n=500
42 Correct 13 ms 23928 KB n=500
43 Correct 13 ms 23956 KB n=500
44 Correct 13 ms 23944 KB n=500
45 Correct 16 ms 23952 KB n=500
46 Correct 14 ms 24016 KB n=500
47 Correct 14 ms 23936 KB n=500
48 Correct 14 ms 23940 KB n=500
49 Correct 13 ms 24012 KB n=500
50 Correct 13 ms 23940 KB n=500
51 Correct 13 ms 23944 KB n=500
52 Correct 13 ms 23976 KB n=500
53 Correct 13 ms 24028 KB n=500
54 Correct 13 ms 24012 KB n=500
55 Correct 13 ms 23884 KB n=278
56 Correct 12 ms 24008 KB n=500
57 Correct 17 ms 24012 KB n=500
58 Correct 13 ms 24012 KB n=500
59 Correct 16 ms 24644 KB n=2000
60 Correct 16 ms 24652 KB n=2000
61 Correct 18 ms 24616 KB n=2000
62 Correct 17 ms 24612 KB n=2000
63 Correct 16 ms 24652 KB n=2000
64 Correct 16 ms 24708 KB n=2000
65 Correct 15 ms 24652 KB n=2000
66 Correct 15 ms 24652 KB n=2000
67 Correct 15 ms 24584 KB n=2000
68 Correct 15 ms 24708 KB n=2000
69 Correct 15 ms 24652 KB n=2000
70 Correct 16 ms 24576 KB n=2000
71 Correct 15 ms 24652 KB n=2000
72 Correct 14 ms 24652 KB n=2000
73 Correct 15 ms 24652 KB n=2000
74 Correct 20 ms 24512 KB n=1844
75 Correct 15 ms 24668 KB n=2000
76 Correct 16 ms 24644 KB n=2000
77 Correct 16 ms 24652 KB n=2000
78 Correct 17 ms 24652 KB n=2000
79 Correct 19 ms 24652 KB n=2000
80 Correct 17 ms 24652 KB n=2000
81 Correct 20 ms 24580 KB n=2000
82 Correct 15 ms 24584 KB n=2000
83 Correct 15 ms 24652 KB n=2000
84 Correct 15 ms 24592 KB n=2000
85 Correct 17 ms 24584 KB n=2000
86 Correct 16 ms 24588 KB n=2000
87 Correct 15 ms 24652 KB n=2000
88 Correct 14 ms 24652 KB n=2000
89 Correct 19 ms 24712 KB n=2000
90 Correct 17 ms 24712 KB n=2000
91 Correct 15 ms 24640 KB n=2000
92 Correct 718 ms 109828 KB n=200000
93 Correct 840 ms 114688 KB n=200000
94 Correct 638 ms 117632 KB n=200000
95 Correct 673 ms 109632 KB n=200000
96 Correct 684 ms 109544 KB n=200000
97 Correct 922 ms 113800 KB n=200000
98 Correct 690 ms 109636 KB n=200000
99 Correct 871 ms 110172 KB n=200000
100 Correct 678 ms 109564 KB n=200000
101 Correct 507 ms 118476 KB n=200000
102 Correct 435 ms 110980 KB n=200000
103 Correct 441 ms 110948 KB n=200000
104 Correct 424 ms 110976 KB n=200000
105 Correct 481 ms 111416 KB n=200000
106 Correct 418 ms 111400 KB n=200000
107 Correct 439 ms 111376 KB n=200000
108 Correct 817 ms 109960 KB n=200000
109 Correct 827 ms 109892 KB n=200000
110 Correct 796 ms 109884 KB n=200000
111 Correct 701 ms 109060 KB n=200000
112 Correct 555 ms 117756 KB n=200000
113 Correct 872 ms 113776 KB n=200000
114 Correct 672 ms 109196 KB n=200000
115 Correct 1058 ms 111784 KB n=200000
116 Correct 675 ms 109736 KB n=200000
117 Correct 509 ms 117884 KB n=200000
118 Correct 958 ms 112760 KB n=200000
119 Correct 641 ms 109616 KB n=200000
120 Correct 448 ms 117424 KB n=200000
121 Correct 443 ms 117444 KB n=200000
122 Correct 464 ms 117728 KB n=200000
123 Correct 437 ms 111120 KB n=200000
124 Correct 158 ms 43348 KB n=25264