結果

問題 No.875 Range Mindex Query
ユーザー htensaihtensai
提出日時 2020-06-11 09:11:33
言語 Java19
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,620 bytes
コンパイル時間 3,617 ms
コンパイル使用メモリ 74,452 KB
実行使用メモリ 68,748 KB
最終ジャッジ日時 2023-09-06 08:03:52
合計ジャッジ時間 15,249 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
56,388 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
権限があれば一括ダウンロードができます

ソースコード

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