/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 532.0 KiB
#2 Accepted 85ms 2.523 MiB
#3 Accepted 43ms 624.0 KiB
#4 Accepted 70ms 1.02 MiB
#5 Accepted 90ms 2.52 MiB
#6 Accepted 115ms 8.133 MiB
#7 Accepted 362ms 60.527 MiB
#8 Accepted 333ms 60.469 MiB
#9 Accepted 331ms 60.5 MiB
#10 Accepted 358ms 60.48 MiB
#11 Accepted 332ms 60.512 MiB
#12 Accepted 333ms 60.52 MiB
#13 Accepted 134ms 63.969 MiB
#14 Accepted 239ms 64.211 MiB
#15 Accepted 239ms 64.211 MiB
#16 Accepted 242ms 64.211 MiB

Code

#include <bits/stdc++.h>

#ifdef LOCAL
#include "template.cpp.h"
#else
#define debug(...)
#endif

#define int long long
using namespace std;
#define cinv(v) for (auto &it:v) cin>>it;
#define coutv(v) for (auto &it:v) cout<< it<<' '; cout<<'\n';

const int N = 1e5 + 5;
vector<int> adj[N];
int n, q, tin[N], tout[N], timer = 0;
array<int, 26> tree[4 * N];
char arr[N];

array<int, 26> merge(array<int, 26> &a, array<int, 26> &b) {
    array<int, 26> ret = a;
    for (int i = 0; i < 26; ++i) ret[i] += b[i];
    return ret;
}

void update(int node, int start, int end, int idx, int c, int val) {
    if (start > idx || end < idx) return;
    if (start == end) {
        tree[node][c] += val;
        return;
    }
    int mid = (start + end) / 2;
    update(node * 2, start, mid, idx, c, val);
    update(node * 2 + 1, mid + 1, end, idx, c, val);
    tree[node] = merge(tree[node * 2], tree[node * 2 + 1]);
}

array<int, 26> query(int node, int start, int end, int l, int r) {
    if (start > r || end < l) return array<int, 26>{};
    if (start >= l && end <= r) return tree[node];
    int mid = (start + end) / 2;
    array<int, 26> q1 = query(node * 2, start, mid, l, r);
    array<int, 26> q2 = query(node * 2 + 1, mid + 1, end, l, r);
    return merge(q1, q2);
}

void euler_tour(int now, int par) {
    tin[now] = ++timer;
    for (auto &it: adj[now]) {
        if (it == par) continue;
        euler_tour(it, now);
    }
    tout[now] = timer;
}

void shelby() {
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> arr[i];
    for (int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    euler_tour(1, -1);
    for (int i = 1; i <= n; ++i) update(1, 1, n, tin[i], arr[i] - 'a', 1);
    cin >> q;
    while (q--) {
        int t, k;
        cin >> t >> k;
        if (t == 1) {
            char c;
            cin >> c;
            update(1, 1, n, tin[k], arr[k] - 'a', -1);
            arr[k] = c;
            update(1, 1, n, tin[k], arr[k] - 'a', 1);
        }
        else {
            array<int, 26> get = query(1, 1, n, tin[k], tout[k]);
            int ans = -1, mx = 0;
            for (int i = 0; i < 26; ++i) {
                if (get[i] > mx) ans = i, mx = get[i];
            }
            debug(ans, k, tin[k], tout[k], get);
            cout << char(ans + 'a') << '\n';
        }
    }
}

signed main() {
    cin.tie(0)->ios_base::sync_with_stdio(0);
    int t = 1;
//    cin >> t;
    for (int _ = 1; _ <= t; ++_) {
//        cout << "Case " << _ << ": ";
        shelby();
    }
}

Information

Submit By
Type
Submission
Problem
P1091 Alphabetical Kingdom
Language
C++20 (G++ 13.2.0)
Submit At
2024-09-09 07:09:38
Judged At
2024-09-09 07:09:38
Judged By
Score
100
Total Time
362ms
Peak Memory
64.211 MiB