結果

問題 No.2242 Cities and Teleporters
ユーザー akakimidoriakakimidori
提出日時 2023-03-10 22:56:53
言語 Rust
(1.77.0 + proconio)
結果
WA  
実行時間 -
コード長 6,410 bytes
コンパイル時間 14,236 ms
コンパイル使用メモリ 385,228 KB
実行使用メモリ 78,136 KB
最終ジャッジ日時 2024-09-18 05:06:18
合計ジャッジ時間 21,861 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 WA -
testcase_05 AC 388 ms
76,980 KB
testcase_06 AC 362 ms
77,028 KB
testcase_07 AC 417 ms
78,136 KB
testcase_08 AC 509 ms
77,904 KB
testcase_09 AC 345 ms
77,396 KB
testcase_10 AC 186 ms
77,188 KB
testcase_11 AC 190 ms
27,612 KB
testcase_12 AC 191 ms
27,228 KB
testcase_13 AC 248 ms
55,972 KB
testcase_14 AC 313 ms
65,080 KB
testcase_15 AC 245 ms
55,268 KB
testcase_16 AC 257 ms
56,752 KB
testcase_17 AC 371 ms
64,484 KB
testcase_18 AC 219 ms
46,464 KB
testcase_19 AC 164 ms
30,448 KB
testcase_20 AC 237 ms
53,496 KB
testcase_21 AC 207 ms
42,416 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: type alias `Map` is never used
 --> src/main.rs:4:6
  |
4 | type Map<K, V> = BTreeMap<K, V>;
  |      ^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: type alias `Set` is never used
 --> src/main.rs:5:6
  |
5 | type Set<T> = BTreeSet<T>;
  |      ^^^

warning: type alias `Deque` is never used
 --> src/main.rs:6:6
  |
6 | type Deque<T> = VecDeque<T>;
  |      ^^^^^

ソースコード

diff #

use std::io::Write;
use std::collections::*;

type Map<K, V> = BTreeMap<K, V>;
type Set<T> = BTreeSet<T>;
type Deque<T> = VecDeque<T>;

fn run() {
    input! {
        n: usize,
        h: [i32; n],
        t: [i32; n],
        q: usize,
        ask: [(usize1, usize1); q],
    }
    let mut ord = (0..n).collect::<Vec<_>>();
    ord.sort_by_key(|x| h[*x]);
    let mut trans = vec![0; n];
    for i in 0..n {
        let pos = ord.upper_bound_by_key(&t[ord[i]], |x| h[*x]);
        trans[i] = pos;
    }
    let mut memo = vec![];
    let mut memo_max = vec![];
    loop {
        let mut max = trans.clone();
        let mut v = max[0];
        let mut r = 1;
        for i in 1..n {
            while r < n && h[ord[r]] == h[ord[i]] {
                v = v.max(max[r]);
                r += 1;
            }
            max[i] = v;
        }
        let mut next = vec![0; n];
        for i in 0..n {
            if trans[i] > 0 {
                next[i] = trans[i].max(max[trans[i] - 1]);
            }
        }
        memo.push(trans.clone());
        memo_max.push(max);
        if next == trans {
            break;
        }
        trans = next;
    }
    let mut inv = vec![0; n];
    for i in 0..n {
        inv[ord[i]] = i;
    }
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    for (a, b) in ask {
        if memo[0][inv[a]] == 0 {
            writeln!(out, "-1").ok();
            continue;
        }
        let mut ans = 1;
        let mut x = memo[0][inv[a]] - 1;
        for (i, memo) in memo_max.iter().enumerate().rev() {
            if memo[x] > 0 && memo[x] <= inv[b] {
                ans += 1 << i;
                x = memo[x] - 1;
            }
        }
        if x < inv[b] && memo_max[0][x] > 0 {
            ans += 1;
            x = memo_max[0][x] - 1;
        }
        if x < inv[b] {
            ans = -1;
        }
        writeln!(out, "{}", ans).ok();
    }
}

fn main() {
    run();
}

// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[macro_export]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[macro_export]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[macro_export]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, bytes) => {
        read_value!($iter, String).bytes().collect::<Vec<u8>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}
// ---------- end input macro ----------
// ---------- begin super slice ----------
pub trait SuperSlice {
    type Item;
    fn lower_bound(&self, key: &Self::Item) -> usize
    where
        Self::Item: Ord;
    fn lower_bound_by<F>(&self, f: F) -> usize
    where
        F: FnMut(&Self::Item) -> std::cmp::Ordering;
    fn lower_bound_by_key<K, F>(&self, key: &K, f: F) -> usize
    where
        K: Ord,
        F: FnMut(&Self::Item) -> K;
    fn upper_bound(&self, key: &Self::Item) -> usize
    where
        Self::Item: Ord;
    fn upper_bound_by<F>(&self, f: F) -> usize
    where
        F: FnMut(&Self::Item) -> std::cmp::Ordering;
    fn upper_bound_by_key<K, F>(&self, key: &K, f: F) -> usize
    where
        K: Ord,
        F: FnMut(&Self::Item) -> K;
    fn next_permutation(&mut self) -> bool
    where
        Self::Item: Ord;
    fn next_permutation_by<F>(&mut self, f: F) -> bool
    where
        F: FnMut(&Self::Item, &Self::Item) -> std::cmp::Ordering;
    fn prev_permutation(&mut self) -> bool
    where
        Self::Item: Ord;
}

impl<T> SuperSlice for [T] {
    type Item = T;
    fn lower_bound(&self, key: &Self::Item) -> usize
    where
        T: Ord,
    {
        self.lower_bound_by(|p| p.cmp(key))
    }
    fn lower_bound_by<F>(&self, mut f: F) -> usize
    where
        F: FnMut(&Self::Item) -> std::cmp::Ordering,
    {
        self.binary_search_by(|p| f(p).then(std::cmp::Ordering::Greater))
            .unwrap_err()
    }
    fn lower_bound_by_key<K, F>(&self, key: &K, mut f: F) -> usize
    where
        K: Ord,
        F: FnMut(&Self::Item) -> K,
    {
        self.lower_bound_by(|p| f(p).cmp(key))
    }
    fn upper_bound(&self, key: &Self::Item) -> usize
    where
        T: Ord,
    {
        self.upper_bound_by(|p| p.cmp(key))
    }
    fn upper_bound_by<F>(&self, mut f: F) -> usize
    where
        F: FnMut(&Self::Item) -> std::cmp::Ordering,
    {
        self.binary_search_by(|p| f(p).then(std::cmp::Ordering::Less))
            .unwrap_err()
    }
    fn upper_bound_by_key<K, F>(&self, key: &K, mut f: F) -> usize
    where
        K: Ord,
        F: FnMut(&Self::Item) -> K,
    {
        self.upper_bound_by(|p| f(p).cmp(key))
    }
    fn next_permutation(&mut self) -> bool
    where
        T: Ord,
    {
        self.next_permutation_by(|a, b| a.cmp(b))
    }
    fn next_permutation_by<F>(&mut self, mut f: F) -> bool
    where
        F: FnMut(&Self::Item, &Self::Item) -> std::cmp::Ordering,
    {
        use std::cmp::Ordering::*;
        if let Some(x) = self.windows(2).rposition(|a| f(&a[0], &a[1]) == Less) {
            let y = self.iter().rposition(|b| f(&self[x], b) == Less).unwrap();
            self.swap(x, y);
            self[(x + 1)..].reverse();
            true
        } else {
            self.reverse();
            false
        }
    }
    fn prev_permutation(&mut self) -> bool
    where
        T: Ord,
    {
        self.next_permutation_by(|a, b| a.cmp(b).reverse())
    }
}
// ---------- end super slice ----------

0