bouzuya.hatenablog.com

ぼうずやのにっき

firestore-structured-query 0.3.0 / めんたいこの歌 / ABC218 E

bouzuya/firestore-structured-path 0.3.0 をつくった。

unary filter に対応した。

いまは↓のような感じ。

use firestore_structured_query::{
    FieldPath, FieldPathFilterExt as _, FieldPathOrderExt as _, Filter,
};
use google_api_proto::google::firestore::v1::StructuredQuery;

let _ = StructuredQuery {
    select: None,
    from: vec![],
    r#where: Some(
        Filter::and([
            // field filters
            FieldPath::raw("field1").less_than(&1)?,
            FieldPath::raw("field2").less_than_or_equal(&2)?,
            FieldPath::raw("field3").greater_than(&3)?,
            FieldPath::raw("field4").greater_than_or_equal(&4)?,
            FieldPath::raw("field5").equal(&5)?,
            FieldPath::raw("field6").not_equal(&6)?,
            FieldPath::raw("field7").array_contains(&7)?,
            FieldPath::raw("field8").r#in(&[8])?,
            FieldPath::raw("field9").array_contains_any(&[9])?,
            FieldPath::raw("field10").not_in(&[10])?,
            // unary filters
            FieldPath::raw("field11").is_nan()?,
            FieldPath::raw("field12").is_not_nan()?,
            FieldPath::raw("field13").is_not_null()?,
            FieldPath::raw("field14").is_null()?,
            // composite filters
            Filter::and([
                FieldPath::raw("f").equal(&"a")?,
                FieldPath::raw("f").equal(&"b")?,
            ]),
            Filter::or([
                FieldPath::raw("f").equal(&"a")?,
                FieldPath::raw("f").equal(&"b")?,
            ]),
        ])
        .into(),
    ),
    order_by: vec![
        FieldPath::raw("field1").ascending(),
        FieldPath::raw("field2").descending(),
    ],
    start_at: None,
    end_at: None,
    offset: 0_i32,
    limit: None,
};

ドキュメントコメントは未対応。次は StructuredQuery のビルダーを追加する。


Firestore の Collection Group クエリを使うといろいろな階層における同一 collection_id をまとめて取得できるのだけど、誤爆することがあるので collection_id はなるべく重ならないように設計しておくのが吉。


下の子はめんたいこの歌を歌っている。保育所の友達がめんたいパークに行ってきたらしい。保育所でそこで流れていた音楽を流した結果覚えたのだろう。

上の子は数字の 5 がひっくり返らなくなってきた。今月はずっと 5 や 6 が左右反転していたのだけどおさまってきた。 3 歳ごろの急成長とは違うがじわじわと成長している。


ABC218 : AtCoder Beginner Contest 218

  • E - Destruction https://atcoder.jp/contests/abc218/tasks/abc218_e
    • 提出: https://atcoder.jp/contests/abc218/submissions/49842245
    • 雑にクラスカル法じゃんと思って書いたら 1WA
    • 負の重みの辺は取り除きたくない&非負の重みの辺は取り除きたい
    • つまり重みの大きい辺から取り除く
    • 取り除くのは難しいので、逆に重みの小さい辺だけを残す (連結する) ことを考える
    • クラスカル法っぽく重みの小さい辺から Dsu (Union-Find) を使っていく
    • 求めたいのは取り除いた量なので、事前に重みの総和を求めて、そこから連結に使用した辺を除けばよい
    • WA の原因になったのは、既に連結済みの場合の負の辺の扱いで、最初は連結済みならすべて取り除いていたのだけど、負の辺に関しては連結済みでも残しておけば良かった
use ac_library::Dsu;
use proconio::{input, marker::Usize1};

fn main() {
    input! {
        n: usize,
        m: usize,
        mut abc: [(Usize1, Usize1, i64); m]
    };

    abc.sort_by_key(|&(_, _, c)| c);

    let mut ans = abc.iter().copied().map(|(_, _, c)| c).sum::<i64>();
    let mut dsu = Dsu::new(n);
    for (a, b, c) in abc {
        if dsu.same(a, b) {
            if c < 0 {
                ans -= c;
            }
            continue;
        }
        dsu.merge(a, b);
        ans -= c;
    }

    println!("{}", ans);
}

今日のコミット。