結果

問題 No.318 学学学学学
ユーザー htensaihtensai
提出日時 2020-06-11 21:33:21
言語 Java19
(openjdk 21)
結果
AC  
実行時間 693 ms / 2,000 ms
コード長 2,980 bytes
コンパイル時間 4,092 ms
コンパイル使用メモリ 75,376 KB
実行使用メモリ 82,036 KB
最終ジャッジ日時 2023-09-06 08:37:23
合計ジャッジ時間 14,398 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 168 ms
55,088 KB
testcase_01 AC 255 ms
58,644 KB
testcase_02 AC 283 ms
58,500 KB
testcase_03 AC 214 ms
60,080 KB
testcase_04 AC 258 ms
58,920 KB
testcase_05 AC 693 ms
81,884 KB
testcase_06 AC 622 ms
74,612 KB
testcase_07 AC 567 ms
75,216 KB
testcase_08 AC 521 ms
70,572 KB
testcase_09 AC 471 ms
68,436 KB
testcase_10 AC 377 ms
67,216 KB
testcase_11 AC 693 ms
82,036 KB
testcase_12 AC 525 ms
74,536 KB
testcase_13 AC 500 ms
74,616 KB
testcase_14 AC 453 ms
71,776 KB
testcase_15 AC 400 ms
68,036 KB
testcase_16 AC 351 ms
69,844 KB
testcase_17 AC 465 ms
74,980 KB
testcase_18 AC 454 ms
74,784 KB
testcase_19 AC 459 ms
74,896 KB
testcase_20 AC 303 ms
64,336 KB
testcase_21 AC 44 ms
49,464 KB
testcase_22 AC 45 ms
49,240 KB
testcase_23 AC 44 ms
49,504 KB
testcase_24 AC 43 ms
49,444 KB
testcase_25 AC 44 ms
49,476 KB
testcase_26 AC 45 ms
49,420 KB
testcase_27 AC 45 ms
49,636 KB
testcase_28 AC 45 ms
49,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
	public static void main (String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		SegmentTree st = new SegmentTree(n + 1);
		TreeMap<Integer, ArrayList<Integer>> map = new TreeMap<>();
		String[] second = br.readLine().split(" ", n);
		for (int i = 0; i < n; i++) {
		    int x = Integer.parseInt(second[i]);
		    if (!map.containsKey(x)) {
		        map.put(x, new ArrayList<>());
		    }
		    map.get(x).add(i);
		    st.set(i, x);
		}
		for (int x : map.keySet()) {
		    ArrayList<Integer> list = map.get(x);
		    int left = list.get(0);
		    int right = list.get(list.size() - 1);
	        st.update(left, right, x);
		}
		st.updateAll();
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < n; i++) {
		    if (i > 0) {
		        sb.append(" ");
		    }
		    sb.append(st.getCurrent(i));
		}
		System.out.println(sb);
	}
}

class SegmentTree {
    int size;
    int base;
    int[] tree;
    
    public SegmentTree(int size) {
        this.size = size;
        base = 1;
        while (base < size) {
            base <<= 1;
        }
        tree = new int[base * 2 - 1];
    }
    
    public void set(int idx, int value) {
        tree[idx + base - 1] = value;
    }
    
    public int getCurrent(int idx) {
        return tree[idx + base - 1];
    }
    
    public int get(int idx) {
        return getTree(idx + base - 1);
    }
    
    private int getTree(int idx) {
        if (idx == 0) {
            return tree[idx];
        }
        int ans;
        if (idx % 2 == 1) {
            ans = getTree((idx - 1) / 2);
        } else {
            ans = getTree((idx - 1) / 2);
        }
        if (ans == 0) {
            return tree[idx];
        } else {
            return ans;
        }
    }
    
    public void updateAll() {
        updateAll(0, 0);
    }
    
    private void updateAll(int idx, int value) {
        if (value > 0) {
            tree[idx] = value;
        }
        if (idx >= base - 1) {
            return;
        }
        updateAll(2 * idx + 1, tree[idx]);
        updateAll(2 * idx + 2, tree[idx]);
    }
    
    public void update(int min, int max, int value) {
        update(min, max, value, 0, 0, base);
    }
    
    private void update(int min, int max, int value, int idx, int left, int right) {
        if (min >= right || max < left) {
            return;
        }
        if (min <= left && right - 1 <= max) {
            tree[idx] = value;
            return;
        }
        update(min, max, value, 2 * idx + 1, left, (left + right) / 2);
        update(min, max, value, 2 * idx + 2, (left + right) / 2, right);
        if (tree[idx] > 0) {
            int tmp = tree[idx];
            tree[idx] = 0;
            update(left, min - 1, tmp, idx, left, right);
            update(max + 1, right - 1, tmp, idx, left, right);
        }
    }
 }
0