/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 4.527 MiB
#2 Accepted 34ms 4.863 MiB
#3 Accepted 24ms 4.754 MiB
#4 Accepted 52ms 4.621 MiB
#5 Accepted 47ms 5.281 MiB
#6 Accepted 43ms 9.02 MiB
#7 Accepted 102ms 34.086 MiB
#8 Accepted 97ms 35.27 MiB
#9 Accepted 98ms 34.137 MiB
#10 Accepted 155ms 34.355 MiB
#11 Accepted 101ms 35.684 MiB
#12 Accepted 145ms 34.656 MiB
#13 Accepted 46ms 40.973 MiB
#14 Accepted 73ms 40.34 MiB
#15 Accepted 74ms 39.688 MiB
#16 Accepted 73ms 40.312 MiB

Code

#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define dbg(a,b,c,d) cerr<<a<<"  "<<b<<"  "<<c<<"  "<<d<<endl;
#define kill(a) {cout<<a<<endl;continue;}
#define KILL(a) {cout<<a<<endl;return 0;}
#define debug cerr<<"Error Found"<<endl;
#define mem(a,b) memset(a,b,sizeof(a))
#define lcm(a, b) (a/__gcd(a,b))*b
#define w(t) cin>>t;while(t--)
#define pi  2 * acos(0.0)
#define endl "\n"
int t, cs = 0;
const int mxn = 1e5 + 3, mod = 1e9 + 7;
int tree[mxn * 4][26];
char ar[mxn];
vector<int>adj[mxn];
int child[mxn];
int id[mxn], ID[mxn];
void dfs(int n, int par)
{
    id[n] = ++cs;
    ID[cs] = n;
    child[id[n]] = 1;
    for(auto i:adj[n])
    {
        if(i == par)continue;
        dfs(i, n);
        child[id[n]] += child[id[i]];
    }
}
void build(int st, int ed, int node)
{

    for(int i = st; i <= ed; i++)tree[node][ar[i] - 'a']++;
    //dbg(st, ed, node, -1);
    if(st == ed)return;
    int mid = st + ed >> 1;
    build(st, mid, node * 2);
    build(mid + 1, ed, node * 2 + 1);
}
void update(int st, int ed, int node, int idx, char dlt, char add)
{
    if(st > idx or ed < idx)return;
    tree[node][dlt - 'a']--, tree[node][add - 'a']++;
    if(st == ed)return;
    int mid = st + ed >> 1;
    update(st, mid, node * 2, idx, dlt, add);
    update(mid + 1, ed, node * 2 + 1, idx, dlt, add);
}
int freq[26];
void query(int st, int ed, int node, int l, int r)
{
    if(st > r or ed < l)return;
    if(st >= l and ed <= r)
    {
        //dbg(st, ed, node, -1);
        for(int j = 0; j < 26; j++)
        {
            freq[j] += tree[node][j];
        }

        return;
    }
    int mid = st + ed >> 1;
    query(st, mid, node * 2, l, r), query(mid + 1, ed, node * 2 + 1, l, r);
}
int32_t main()
{

    fast
    int n;
    cin >> n;
    char arr[n + 1];
    for(int i = 1; i <= n; i++)cin >> arr[i];
    for(int i = 1; i < n; i++)
    {
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b), adj[b].push_back(a);
    }
    dfs(1, -1);
    for(int i = 1; i <= n; i++)ar[i] = arr[ID[i]];
//    for(int i = 1; i <= n; i++)cout << ar[i] <<" ";cout << endl;
//    for(int i = 1; i <= n; i++)cout << id[i] <<" ";cout << endl;
//    for(int i = 1; i <= n; i++)cout << child[i] << " ";cout << endl;
    build(1, n, 1);

    int q;
    cin >> q;
    while(q--)
    {
        int d, idx;
        cin >> d >> idx;
        int last = id[idx] + child[id[idx]] - 1;
        if(d == 2)
        {

            query(1, n, 1, id[idx], last);
            //cout << id[idx] <<" "<< last << endl;
            char ans;
            int mx = 0;
            //for(int i = 0; i < 26; i++)cout << freq[i] <<" ";cout << endl;
            for(int i = 0; i < 26; i++)if(freq[i] > mx)mx = freq[i], ans = i;
            for(int i = 0; i < 26; i++)freq[i] = 0;
            cout << char(ans + 'a') << endl;
        }
        else
        {
            char cur;
            cin >> cur;
            update(1, n, 1, id[idx], ar[id[idx]], cur);
            ar[id[idx]] = cur;
        }
    }
}

Information

Submit By
Type
Submission
Problem
P1091 Alphabetical Kingdom
Language
C++17 (G++ 13.2.0)
Submit At
2024-11-12 11:37:38
Judged At
2024-11-12 11:37:38
Judged By
Score
100
Total Time
155ms
Peak Memory
40.973 MiB