結果

問題 No.2554 MMA文字列2 (Query Version)
ユーザー Nzt3Nzt3
提出日時 2024-01-12 00:35:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 421 ms / 5,000 ms
コード長 1,535 bytes
コンパイル時間 2,368 ms
コンパイル使用メモリ 208,060 KB
実行使用メモリ 78,372 KB
最終ジャッジ日時 2024-01-12 00:35:50
合計ジャッジ時間 19,041 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 4 ms
6,676 KB
testcase_04 AC 5 ms
6,676 KB
testcase_05 AC 6 ms
6,676 KB
testcase_06 AC 4 ms
6,676 KB
testcase_07 AC 3 ms
6,676 KB
testcase_08 AC 3 ms
6,676 KB
testcase_09 AC 5 ms
6,676 KB
testcase_10 AC 5 ms
6,676 KB
testcase_11 AC 3 ms
6,676 KB
testcase_12 AC 51 ms
22,752 KB
testcase_13 AC 354 ms
72,116 KB
testcase_14 AC 71 ms
20,276 KB
testcase_15 AC 304 ms
77,488 KB
testcase_16 AC 170 ms
22,200 KB
testcase_17 AC 262 ms
40,348 KB
testcase_18 AC 84 ms
6,676 KB
testcase_19 AC 129 ms
21,824 KB
testcase_20 AC 325 ms
74,964 KB
testcase_21 AC 107 ms
8,064 KB
testcase_22 AC 391 ms
78,372 KB
testcase_23 AC 416 ms
78,372 KB
testcase_24 AC 391 ms
78,372 KB
testcase_25 AC 389 ms
78,372 KB
testcase_26 AC 421 ms
78,372 KB
testcase_27 AC 404 ms
78,372 KB
testcase_28 AC 397 ms
78,372 KB
testcase_29 AC 403 ms
78,372 KB
testcase_30 AC 399 ms
78,372 KB
testcase_31 AC 396 ms
78,372 KB
testcase_32 AC 31 ms
6,676 KB
testcase_33 AC 32 ms
6,676 KB
testcase_34 AC 37 ms
6,676 KB
testcase_35 AC 37 ms
6,676 KB
testcase_36 AC 38 ms
6,676 KB
testcase_37 AC 370 ms
78,372 KB
testcase_38 AC 366 ms
78,372 KB
testcase_39 AC 382 ms
78,372 KB
testcase_40 AC 376 ms
78,372 KB
testcase_41 AC 372 ms
78,372 KB
testcase_42 AC 382 ms
78,372 KB
testcase_43 AC 376 ms
78,372 KB
testcase_44 AC 377 ms
78,372 KB
testcase_45 AC 385 ms
78,372 KB
testcase_46 AC 377 ms
78,372 KB
testcase_47 AC 399 ms
78,372 KB
testcase_48 AC 415 ms
78,372 KB
testcase_49 AC 395 ms
78,372 KB
testcase_50 AC 389 ms
78,372 KB
testcase_51 AC 417 ms
78,372 KB
testcase_52 AC 391 ms
78,372 KB
testcase_53 AC 400 ms
78,372 KB
testcase_54 AC 419 ms
78,372 KB
testcase_55 AC 399 ms
78,372 KB
testcase_56 AC 402 ms
78,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/segtree>
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;
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin>>N;
  string T;
  cin>>T;
  atcoder::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