結果

問題 No.2333 Slime Structure
ユーザー tentententen
提出日時 2023-06-01 10:04:35
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 6,767 bytes
コンパイル時間 6,037 ms
コンパイル使用メモリ 83,372 KB
実行使用メモリ 106,064 KB
最終ジャッジ日時 2023-08-28 01:54:27
合計ジャッジ時間 22,248 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
49,916 KB
testcase_01 AC 46 ms
49,604 KB
testcase_02 AC 1,407 ms
106,064 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
権限があれば一括ダウンロードができます

ソースコード

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();
        TreeMap<Long, Integer> strengths = new TreeMap<>();
        TreeMap<Long, Integer> compress = new TreeMap<>();
        long current = 0;
        for (int i = 0; i < n; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            strengths.put(current, a);
            compress.put(current, null);
            current += b;
        }
        compress.put(current, null);
        int q = sc.nextInt();
        int[] types = new int[q];
        long[] lefts = new long[q];
        long[] rights = new long[q];
        for (int i = 0; i < q; i++) {
            types[i] = sc.nextInt();
            lefts[i] = sc.nextInt();
            rights[i] = sc.nextInt();
            if (types[i] == 1) {
                compress.put(lefts[i] - 1, null);
                compress.put(lefts[i], null);
            } else {
                compress.put(lefts[i] - 1, null);
                compress.put(rights[i], null);
                compress.put(lefts[i], null);
                compress.put(rights[i] - 1, null);
            }
        }
        int idx = 0;
        long prev = -1;
        long[] sizes = new long[compress.size()];
        long[] values = new long[compress.size()];
        for (long x : compress.keySet()) {
            compress.put(x, idx);
            if (prev >= 0) {
                sizes[idx - 1] = x - prev;
                values[idx - 1] = strengths.floorEntry(prev).getValue();
            }
            prev = x;
            idx++;
        }
        SegmentTree st = new SegmentTree(idx);
        for (int i = 0; i < sizes.length; i++) {
            st.setValue(i, sizes[i] * values[i]);
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < q; i++) {
            if (types[i] == 1) {
                int x = compress.get(lefts[i] - 1);
                st.setValue(x, rights[i]);
            } else {
                sb.append(st.getMax(compress.get(lefts[i] - 1), compress.get(rights[i]))).append("\n");
            }
        }
        System.out.print(sb);
    }
    
    static class SegmentTree {
        int size;
        long[] lefts;
        long[] rights;
        long[] maxes;
        long[] alls;
        
        public SegmentTree(int x) {
            size = 1;
            while (x > size) {
                size <<= 1;
            }
            lefts = new long[size * 2 - 1];
            rights = new long[size * 2 - 1];
            maxes = new long[size * 2 - 1];
            alls = new long[size * 2 - 1];
        }
        
        public void setValue(int idx, long v) {
            int current = size - 1 + idx;
            lefts[current] = v;
            rights[current] = v;
            maxes[current] = v;
            alls[current] = v;
            setTreeValue((current - 1) / 2);
        }
        
        private void setTreeValue(int idx) {
            alls[idx] = alls[idx * 2 + 1] + alls[idx * 2 + 2];
            lefts[idx] = Math.max(alls[idx * 2 + 1] + lefts[idx * 2 + 2], lefts[idx * 2 + 1]);
            rights[idx] = Math.max(rights[idx * 2 + 1] + alls[idx * 2 + 2], rights[idx * 2 + 2]);
            maxes[idx] = Math.max(rights[idx * 2 + 1] + lefts[idx * 2 + 2], Math.max(maxes[idx * 2 + 1], maxes[idx * 2 + 2]));
            if (idx > 0) {
                setTreeValue((idx - 1) / 2);
            }
        }
        
        public long getMax(int left, int right) {
            return getTreeMax(0, 0, size, left, right);
        }
        
        private long getTreeMax(int idx, int min, int max, int left, int right) {
            if (max <= left || right <= min) {
                return Long.MIN_VALUE;
            }
            if (left <= min && max <= right) {
                return maxes[idx];
            }
            return Math.max(getRightMax(idx * 2 + 1, min, (min + max) / 2, left, right) + getLeftMax(idx * 2 + 2, (min + max) / 2, max, left, right), 
                Math.max(getTreeMax(idx * 2 + 1, min, (min + max) / 2, left, right), getTreeMax(idx * 2 + 2, (min + max) / 2, max, left, right)));
        }
        
        private long getLeftMax(int idx, int min, int max, int left, int right) {
            if (min < left || max <= left || right <= min) {
                return Long.MIN_VALUE / 2;
            }
            if (right >= max) {
                return lefts[idx];
            }
            return Math.max(getAll(idx * 2 + 1, min, (min + max) / 2, left, right) + getLeftMax(idx * 2 + 2, (min + max) / 2, max, left, right), getLeftMax(idx * 2 + 1, min, (min + max) / 2, left, right));
        }
        
        private long getRightMax(int idx, int min, int max, int left, int right) {
            if (right < max || max <= left || right <= min) {
                return Long.MIN_VALUE / 2;
            }
            if (left <= min) {
                return rights[idx];
            }
            return Math.max(getRightMax(idx * 2 + 1, min, (min + max) / 2, left, right) + getAll(idx * 2 + 2, (min + max) / 2, max, left, right), getRightMax(idx * 2 + 2, (min + max) / 2, max, left, right));
        }
        
        private long getAll(int idx, int min, int max, int left, int right) {
            if (left <= min && max <= right) {
                return alls[idx];
            } else {
                return Long.MIN_VALUE / 2;
            }
        }
    }
}

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