結果
| 問題 |
No.878 Range High-Element Query
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2022-08-29 14:05:14 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 798 ms / 2,000 ms |
| コード長 | 2,411 bytes |
| コンパイル時間 | 2,431 ms |
| コンパイル使用メモリ | 78,948 KB |
| 実行使用メモリ | 77,888 KB |
| 最終ジャッジ日時 | 2024-11-06 11:53:21 |
| 合計ジャッジ時間 | 11,235 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 18 |
ソースコード
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
int n = sc.nextInt();
int q = sc.nextInt();
PriorityQueue<Unit> queue = new PriorityQueue<>();
for (int i = 0; i < n; i++) {
queue.add(new Unit(sc.nextInt(), i));
}
TreeSet<Integer> idxes = new TreeSet<>();
idxes.add(n);
int[][] nexts = new int[17][n + 1];
while (queue.size() > 0) {
Unit u = queue.poll();
nexts[0][u.idx] = idxes.higher(u.idx);
idxes.add(u.idx);
}
nexts[0][n] = n;
for (int i = 1; i < 17; i++) {
for (int j = 0; j <= n; j++) {
nexts[i][j] = nexts[i - 1][nexts[i - 1][j]];
}
}
StringBuilder sb = new StringBuilder();
while (q-- > 0) {
sc.nextInt();
int left = sc.nextInt() - 1;
int right = sc.nextInt() - 1;
int ans = 1;
for (int i = 16; i >= 0; i--) {
if (nexts[i][left] <= right) {
left = nexts[i][left];
ans += (1 << i);
}
}
sb.append(ans).append("\n");
}
System.out.print(sb);
}
static class Unit implements Comparable<Unit> {
int value;
int idx;
public Unit(int value, int idx) {
this.value = value;
this.idx = idx;
}
public int compareTo(Unit another) {
return another.value - value;
}
}
}
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 String next() throws Exception {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten