Solution -「模拟赛」草莓蛋糕

solution,模拟,草莓,蛋糕 · 浏览次数 : 5

小编点评

```cpp const int N = 1e6;const int INF = numeric_limits<int>::max();int Q, opt[N + 100], cat[N + 100], a[N + 100], b[N + 100];using MSET = multiset<ll>;struct node { ll mn[4], ans; node() : ans(INF) { for (int i = 0; i < 4; ++i) mn[i] = INF; }};struct SegmentTree {#define NODE int u, int l, int r#define mid (((l) + (r)) / 2)#define ls (u * 2)#define rs (u * 2 + 1)#define LC ls, l, mid#define RC rs, mid + 1, r#define SetAcc(id, i) (*leaves[id][i].begin()) const int __n; vector<node> body; vector<array<MSET, 4>> leaves; void pullup(int u) { body[u].ans = min({body[ls].mn[2] + body[rs].mn[0], body[ls].mn[1] + body[rs].mn[3], body[ls].ans, body[rs].ans}); for (int i = 0; i < 4; ++i) body[u].mn[i] = min(body[ls].mn[i], body[rs].mn[i]); } void upd(int pos, NODE) { if (l == r) { for (int i = 0; i < 4; ++i) body[u].mn[i] = SetAcc(l, i); body[u].ans = min(SetAcc(l, 0) + SetAcc(l, 2), SetAcc(l, 1) + SetAcc(l, 3)); } else mid >= pos ? upd(pos, LC) : upd(pos, RC), pullup(u); }public: void upd(int pos) {upd(pos,1,1,__n);} int Ask() {return body[1].ans;} SegmentTree(int n) : __n(n), body(4 * n + 5), leaves(n + 5) { rep (j, 1, n) for (int i = 0; i < 4; ++i) leaves[j][i].emplace(INF); }};int main(){ ios::sync_with_stdio(0); cin.tie(nullptr); rd(Q); bsi dat; rep (i, 1, Q) { rd(opt[i], cat[i], a[i], b[i]); dat.pb(cat[i] == 0 ? a[i] - b[i] : b[i] - a[i]); } sort(allu(dat)), dat.resize(distance(dat.begin(), unique(allu(dat)))); auto getter = [&](int x) -> int { return distance(dat.begin(), lower_bound(allu(dat), x)) + 1; }; const int dataLength = dat.size(); SegmentTree treeask(dataLength); rep (i, 1, Q) { int pos = getter(cat[i] == 0 ? a[i] - b[i] : b[i] - a[i]); assert(1 <= pos && pos <= dataLength); auto& cur = treeask.leaves[pos]; int idx = 2 * cat[i]; if (opt[i] == 1) cur[idx].emplace(a[i]), cur[idx + 1].emplace(b[i]); else cur[idx].erase(cur[idx].find(a[i])), cur[idx + 1].erase(cur[idx + 1].find(b[i])); treeask.upd(pos); int res = treeask.Ask(); if (res >= INF) cout << \"-1\\"; else cout << res << \"\\"; }} 归纳总结以上内容,生成内容时需要带简单的排版 ```

正文

  \(\max(a_x + a_y, b_y + b_x)\) 的贡献形式不是独立的,并不好进行分析。考虑通过分类讨论将 \(\max\) 拆开。若令 \(h_i = a_i - b_i\)\(h'_i = b_i - a_i\),可以发现若 \(h_x \geqslant h'_y\) 取值则为 \(b_x + b_y\),反之亦然。

  注意到 \(h\) 本身自带一个一维偏序关系,于是放到数轴上用线段树维护,每个节点维护 \(a_x\)\(a_y\)\(b_x\)\(b_y\) 的最小值,以及区间的答案。修改操作在线段树的叶子节点维护四个 std::multiset 即可。

const int N = 1e6;
const int INF = numeric_limits<int>::max();
int Q, opt[N + 100], cat[N + 100], a[N + 100], b[N + 100];
using MSET = multiset<ll>;
struct node {
    ll mn[4], ans;
    node() : ans(INF) {
        for (int i = 0; i < 4; ++i) mn[i] = INF;
    }
};
struct SegmentTree {
#define NODE int u, int l, int r
#define mid (((l) + (r)) / 2)
#define ls (u * 2)
#define rs (u * 2 + 1)
#define LC ls, l, mid
#define RC rs, mid + 1, r
#define SetAcc(id, i) (*leaves[id][i].begin())
    const int __n;
    vector<node> body;
    vector<array<MSET, 4>> leaves;
    void pullup(int u) {
        body[u].ans = min({body[ls].mn[2] + body[rs].mn[0], body[ls].mn[1] + body[rs].mn[3], body[ls].ans, body[rs].ans});
        for (int i = 0; i < 4; ++i) body[u].mn[i] = min(body[ls].mn[i], body[rs].mn[i]);
    }
    void upd(int pos, NODE) {
        if (l == r) {
            for (int i = 0; i < 4; ++i) body[u].mn[i] = SetAcc(l, i);
            body[u].ans = min(SetAcc(l, 0) + SetAcc(l, 2), SetAcc(l, 1) + SetAcc(l, 3));
        } else mid >= pos ? upd(pos, LC) : upd(pos, RC), pullup(u);
    }
public:
    void upd(int pos) {upd(pos,1,1,__n);}
    int Ask() {return body[1].ans;}
    SegmentTree(int n) : __n(n), body(4 * n + 5), leaves(n + 5) {
        rep (j, 1, n) for (int i = 0; i < 4; ++i) leaves[j][i].emplace(INF);
    }
};
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    rd(Q);
    bsi dat;
    rep (i, 1, Q) {
        rd(opt[i], cat[i], a[i], b[i]);
        dat.pb(cat[i] == 0 ? a[i] - b[i] : b[i] - a[i]);
    }
    sort(allu(dat)), dat.resize(distance(dat.begin(), unique(allu(dat))));
    auto getter = [&](int x) -> int {
        return distance(dat.begin(), lower_bound(allu(dat), x)) + 1;
    };
    const int dataLength = dat.size();
    SegmentTree treeask(dataLength);
    rep (i, 1, Q) {
        int pos = getter(cat[i] == 0 ? a[i] - b[i] : b[i] - a[i]);
        assert(1 <= pos && pos <= dataLength);
        auto& cur = treeask.leaves[pos];
        int idx = 2 * cat[i];
        if (opt[i] == 1) cur[idx].emplace(a[i]), cur[idx + 1].emplace(b[i]);
        else cur[idx].erase(cur[idx].find(a[i])), cur[idx + 1].erase(cur[idx + 1].find(b[i]));
        treeask.upd(pos);
        int res = treeask.Ask();
        if (res >= INF) cout << "-1\n";
        else cout << res << "\n";
    }
}

与Solution -「模拟赛」草莓蛋糕相似的内容:

Solution -「模拟赛」草莓蛋糕

\(\max(a_x + a_y, b_y + b_x)\) 的贡献形式不是独立的,并不好进行分析。考虑通过分类讨论将 \(\max\) 拆开。若令 \(h_i = a_i - b_i\),\(h'_i = b_i - a_i\),可以发现若 \(h_x \geqslant h'_y\) 取值则为

Solution -「HDU」Ridiculous Netizens

Desc. 给定一棵 \(N\) 个节点无根树,找出满足以下条件的集合 \(S\) 的数量: \(S \subseteq \{1,\dots,n\}\); \(S\) 的导出子图联通; \(\displaystyle\prod_{v \in S} a_v \leqslant M\)。 Sol. 点分

Solution -「JOISC 2020」建筑装饰 4

朴素的 DP 形式是定义 \(f_{i, j, A/B}\) 表示前 \(i\) 个元素选择了 \(j\) 个 \(A\) 的可达性. \(\mathcal O(n^2)\). 交换状态与值域, 定义 \(f_{i, A/B, A/B}\) 表示前 \(i\) 个元素中的最后一个元素 (即 \(i\

Solution -「ARC 106E」Medals

Desc. Link. 你有 \(n\) 个朋友,他们会来你家玩,第 \(i\) 个人 \(1...A_i\) 天来玩,然后 \(A_i+1...2A_i\) 天不来,然后 \(2A_i+1...3A_i\) 又会来,以此类推; 每天你会选一个来玩的人,给他颁个奖,如果没人来玩,你就不颁奖。 你要给

Solution -「LOJ #3310」丁香之路

首先有两个前置技巧:1) 两点间的最短距离就是直接连接两点的边的长度;2) 遍历一个子图的最小花费是最小生成树的边权之和乘二。原问题让我们找出一条最短且必经过钦定边的 \(( s, i )\) 路径,那么我们先将 \(\lang s , i \rang\) 连上,问题就变成了找出一条最短且必经过钦定

MetaTown:一个可以自己构建数字资产的平台

摘要:华为云Solution as Code重磅推出《基于MetaTown构建数字资产平台》解决方案。 本文分享自华为云社区《基于MetaTown构建数字资产平台》,作者: 阿米托福。 华为云Solution as Code重磅推出《基于MetaTown构建数字资产平台》解决方案,由华为云数字资产链

带你了解基于Ploto构建自动驾驶平台

摘要:华为云Solution as Code推出基于Ploto构建自动驾驶平台解决方案。 本文分享自华为云社区《基于Ploto构建自动驾驶平台》,作者:阿米托福 。 2022年6月15日,主题为“因聚而生 为你所能”的华为伙伴暨开发者大会 2022 正式开启,在自动驾驶专场中,华为云携手合作伙伴联合

pbjs 无法编码 bytes 类型数据问题的解决方案

一段包含 bytes 类型的 protobuf 二进制数据,经过 pbjs 解码生成的 json 文件,再传递给 pbjs 编码后生成的二进制数据和原始数据差异巨大,经过一番探究,发现居然是 pbjs 的一个 bug,快来看看你是否踩过这个坑吧~