結果
| 問題 | No.3446 Range Adjacent Differences |
| ユーザー |
|
| 提出日時 | 2026-02-20 16:03:18 |
| 言語 | Rust (1.93.0 + proconio + num + itertools) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 4,854 bytes |
| 記録 | |
| コンパイル時間 | 15,476 ms |
| コンパイル使用メモリ | 220,568 KB |
| 実行使用メモリ | 7,844 KB |
| 最終ジャッジ日時 | 2026-02-20 16:03:41 |
| 合計ジャッジ時間 | 10,742 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | TLE * 1 -- * 25 |
ソースコード
#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
#[allow(unused_imports)]
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
($($r:tt)*) => {
let stdin = std::io::stdin();
let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
let mut next = move || -> String{
bytes.by_ref().map(|r|r.unwrap() as char)
.skip_while(|c|c.is_whitespace())
.take_while(|c|!c.is_whitespace())
.collect()
};
input_inner!{next, $($r)*}
};
}
macro_rules! input_inner {
($next:expr) => {};
($next:expr,) => {};
($next:expr, $var:ident : $t:tt $($r:tt)*) => {
let $var = read_value!($next, $t);
input_inner!{$next $($r)*}
};
}
macro_rules! read_value {
($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) };
($next:expr, [ $t:tt ; $len:expr ]) => {
(0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
};
($next:expr, chars) => {
read_value!($next, String).chars().collect::<Vec<char>>()
};
($next:expr, usize1) => (read_value!($next, usize) - 1);
($next:expr, [ $t:tt ]) => {{
let len = read_value!($next, usize);
read_value!($next, [$t; len])
}};
($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}
#[derive(Default)]
struct Yuki3346State {
bs: BTreeSet<(i64, usize)>,
diff: BTreeSet<(i64, usize, usize)>,
}
impl Yuki3346State {
fn new() -> Self {
Self::default()
}
fn add(&mut self, x: (i64, usize)) {
let nxt = self.bs.range(x..).next().copied();
let prv = self.bs.range(..x).rev().next().copied();
match (prv, nxt) {
(Some(a), Some(b)) => {
let old = (b.0 - a.0, a.1, b.1);
self.diff.remove(&old);
self.diff.insert((x.0 - a.0, a.1, x.1));
self.diff.insert((b.0 - x.0, x.1, b.1));
}
(Some(a), None) => {
self.diff.insert((x.0 - a.0, a.1, x.1));
}
(None, Some(b)) => {
self.diff.insert((b.0 - x.0, x.1, b.1));
}
(None, None) => {}
}
self.bs.insert(x);
}
fn del(&mut self, x: (i64, usize)) {
let nxt = self.bs.range((x.0, x.1 + 1)..).next().copied();
let prv = self.bs.range(..x).rev().next().copied();
match (prv, nxt) {
(Some(a), Some(b)) => {
let old = (b.0 - a.0, a.1, b.1);
self.diff.remove(&(x.0 - a.0, a.1, x.1));
self.diff.remove(&(b.0 - x.0, x.1, b.1));
self.diff.insert(old);
}
(Some(a), None) => {
self.diff.remove(&(x.0 - a.0, a.1, x.1));
}
(None, Some(b)) => {
self.diff.remove(&(b.0 - x.0, x.1, b.1));
}
(None, None) => {}
}
self.bs.remove(&x);
}
fn query(&self, x: i64, c: char) -> i64 {
match c {
'L' => {
let a = self.diff.range(..=(x, !0, !0)).rev().next();
a.map(|a| a.0).unwrap_or(-1)
}
'R' => {
let a = self.diff.range((x, 0, 0)..).next();
a.map(|a| a.0).unwrap_or(-1)
}
_ => panic!(),
}
}
}
// https://yukicoder.me/problems/no/3446 (3.5)
// Mo's algorithm を使う。
fn main() {
#[allow(unused)]
let out = std::io::stdout();
#[allow(unused)]
let mut out = BufWriter::new(out.lock());
macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););}
input! {
n: usize, q: usize,
a: [i64; n],
lrxc: [(usize1, usize, i64, char); q],
}
const B: usize = 500;
let mut lri: Vec<_> = (0..q).map(|i| {
let (l, r, _, _) = lrxc[i];
(l, r, i)
}).collect();
lri.sort_by_key(|&(l, r, _idx)| {
let q = l / B;
if q % 2 == 1 {
(q, n - r)
} else {
(q, r)
}
});
let mut ans = vec![0; q];
// pointer
let mut cl = 0;
let mut cr = 0;
// state
let mut st = Yuki3346State::new();
for &(l, r, idx) in &lri {
while cr < r {
st.add((a[cr], cr));
cr += 1;
}
while cl > l {
cl -= 1;
st.add((a[cl], cl));
}
while cr > r {
cr -= 1;
st.del((a[cr], cr));
}
while cl < l {
st.del((a[cl], cl));
cl += 1;
}
let (_, _, x, c) = lrxc[idx];
ans[idx] = st.query(x, c);
}
for a in ans {
puts!("{a}\n");
}
}