当前位置:网站首页>洛谷P1110 报表统计 multiset stl好题
洛谷P1110 报表统计 multiset stl好题
2022-08-09 07:01:00 【swust_fang】
题目链接
一开始自己想的是multiset维护顺序结构,然后查找所有的值差最小时候找加入的当前点的前驱以及后继,然后查找相邻两个的就用线段树维护一个差最小值~,但是后来发现是全局的,直接再开一个multiset维护相邻段之差就行了,但是set的话要删除,也挺费时间的,不是所有点相邻都往set里边装,比如在某一个段加一个点的时候他前一个点的差值直接用一个ans来储存最小,然后输出的时候差的set里边最小值与ans比较一下就行。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<set>
#include<stack>
#include<vector>
#include<map>
#include<queue>
#include<time.h>
#define myself i,l,r
#define lson i<<1
#define rson i<<1|1
#define Lson i<<1,l,mid
#define Rson i<<1|1,mid+1,r
#define half (l+r)/2
#define inff 0x3f3f3f3f
#define lowbit(x) x&(-x)
#define PI 3.14159265358979323846
#define min4(a, b, c, d) min(min(a,b),min(c,d))
#define min3(x, y, z) min(min(x,y),min(y,z))
#define pii make_pair
#define pr pair<int,int>
#define li __int128
void print(li x) {if(x>9) print(x/10);putchar(x%10+48);}
const int dir[4][2] = {0, -1, -1, 0, 0, 1, 1, 0};
typedef long long ll;
typedef unsigned long long ull;
const ll inFF = 9223372036854775807;
using namespace std;
const int maxn=1e6+5;
int st[maxn],ed[maxn];
multiset<int> s,ss;
char str[20];
int main()
{
int n,m,x,val;
cin>>n>>m;
int ans=inff,anss=inff;
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
st[i]=ed[i]=x;
}
for(int i=1;i<=n;i++)
{
if(i<n) ss.insert(abs(st[i]-st[i+1]));
auto it=s.lower_bound(st[i]);
if(it!=s.end()) ans=min(ans,*it-st[i]);
if(it!=s.begin()) it--,ans=min(ans,st[i]-*it);
s.insert(st[i]);
}
for(int i=1;i<=m;i++)
{
scanf("%s",str);
if(strcmp(str,"INSERT")==0)
{
scanf("%d %d",&x,&val);
auto it=s.lower_bound(val);
if(it!=s.end()) ans=min(ans,*it-val);
if(it!=s.begin()) it--,ans=min(ans,val-*it);
s.insert(val);
anss=min(anss,abs(ed[x]-val));
if(x!=n)
{
it=ss.lower_bound(abs(st[x+1]-ed[x]));
ss.erase(it);
ss.insert(abs(st[x+1]-val));
}
ed[x]=val;
}
else if(strcmp(str,"MIN_SORT_GAP")==0) printf("%d\n",ans);
else printf("%d\n",min(anss,*ss.begin()));
}
return 0;
}
边栏推荐
- vlucas/phpdotenv phpdotenv获取变量内容偶尔出现返回false
- 当酷雷曼VR直播遇上视频号,会摩擦出怎样的火花?
- 【报错】Root Cause com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
- 我入职阿里后,才知道原来简历这么写
- 查看日志常用命令
- 【sqlite3】sqlite3.OperationalError: table addresses has 7 columns but 6 values were supplied
- 细谈VR全景:数字营销时代的宠儿
- 【Oracle 11g】Redhat 6.5 安装 Oracle11g
- 顺序表删除所有值为e的元素
- SAP ALV 数据导出被截断的bug
猜你喜欢
leetcode 之 70 爬楼梯问题 (斐波那契数)
2022-08-08: Given an array arr, it represents the height of the missiles that will appear in order from morning to night.When the cannon shoots missiles, once the cannon is set to shoot at a certain h
RK3568商显版开源鸿蒙板卡产品解决方案
无重复的字符的最长子串
Tkinter可以选择的颜色
【Oracle 11g】Redhat 6.5 安装 Oracle11g
虚拟机网卡报错:Bringing up interface eth0: Error: No suitable device found: no device found for connection
排序第一节——插入排序(直接插入排序+希尔排序)(视频讲解26分钟)
XILINX K7 FPGA+RK3399 PCIE驱动调试
leetcode 之盛水问题
随机推荐
多米诺骨牌
如何 认识与学习BASH
找不到和chrome浏览器版本不同的chromedriver的解决方法
【sqlite3】sqlite3.OperationalError: table addresses has 7 columns but 6 values were supplied
C语言的内置宏(定义日志宏)
bzoj 5333 [Sdoi2018]荣誉称号
分布式id 生成器实现
学习小笔记---机器学习
stm32定时器之简单封装
顺序表删除所有值为e的元素
Mysql实操
排序第四节——归并排序(附有自己的视频讲解)
找出数组中不重复的值php
The Integer thread safe
分布式事务产生的原因
AD picture PCB tutorial 20 minutes clear label shop operation process, copper network
Flask failed to create database without error
P6 ali machine test of 2020 Fibonacci number
详解C语言中的wait()函数和waitpid()函数
力扣第 305 场周赛复盘