/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 320.0 KiB
#2 Accepted 60ms 2.43 MiB
#3 Accepted 31ms 536.0 KiB
#4 Accepted 46ms 1.09 MiB
#5 Accepted 61ms 2.477 MiB
#6 Accepted 87ms 7.988 MiB
#7 Accepted 199ms 59.215 MiB
#8 Accepted 199ms 59.266 MiB
#9 Accepted 202ms 59.27 MiB
#10 Accepted 198ms 59.23 MiB
#11 Accepted 196ms 59.273 MiB
#12 Accepted 198ms 59.266 MiB
#13 Accepted 80ms 62.469 MiB
#14 Accepted 128ms 62.527 MiB
#15 Accepted 128ms 62.527 MiB
#16 Accepted 131ms 62.328 MiB

Code

#include<bits/stdc++.h>
#define endl        '\n'
#define F           first
#define S           second
#define pb          push_back
#define yes         cout<<"YES\n"
#define no          cout<<"NO\n"
#define all(x)      x.begin(),x.end()
#define allr(x)     x.rbegin(),x.rend()
#define error1(x)   cerr<<#x<<" = "<<(x)<<endl
#define error2(a,b) cerr<<"("<<#a<<", "<<#b<<") = ("<<(a)<<", "<<(b)<<")\n";
#define coutall(v)  for(auto &it: v) cout << it << " "; cout << endl;
using namespace std;
using ll = long long;
using ld = long double;

const int N = 3e5 + 9;
array<ll, 26> allZero;

struct Segment_Tree {
    array<ll, 26> t[4 * N];

    auto merge(array<ll, 26> &l, array<ll, 26> &r) { // <== Change this function
        array<ll, 26> x;
        for(int i = 0; i < 26; i++) {
            x[i] = l[i] + r[i];
        }
        return x;
    }
    void build(int node, int st, int en, vector<char> &arr) { //=> O(N)
        if (st == en) {
            t[node] = allZero;
            t[node][arr[st] - 'a'] = 1;
            return;
        }
        int mid = (st + en) >> 1;
        build(node << 1, st, mid, arr); // divide left side
        build(node << 1 | 1, mid + 1, en, arr); // divide right side
        // Merging left and right portion
        auto &Cur = t[node];
        auto &Left = t[node << 1];
        auto &Right = t[node << 1 | 1];
        Cur = merge(Left, Right);
        return;
    }
    void update(int node, int st, int en, int idx, char val, char prev) { //=> O(log n)
        if (st == en) {
            t[node][prev - 'a'] -= 1;
            t[node][val - 'a'] += 1;
            return;
        }
        int mid = (st + en) >> 1;
        if (idx <= mid) update(node << 1, st, mid, idx, val, prev);
        else update(node << 1 | 1, mid + 1, en, idx, val, prev);
        // Merging left and right portion
        auto &Cur = t[node];
        auto &Left = t[node << 1];
        auto &Right = t[node << 1 | 1];
        Cur = merge(Left, Right);
        return;
    }
    auto query(int node, int st, int en, int l, int r) { //=> O(log n)
        if (st > r || en < l) { // No overlapping and out of range
            return allZero; // <== careful 
        }
        if (l <= st && en <= r) { // Complete overlapped (l-r in range)
            return t[node];
        }
        // Partial overlapping
        int mid = (st + en) >> 1;
        auto Left = query(node << 1, st, mid, l, r);
        auto Right = query(node << 1 | 1, mid + 1, en, l, r);
        return merge(Left, Right);
    }
} st1;

void solve() {
    for(int i = 0; i < 26; i++) allZero[i] = 0;

    int n;
    cin >> n;
    vector<char> val(n + 1);
    for (int i = 1; i <= n; i++) cin >> val[i];
    vector<vector<int>> g(n + 1);
    for(int i = 1; i < n; i++) {
        int u, v; cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    
    vector<int> st(n + 1), en(n + 1);
    int time = 0;
    auto dfs = [&](int u, int par, auto&& self) -> void {
        st[u] = ++time;
        for(auto &v: g[u]) {
            if(v == par) continue;
            self(v, u, self);
        }
        en[u] = time; 
    };
    dfs(1, -1, dfs);

    vector<char> vec(n + 1);
    for(int i = 1; i <= n; i++) {
        vec[st[i]] = val[i];
        // cout << st[i] << " " << en[i] << endl;
    }
    st1.build(1, 1, n, vec);
    int q; cin >> q;
    while(q--) {
        short type;
        cin >> type;
        if(type == 1) {
            int pos; char ch;
            cin >> pos >> ch;
            pos = st[pos];
            st1.update(1, 1, n, pos, ch, vec[pos]);
            vec[pos] = ch;
        }
        else {
            int u; cin >> u;
            auto x = st1.query(1, 1, n, st[u], en[u]);
            int mx = -1, ans = 0;
            for(int i = 0; i < 26; i++) {
                assert(x[i] >= 0);
                if(x[i] > mx) mx = x[i], ans = i;
            }
            // coutall(x);
            cout << char(ans + 'a') << endl;
        }
    }
    return;
}

signed main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int tc = 1;
    // cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case " << t << ": ";
        solve();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1091 Alphabetical Kingdom
Language
C++20 (G++ 13.2.0)
Submit At
2024-09-06 09:55:16
Judged At
2024-11-11 02:56:26
Judged By
Score
100
Total Time
202ms
Peak Memory
62.527 MiB