結果

問題 No.2977 Kth Xor Pair
ユーザー titia
提出日時 2024-12-15 05:07:24
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 2,475 ms / 3,000 ms
コード長 4,525 bytes
コンパイル時間 13,096 ms
コンパイル使用メモリ 401,460 KB
実行使用メモリ 89,240 KB
最終ジャッジ日時 2024-12-15 05:08:40
合計ジャッジ時間 57,180 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

use std::io;
fn main() {
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
let mut iter = input.trim().split_whitespace();
let n: usize = iter.next().unwrap().parse().unwrap();
let k: usize = iter.next().unwrap().parse().unwrap();
input.clear();
io::stdin().read_line(&mut input).unwrap();
let a: Vec<i32> = input
.trim()
.split_whitespace()
.map(|x| x.parse().unwrap())
.collect();
let r = n * (n - 1) / 2 - k + 1;
let mut next_node_id0 = vec![-1];
let mut next_node_id1 = vec![-1];
let mut parent_id = vec![-1];
let mut depth = vec![0];
let mut count = vec![0];
let mut last = Vec::new();
let mut nodes_id = 1;
for &num in &a {
let mut now = 0;
for i in (0..=31).rev() {
let next = if num & (1 << i) == 0 { 0 } else { 1 };
if next == 0 {
if next_node_id0[now] == -1 {
next_node_id0[now] = nodes_id as i32;
next_node_id0.push(-1);
next_node_id1.push(-1);
parent_id.push(now as i32);
depth.push(depth[now] + 1);
count.push(0);
now = nodes_id;
nodes_id += 1;
} else {
now = next_node_id0[now] as usize;
}
} else {
if next_node_id1[now] == -1 {
next_node_id1[now] = nodes_id as i32;
next_node_id0.push(-1);
next_node_id1.push(-1);
parent_id.push(now as i32);
depth.push(depth[now] + 1);
count.push(0);
now = nodes_id;
nodes_id += 1;
} else {
now = next_node_id1[now] as usize;
}
}
}
count[now] += 1;
last.push(now);
}
let mut count2 = vec![0; count.len()];
for &x in &last {
let mut node = x as i32;
while node != -1 {
count2[node as usize] += 1;
node = parent_id[node as usize];
}
}
let mut ok = 0;
let mut ng = 2147483647;
while ng > ok + 1 {
let mid = (ok + ng) / 2;
let mut total_count = 0;
for &num in &a {
if total_count / 2 >= r {
break;
}
let mut now = 0;
let mut current_count = 0;
let mut flag = false;
for dep in (0..=31).rev() {
let x = next_node_id0[now];
let y = next_node_id1[now];
let num_bit = num & (1 << dep);
let mid_bit = mid & (1 << dep);
match (num_bit == 0, mid_bit == 0) {
(true, true) => {
if y != -1 {
current_count += count2[y as usize];
}
if x != -1 {
now = x as usize;
} else {
flag = true;
break;
}
}
(true, false) => {
if y != -1 {
now = y as usize;
} else {
flag = true;
break;
}
}
(false, true) => {
if x != -1 {
current_count += count2[x as usize];
}
if y != -1 {
now = y as usize;
} else {
flag = true;
break;
}
}
(false, false) => {
if x != -1 {
now = x as usize;
} else {
flag = true;
break;
}
}
}
}
if !flag {
current_count += count2[now];
}
total_count += current_count;
}
if total_count / 2 >= r {
ok = mid;
} else {
ng = mid;
}
}
println!("{}", ok);
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0