Submission #528973

# Submission time Handle Problem Language Result Execution time Memory
528973 2022-02-21T20:51:37 Z Alex_tz307 Birthday gift (IZhO18_treearray) C++17
100 / 100
858 ms 82136 KB
#include <bits/stdc++.h>

using namespace std;

const int kN = 2e5;
const int kLog = 17;
int n, m, q, timer, a[1 + kN], tin[1 + kN], tout[1 + kN], depth[1 + kN], lg2[1 + kN], anc[1 + kN][1 + kLog];
vector<int> g[1 + kN];
set<int> one[1 + kN], two[1 + kN];

void precalc() {
  for (int i = 2; i <= kN; ++i) {
    lg2[i] = lg2[i / 2] + 1;
  }
}

void dfs(int u) {
  tin[u] = ++timer;
  for (int i = 1; i <= kLog; ++i) {
    anc[u][i] = anc[anc[u][i - 1]][i - 1];
    if (anc[u][i] == 0) {
      break;
    }
  }
  for (int v : g[u]) {
    if (v != anc[u][0]) {
      anc[v][0] = u;
      depth[v] = depth[u] + 1;
      dfs(v);
    }
  }
  tout[u] = timer;
}

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

int getLca(int u, int v) {
  if (depth[u] < depth[v]) {
    swap(u, v);
  }
  if (isAncestor(v, u)) {
    return v;
  }
  for (int i = lg2[depth[v]]; i >= 0; --i) {
    if (anc[v][i] && !isAncestor(anc[v][i], u)) {
      v = anc[v][i];
    }
  }
  return anc[v][0];
}

void testCase() {
  cin >> n >> m >> q;
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].emplace_back(v);
    g[v].emplace_back(u);
  }
  dfs(1);
  for (int i = 1; i <= m; ++i) {
    cin >> a[i];
    one[a[i]].emplace(i);
    if (i > 1) {
      two[getLca(a[i - 1], a[i])].emplace(i - 1);
    }
  }
  for (int i = 0; i < q; ++i) {
    char op;
    cin >> op;
    if (op == '1') {
      int pos, v;
      cin >> pos >> v;
      one[a[pos]].erase(pos);
      if (pos > 1) {
        two[getLca(a[pos - 1], a[pos])].erase(pos - 1);
      }
      if (pos < m) {
        two[getLca(a[pos], a[pos + 1])].erase(pos);
      }
      a[pos] = v;
      one[a[pos]].emplace(pos);
      if (pos > 1) {
        two[getLca(a[pos - 1], a[pos])].emplace(pos - 1);
      }
      if (pos < m) {
        two[getLca(a[pos], a[pos + 1])].emplace(pos);
      }
    } else {
      int l, r, v;
      cin >> l >> r >> v;
      auto it = one[v].lower_bound(l);
      if (it != one[v].end() && *it <= r) {
        cout << *it << ' ' << *it << '\n';
      } else {
        it = two[v].lower_bound(l);
        if (it != two[v].end() && *it < r) {
          cout << *it << ' ' << *it + 1 << '\n';
        } else {
          cout << "-1 -1\n";
        }
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  precalc();
  int tests = 1;
  for (int tc = 0; tc < tests; ++tc) {
    testCase();
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 24524 KB n=5
2 Correct 13 ms 24588 KB n=100
3 Correct 13 ms 24524 KB n=100
4 Correct 13 ms 24524 KB n=100
5 Correct 12 ms 24568 KB n=100
6 Correct 12 ms 24504 KB n=100
7 Correct 14 ms 24564 KB n=100
8 Correct 14 ms 24568 KB n=100
9 Correct 17 ms 24564 KB n=100
10 Correct 13 ms 24628 KB n=100
11 Correct 16 ms 24644 KB n=100
12 Correct 13 ms 24580 KB n=100
13 Correct 18 ms 24592 KB n=100
14 Correct 16 ms 24672 KB n=100
15 Correct 13 ms 24524 KB n=100
16 Correct 13 ms 24572 KB n=100
17 Correct 13 ms 24592 KB n=100
18 Correct 14 ms 24568 KB n=100
19 Correct 13 ms 24588 KB n=100
20 Correct 12 ms 24524 KB n=100
21 Correct 13 ms 24592 KB n=100
22 Correct 16 ms 24508 KB n=100
23 Correct 15 ms 24524 KB n=100
24 Correct 13 ms 24576 KB n=100
25 Correct 13 ms 24524 KB n=100
26 Correct 12 ms 24524 KB n=12
27 Correct 12 ms 24540 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 13 ms 24524 KB n=5
2 Correct 13 ms 24588 KB n=100
3 Correct 13 ms 24524 KB n=100
4 Correct 13 ms 24524 KB n=100
5 Correct 12 ms 24568 KB n=100
6 Correct 12 ms 24504 KB n=100
7 Correct 14 ms 24564 KB n=100
8 Correct 14 ms 24568 KB n=100
9 Correct 17 ms 24564 KB n=100
10 Correct 13 ms 24628 KB n=100
11 Correct 16 ms 24644 KB n=100
12 Correct 13 ms 24580 KB n=100
13 Correct 18 ms 24592 KB n=100
14 Correct 16 ms 24672 KB n=100
15 Correct 13 ms 24524 KB n=100
16 Correct 13 ms 24572 KB n=100
17 Correct 13 ms 24592 KB n=100
18 Correct 14 ms 24568 KB n=100
19 Correct 13 ms 24588 KB n=100
20 Correct 12 ms 24524 KB n=100
21 Correct 13 ms 24592 KB n=100
22 Correct 16 ms 24508 KB n=100
23 Correct 15 ms 24524 KB n=100
24 Correct 13 ms 24576 KB n=100
25 Correct 13 ms 24524 KB n=100
26 Correct 12 ms 24524 KB n=12
27 Correct 12 ms 24540 KB n=100
28 Correct 13 ms 24652 KB n=500
29 Correct 13 ms 24736 KB n=500
30 Correct 13 ms 24736 KB n=500
31 Correct 14 ms 24700 KB n=500
32 Correct 15 ms 24724 KB n=500
33 Correct 13 ms 24736 KB n=500
34 Correct 16 ms 24668 KB n=500
35 Correct 13 ms 24652 KB n=500
36 Correct 13 ms 24644 KB n=500
37 Correct 13 ms 24728 KB n=500
38 Correct 13 ms 24708 KB n=500
39 Correct 13 ms 24652 KB n=500
40 Correct 14 ms 24736 KB n=500
41 Correct 14 ms 24632 KB n=500
42 Correct 13 ms 24720 KB n=500
43 Correct 17 ms 24636 KB n=500
44 Correct 13 ms 24652 KB n=500
45 Correct 12 ms 24652 KB n=500
46 Correct 13 ms 24652 KB n=500
47 Correct 13 ms 24716 KB n=500
48 Correct 14 ms 24652 KB n=500
49 Correct 14 ms 24652 KB n=500
50 Correct 15 ms 24616 KB n=500
51 Correct 13 ms 24652 KB n=500
52 Correct 13 ms 24652 KB n=500
53 Correct 13 ms 24652 KB n=500
54 Correct 13 ms 24732 KB n=500
55 Correct 12 ms 24644 KB n=278
56 Correct 13 ms 24756 KB n=500
57 Correct 16 ms 24652 KB n=500
58 Correct 13 ms 24700 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 13 ms 24524 KB n=5
2 Correct 13 ms 24588 KB n=100
3 Correct 13 ms 24524 KB n=100
4 Correct 13 ms 24524 KB n=100
5 Correct 12 ms 24568 KB n=100
6 Correct 12 ms 24504 KB n=100
7 Correct 14 ms 24564 KB n=100
8 Correct 14 ms 24568 KB n=100
9 Correct 17 ms 24564 KB n=100
10 Correct 13 ms 24628 KB n=100
11 Correct 16 ms 24644 KB n=100
12 Correct 13 ms 24580 KB n=100
13 Correct 18 ms 24592 KB n=100
14 Correct 16 ms 24672 KB n=100
15 Correct 13 ms 24524 KB n=100
16 Correct 13 ms 24572 KB n=100
17 Correct 13 ms 24592 KB n=100
18 Correct 14 ms 24568 KB n=100
19 Correct 13 ms 24588 KB n=100
20 Correct 12 ms 24524 KB n=100
21 Correct 13 ms 24592 KB n=100
22 Correct 16 ms 24508 KB n=100
23 Correct 15 ms 24524 KB n=100
24 Correct 13 ms 24576 KB n=100
25 Correct 13 ms 24524 KB n=100
26 Correct 12 ms 24524 KB n=12
27 Correct 12 ms 24540 KB n=100
28 Correct 13 ms 24652 KB n=500
29 Correct 13 ms 24736 KB n=500
30 Correct 13 ms 24736 KB n=500
31 Correct 14 ms 24700 KB n=500
32 Correct 15 ms 24724 KB n=500
33 Correct 13 ms 24736 KB n=500
34 Correct 16 ms 24668 KB n=500
35 Correct 13 ms 24652 KB n=500
36 Correct 13 ms 24644 KB n=500
37 Correct 13 ms 24728 KB n=500
38 Correct 13 ms 24708 KB n=500
39 Correct 13 ms 24652 KB n=500
40 Correct 14 ms 24736 KB n=500
41 Correct 14 ms 24632 KB n=500
42 Correct 13 ms 24720 KB n=500
43 Correct 17 ms 24636 KB n=500
44 Correct 13 ms 24652 KB n=500
45 Correct 12 ms 24652 KB n=500
46 Correct 13 ms 24652 KB n=500
47 Correct 13 ms 24716 KB n=500
48 Correct 14 ms 24652 KB n=500
49 Correct 14 ms 24652 KB n=500
50 Correct 15 ms 24616 KB n=500
51 Correct 13 ms 24652 KB n=500
52 Correct 13 ms 24652 KB n=500
53 Correct 13 ms 24652 KB n=500
54 Correct 13 ms 24732 KB n=500
55 Correct 12 ms 24644 KB n=278
56 Correct 13 ms 24756 KB n=500
57 Correct 16 ms 24652 KB n=500
58 Correct 13 ms 24700 KB n=500
59 Correct 16 ms 25036 KB n=2000
60 Correct 18 ms 25208 KB n=2000
61 Correct 16 ms 25164 KB n=2000
62 Correct 15 ms 25036 KB n=2000
63 Correct 16 ms 24988 KB n=2000
64 Correct 15 ms 25120 KB n=2000
65 Correct 16 ms 25036 KB n=2000
66 Correct 15 ms 25164 KB n=2000
67 Correct 15 ms 25036 KB n=2000
68 Correct 16 ms 25240 KB n=2000
69 Correct 15 ms 25036 KB n=2000
70 Correct 15 ms 25036 KB n=2000
71 Correct 15 ms 25060 KB n=2000
72 Correct 15 ms 25036 KB n=2000
73 Correct 14 ms 25012 KB n=2000
74 Correct 14 ms 25036 KB n=1844
75 Correct 14 ms 24988 KB n=2000
76 Correct 19 ms 25036 KB n=2000
77 Correct 16 ms 25008 KB n=2000
78 Correct 16 ms 25036 KB n=2000
79 Correct 15 ms 25036 KB n=2000
80 Correct 15 ms 25164 KB n=2000
81 Correct 17 ms 25024 KB n=2000
82 Correct 15 ms 25036 KB n=2000
83 Correct 15 ms 25164 KB n=2000
84 Correct 16 ms 25096 KB n=2000
85 Correct 15 ms 25112 KB n=2000
86 Correct 16 ms 25128 KB n=2000
87 Correct 15 ms 25164 KB n=2000
88 Correct 14 ms 25164 KB n=2000
89 Correct 15 ms 25164 KB n=2000
90 Correct 14 ms 25164 KB n=2000
91 Correct 15 ms 24988 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 13 ms 24524 KB n=5
2 Correct 13 ms 24588 KB n=100
3 Correct 13 ms 24524 KB n=100
4 Correct 13 ms 24524 KB n=100
5 Correct 12 ms 24568 KB n=100
6 Correct 12 ms 24504 KB n=100
7 Correct 14 ms 24564 KB n=100
8 Correct 14 ms 24568 KB n=100
9 Correct 17 ms 24564 KB n=100
10 Correct 13 ms 24628 KB n=100
11 Correct 16 ms 24644 KB n=100
12 Correct 13 ms 24580 KB n=100
13 Correct 18 ms 24592 KB n=100
14 Correct 16 ms 24672 KB n=100
15 Correct 13 ms 24524 KB n=100
16 Correct 13 ms 24572 KB n=100
17 Correct 13 ms 24592 KB n=100
18 Correct 14 ms 24568 KB n=100
19 Correct 13 ms 24588 KB n=100
20 Correct 12 ms 24524 KB n=100
21 Correct 13 ms 24592 KB n=100
22 Correct 16 ms 24508 KB n=100
23 Correct 15 ms 24524 KB n=100
24 Correct 13 ms 24576 KB n=100
25 Correct 13 ms 24524 KB n=100
26 Correct 12 ms 24524 KB n=12
27 Correct 12 ms 24540 KB n=100
28 Correct 13 ms 24652 KB n=500
29 Correct 13 ms 24736 KB n=500
30 Correct 13 ms 24736 KB n=500
31 Correct 14 ms 24700 KB n=500
32 Correct 15 ms 24724 KB n=500
33 Correct 13 ms 24736 KB n=500
34 Correct 16 ms 24668 KB n=500
35 Correct 13 ms 24652 KB n=500
36 Correct 13 ms 24644 KB n=500
37 Correct 13 ms 24728 KB n=500
38 Correct 13 ms 24708 KB n=500
39 Correct 13 ms 24652 KB n=500
40 Correct 14 ms 24736 KB n=500
41 Correct 14 ms 24632 KB n=500
42 Correct 13 ms 24720 KB n=500
43 Correct 17 ms 24636 KB n=500
44 Correct 13 ms 24652 KB n=500
45 Correct 12 ms 24652 KB n=500
46 Correct 13 ms 24652 KB n=500
47 Correct 13 ms 24716 KB n=500
48 Correct 14 ms 24652 KB n=500
49 Correct 14 ms 24652 KB n=500
50 Correct 15 ms 24616 KB n=500
51 Correct 13 ms 24652 KB n=500
52 Correct 13 ms 24652 KB n=500
53 Correct 13 ms 24652 KB n=500
54 Correct 13 ms 24732 KB n=500
55 Correct 12 ms 24644 KB n=278
56 Correct 13 ms 24756 KB n=500
57 Correct 16 ms 24652 KB n=500
58 Correct 13 ms 24700 KB n=500
59 Correct 16 ms 25036 KB n=2000
60 Correct 18 ms 25208 KB n=2000
61 Correct 16 ms 25164 KB n=2000
62 Correct 15 ms 25036 KB n=2000
63 Correct 16 ms 24988 KB n=2000
64 Correct 15 ms 25120 KB n=2000
65 Correct 16 ms 25036 KB n=2000
66 Correct 15 ms 25164 KB n=2000
67 Correct 15 ms 25036 KB n=2000
68 Correct 16 ms 25240 KB n=2000
69 Correct 15 ms 25036 KB n=2000
70 Correct 15 ms 25036 KB n=2000
71 Correct 15 ms 25060 KB n=2000
72 Correct 15 ms 25036 KB n=2000
73 Correct 14 ms 25012 KB n=2000
74 Correct 14 ms 25036 KB n=1844
75 Correct 14 ms 24988 KB n=2000
76 Correct 19 ms 25036 KB n=2000
77 Correct 16 ms 25008 KB n=2000
78 Correct 16 ms 25036 KB n=2000
79 Correct 15 ms 25036 KB n=2000
80 Correct 15 ms 25164 KB n=2000
81 Correct 17 ms 25024 KB n=2000
82 Correct 15 ms 25036 KB n=2000
83 Correct 15 ms 25164 KB n=2000
84 Correct 16 ms 25096 KB n=2000
85 Correct 15 ms 25112 KB n=2000
86 Correct 16 ms 25128 KB n=2000
87 Correct 15 ms 25164 KB n=2000
88 Correct 14 ms 25164 KB n=2000
89 Correct 15 ms 25164 KB n=2000
90 Correct 14 ms 25164 KB n=2000
91 Correct 15 ms 24988 KB n=2000
92 Correct 603 ms 69732 KB n=200000
93 Correct 675 ms 74976 KB n=200000
94 Correct 557 ms 79412 KB n=200000
95 Correct 613 ms 69484 KB n=200000
96 Correct 557 ms 69712 KB n=200000
97 Correct 743 ms 73768 KB n=200000
98 Correct 611 ms 69544 KB n=200000
99 Correct 713 ms 69172 KB n=200000
100 Correct 611 ms 69692 KB n=200000
101 Correct 488 ms 81276 KB n=200000
102 Correct 395 ms 70504 KB n=200000
103 Correct 350 ms 70620 KB n=200000
104 Correct 359 ms 70500 KB n=200000
105 Correct 364 ms 70220 KB n=200000
106 Correct 386 ms 70276 KB n=200000
107 Correct 362 ms 70256 KB n=200000
108 Correct 656 ms 69212 KB n=200000
109 Correct 644 ms 69232 KB n=200000
110 Correct 623 ms 69172 KB n=200000
111 Correct 613 ms 69588 KB n=200000
112 Correct 530 ms 79872 KB n=200000
113 Correct 706 ms 73644 KB n=200000
114 Correct 588 ms 69544 KB n=200000
115 Correct 858 ms 71008 KB n=200000
116 Correct 582 ms 69248 KB n=200000
117 Correct 498 ms 80416 KB n=200000
118 Correct 766 ms 72276 KB n=200000
119 Correct 563 ms 69284 KB n=200000
120 Correct 447 ms 80832 KB n=200000
121 Correct 508 ms 80808 KB n=200000
122 Correct 522 ms 82136 KB n=200000
123 Correct 396 ms 70916 KB n=200000
124 Correct 197 ms 39824 KB n=25264