/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 576.0 KiB
#2 Accepted 93ms 2.539 MiB
#3 Accepted 47ms 656.0 KiB
#4 Accepted 72ms 1.117 MiB
#5 Accepted 93ms 2.559 MiB
#6 Accepted 141ms 8.199 MiB
#7 Accepted 380ms 60.488 MiB
#8 Accepted 392ms 60.508 MiB
#9 Accepted 405ms 60.512 MiB
#10 Accepted 406ms 60.508 MiB
#11 Accepted 379ms 60.332 MiB
#12 Accepted 374ms 60.523 MiB
#13 Accepted 147ms 64.062 MiB
#14 Accepted 264ms 64.207 MiB
#15 Accepted 261ms 64.207 MiB
#16 Accepted 266ms 64.203 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-11-11 02:54:34
Judged By
Score
100
Total Time
406ms
Peak Memory
64.207 MiB