結果

問題 No.2554 MMA文字列2 (Query Version)
ユーザー tentententen
提出日時 2023-11-27 16:15:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 2,645 ms / 5,000 ms
コード長 5,680 bytes
コンパイル時間 2,896 ms
コンパイル使用メモリ 89,796 KB
実行使用メモリ 140,332 KB
最終ジャッジ日時 2023-11-27 16:17:25
合計ジャッジ時間 91,505 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
53,984 KB
testcase_01 AC 60 ms
53,988 KB
testcase_02 AC 173 ms
56,392 KB
testcase_03 AC 196 ms
58,868 KB
testcase_04 AC 188 ms
58,928 KB
testcase_05 AC 161 ms
56,272 KB
testcase_06 AC 181 ms
56,548 KB
testcase_07 AC 183 ms
56,496 KB
testcase_08 AC 112 ms
55,920 KB
testcase_09 AC 176 ms
58,548 KB
testcase_10 AC 185 ms
58,472 KB
testcase_11 AC 162 ms
56,668 KB
testcase_12 AC 265 ms
70,892 KB
testcase_13 AC 2,205 ms
140,332 KB
testcase_14 AC 829 ms
77,000 KB
testcase_15 AC 1,617 ms
138,280 KB
testcase_16 AC 1,601 ms
77,212 KB
testcase_17 AC 2,064 ms
98,164 KB
testcase_18 AC 1,145 ms
60,484 KB
testcase_19 AC 1,280 ms
77,112 KB
testcase_20 AC 1,975 ms
140,328 KB
testcase_21 AC 1,382 ms
64,464 KB
testcase_22 AC 2,236 ms
138,256 KB
testcase_23 AC 2,241 ms
138,240 KB
testcase_24 AC 2,241 ms
138,248 KB
testcase_25 AC 2,228 ms
138,268 KB
testcase_26 AC 2,241 ms
138,252 KB
testcase_27 AC 2,231 ms
138,256 KB
testcase_28 AC 2,263 ms
136,304 KB
testcase_29 AC 2,246 ms
136,300 KB
testcase_30 AC 2,232 ms
138,256 KB
testcase_31 AC 2,225 ms
138,248 KB
testcase_32 AC 521 ms
62,288 KB
testcase_33 AC 494 ms
62,424 KB
testcase_34 AC 650 ms
62,088 KB
testcase_35 AC 677 ms
62,060 KB
testcase_36 AC 700 ms
62,056 KB
testcase_37 AC 2,588 ms
140,292 KB
testcase_38 AC 2,600 ms
140,320 KB
testcase_39 AC 2,645 ms
140,308 KB
testcase_40 AC 2,617 ms
140,320 KB
testcase_41 AC 2,613 ms
138,352 KB
testcase_42 AC 2,572 ms
140,304 KB
testcase_43 AC 2,549 ms
140,324 KB
testcase_44 AC 2,534 ms
140,324 KB
testcase_45 AC 2,525 ms
140,324 KB
testcase_46 AC 2,562 ms
138,356 KB
testcase_47 AC 1,924 ms
133,816 KB
testcase_48 AC 1,909 ms
135,796 KB
testcase_49 AC 1,927 ms
135,776 KB
testcase_50 AC 1,939 ms
135,784 KB
testcase_51 AC 1,886 ms
135,764 KB
testcase_52 AC 1,799 ms
123,228 KB
testcase_53 AC 1,805 ms
123,188 KB
testcase_54 AC 1,791 ms
123,232 KB
testcase_55 AC 1,804 ms
123,236 KB
testcase_56 AC 1,805 ms
123,228 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);
        }
        st.allCalc(0);
        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);
                st.calc(idx);
            } 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;
        }
        
        public void allCalc(int idx) {
            if (idx >= size - 1) {
                return;
            }
            allCalc(idx * 2 + 1);
            allCalc(idx * 2 + 2);
            coreCalc(idx);
        }
        
        public void calc(int idx) {
            calcTree((size + idx - 2) / 2);
        }
        
        public void calcTree(int idx) {
            coreCalc(idx);
            if (idx > 0) {
                calcTree((idx - 1) / 2);
            }
        }
        
        private void coreCalc(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];
                }
            }
        }
        
        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