結果

問題 No.1290 Addition and Subtraction Operation
ユーザー koba-e964koba-e964
提出日時 2021-11-18 23:55:19
言語 Rust
(1.77.0)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 2,874 bytes
コンパイル時間 2,839 ms
コンパイル使用メモリ 151,340 KB
実行使用メモリ 6,440 KB
最終ジャッジ日時 2023-08-27 18:44:33
合計ジャッジ時間 7,896 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,384 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 22 ms
4,376 KB
testcase_43 AC 22 ms
4,376 KB
testcase_44 AC 22 ms
4,380 KB
testcase_45 AC 22 ms
4,380 KB
testcase_46 AC 22 ms
4,380 KB
testcase_47 AC 22 ms
4,376 KB
testcase_48 AC 22 ms
4,376 KB
testcase_49 AC 22 ms
4,380 KB
testcase_50 AC 22 ms
4,380 KB
testcase_51 AC 22 ms
4,380 KB
testcase_52 AC 22 ms
4,380 KB
testcase_53 AC 22 ms
4,376 KB
testcase_54 AC 22 ms
4,376 KB
testcase_55 AC 22 ms
4,376 KB
testcase_56 AC 22 ms
4,380 KB
testcase_57 AC 20 ms
5,656 KB
testcase_58 AC 25 ms
5,644 KB
testcase_59 AC 19 ms
5,696 KB
testcase_60 AC 42 ms
6,400 KB
testcase_61 AC 34 ms
6,140 KB
testcase_62 AC 37 ms
6,128 KB
testcase_63 AC 25 ms
5,688 KB
testcase_64 AC 14 ms
5,868 KB
testcase_65 AC 17 ms
5,704 KB
testcase_66 AC 30 ms
5,800 KB
testcase_67 AC 43 ms
6,396 KB
testcase_68 AC 35 ms
6,168 KB
testcase_69 AC 25 ms
5,656 KB
testcase_70 AC 23 ms
5,696 KB
testcase_71 AC 28 ms
5,908 KB
testcase_72 AC 18 ms
5,580 KB
testcase_73 AC 32 ms
5,844 KB
testcase_74 AC 13 ms
5,900 KB
testcase_75 AC 15 ms
5,628 KB
testcase_76 AC 24 ms
5,664 KB
testcase_77 AC 46 ms
6,156 KB
testcase_78 AC 42 ms
5,880 KB
testcase_79 AC 32 ms
5,580 KB
testcase_80 AC 51 ms
6,440 KB
testcase_81 AC 50 ms
6,436 KB
testcase_82 AC 37 ms
5,892 KB
testcase_83 AC 51 ms
6,408 KB
testcase_84 AC 47 ms
6,152 KB
testcase_85 AC 27 ms
5,624 KB
testcase_86 AC 23 ms
5,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 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, usize1) => (read_value!($next, usize) - 1);
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

/**
 * Union-Find tree.
 * Verified by https://atcoder.jp/contests/pakencamp-2019-day3/submissions/9253305
 */
struct UnionFind { disj: Vec<usize>, rank: Vec<usize> }

impl UnionFind {
    fn new(n: usize) -> Self {
        let disj = (0..n).collect();
        UnionFind { disj: disj, rank: vec![1; n] }
    }
    fn root(&mut self, x: usize) -> usize {
        if x != self.disj[x] {
            let par = self.disj[x];
            let r = self.root(par);
            self.disj[x] = r;
        }
        self.disj[x]
    }
    fn unite(&mut self, x: usize, y: usize) {
        let mut x = self.root(x);
        let mut y = self.root(y);
        if x == y { return }
        if self.rank[x] > self.rank[y] {
            std::mem::swap(&mut x, &mut y);
        }
        self.disj[x] = y;
        self.rank[y] += self.rank[x];
    }
    #[allow(unused)]
    fn is_same_set(&mut self, x: usize, y: usize) -> bool {
        self.root(x) == self.root(y)
    }
    #[allow(unused)]
    fn size(&mut self, x: usize) -> usize {
        let x = self.root(x);
        self.rank[x]
    }
}

// https://yukicoder.me/problems/no/1290 (3)
// 奇数番目を -1 倍して imos 法。
fn main() {
    input! {
        n: usize, m: usize,
        b: [i64; n],
        lr: [(usize1, usize); m],
    }
    let mut b = b;
    for i in 0..n {
        if i % 2 == 1 {
            b[i] = -b[i];
        }
    }
    let mut c = vec![0; n + 1];
    c[0] = b[0];
    for i in 1..n {
        c[i] = b[i] - b[i - 1];
    }
    c[n] = -b[n - 1];
    let mut uf = UnionFind::new(n + 1);
    for (l, r) in lr {
        uf.unite(l, r);
    }
    let mut tot = vec![0; n + 1];
    for i in 0..n + 1 {
        tot[uf.root(i)] += c[i];
    }
    println!("{}", if tot.iter().all(|&x| x == 0) { "YES" } else { "NO" });
}
0