結果

問題 No.2077 Get Minimum Algorithm
ユーザー akakimidoriakakimidori
提出日時 2022-09-16 21:47:01
言語 Rust
(1.77.0)
結果
AC  
実行時間 100 ms / 3,000 ms
コード長 4,600 bytes
コンパイル時間 2,108 ms
コンパイル使用メモリ 158,560 KB
実行使用メモリ 14,676 KB
最終ジャッジ日時 2023-08-23 14:48:23
合計ジャッジ時間 7,093 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 5 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 5 ms
4,376 KB
testcase_09 AC 6 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 5 ms
4,380 KB
testcase_12 AC 96 ms
14,568 KB
testcase_13 AC 95 ms
14,520 KB
testcase_14 AC 95 ms
14,564 KB
testcase_15 AC 93 ms
14,492 KB
testcase_16 AC 93 ms
14,528 KB
testcase_17 AC 95 ms
14,472 KB
testcase_18 AC 96 ms
14,560 KB
testcase_19 AC 100 ms
14,504 KB
testcase_20 AC 97 ms
14,548 KB
testcase_21 AC 94 ms
14,540 KB
testcase_22 AC 72 ms
14,580 KB
testcase_23 AC 71 ms
14,568 KB
testcase_24 AC 73 ms
14,584 KB
testcase_25 AC 70 ms
14,556 KB
testcase_26 AC 72 ms
14,584 KB
testcase_27 AC 92 ms
14,568 KB
testcase_28 AC 77 ms
13,484 KB
testcase_29 AC 65 ms
10,528 KB
testcase_30 AC 96 ms
14,676 KB
testcase_31 AC 84 ms
13,476 KB
testcase_32 AC 68 ms
10,528 KB
testcase_33 AC 71 ms
14,548 KB
testcase_34 AC 66 ms
13,464 KB
testcase_35 AC 56 ms
10,488 KB
testcase_36 AC 70 ms
14,556 KB
testcase_37 AC 94 ms
14,624 KB
testcase_38 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: type alias `Map` is never used
  --> Main.rs:23:6
   |
23 | type Map<K, V> = BTreeMap<K, V>;
   |      ^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: type alias `Set` is never used
  --> Main.rs:24:6
   |
24 | type Set<T> = BTreeSet<T>;
   |      ^^^

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

warning: 3 warnings emitted

ソースコード

diff #

// x_i 回操作した時のy_i の場所を答えて
// (x < y)
//
// 操作回数はN^2になりうる
// n, n-1, ..., 1
//
// 2. の操作は X > P_i ならX, P_i をswapと言える
// うーん?
//
// 1クエリを考えよう
// 考えるべき値は大きい、y, 小さいのみ
// L, S としよう
// yの左にSがあるなら最左のそれを消して終わり
// Sがないならyを最寄りのSに移して終わり
// Sの場所がわかれば解ける
//
// クエリに対応するには?
//

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,
        p: [usize; n],
        q: usize,
        ask: [(usize, usize); q],
    }
    let mut ip = vec![0; n];
    for i in 0..n {
        ip[p[i] - 1] = i;
    }
    let mut ord = (0..q).collect::<Vec<_>>();
    ord.sort_by_key(|p| !ask[*p].1);
    let mut ans = vec![0; q];
    let mut bit = Fenwick::new(n, 0);
    for i in 1..=n {
        let pos = ip[i - 1];
        while ord.last().map_or(false, |p| ask[*p].1 == i) {
            let k = ord.pop().unwrap();
            let (x, _) = ask[k];
            if bit.sum(pos) >= x {
                ans[k] = pos + 1;
            } else {
                ans[k] = bit.search(x);
            }
        }
        bit.add(pos + 1, 1);
    }
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    for a in ans {
        writeln!(out, "{}", a).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 ----------
// 1-indexedなBIT
// 座標xへの加算、[1,x]の和, ([1,x]の和)>=s となる最小のxの探索
// ---------- begin fenwick tree ----------
pub struct Fenwick<T> {
    zero: T,
    a: Box<[T]>,
}

impl<T> Fenwick<T>
where
    T: Copy + std::ops::Add<Output = T>,
{
    pub fn new(size: usize, zero: T) -> Fenwick<T> {
        Fenwick {
            zero: zero,
            a: vec![zero; size + 1].into_boxed_slice(),
        }
    }
    pub fn init(&mut self) {
        for a in self.a.iter_mut() {
            *a = self.zero;
        }
    }
    pub fn add(&mut self, mut x: usize, v: T) {
        assert!(x > 0);
        while let Some(a) = self.a.get_mut(x) {
            *a = *a + v;
            x += x & (!x + 1);
        }
    }
    pub fn sum(&self, mut x: usize) -> T {
        assert!(x < self.a.len());
        let mut res = self.zero;
        while x > 0 {
            res = res + self.a[x];
            x -= x & (!x + 1);
        }
        res
    }
}

impl<T> Fenwick<T>
where
    T: Copy + std::ops::Add<Output = T> + PartialOrd,
{
    pub fn search(&self, s: T) -> usize {
        debug_assert!(self.sum(self.a.len() - 1) >= s);
        let mut k = 1;
        while 2 * k < self.a.len() {
            k *= 2;
        }
        let mut x = 0;
        let mut w = self.zero;
        while k > 0 {
            self.a.get(x + k).map(|a| {
                if w + *a < s {
                    w = w + *a;
                    x += k;
                }
            });
            k >>= 1;
        }
        x + 1
    }
}
// ---------- end fenwick tree ----------

0