結果
| 問題 |
No.875 Range Mindex Query
|
| コンテスト | |
| ユーザー |
htensai
|
| 提出日時 | 2020-06-11 09:11:33 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,620 bytes |
| コンパイル時間 | 2,259 ms |
| コンパイル使用メモリ | 78,452 KB |
| 実行使用メモリ | 71,632 KB |
| 最終ジャッジ日時 | 2024-06-24 02:49:30 |
| 合計ジャッジ時間 | 15,757 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | WA * 18 |
ソースコード
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;
}
}
}
htensai