当前位置:网站首页>洛谷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;
}
边栏推荐
猜你喜欢
报错:FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS重大开销和将disab补充道
Variable used in lambda expression should be final or effectively final报错解决方案
什么是分布式事务
移远EC20 4G模块拨号相关
stm32定时器之简单封装
Use baidu EasyDL intelligent bin
【nuxt】服务器部署步骤
INSTALL_RPATH and BUILD_RPATH problem in CMake
Inception V3 Eye Closure Detection
Error jinja2.exceptions.UndefinedError: 'form' is undefined
随机推荐
P7阿里面试题2020.07 之滑动窗算法(阿里云面试)
Error: flask: TypeError: 'function' object is not iterable
2022.8.8DAY628
TCP段重组PDU
半导体新能源智能装备整机软件系统方案设计
虚拟机网卡报错:Bringing up interface eth0: Error: No suitable device found: no device found for connection
Explain the wait() function and waitpid() function in C language in detail
leetcode 之盛水问题
P7 Alibaba Interview Questions 2020.07 Sliding Window Algorithm (Alibaba Cloud Interview)
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
先序遍历,中序遍历,后序遍历,层序遍历
Rsync常见错误
灵活好用的sql monitoring 脚本 part7
用tensorflow.keras模块化搭建神经网络模型
高项 03 项目立项管理
composer 内存不足够
cut命令的使用实例
longest substring without repeating characters
MongDb的查询方式
VS2019 common shortcut keys