結果

問題 No.2333 Slime Structure
ユーザー tentententen
提出日時 2023-06-01 10:07:37
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 6,769 bytes
コンパイル時間 6,005 ms
コンパイル使用メモリ 91,868 KB
実行使用メモリ 165,120 KB
最終ジャッジ日時 2024-06-08 21:30:55
合計ジャッジ時間 83,454 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
37,096 KB
testcase_01 AC 55 ms
37,164 KB
testcase_02 AC 1,510 ms
95,336 KB
testcase_03 AC 2,008 ms
133,480 KB
testcase_04 AC 2,062 ms
123,668 KB
testcase_05 AC 1,666 ms
102,592 KB
testcase_06 AC 2,127 ms
132,128 KB
testcase_07 AC 2,393 ms
151,972 KB
testcase_08 AC 2,456 ms
143,896 KB
testcase_09 AC 2,413 ms
141,928 KB
testcase_10 AC 2,448 ms
152,376 KB
testcase_11 AC 2,476 ms
151,900 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 2,366 ms
149,548 KB
testcase_18 AC 2,374 ms
150,212 KB
testcase_19 AC 2,402 ms
150,240 KB
testcase_20 AC 2,376 ms
150,036 KB
testcase_21 AC 2,372 ms
150,416 KB
testcase_22 AC 2,857 ms
163,480 KB
testcase_23 AC 2,827 ms
163,540 KB
testcase_24 AC 2,867 ms
165,120 KB
testcase_25 AC 2,913 ms
163,528 KB
testcase_26 AC 2,830 ms
163,612 KB
testcase_27 AC 2,839 ms
163,672 KB
testcase_28 AC 2,820 ms
163,476 KB
testcase_29 AC 2,885 ms
163,652 KB
testcase_30 AC 2,791 ms
163,492 KB
testcase_31 AC 2,786 ms
165,096 KB
testcase_32 AC 2,117 ms
149,704 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();
        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.nextLong();
            rights[i] = sc.nextLong();
            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