結果

問題 No.875 Range Mindex Query
ユーザー htensaihtensai
提出日時 2020-06-11 09:23:01
言語 Java19
(openjdk 21)
結果
AC  
実行時間 1,066 ms / 2,000 ms
コード長 2,620 bytes
コンパイル時間 3,048 ms
コンパイル使用メモリ 80,408 KB
実行使用メモリ 66,964 KB
最終ジャッジ日時 2023-09-06 08:04:12
合計ジャッジ時間 14,016 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
55,740 KB
testcase_01 AC 201 ms
58,528 KB
testcase_02 AC 206 ms
58,840 KB
testcase_03 AC 180 ms
56,312 KB
testcase_04 AC 190 ms
58,552 KB
testcase_05 AC 191 ms
56,120 KB
testcase_06 AC 200 ms
58,928 KB
testcase_07 AC 204 ms
59,060 KB
testcase_08 AC 194 ms
58,388 KB
testcase_09 AC 194 ms
58,760 KB
testcase_10 AC 213 ms
58,896 KB
testcase_11 AC 1,012 ms
66,684 KB
testcase_12 AC 992 ms
64,664 KB
testcase_13 AC 971 ms
66,656 KB
testcase_14 AC 909 ms
66,708 KB
testcase_15 AC 1,066 ms
66,784 KB
testcase_16 AC 1,005 ms
66,964 KB
testcase_17 AC 1,045 ms
66,608 KB
testcase_18 AC 1,002 ms
66,556 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int q = sc.nextInt();
		SegmentTree st = new SegmentTree(n);
        for (int i = 0; i < n; i++) {
            st.update(i, sc.nextInt());
        }
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < q; i++) {
		    int type = sc.nextInt();
		    int x = sc.nextInt() - 1;
		    int y = sc.nextInt() - 1;
		    if (type == 1) {
		        int ax = st.get(x);
		        int ay = st.get(y);
		        st.update(x, ay);
		        st.update(y, ax);
		    } else {
		        sb.append(st.query(x, y) + 1).append("\n");
		    }
		}
		System.out.print(sb);
	}
}

class SegmentTree {
    int size;
    int base;
    int[] tree;
    int[] idxes;
    
    public SegmentTree(int size) {
        this.size = size;
        base = 1;
        while (base < size) {
            base <<= 1;
        }
        tree = new int[base * 2 - 1];
        idxes = new int[base * 2 - 1];
        Arrays.fill(tree, Integer.MAX_VALUE);
        for (int i = 0; i < size; i++) {
            idxes[i + base - 1] = i;
        }
    }
    
    public int get(int idx) {
        return tree[idx + base - 1];
    }
    
    public void update(int idx, int value) {
        updateTree(idx + base - 1, value, idx);
    }
    
    private void updateTree(int idx, int value, int min) {
        tree[idx] = value;
        idxes[idx] = min;
        if (idx == 0) {
            return;
        }
        
        if (idx % 2 == 1) {
            if (value < tree[idx + 1]) {
                updateTree((idx - 1) / 2, value, min);
            } else {
                updateTree((idx - 1) / 2, tree[idx + 1], idxes[idx + 1]);
            }
        } else {
            if (value < tree[idx - 1]) {
                updateTree((idx - 1) / 2, value, min);
            } else {
                updateTree((idx - 1) / 2, tree[idx - 1], idxes[idx - 1]);
            }
        }
    }
    
    public int query(int min, int max) {
        return idxes[query(min, max, 0, 0, base)];
    }
    
    public int query(int min, int max, int idx, int left, int right) {
        if (min >= right || max < left) {
            return -1;
        }
        if (min <= left && right - 1 <= max) {
            return idx;
        }
        int x1 = query(min, max, 2 * idx + 1, left, (left + right) / 2);
        int x2 = query(min, max, 2 * idx + 2, (left + right) / 2, right);
        if (x1 != -1 && (x2 == -1 || tree[x1] < tree[x2])) {
            return x1;
        } else {
            return x2;
        }
    }
 }
0