結果
| 問題 |
No.2554 MMA文字列2 (Query Version)
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2023-11-27 15:54:35 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 4,071 ms / 5,000 ms |
| コード長 | 5,248 bytes |
| コンパイル時間 | 3,203 ms |
| コンパイル使用メモリ | 89,472 KB |
| 実行使用メモリ | 123,896 KB |
| 最終ジャッジ日時 | 2024-09-26 12:26:43 |
| 合計ジャッジ時間 | 151,503 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 55 |
ソースコード
import java.io.*;
import java.util.*;
import java.util.stream.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
int n = sc.nextInt();
char[] inputs = sc.next().toCharArray();
int q = sc.nextInt();
SegmentTree st = new SegmentTree(n);
for (int i = 0; i < n; i++) {
st.add(i, inputs[i] - 'A', 1);
}
StringBuilder sb = new StringBuilder();
while (q-- > 0) {
if (sc.nextInt() == 1) {
int idx = sc.nextInt() - 1;
char c = sc.next().charAt(0);
st.add(idx, inputs[idx] - 'A', -1);
inputs[idx] = c;
st.add(idx, inputs[idx] - 'A', 1);
} else {
int left = sc.nextInt() - 1;
int right = sc.nextInt();
sb.append(st.getMMA(left, right)).append("\n");
}
}
System.out.print(sb);
}
static class SegmentTree {
int size;
int[][] counts;
long[][] ma;
long[] mma;
public SegmentTree(int x) {
size = 1;
while (size < x) {
size <<= 1;
}
counts = new int[size * 2 - 1][26];
ma = new long[size * 2 - 1][26];
mma = new long[size * 2 - 1];
}
public void add(int idx, int c, int v) {
counts[size + idx - 1][c] += v;
calc((size + idx - 2) / 2);
}
private void calc(int idx) {
mma[idx] = mma[idx * 2 + 1] + mma[idx * 2 + 2];
for (int i = 0; i < 26; i++) {
counts[idx][i] = counts[idx * 2 + 1][i] + counts[idx * 2 + 2][i];
mma[idx] += counts[idx * 2 + 1][i] * ma[idx * 2 + 2][i];
ma[idx][i] = ma[idx * 2 + 1][i] + ma[idx * 2 + 2][i];
for (int j = 0; j < 26; j++) {
if (i == j) {
continue;
}
ma[idx][i] += counts[idx * 2 + 1][i] * counts[idx * 2 + 2][j];
mma[idx] += (counts[idx * 2 + 1][i] - 1) * counts[idx * 2 + 1][i] / 2 * counts[idx * 2 + 2][j];
}
}
if (idx > 0) {
calc((idx - 1) / 2);
}
}
public long getMMA(int left, int right) {
MMA x = getMMA(0, left, right, 0, size);
return x.mma;
}
private MMA getMMA(int idx, int left, int right, int min, int max) {
if (left <= min && max <= right) {
return new MMA(counts[idx], ma[idx], mma[idx]);
}
if (left >= max || right <= min) {
return MMA.INI;
}
return getMMA(idx * 2 + 1, left, right, min, (min + max) / 2).merge(getMMA(idx * 2 + 2, left, right, (min + max) / 2, max));
}
}
static class MMA {
int[] counts;
long[] ma;
long mma;
static MMA INI = new MMA();
public MMA(int[] counts, long[] ma, long mma) {
this.counts = counts;
this.ma = ma;
this.mma = mma;
}
public MMA() {
this(new int[26], new long[26], 0);
}
public MMA merge(MMA x) {
MMA ans = new MMA();
ans.mma = mma + x.mma;
for (int i = 0; i < 26; i++) {
ans.counts[i] = counts[i] + x.counts[i];
ans.mma += counts[i] * x.ma[i];
ans.ma[i] = ma[i] + x.ma[i];
for (int j = 0; j < 26; j++) {
if (i == j) {
continue;
}
ans.ma[i] += counts[i] * x.counts[j];
ans.mma += (counts[i] - 1) * counts[i] / 2 * x.counts[j];
}
}
return ans;
}
}
}
class Utilities {
static String arrayToLineString(Object[] arr) {
return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
}
static String arrayToLineString(int[] arr) {
return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
}
}
class Scanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
StringBuilder sb = new StringBuilder();
public Scanner() throws Exception {
}
public int nextInt() throws Exception {
return Integer.parseInt(next());
}
public long nextLong() throws Exception {
return Long.parseLong(next());
}
public double nextDouble() throws Exception {
return Double.parseDouble(next());
}
public int[] nextIntArray() throws Exception {
return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
}
public String next() throws Exception {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten