結果
| 問題 |
No.2554 MMA文字列2 (Query Version)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-10-27 02:02:02 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 418 ms / 5,000 ms |
| コード長 | 2,620 bytes |
| コンパイル時間 | 2,175 ms |
| コンパイル使用メモリ | 200,148 KB |
| 最終ジャッジ日時 | 2025-02-17 14:05:04 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 55 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
template <typename X>
struct SegTree{
using FX = function<X(X&, X&)>; // X•X -> X となる関数の型
int n;
FX fx;
const X ex;
vector<X> dat;
SegTree(int n_, const FX &fx_, const X &ex_) : n(), fx(fx_), ex(ex_){
int x = 1;
while(n_ > x){
x *= 2;
}
n = x;
dat.assign(n * 2, ex);
}
X get(int i) const {
return dat[i + n];
}
void set(int i, X &x){ dat[i + n] = x; }
void build(){
for(int k = n - 1; k >= 1; k--) dat[k] = fx(dat[k * 2], dat[k * 2 + 1]);
}
void update(int i, X &x){
i += n;
dat[i] = x;
while(i > 0){
i >>= 1; // parent
dat[i] = fx(dat[i * 2], dat[i * 2 + 1]);
}
}
X query(int a, int b){
X vl = ex;
X vr = ex;
int l = a + n;
int r = b + n;
while(l < r){
if(l & 1) vl = fx(vl, dat[l++]);
if(r & 1) vr = fx(dat[--r], vr);
l >>= 1;
r >>= 1;
}
return fx(vl, vr);
}
X operator [](int i) const {
return dat[i + n];
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
string s;
cin >> n >> s >> q;
using T = array<long long, 53>;
// A, B, ..., Z, A*, B*, ..., Z*, ans
T ex;
ex.fill(0);
auto fx = [](T &a, T &b){
T res;
res.fill(0);
for(int i = 0; i < 53; i++){
res[i] = a[i] + b[i];
}
long long s = 0;
for(int i = 0; i < 26; i++){
s += b[i];
}
// M A
for(int i = 0; i < 26; i++){
res[26 + i] += a[i] * (s - b[i]);
}
// MM A
for(int i = 0; i < 26; i++){
long long cnt2 = a[i] * (a[i] - 1) / 2;
res[52] += cnt2 * (s - b[i]);
}
// M MA
for(int i = 0; i < 26; i++){
res[52] += a[i] * b[26 + i];
}
return res;
};
SegTree<T> seg(n, fx, ex);
for(int i = 0; i < n; i++){
T cnt;
cnt.fill(0);
cnt[s[i] - 'A']++;
seg.update(i, cnt);
}
while(q--){
int t; cin >> t;
if(t == 1){
int x;
char c;
cin >> x >> c; x--;
T cnt;
cnt.fill(0);
cnt[c - 'A']++;
seg.update(x, cnt);
}else{
int l, r;
cin >> l >> r; l--;
cout << seg.query(l, r)[52] << endl;
}
}
}