/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Wrong Answer 58ms 1.676 MiB
#3 Wrong Answer 32ms 604.0 KiB

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<int, 26> allZero;

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

    auto merge(array<int, 26> &l, array<int, 26> &r) { // <== Change this function
        array<int, 26> x = allZero;
        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][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() {
    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;
            st1.update(1, 1, n, pos, ch, vec[st[pos]]);
            vec[st[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++) {
                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:20:46
Judged At
2024-11-11 02:56:31
Judged By
Score
5
Total Time
58ms
Peak Memory
1.676 MiB