結果

問題 No.2554 MMA文字列2 (Query Version)
ユーザー tentententen
提出日時 2023-11-27 16:15:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 2,877 ms / 5,000 ms
コード長 5,680 bytes
コンパイル時間 3,015 ms
コンパイル使用メモリ 87,148 KB
実行使用メモリ 123,496 KB
最終ジャッジ日時 2024-09-26 12:28:58
合計ジャッジ時間 98,698 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
36,920 KB
testcase_01 AC 63 ms
36,944 KB
testcase_02 AC 188 ms
40,528 KB
testcase_03 AC 197 ms
41,048 KB
testcase_04 AC 203 ms
42,632 KB
testcase_05 AC 170 ms
41,048 KB
testcase_06 AC 192 ms
41,108 KB
testcase_07 AC 188 ms
40,796 KB
testcase_08 AC 117 ms
39,852 KB
testcase_09 AC 189 ms
41,072 KB
testcase_10 AC 197 ms
41,944 KB
testcase_11 AC 176 ms
40,160 KB
testcase_12 AC 294 ms
57,784 KB
testcase_13 AC 2,330 ms
123,496 KB
testcase_14 AC 837 ms
63,108 KB
testcase_15 AC 1,725 ms
121,800 KB
testcase_16 AC 1,679 ms
63,580 KB
testcase_17 AC 2,158 ms
82,804 KB
testcase_18 AC 1,183 ms
48,596 KB
testcase_19 AC 1,303 ms
63,300 KB
testcase_20 AC 2,142 ms
123,472 KB
testcase_21 AC 1,389 ms
49,992 KB
testcase_22 AC 2,446 ms
121,820 KB
testcase_23 AC 2,422 ms
121,860 KB
testcase_24 AC 2,419 ms
121,876 KB
testcase_25 AC 2,411 ms
121,856 KB
testcase_26 AC 2,436 ms
121,876 KB
testcase_27 AC 2,392 ms
121,724 KB
testcase_28 AC 2,395 ms
121,812 KB
testcase_29 AC 2,421 ms
121,804 KB
testcase_30 AC 2,473 ms
121,988 KB
testcase_31 AC 2,401 ms
121,716 KB
testcase_32 AC 534 ms
46,732 KB
testcase_33 AC 508 ms
46,784 KB
testcase_34 AC 676 ms
46,684 KB
testcase_35 AC 674 ms
46,656 KB
testcase_36 AC 671 ms
46,644 KB
testcase_37 AC 2,830 ms
122,180 KB
testcase_38 AC 2,825 ms
122,164 KB
testcase_39 AC 2,827 ms
122,140 KB
testcase_40 AC 2,818 ms
122,284 KB
testcase_41 AC 2,877 ms
122,280 KB
testcase_42 AC 2,763 ms
122,084 KB
testcase_43 AC 2,716 ms
122,232 KB
testcase_44 AC 2,727 ms
122,028 KB
testcase_45 AC 2,747 ms
122,192 KB
testcase_46 AC 2,749 ms
122,216 KB
testcase_47 AC 2,051 ms
119,328 KB
testcase_48 AC 2,049 ms
119,484 KB
testcase_49 AC 2,053 ms
119,588 KB
testcase_50 AC 2,051 ms
119,544 KB
testcase_51 AC 2,050 ms
119,452 KB
testcase_52 AC 1,926 ms
109,804 KB
testcase_53 AC 1,943 ms
109,684 KB
testcase_54 AC 1,928 ms
109,672 KB
testcase_55 AC 1,917 ms
109,808 KB
testcase_56 AC 1,945 ms
109,752 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