답안 #525922

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
525922 2022-02-13T08:19:50 Z Dilshod_Imomov Birthday gift (IZhO18_treearray) C++17
0 / 100
15 ms 23844 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);
   }
   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 13 ms 23844 KB n=100
3 Correct 12 ms 23804 KB n=100
4 Correct 13 ms 23784 KB n=100
5 Correct 13 ms 23756 KB n=100
6 Correct 12 ms 23768 KB n=100
7 Correct 12 ms 23812 KB n=100
8 Correct 12 ms 23800 KB n=100
9 Correct 12 ms 23836 KB n=100
10 Correct 13 ms 23756 KB n=100
11 Correct 12 ms 23756 KB n=100
12 Correct 13 ms 23756 KB n=100
13 Correct 12 ms 23756 KB n=100
14 Correct 12 ms 23756 KB n=100
15 Correct 15 ms 23804 KB n=100
16 Correct 12 ms 23756 KB n=100
17 Incorrect 13 ms 23756 KB Jury has the answer but participant has not
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23756 KB n=5
2 Correct 13 ms 23844 KB n=100
3 Correct 12 ms 23804 KB n=100
4 Correct 13 ms 23784 KB n=100
5 Correct 13 ms 23756 KB n=100
6 Correct 12 ms 23768 KB n=100
7 Correct 12 ms 23812 KB n=100
8 Correct 12 ms 23800 KB n=100
9 Correct 12 ms 23836 KB n=100
10 Correct 13 ms 23756 KB n=100
11 Correct 12 ms 23756 KB n=100
12 Correct 13 ms 23756 KB n=100
13 Correct 12 ms 23756 KB n=100
14 Correct 12 ms 23756 KB n=100
15 Correct 15 ms 23804 KB n=100
16 Correct 12 ms 23756 KB n=100
17 Incorrect 13 ms 23756 KB Jury has the answer but participant has not
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23756 KB n=5
2 Correct 13 ms 23844 KB n=100
3 Correct 12 ms 23804 KB n=100
4 Correct 13 ms 23784 KB n=100
5 Correct 13 ms 23756 KB n=100
6 Correct 12 ms 23768 KB n=100
7 Correct 12 ms 23812 KB n=100
8 Correct 12 ms 23800 KB n=100
9 Correct 12 ms 23836 KB n=100
10 Correct 13 ms 23756 KB n=100
11 Correct 12 ms 23756 KB n=100
12 Correct 13 ms 23756 KB n=100
13 Correct 12 ms 23756 KB n=100
14 Correct 12 ms 23756 KB n=100
15 Correct 15 ms 23804 KB n=100
16 Correct 12 ms 23756 KB n=100
17 Incorrect 13 ms 23756 KB Jury has the answer but participant has not
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23756 KB n=5
2 Correct 13 ms 23844 KB n=100
3 Correct 12 ms 23804 KB n=100
4 Correct 13 ms 23784 KB n=100
5 Correct 13 ms 23756 KB n=100
6 Correct 12 ms 23768 KB n=100
7 Correct 12 ms 23812 KB n=100
8 Correct 12 ms 23800 KB n=100
9 Correct 12 ms 23836 KB n=100
10 Correct 13 ms 23756 KB n=100
11 Correct 12 ms 23756 KB n=100
12 Correct 13 ms 23756 KB n=100
13 Correct 12 ms 23756 KB n=100
14 Correct 12 ms 23756 KB n=100
15 Correct 15 ms 23804 KB n=100
16 Correct 12 ms 23756 KB n=100
17 Incorrect 13 ms 23756 KB Jury has the answer but participant has not
18 Halted 0 ms 0 KB -