結果

問題 No.2554 MMA文字列2 (Query Version)
ユーザー Nzt3Nzt3
提出日時 2024-01-12 00:33:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 380 ms / 5,000 ms
コード長 3,036 bytes
コンパイル時間 2,222 ms
コンパイル使用メモリ 205,996 KB
実行使用メモリ 57,572 KB
最終ジャッジ日時 2024-01-12 00:33:29
合計ジャッジ時間 17,718 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 3 ms
6,676 KB
testcase_03 AC 3 ms
6,676 KB
testcase_04 AC 5 ms
6,676 KB
testcase_05 AC 4 ms
6,676 KB
testcase_06 AC 3 ms
6,676 KB
testcase_07 AC 3 ms
6,676 KB
testcase_08 AC 4 ms
6,676 KB
testcase_09 AC 4 ms
6,676 KB
testcase_10 AC 4 ms
6,676 KB
testcase_11 AC 3 ms
6,676 KB
testcase_12 AC 42 ms
16,784 KB
testcase_13 AC 287 ms
57,556 KB
testcase_14 AC 59 ms
16,780 KB
testcase_15 AC 251 ms
57,568 KB
testcase_16 AC 159 ms
16,784 KB
testcase_17 AC 237 ms
30,380 KB
testcase_18 AC 81 ms
6,676 KB
testcase_19 AC 122 ms
16,784 KB
testcase_20 AC 290 ms
57,564 KB
testcase_21 AC 106 ms
6,676 KB
testcase_22 AC 380 ms
57,572 KB
testcase_23 AC 351 ms
57,572 KB
testcase_24 AC 350 ms
57,572 KB
testcase_25 AC 350 ms
57,572 KB
testcase_26 AC 343 ms
57,572 KB
testcase_27 AC 350 ms
57,572 KB
testcase_28 AC 352 ms
57,572 KB
testcase_29 AC 352 ms
57,572 KB
testcase_30 AC 353 ms
57,572 KB
testcase_31 AC 360 ms
57,572 KB
testcase_32 AC 31 ms
6,676 KB
testcase_33 AC 30 ms
6,676 KB
testcase_34 AC 36 ms
6,676 KB
testcase_35 AC 36 ms
6,676 KB
testcase_36 AC 37 ms
6,676 KB
testcase_37 AC 332 ms
57,572 KB
testcase_38 AC 353 ms
57,572 KB
testcase_39 AC 335 ms
57,572 KB
testcase_40 AC 335 ms
57,572 KB
testcase_41 AC 341 ms
57,572 KB
testcase_42 AC 340 ms
57,572 KB
testcase_43 AC 342 ms
57,572 KB
testcase_44 AC 340 ms
57,572 KB
testcase_45 AC 341 ms
57,572 KB
testcase_46 AC 341 ms
57,572 KB
testcase_47 AC 368 ms
57,572 KB
testcase_48 AC 371 ms
57,572 KB
testcase_49 AC 368 ms
57,572 KB
testcase_50 AC 368 ms
57,572 KB
testcase_51 AC 364 ms
57,572 KB
testcase_52 AC 373 ms
57,572 KB
testcase_53 AC 370 ms
57,572 KB
testcase_54 AC 368 ms
57,572 KB
testcase_55 AC 368 ms
57,572 KB
testcase_56 AC 366 ms
57,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
constexpr int CHAR_NUM=26;

struct S{
  array<ll,CHAR_NUM>MA,A;
  ll MMA;
  void print(){
    cout<<MMA<<'/';
    for(int i=0;i<26;i++){
      cout<<A[i]<<',';
    }cout<<'/';
    for(int i=0;i<26;i++){
      cout<<MA[i]<<',';
    }cout<<'\n';
  }
};
S seg_e(){
  S ret;
  for(int i=0;i<CHAR_NUM;i++){
    ret.MA[i]=0;
    ret.A[i]=0;
  }
  ret.MMA=0;
  return ret;
}
S seg_op(S a,S b){
  S ret=seg_e();
  ret.MMA=a.MMA+b.MMA;
  for(int i=0;i<CHAR_NUM;i++){
    ret.A[i]+=a.A[i]+b.A[i];
    ret.MA[i]+=a.MA[i]+b.MA[i];
  }
  ll aMA_s=accumulate(a.MA.begin(),a.MA.end(),0ll),
  aA_s=accumulate(a.A.begin(),a.A.end(),0ll);
  ll bMA_s=accumulate(b.MA.begin(),b.MA.end(),0ll),
  bA_s=accumulate(b.A.begin(),b.A.end(),0ll);
  for(int i=0;i<CHAR_NUM;i++){
    ret.MMA+=a.A[i]*b.MA[i];
    ret.MMA+=a.A[i]*(a.A[i]-1)/2*(bA_s-b.A[i]);
    ret.MA[i]+=a.A[i]*(bA_s-b.A[i]);
  }
  return ret;
}

namespace Lib {
template <class S, auto op, auto e>
struct segtree {
  int size, _n;
  vector<S> seg;
  segtree(int n) {
    _n = n;
    size = 1;
    while (size < n) size <<= 1;
    seg = vector<S>(size * 2, e());
  }
  void set(int p, S val) {
    p += size;
    seg[p] = val;
    while (p /= 2) {
      seg[p] = op(seg[p * 2], seg[p * 2 + 1]);
    }
  }
  S prod(int l, int r) {
    S retL = e(), retR = e();
    for (l += size, r += size; l < r; l /= 2, r /= 2) {
      if (l % 2) retL = op(retL, seg[l++]);
      if (r % 2) retR = op(seg[--r], retR);
    }
    return op(retL, retR);
  }
  S get(int p) { return seg[p + size]; }
  template <class F>
  int max_right(int L, F f) {
    L += size;
    S sm = e();
    do {
      while (L % 2 == 0) L /= 2;
      if (!f(op(sm, seg[L]))) {
        while (L < size) {
          L *= 2;
          if (f(op(sm, seg[L]))) {
            sm = op(sm, seg[L]);
            L++;
          }
        }
        return L - size;
      }
      sm = op(sm, seg[L]);
      L++;
    } while ((L & -L) != L);
    return _n;
  }

  template <class F>
  int min_left(int R, F f) {
    R += size;
    S sm = e();
    do {
      R--;
      while (R > 1 && R % 2 == 1) R /= 2;
      if (!f(op(seg[R], sm))) {
        while (R < size) {
          R = R * 2 + 1;
          if (f(op(seg[R], sm))) {
            sm = op(seg[R], sm);
            R--;
          }
        }
        return R + 1 - size;
      }
      sm = op(seg[R], sm);
    } while ((R & -R) != R);
    return 0;
  }
};
}  // namespace Lib
int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin>>N;
  string T;
  cin>>T;
  Lib::segtree<S,seg_op,seg_e> seg(N);
  for(int i=0;i<N;i++){
    S t=seg_e();
    t.A[T[i]-'A']=1;
    seg.set(i,t);
  }
  int Q;
  cin>>Q;
  while(Q--){
    int a;;
    cin>>a;
    if(a==1){
      int x;
      char c;
      cin>>x>>c;
      --x;
      S t=seg_e();
      t.A[c-'A']=1;
      seg.set(x,t);
    }else if(a==2){
      int l,r;
      cin>>l>>r;
      --l;
      cout<<seg.prod(l,r).MMA<<'\n';
      // seg.prod(l,r).print();
    }
  }
}
0