/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 53ms 2.238 MiB
#3 Accepted 30ms 604.0 KiB
#4 Accepted 44ms 896.0 KiB
#5 Accepted 50ms 2.293 MiB
#6 Accepted 69ms 6.969 MiB
#7 Accepted 163ms 46.699 MiB
#8 Accepted 164ms 46.883 MiB
#9 Accepted 162ms 46.855 MiB
#10 Accepted 162ms 46.91 MiB
#11 Accepted 163ms 46.891 MiB
#12 Accepted 165ms 46.871 MiB
#13 Accepted 69ms 49.906 MiB
#14 Accepted 98ms 49.922 MiB
#15 Accepted 103ms 50.012 MiB
#16 Accepted 97ms 50.004 MiB

Code

/**
 *  Author: AhSaN (JUST-22)
 *  Created: 05-09-2024  23:33:13
**/
#include <bits/stdc++.h>

#pragma GCC optimize("Ofast")
using namespace std;

const int N = 1e5 + 2;
struct ST {
  int n;
  vector<array<int, 26>> tree;
  ST(int size) : n(size) {
    tree.resize(4 * n);
  }
  
  void merge(int node) {
    for (int i = 0; i < 26; i++) {
      tree[node][i] = tree[node << 1][i] + tree[node << 1 | 1][i];
    }
  }

  void build(string &s, int node, int start, int end) {
    if (start == end) {
      tree[node].fill(0);
      tree[node][s[end] - 'a'] += 1;
    } else {
      int mid = (start + end) >> 1;
      build(s, node << 1, start, mid);
      build(s, node << 1 | 1, mid + 1, end);
      merge(node);
    }
  }
  void update (int node, int start, int end, int id, int old, int newchar) {
    if (start == end) {
      tree[node][old - 'a'] -= 1;
      tree[node][newchar - 'a'] += 1;
    } else {
      int mid = (start + end) >> 1;
      if (id <= mid) update(node << 1, start, mid, id, old, newchar);
      else update(node << 1 | 1, mid + 1, end, id, old, newchar);
      merge(node);
    }
  }
  array<int, 26> Qry(int node, int start, int end, int l, int r) {
    if (l > r) return array<int, 26>{};
    if (start == l && end == r) {
      return tree[node];
    }
    int mid = (start + end) >> 1;
    auto left = Qry(node << 1, start, mid, l, min(r, mid));
    auto right = Qry(node << 1 | 1, mid + 1, end, max(l, mid + 1), r);
    array<int, 26> res {};
    for (int i = 0; i < 26; i++) res[i] = left[i] + right[i];
    return res;
  }
  char get(int l, int r) {
    auto f = Qry(1, 0, n - 1, l, r);
    int id = max_element(f.begin(), f.end()) - f.begin();
    return char('a' + id);
  }
};

void Sol(int Cs) {
  int n;
  cin >> n;
  string s;
  for (int i = 0; i < n; i++) {
    char c;
    cin >> c;
    s += c;
  }
  vector<vector<int>> g(n);
  for (int i = 0, u, v; i < n - 1; i++) {
    cin >> u >> v, --u, --v;
    g[u].push_back(v), g[v].push_back(u);
  }

  vector<int> start(n, 0), end(n, 0);
  int timer = 0;
  auto dfs = [&](auto &&gorib, int node, int par) -> void {
    start[node] = timer++;
    for (auto &son : g[node]) {
      if (son != par) {
        gorib(gorib, son, node);
      }
    }
    end[node] = timer - 1;
  };
  dfs(dfs, 0, -1);
  string S(n, 'a');
  for (int i = 0; i < n; i++) {
    S[start[i]] = s[i];
  }
  ST st(n);
  st.build(S, 1, 0, n - 1);
  int Q;
  cin >> Q;
  while (Q--) {
    int t;
    cin >> t;
    if (t == 1) {
      int id;
      char c;
      cin >> id >> c, --id;
      id = start[id];
      st.update(1, 0, n - 1, id, S[id], c);
      S[id] = c;
    } else {
      int node;
      cin >> node;
      --node;
      cout << st.get(start[node], end[node]) << "\n";
    }
  }

}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int Tc = 1;
  // cin >> Tc;
  for (int Cs = 1; Cs <= Tc; Cs++) {
    Sol(Cs);
  }
  return 0;
}

Information

Submit By
Type
Submission
Problem
P1091 Alphabetical Kingdom
Language
C++20 (G++ 13.2.0)
Submit At
2024-09-05 19:03:52
Judged At
2024-11-11 02:57:15
Judged By
Score
100
Total Time
165ms
Peak Memory
50.012 MiB