首页 > 其他分享 >Solution -「模拟赛」草莓蛋糕

Solution -「模拟赛」草莓蛋糕

时间:2023-09-28 11:13:01浏览次数:44  
标签:body int 草莓 mn Solution pos dat 蛋糕 define

  \(\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";
    }
}

标签:body,int,草莓,mn,Solution,pos,dat,蛋糕,define
From: https://www.cnblogs.com/orchid-any/p/17735235.html

相关文章

  • M-SOLUTIONS Programming Contest
    A-SumofInteriorAngles答案为\(180(n-2)\)。#include<iostream>#include<cstdio>usingnamespacestd;intn;intmain(){ scanf("%d",&n); printf("%d",180*(n-2)); return0;}B-Sumo判断一下x的个数是否小于等于\(7\)。#......
  • Solution Set - 图上问题
    CF360ELink&Submission.首先显然可以选择的边的权值一定会取端点值。事实上,第一个人经过的边选最小,第一个人不经过的边选最大,这样一定不劣。进一步,如果\(s_1\)到点\(u\)的距离小于等于\(s_2\),则\((u,v)\)这条边应该取最小值。所以可以初始全部当作最大值,不断选择一条边修......
  • IPQ9554|Why Is the Wi-Fi 7 Solution So Irresistible?
    Intoday'sfast-paceddigitalworld,wirelessconnectivityplaysapivotalroleinourlives.Thedemandforfasterspeeds,enhancedcapacity,andlowerlatencyhasdrivencontinuousinnovationinthefieldofwirelesstechnology.EnterWi-Fi7,also......
  • the solution of Mining Your Own Business
    thedescriptionofproblem(我看的是PDF里面的原题所以这里描述会和题目不一样,但是大意一致)给定一个未必连通的无向图,问最少在几个点设置出口,可以保证任意一个点坍塌后,工人们仍然可以从出口逃生,同时问设置最少出口的方案数量。thoughts&solution我们可以知道每个连通块之......
  • solution-at-abc321-c
    题意将所有每位满足递减的整数排序,问第\(k\)大的是多少,不包括\(0\)。思路我们发现最大的满足要求的整数是\(9876543210\),只有\(1e10\)的大小,\(k\)只有不到\(3000\)的大小,可以从小到大枚举所有的数,从T1粘来判断函数打一个表就解决了。打表程序#include<iostream......
  • Ubuntu20.04 ping Temporary failure in name resolution问题
    解决步骤vi/etc/systemd/resolved.conf将DNS的注释取消掉并改成8.8.8.8即可参考:https://blog.csdn.net/weixin_43354181/article/details/105352203......
  • Solution -「HDU」Ridiculous Netizens
    Desc.  给定一棵\(N\)个节点无根树,找出满足以下条件的集合\(S\)的数量:\(S\subseteq\{1,\dots,n\}\);\(S\)的导出子图联通;\(\displaystyle\prod_{v\inS}a_v\leqslantM\)。Sol.  点分治,统计包括当前分治中心的集合数量,如果从子树的角度入手会发现并不好做—......
  • Exam DP-300: Administering Microsoft Azure SQL Solutions 微软Azure SQL Solutions
    作为该考试的考生,您应具备构建数据库解决方案方面的主题专业知识,这些解决方案旨在支持使用数据库构建的多种工作负载:企业内部SQLServerAzureSQL服务您是一名数据库管理员,负责管理使用SQLServer和AzureSQL服务构建的内部部署和云数据库。作为Azure数据库管理员,您......
  • AS-2020?TSN CoreSolution直接拿下!
    协议简介  IEEE802.1AS是一个网络时间同步协议,它是IEEE802.1工作组的一部分,主要用于支持时间敏感的应用在桥接网络中的时间同步。802.1AS协议专门为了满足TSN网络中设备的时间同步需求而设计。TSN是一种网络技术,它可以提供精确的时间同步和低延迟,从而保障音视频、传感器、......
  • IPQ6010 IPQ6018 WiFi6 2X2 QSDK OpenWiFi Cloud AP AC Customizable Solution
    IPQ6010IPQ6018WiFi62X2QSDKOpenWiFiCloudAPACCustomizableSolutionWiFi6,alsoknownas802.11ax,representsthelatestgenerationofwirelessnetworkingtechnology.Itbuildsuponthefoundationestablishedbyitspredecessor,WiFi5(802.11ac),an......