結果

問題 No.2554 MMA文字列2 (Query Version)
ユーザー tentententen
提出日時 2023-11-27 15:54:35
言語 Java21
(openjdk 21)
結果
AC  
実行時間 4,151 ms / 5,000 ms
コード長 5,248 bytes
コンパイル時間 3,258 ms
コンパイル使用メモリ 92,344 KB
実行使用メモリ 140,348 KB
最終ジャッジ日時 2023-11-27 15:57:11
合計ジャッジ時間 154,330 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
53,992 KB
testcase_01 AC 68 ms
54,108 KB
testcase_02 AC 226 ms
57,564 KB
testcase_03 AC 247 ms
59,564 KB
testcase_04 AC 266 ms
59,656 KB
testcase_05 AC 225 ms
57,652 KB
testcase_06 AC 238 ms
57,660 KB
testcase_07 AC 241 ms
57,560 KB
testcase_08 AC 184 ms
57,640 KB
testcase_09 AC 251 ms
59,536 KB
testcase_10 AC 256 ms
59,532 KB
testcase_11 AC 220 ms
57,568 KB
testcase_12 AC 569 ms
70,868 KB
testcase_13 AC 3,580 ms
140,312 KB
testcase_14 AC 1,029 ms
77,032 KB
testcase_15 AC 3,010 ms
138,264 KB
testcase_16 AC 2,265 ms
77,164 KB
testcase_17 AC 3,015 ms
98,152 KB
testcase_18 AC 1,516 ms
62,472 KB
testcase_19 AC 1,777 ms
77,028 KB
testcase_20 AC 3,365 ms
140,312 KB
testcase_21 AC 1,775 ms
64,544 KB
testcase_22 AC 3,935 ms
138,260 KB
testcase_23 AC 3,931 ms
138,256 KB
testcase_24 AC 3,937 ms
138,248 KB
testcase_25 AC 3,922 ms
138,260 KB
testcase_26 AC 3,914 ms
138,256 KB
testcase_27 AC 3,925 ms
138,260 KB
testcase_28 AC 3,931 ms
138,264 KB
testcase_29 AC 3,951 ms
138,264 KB
testcase_30 AC 3,966 ms
138,284 KB
testcase_31 AC 3,923 ms
138,256 KB
testcase_32 AC 686 ms
62,284 KB
testcase_33 AC 667 ms
62,268 KB
testcase_34 AC 839 ms
62,032 KB
testcase_35 AC 780 ms
62,036 KB
testcase_36 AC 804 ms
62,044 KB
testcase_37 AC 3,728 ms
140,312 KB
testcase_38 AC 3,710 ms
140,316 KB
testcase_39 AC 3,719 ms
140,316 KB
testcase_40 AC 3,744 ms
140,316 KB
testcase_41 AC 3,736 ms
140,328 KB
testcase_42 AC 3,767 ms
140,332 KB
testcase_43 AC 3,776 ms
140,324 KB
testcase_44 AC 3,781 ms
140,328 KB
testcase_45 AC 3,770 ms
140,320 KB
testcase_46 AC 3,781 ms
140,348 KB
testcase_47 AC 4,111 ms
135,748 KB
testcase_48 AC 4,105 ms
135,724 KB
testcase_49 AC 4,064 ms
135,712 KB
testcase_50 AC 4,066 ms
135,724 KB
testcase_51 AC 4,068 ms
135,724 KB
testcase_52 AC 4,114 ms
123,496 KB
testcase_53 AC 4,082 ms
123,456 KB
testcase_54 AC 4,083 ms
123,508 KB
testcase_55 AC 4,079 ms
123,504 KB
testcase_56 AC 4,151 ms
123,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
    }
    
}
0