/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 85ms 95.309 MiB
#2 Accepted 185ms 95.848 MiB
#3 Accepted 133ms 95.281 MiB
#4 Accepted 155ms 95.543 MiB
#5 Accepted 175ms 95.879 MiB
#6 Accepted 210ms 96.934 MiB
#7 Accepted 348ms 107.148 MiB
#8 Accepted 338ms 107.09 MiB
#9 Accepted 339ms 107.062 MiB
#10 Accepted 330ms 107.129 MiB
#11 Accepted 337ms 107.133 MiB
#12 Accepted 347ms 107.133 MiB
#13 Accepted 163ms 112.73 MiB
#14 Accepted 245ms 112.941 MiB
#15 Accepted 246ms 112.953 MiB
#16 Accepted 247ms 112.945 MiB

Code

#include <bits/stdc++.h>
// #define ll long long
#define endll '\n';
#define pb push_back
#define all(v) v.begin(), v.end()
using namespace std;

const int N = 2e5 + 9;

map<int, int> indx;
vector<int> g[N];
int step = 1;
int start[N], endd[N];
int vis[N];
struct node
{
   int value;
   int mp[30] = {0};
};
node tmp;

node t[4 * N];
int lazy[4 * N];
int a[N];

node merge(node l, node r)
{ // what question need ??
   node ans;
   for (int i = 0; i <= 25; i++)
   {
      ans.mp[i] = l.mp[i] + r.mp[i];
   }
   ans.value = 0;

   return ans;
}

void build(int n, int b, int e)
{
   if (b == e)
   {

      for (int i = 0; i <= 26; i++)
         t[n].mp[i] = 0;

      t[n].value = 0;

      t[n].mp[a [indx[b]]]++;
      return;
   }
   int mid = (b + e) / 2, l = 2 * n, r = 2 * n + 1;
   build(l, b, mid);
   build(r, mid + 1, e);
   t[n] = merge(t[l], t[r]);
}

void upd(int n, int b, int e, int i, int j, int v)
{

   if (e < i or j < b)
      return;
   if (b >= i and e <= j)
   {
      for (int i = 0; i <= 26; i++)
         t[n].mp[i] = 0;





      t[n].mp[v]++;

      return;
   }
   int mid = (b + e) / 2, l = 2 * n, r = 2 * n + 1;
   upd(l, b, mid, i, j, v);
   upd(r, mid + 1, e, i, j, v);
   t[n] = merge(t[l], t[r]);
}

node query(int n, int b, int e, int i, int j)
{

   if (e < i or j < b)
      return tmp; // resutl
   if (b >= i and e <= j)
   {
      return t[n]; // result
   }
   int mid = (b + e) / 2, l = 2 * n, r = 2 * n + 1;
   return merge(query(l, b, mid, i, j), query(r, mid + 1, e, i, j)); // result
}

void dfs(int u, int p = -1)
{
   start[u] = step;
   indx[step] = u;
   vis[u] = 1;
   for (auto v : g[u])
   {
      if (vis[v] != 1)
      {
         step++;
         dfs(v, u);
      }
   }
   endd[u] = step;
}
void solve()
{

   int n;
   cin >> n;
   for (int i = 1; i <= n; i++)
   {
      char ch;
      cin >> ch;
      a[i] = ch - 'a';
   }
   for (int i = 1; i < n; i++)
   {
      int u, v;
      cin >> u >> v;
      g[u].pb(v);
      g[v].pb(u);
   }

   dfs(1);

   build(1, 1, n);
   // for (int i = 1; i <= n; i++)
   // {
   //    cout << start[i] << " " << endd[i] << endl;
   // }

   // for (int i = 1; i <= n; i++)
   // {
   //    node x = query(1, 1, n, start[i], endd[i]);
   //    for (auto j : x.mp)
   //    {
   //       cout << j.first << " " << j.second << endl;
   //    }
   //    cout << endll;
   // }
   int q;
   cin >> q;
   // cout << q << endll;
   while (q--)
   {
      int op;
      cin >> op;
      // cout << op << endll;
      if (op == 1)
      {
         int pos;
         char ch;

         cin >> pos;
         cin >> ch;

         // cout << "enter in 1st\n";

         a[pos] = ch - 'a';


         upd(1, 1, n, start[pos], start[pos], ch - 'a');
      }
      else
      {

         // cout << "ERROR: " ;

         int z;
         cin >> z;
         node x = query(1, 1, n, start[z], endd[z]);
         // cout << "For z = " << z << endll;
         // cout << start[z] << " " << endd[z] << endl;
         vector<pair<int, int>> v;

         // char c = 'z';
         // int cnt = 0;
         // for (int i = 25; i >= 0; i--)
         // {
         //    if (x.mp[i] >= cnt)
         //    {
         //       cnt = x.mp[i];
         //       c = 'a' + i;
         //    }
         // }
         // cout << c << endl;

         for(int i = 0; i <=25; i++) {
            if(x.mp[i]) {
               v.pb({x.mp[i], i});
            }
         }
         sort(all(v), [&](const pair<int, int> v1, const pair<int, int> v2)
              {
            if(v1.first == v2.first) {
               return v1.second < v2.second;
            }
            return v1.first > v2.first; });
         cout << char(v.front().second + 'a') << endll;
      }
   }
}

int32_t main()
{
   ios::sync_with_stdio(false);
   cin.tie(0);
   int t = 1;

   for (int i = 0; i <= 26; i++)
      tmp.mp[i] = 0;
   tmp.value = 0; // result

   // cin >> t;
   while (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 07:51:52
Judged At
2024-11-11 02:56:38
Judged By
Score
100
Total Time
348ms
Peak Memory
112.953 MiB