题意:
维护一个数列,支持加数,查询相邻最小差,全局最小差。
题解:
STL练习题。
我STL太菜了。
思路大概想了出来,然而不会写。
对于每一个原位置,记录第一个数与最后一个数,然后可以瞎维护。
orz各种用法,orz hzwer,终于敲(chao)了一份代码。
重复情况可以用map解决。
15s题16s过2333
code:
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<cmath>
#include<map>
#include<algorithm>
using namespace std;
const int inf=1000000000;
map<int,int> mp;
set<int> a,b;//a:相邻,b:全局
int st[500010],ed[500010];
priority_queue<int,vector<int>,greater<int> > q;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void add_a(int x)
{
mp[x]++;
if(mp[x]==1) a.insert(x);
}
void add_b(int x)
{
int l=*(--b.lower_bound(x)),r=*b.lower_bound(x);
q.push(min(x-l,r-x));
b.insert(x);
}
int main()
{
int n=read(),m=read();
b.insert(inf);b.insert(-inf);
for(int i=1;i<=n;i++)
{
int x=read();
st[i]=ed[i]=x;
add_b(x);
}
for(int i=2;i<=n;i++) add_a(abs(st[i]-st[i-1]));
char s[15];
while(m--)
{
scanf("%s",s+1);
if(s[1]=='I')
{
int p=read(),x=read();
if(p!=n)
{
int tmp=abs(ed[p]-st[p+1]);
mp[tmp]--;if(!mp[tmp]) a.erase(tmp);
}
add_a(abs(ed[p]-x));
add_a(abs(x-st[p+1]));
ed[p]=x;add_b(x);
}
else if(s[5]=='S') printf("%d\n",q.top());
else printf("%d\n",*a.begin());
}
}