/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 564.0 KiB
#2 Accepted 118ms 2.77 MiB
#3 Accepted 66ms 688.0 KiB
#4 Accepted 93ms 1.027 MiB
#5 Accepted 124ms 2.898 MiB
#6 Accepted 160ms 8.777 MiB
#7 Accepted 443ms 64.453 MiB
#8 Accepted 434ms 64.461 MiB
#9 Accepted 653ms 64.535 MiB
#10 Accepted 721ms 64.543 MiB
#11 Accepted 720ms 64.57 MiB
#12 Accepted 737ms 64.543 MiB
#13 Accepted 266ms 68.219 MiB
#14 Accepted 505ms 68.223 MiB
#15 Accepted 500ms 68.234 MiB
#16 Accepted 479ms 68.219 MiB

Code

#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define int ll
# define double long double
const int N = 2e5 + 2, MOD = 1e9 + 7;
#define deb(x) cout<<#x<<"="<<x<<endl;
#define F first
#define S second
int n;
vector<vector<int>> adj;
vector<char> vals;
vector<int> in;
vector<int> out;
int timer{};

struct node {
    char c;
    int a[27];

    node() {
        memset(a, 0, sizeof a);
//        c = 'z';
    }
};

struct SEGT {
    int sz = 1;
    vector<node> seg;

    node op(node a, node b) {
        int mx{};
        char tmp = 'a';
        for (int i = 0; i < 26; ++i) {
            a.a[i] += b.a[i];
            if (a.a[i] > mx) {
                mx = a.a[i];
                tmp = char(i + 'a');
            }
        }
        a.c = tmp;
        return a;
    }

    void update(int i, char v, int x, int lx, int rx) {
        if (lx == rx) {
            for (int j = 0; j < 26; ++j) {
                seg[x].a[j] = 0;
            }
            seg[x].c = v;
            seg[x].a[v - 'a']++;
            return;
        }
        int md = (lx + rx) >> 1;
        if (i <= md) {
            update(i, v, 2 * x + 1, lx, md);
        } else {
            update(i, v, 2 * x + 2, md + 1, rx);
        }
        seg[x] = op(seg[2 * x + 1], seg[2 * x + 2]);

    }

    node query(int l, int r, int x, int lx, int rx) {
        if (r < lx or rx < l) {
            return {};
        }
        if (l <= lx and rx <= r) {
            return seg[x];
        }
        ll mid = (lx + rx) >> 1;
        return op(query(l, r, 2 * x + 1, lx, mid), query(l, r, 2 * x + 2, mid + 1, rx));
    }

public:
    SEGT(int n) {
        sz = 1;
        while (sz < n) { sz <<= 1; }
        seg = vector<node>(sz << 1);
    }

    void update(int i, char v) {
        update(i, v, 0, 0, sz - 1);
    }

    char query(int l, int r) {
        return query(l, r, 0, 0, sz - 1).c;
    }
};


void dfs(int u, int par) {
    in[u] = timer++;
    for (auto it: adj[u]) {
        if (it == par)continue;
        dfs(it, u);
    }
    out[u] = timer - 1;
}


void solve() {
    cin >> n;
    adj = vector<vector<int>>(n + 1);
    vals = vector<char>(n + 1);
    in = vector<int>(n + 1);
    out = vector<int>(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> vals[i];
    }
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(1, -1);
    SEGT seg(n + 1);
    for (int i = 1; i <= n; ++i) {
        seg.update(in[i], vals[i]);
    }
    int q;
    cin >> q;
    while (q--) {
        int ty, u;
        cin >> ty >> u;
        if (ty == 1) {
            char c;
            cin >> c;
            seg.update(in[u], c);
        } else {
            cout << seg.query(in[u], out[u]) << "\n";
        }
    }

}

signed main() {
    ios_base::sync_with_stdio(false);
    cout.tie(NULL);
    cin.tie(NULL);
//#ifndef ONLINE_JUDGE
//    freopen("output.txt", "w", stdout);
//    freopen("input.txt", "r", stdin);
//#endif
    int tt = 1;
//    cin >> tt;
    for (int i = 0; i < tt; i++) {
//        cout << "Case " << i + 1 << ": ";
        solve();
//        cout << "\n";
    }
}

Information

Submit By
Type
Submission
Problem
P1091 Alphabetical Kingdom
Contest
Brain Booster #5
Language
C++20 (G++ 13.2.0)
Submit At
2024-09-05 16:37:36
Judged At
2024-09-05 16:37:36
Judged By
Score
100
Total Time
737ms
Peak Memory
68.234 MiB