/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 572.0 KiB
#2 Accepted 39ms 1.27 MiB
#3 Accepted 28ms 792.0 KiB
#4 Accepted 32ms 832.0 KiB
#5 Accepted 38ms 1.273 MiB
#6 Accepted 48ms 2.773 MiB
#7 Accepted 141ms 16.785 MiB
#8 Accepted 142ms 16.902 MiB
#9 Accepted 143ms 16.941 MiB
#10 Accepted 141ms 16.773 MiB
#11 Accepted 138ms 16.891 MiB
#12 Accepted 137ms 16.77 MiB
#13 Accepted 45ms 21.27 MiB
#14 Accepted 65ms 21.02 MiB
#15 Accepted 64ms 21.023 MiB
#16 Accepted 63ms 21.02 MiB

Code

#include<bits/stdc++.h>
using namespace std;
const long long M=1e5+10,MOD=1000000000;
typedef long long ll;
#define READ(f)          freopen(f, "r", stdin)
#define WRITE(f)         freopen(f, "w", stdout)
int level[M];
vector<int>edge[M];
int cnt=1;
int in[M],out[M];
int bit[26][M];
int query(int id,int c){
    int sum=0;
    while(id>0){
        sum+=bit[c][id];
        id-=id & (-id);
    }
    return sum;
}
void update(int id,int n,int val,int c){
    while(id<=n){
        bit[c][id]+=val;
        id+=id & (-id);

    }
}
void dfs(int x,int p){


    in[x]=cnt;
    cnt++;
    for(auto u:edge[x]){
        if(u!=p){
            dfs(u,x);
        }
    }
   out[x]=cnt-1;


}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    // READ("input8.txt");
    // WRITE("output12.txt") ;
    int t=1;
    //cin>>t;
    while(t--){
        int n;
        cin>>n;
        vector<char>s(n);
        for(int i=0;i<n;i++)cin>>s[i];
        for(int i=1;i<n;i++){
            int x,y;
            cin>>x>>y;
            edge[x].push_back(y);
            edge[y].push_back(x);
        }
        dfs(1,0);
        for(char ch='a';ch<='z';ch++){
            for(int j=1;j<=n;j++){
                if(s[j-1]==ch){
                    update(in[j],n,1,ch-'a');
                }
            }
        }


        int q;
        cin>>q;
        while(q--){
            int type;
            cin>>type;
            if(type==1){
                int x;
                char ch;
                cin>>x>>ch;
                if(ch==s[x-1])continue;
                update(in[x],n,-1,s[x-1]-'a');
                s[x-1]=ch;
                update(in[x],n,1,s[x-1]-'a');

            }
            else{
                int x;
                cin>>x;
                int mx=-1;
                char ans='a';
                for(char ch='a';ch<='z';ch++){
                    int sumoffre=query(out[x],ch-'a')-query(in[x]-1,ch-'a');
                    if(sumoffre>mx){
                        mx=sumoffre;
                        ans=ch;
                    }

                }
                cout<<ans<<"\n";
            }

        }
       // for(int i=1;i<=n;i++)edge[i].clear();

    }



   return 0;

}

Information

Submit By
Type
Submission
Problem
P1091 Alphabetical Kingdom
Language
C++20 (G++ 13.2.0)
Submit At
2024-08-24 23:32:55
Judged At
2024-08-26 13:10:38
Judged By
Score
100
Total Time
143ms
Peak Memory
21.27 MiB