結果
問題 | No.2554 MMA文字列2 (Query Version) |
ユーザー | tenten |
提出日時 | 2023-11-27 15:54:35 |
言語 | Java21 (openjdk 21) |
結果 |
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 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 57 ms
37,300 KB |
testcase_01 | AC | 66 ms
37,108 KB |
testcase_02 | AC | 225 ms
41,872 KB |
testcase_03 | AC | 253 ms
42,604 KB |
testcase_04 | AC | 267 ms
43,236 KB |
testcase_05 | AC | 230 ms
42,236 KB |
testcase_06 | AC | 242 ms
42,760 KB |
testcase_07 | AC | 258 ms
42,380 KB |
testcase_08 | AC | 186 ms
41,392 KB |
testcase_09 | AC | 251 ms
42,616 KB |
testcase_10 | AC | 247 ms
42,708 KB |
testcase_11 | AC | 218 ms
41,620 KB |
testcase_12 | AC | 570 ms
57,132 KB |
testcase_13 | AC | 3,465 ms
122,220 KB |
testcase_14 | AC | 1,043 ms
63,400 KB |
testcase_15 | AC | 2,948 ms
122,124 KB |
testcase_16 | AC | 2,221 ms
63,880 KB |
testcase_17 | AC | 3,016 ms
83,200 KB |
testcase_18 | AC | 1,469 ms
48,784 KB |
testcase_19 | AC | 1,813 ms
63,644 KB |
testcase_20 | AC | 3,371 ms
123,896 KB |
testcase_21 | AC | 1,751 ms
50,496 KB |
testcase_22 | AC | 3,980 ms
122,056 KB |
testcase_23 | AC | 3,918 ms
122,208 KB |
testcase_24 | AC | 3,912 ms
122,020 KB |
testcase_25 | AC | 3,914 ms
122,180 KB |
testcase_26 | AC | 3,909 ms
122,052 KB |
testcase_27 | AC | 3,912 ms
122,272 KB |
testcase_28 | AC | 3,897 ms
122,204 KB |
testcase_29 | AC | 3,989 ms
122,144 KB |
testcase_30 | AC | 3,907 ms
122,336 KB |
testcase_31 | AC | 3,923 ms
122,252 KB |
testcase_32 | AC | 672 ms
46,868 KB |
testcase_33 | AC | 666 ms
47,012 KB |
testcase_34 | AC | 779 ms
47,052 KB |
testcase_35 | AC | 776 ms
47,104 KB |
testcase_36 | AC | 836 ms
47,072 KB |
testcase_37 | AC | 3,701 ms
122,540 KB |
testcase_38 | AC | 3,714 ms
122,468 KB |
testcase_39 | AC | 3,703 ms
122,488 KB |
testcase_40 | AC | 3,686 ms
122,616 KB |
testcase_41 | AC | 3,724 ms
122,640 KB |
testcase_42 | AC | 3,769 ms
122,544 KB |
testcase_43 | AC | 3,771 ms
122,512 KB |
testcase_44 | AC | 3,766 ms
122,568 KB |
testcase_45 | AC | 3,754 ms
122,340 KB |
testcase_46 | AC | 3,755 ms
122,660 KB |
testcase_47 | AC | 4,034 ms
119,864 KB |
testcase_48 | AC | 4,055 ms
119,868 KB |
testcase_49 | AC | 4,062 ms
119,856 KB |
testcase_50 | AC | 4,055 ms
119,872 KB |
testcase_51 | AC | 4,068 ms
119,784 KB |
testcase_52 | AC | 4,054 ms
109,916 KB |
testcase_53 | AC | 4,062 ms
110,104 KB |
testcase_54 | AC | 4,047 ms
109,968 KB |
testcase_55 | AC | 4,067 ms
110,156 KB |
testcase_56 | AC | 4,071 ms
109,780 KB |
ソースコード
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(); } }