题目传送门

思路

这题其实非常的简单,完全到不了 \(\mathcal *2100\)。

发现这个题目描述有点诈骗,但是翻译的挺不错,实质上问题就是给你 \(n\) 个点,让你动态维护相邻两个点的差值,最后答案即为 \(\max-\min-\) 最大差值。

于是我们可以二分套动态开点权值线段树或者直接 \(\mathcal multiset\) 瞎搞。

为了简单,可以使用 \(\mathcal multiset\),但是注意插入和删除都需要分类讨论,思路非常简单,可能码量有一点点长。

代码

#include<bits/stdc++.h>
using namespace std;
#define int long long
int const N=1e5+10;
int p[N];
multiset<int>s,S;
inline void cr(int x){
if (!s.size()){s.insert(x);return;}
if (s.lower_bound(x)==s.end()){
int pl=*(--s.lower_bound(x));
S.insert(x-pl);
}else if (s.lower_bound(x)==s.begin()){
int pl=*s.begin();
S.insert(pl-x);
}else{
int pl1=*(--s.lower_bound(x));
int pl2=*(s.lower_bound(x));
S.erase(S.lower_bound(pl2-pl1));
S.insert(x-pl1);S.insert(pl2-x);
}
s.insert(x);
}
inline void del(int x){
s.erase(s.lower_bound(x));
if (!s.size()) return;
if (s.lower_bound(x)==s.end()){
int pl=*(--s.lower_bound(x));
S.erase(S.lower_bound(x-pl));
}else if (s.lower_bound(x)==s.begin()){
int pl=*s.begin();
S.erase(S.lower_bound(pl-x));
}else{
int pl1=*(--s.lower_bound(x));
int pl2=*(s.lower_bound(x));
S.insert(pl2-pl1);
S.erase(S.lower_bound(x-pl1));
S.erase(S.lower_bound(pl2-x));
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int n,q;cin>>n>>q;
for (int i=1;i<=n;++i) cin>>p[i],s.insert(p[i]);
sort(p+1,p+n+1);
for (int i=2;i<=n;++i) S.insert(p[i]-p[i-1]);
if (s.size()<=1) cout<<"0\n";
else cout<<(*(--s.end()))-(*s.begin())-(*(--S.end()))<<'\n';
while (q--){
int opt,x;cin>>opt>>x;
if (opt==1) cr(x);
else del(x);
if (s.size()<=1) cout<<"0\n";
else cout<<(*(--s.end()))-(*s.begin())-(*(--S.end()))<<'\n';
}
return 0;
}

应该非常好理解,插入和删除的分讨是一样的,还有输出答案需要判 \(\mathcal set\) 中是否还有值。

CF1418D Trash Problem的更多相关文章

  1. CodeForces 1418D Trash Problem

    题意 数轴上有 \(n\) 个点,每一次你可以将所有位置在 \(x\) 的点移动到 \(x-1\) 或者是移动到 \(x+1\),花费为 \(1\). 有 \(q\) 次操作,每一次会在数轴上添加一个 ...

  2. CodeForces 题目乱做

    是个补题记录. 1419 除了 F 场上都过了. CF1419A Digit Game 这题好多人 FST 啊-- 考虑如果串长为奇数那么最后操作的肯定是第一个人,串长为偶数的最后操作的肯定是第二个, ...

  3. URAL 1076 Trash Trash(最大权匹配)

    Trash Time limit: 1.0 secondMemory limit: 64 MB You were just hired as CEO of the local junkyard.One ...

  4. 1199 Problem B: 大小关系

    求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...

  5. No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.

    Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...

  6. C - NP-Hard Problem(二分图判定-染色法)

    C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144 ...

  7. Time Consume Problem

    I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...

  8. Programming Contest Problem Types

        Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...

  9. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  10. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

随机推荐

  1. JS笔记合集之对象

    对象 对象基本使用 对象是JS中的一种复合数据类型,它相当于一个容器,在对象中可以存储各种不同类型的数据 而基本数据类型(原始值)只能存储一些简单的数据,如: 语法: 原始创建对象: let obj ...

  2. 爬虫之xpath插件下载与安装

    目录 简介: 下载xpath文件 打开chrome浏览器 点击右上角小圆点 更多工具.阔展程序 拖拽xpath插件放到阔展程序 如果失效,再次拖拽 关闭浏览器重新打开 按ctrl+shift+x 出现 ...

  3. 【Shell案例】【while循环、正则表达式、sed、awk、grep】5、打印空行的行号

    描述写一个 bash脚本以输出一个文本文件 nowcoder.txt中空行的行号,可能连续,从1开始 示例:假设 nowcoder.txt 内容如下:ab c d e f 你的脚本应当输出:35791 ...

  4. 【每日一题】【暴力、动态规划、动规优化、贪心】2022年1月21日-NC19 连续子数组的最大和/最大子序和

    同:最大子序和 https://www.cnblogs.com/liujinhui/p/15574312.html 描述输入一个长度为n的整型数组array,数组中的一个或连续多个整数组成一个子数组. ...

  5. 有来实验室|第一篇:Seata1.5.2版本部署和开源全栈商城订单支付业务实战

    在线体验:Seata实验室 一. 前言 相信 youlai-mall 的实验室大家有曾在项目中见到过,但应该都还处于陌生的阶段,毕竟在此之前实验室多是以概念般的形式存在,所以我想借着此次的机会,对其进 ...

  6. 你真的了解 RSA 加密算法吗?

    作者:小傅哥 博客:https://bugstack.cn 源码:https://github.com/fuzhengwei/java-algorithms 沉淀.分享.成长,让自己和他人都能有所收获 ...

  7. Java开发学习(四十八)----MyBatisPlus删除语句之逻辑删除

    1.逻辑删除 接下来要讲解是删除中比较重要的一个操作,逻辑删除,先来分析下问题: 这是一个员工和其所签的合同表,关系是一个员工可以签多个合同,是一个一(员工)对多(合同)的表 员工ID为1的张业绩,总 ...

  8. Day37:正则表达式详解

    正则表达式 1.1 概述 正则表达式可以用一些规定的字符来制定规则,并用来校验数据格式的合法性. 比如我们在网站上输入用户账号,要求我们输入的账号信息要符合账号的格式,而校验我们输入的账号格式是否正确 ...

  9. 侦察工具——Httrack

    前言 web渗透学习笔记,实验环境为Metasploitable靶机上的DVWA.此随笔介绍Web渗透侦察工具Httrack Httrack 简介 Httrack能够克隆拷贝目标网站上的所有可访问.可 ...

  10. netkit-telnet源码编译安装

    介绍 Linux 下流行的 telnet 实现有两个: GNU inetutils: http://ftp.gnu.org/gnu/inetutils/ 哈佛netkit-telnet 源码包:htt ...