题目描述

Vova decided to clean his room. The room can be represented as the coordinate axis OX. There are n piles of trash in the room, coordinate of the i-th pile is the integer pi. All piles have different coordinates.
Let’s define a total cleanup as the following process. The goal of this process is to collect all the piles in no more than two different x coordinates. To achieve this goal, Vova can do several (possibly, zero) moves. During one move, he can choose some x and move all piles from x to x+1 or x−1 using his broom. Note that he can’t choose how many piles he will move.
Also, there are two types of queries:
0 x — remove a pile of trash from the coordinate x. It is guaranteed that there is a pile in the coordinate x at this moment.
1 x — add a pile of trash to the coordinate x. It is guaranteed that there is no pile in the coordinate x at this moment.
Note that it is possible that there are zero piles of trash in the room at some moment.
Vova wants to know the minimum number of moves he can spend if he wants to do a total cleanup before any queries. He also wants to know this number of moves after applying each query. Queries are applied in the given order. Note that the total cleanup doesn’t actually happen and doesn’t change the state of piles. It is only used to calculate the number of moves.
For better understanding, please read the Notes section below to see an explanation for the first example.

Input

The first line of the input contains two integers n and q (1≤n,q≤105) — the number of piles in the room before all queries and the number of queries, respectively.
The second line of the input contains n distinct integers p1,p2,…,pn (1≤pi≤109), where pi is the coordinate of the i-th pile.
The next q lines describe queries. The i-th query is described with two integers ti and xi (0≤ti≤1;1≤xi≤109), where ti is 0 if you need to remove a pile from the coordinate xi and is 1 if you need to add a pile to the coordinate xi. It is guaranteed that for ti=0 there is such pile in the current set of piles and for ti=1 there is no such pile in the current set of piles.

Output

Print q+1 integers: the minimum number of moves Vova needs to do a total cleanup before the first query and after each of q queries.

Examples

input
5 6
1 2 6 8 10
1 4
1 9
0 6
0 10
1 100
1 50
output
5
7
7
5
4
8
49
input
5 8
5 1 2 4 3
0 1
0 2
0 3
0 4
0 5
1 1000000000
1 1
1 500000000
output
3
2
1
0
0
0
0
0
499999999

Note

Consider the first example.
Initially, the set of piles is [1,2,6,8,10]. The answer before the first query is 5 because you can move all piles from 1 to 2 with one move, all piles from 10 to 8 with 2 moves and all piles from 6 to 8 with 2 moves.
After the first query, the set becomes [1,2,4,6,8,10]. Then the answer is 7 because you can move all piles from 6 to 4 with 2 moves, all piles from 4 to 2 with 2 moves, all piles from 2 to 1 with 1 move and all piles from 10 to 8 with 2 moves.
After the second query, the set of piles becomes [1,2,4,6,8,9,10] and the answer is the same (and the previous sequence of moves can be applied to the current set of piles).
After the third query, the set of piles becomes [1,2,4,8,9,10] and the answer is 5 because you can move all piles from 1 to 2 with 1 move, all piles from 2 to 4 with 2 moves, all piles from 10 to 9 with 1 move and all piles from 9 to 8 with 1 move.
After the fourth query, the set becomes [1,2,4,8,9] and the answer is almost the same (the previous sequence of moves can be applied without moving piles from 10).
After the fifth query, the set becomes [1,2,4,8,9,100]. You can move all piles from 1 and further to 9 and keep 100 at its place. So the answer is 8.
After the sixth query, the set becomes [1,2,4,8,9,50,100]. The answer is 49 and can be obtained with almost the same sequence of moves as after the previous query. The only difference is that you need to move all piles from 50 to 9 too.

题目大意

有n堆垃圾,每一堆的坐标为x,合并两堆垃圾的花费为这两堆垃圾的坐标之差的绝对值。
你可以进行q次操作。1 x表示在x位置上增加一堆垃圾,0 x表示删除x位置上的垃圾。
输出将这n堆垃圾合并成两堆垃圾所需的花费,以及每次操作之后改变的答案。

题目分析

我们可以发现,每次合并垃圾之后,所有相邻的垃圾的坐标差去掉最大值之后的和即为此次合并的最小花费。
但因为有q次操作,因此不能直接用差分来完成。我们可以用权值线段树来做。
先将所有需要用的数进行一个离散化,再用这个离散化之后的数组建树。
因为最开始的时候,不是所有数都加入到了序列中,因此有一些数最开始是没有的。我们可以用一个标记数组st[i],st[i]=true才表示第i个数在当前的序列中。
线段树中我们需要维护的是:相邻的两个有效数的差的最大值max。u段的max等于 两个子段中的max 和 右子段的左端点值(最大值)-左子段的右端点值(最小值)的最大值。因此我们还需要维护两个额外信息:这一段中的最大值ma和最小值mi。
剩下的就是直接套线段树模板即可了。
注意:在pushup操作中要注意不要让st[i]=false的点的信息去更新父段信息。

代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <iomanip>
#define LL long long
#define PII pair<int,int>
using namespace std;
const int N=2e5+5,INF=0x3f3f3f3f;
struct Node{int mi,ma;  //每一段的最小值、最大值int max;   //这一段中相邻两个数的差的最大值
}tr[N*4];
int a[N],b[N];
bool st[N];     //标记每个点十分在当前序列中
int op[N],q[N]; //记录q次操作的询问(离线算法)
void pushup(int u)          //注意不要让st[i]=0的点去跟新信息
{tr[u].ma=max(tr[u<<1].ma,tr[u<<1|1].ma);if(tr[u<<1].mi==0||tr[u<<1|1].mi==0) tr[u].mi=max(tr[u<<1].mi,tr[u<<1|1].mi);else tr[u].mi=min(tr[u<<1].mi,tr[u<<1|1].mi);if(tr[u<<1].ma==0||tr[u<<1|1].mi==0) tr[u].max=max(tr[u<<1].max,tr[u<<1|1].max);else tr[u].max=max(max(tr[u<<1].max,tr[u<<1|1].max),tr[u<<1|1].mi-tr[u<<1].ma);
}
void build(int u,int l,int r)
{if(l==r){if(st[l]) tr[u]={a[l],a[l],0};     //因为此段只有一个点,因此max=0else tr[u]={0,0,0};                 //st[i]=0的点统一赋成 0 0 0}else{int mid=l+r>>1;build(u<<1,l,mid),build(u<<1|1,mid+1,r);pushup(u);}
}
void modify(int u,int l,int r,int x,bool op)
{if(l==r){if(op) tr[u]={a[l],a[l],0};        //op=1,加入一个数else tr[u]={0,0,0};                //op=0,删除一个数}else{int mid=l+r>>1;if(x<=mid) modify(u<<1,l,mid,x,op);else modify(u<<1|1,mid+1,r,x,op);pushup(u); }
}
int query()     //最小合并费用即为整段的相邻数的差(最大值-最小值)-相邻数差的最大值max
{return tr[1].ma-tr[1].mi-tr[1].max;
}
int main()
{int n,Q;scanf("%d %d",&n,&Q);for(int i=1;i<=n;i++){scanf("%d",&a[i]);b[i]=a[i];                //b[]备份初始的n个数}for(int i=1;i<=Q;i++)      //将所有需要用到的坐标放入a[]中{scanf("%d %d",&op[i],&q[i]);a[i+n]=q[i];}sort(b+1,b+1+n);sort(a+1,a+1+n+Q);                   //离散化int cnt=unique(a+1,a+1+n+Q)-a-1;for(int i=1;i<=Q;i++)q[i]=lower_bound(a+1,a+1+cnt,q[i])-a;int k=1;for(int i=1;i<=n;i++)        //将初始的n个数进行标记{while(b[i]!=a[k]) k++;st[k]=true;}build(1,1,cnt);             //建树printf("%d\n",query());       //查询for(int i=1;i<=Q;i++){modify(1,1,cnt,q[i],op[i]);printf("%d\n",query());}return 0;
}

Educational Codeforces Round 95 (Rated for Div. 2)D. Trash Problem(权值线段树+离散化)相关推荐

  1. Educational Codeforces Round 95 (Rated for Div. 2)

    昨天本来想打一下,但是今天早上课很早,就没有打,只是看了看前三个题写了个代码,今天中午课结束交了一下都AC了.y1s1 A题第一次写就出来了,但是答案一直不对,最后结果是样例错了-.- A - Buy ...

  2. Educational Codeforces Round 112 (Rated for Div. 2)-A. PizzaForces-题解

    目录 Educational Codeforces Round 112 (Rated for Div. 2)-A. PizzaForces Problem Description Input Outp ...

  3. Educational Codeforces Round 90 (Rated for Div. 2)(A, B, C, D, E)

    Educational Codeforces Round 90 (Rated for Div. 2) Donut Shops 思路 分三种情况: a==c/ba == c / ba==c/b这个时候两 ...

  4. Educational Codeforces Round 114 (Rated for Div. 2) (A ~ F)全题解

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Educational Codeforces Round 114 (Rated for Div. 2) ...

  5. Educational Codeforces Round 106 (Rated for Div. 2)(A ~ E)题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 Educational Codeforces Round 106 (Rated for Div. ...

  6. Educational Codeforces Round 37 (Rated for Div. 2) 1

    Educational Codeforces Round 37 (Rated for Div. 2) A.Water The Garden 题意:Max想给花园浇水.花园可被视为长度为n的花园床,花园 ...

  7. Educational Codeforces Round 89 (Rated for Div. 2)(A, B, C, D)

    Educational Codeforces Round 89 (Rated for Div. 2) A. Shovels and Swords 思路 题意非常简单,就是得到最多的物品嘛,我们假定a, ...

  8. Educational Codeforces Round 114 (Rated for Div. 2) D. The Strongest Build 暴力 + bfs

    传送门 文章目录 题意: 思路: 题意: 你有nnn个装备槽,每个槽里面有cic_ici​个力量加成,对于每个槽只能选一个力量加成,现在给你mmm个力量组合[b1,b2,...,bn][b_1,b_2 ...

  9. Educational Codeforces Round 72 (Rated for Div. 2) D. Coloring Edges dfs树/拓扑找环

    传送门 文章目录 题意: 思路: 题意: 给你一张图,你需要给这个图的边染色,保证如果有环那么这个环内边的颜色不全相同,输出染色方案和用的颜色个数. n,m≤5e3n,m\le5e3n,m≤5e3 思 ...

最新文章

  1. 【北大-阿里巴巴】深度哈希方法综述,23页pdf,A Survey on Deep Hashing Methods
  2. 数论 - SGU 105 DIV3
  3. 用yacc编写的算术运算计算器_详细的mac计算器操作技巧+快捷键分享
  4. Scala 闭包详解
  5. markdown绘图插件----mermaid简介
  6. 作者:钱卫宁(1976-),男,华东师范大学计算机科学与软件工程学院教授、博士生导师。...
  7. 线性代数 —— 线性基与前缀线性基
  8. postgreSQL除法保留小数
  9. WebSocket 解决javascript跨域问题一剂良药
  10. 设计模式学习每天一个——Decorator模式
  11. open cv+C++错误及经验总结(十)
  12. 数据中心机房建设标准规范
  13. IOS开发之——AFN-文件下载(03)
  14. python移动文件到新的文件夹并重命名
  15. nextjs中阿里icon库的引入使用
  16. 《卓有成效的管理者》读书笔记(二)——卓有成效是可以学会的
  17. android系统相机的使用、及解决拍照闪退的问题
  18. 聊聊新加坡的工作和生活
  19. VIEWGOOD(远古)直播时移技术——让高清网络电视“随心看”
  20. vant 验证手机号_手机维修英汉小辞典(V字头1:VA-VB)

热门文章

  1. Class-Aware Generative Adversarial Transformers for Medical Image Segmentation-用于医学图像分割的生成对抗网络
  2. 数字信号处理学习(二):振动与信号
  3. 氨基/请加/羧基/生物素功能化水溶性CdSeQds,CdTeQds,CdSe/ZnSQds,ZnOQds,CuInS/ZnSQds量子点
  4. Wault Finance闪贷攻击溯源
  5. 基于深度学习的2D图像目标检测
  6. 50套高大上的后台管理系统模板,总有一套适合你
  7. (十九)git版本管理软件——搭建git服务器
  8. 社保查询或者转移的一些事
  9. 准入控制_Kubernetes动态准入控制示例
  10. 在Ubuntu16.04 中安装g2o并运行g2o_viewer