/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 616.0 KiB
#2 Accepted 38ms 1.27 MiB
#3 Accepted 30ms 792.0 KiB
#4 Accepted 59ms 788.0 KiB
#5 Accepted 46ms 1.27 MiB
#6 Accepted 47ms 2.82 MiB
#7 Accepted 132ms 16.77 MiB
#8 Accepted 157ms 16.816 MiB
#9 Accepted 132ms 16.969 MiB
#10 Accepted 132ms 16.77 MiB
#11 Accepted 147ms 16.91 MiB
#12 Accepted 131ms 16.773 MiB

Code

#include<bits/stdc++.h>
using namespace std;
const long long M=1e5+10,MOD=1000000000;
typedef long long ll;
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);
    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-22 14:56:26
Judged At
2024-08-23 23:23:04
Judged By
Score
100
Total Time
157ms
Peak Memory
16.969 MiB