# bzoj 1058: [ZJOI2007]报表统计

### Description

小Q的妈妈是一个出纳，经常需要做一些统计报表的工作。今天是妈妈的生日，小Q希望可以帮妈妈分担一些工

### solution

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <cmath>
#include <set>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const int N=1000005,inf=2e9;
int n,m,nxt[N],cnt=0,pre[N],val[N],last[N],b[N];
char S[41];
struct node{
int x,y,val;
bool operator <(const node &pr)const{return val>pr.val;}
};
priority_queue<node>q;
multiset<int>G;
multiset<int>::iter it;
void work()
{
int x,y,z,ans=inf;node k;
scanf("%d%d",&n,&m);cnt=n;
for(int i=1;i<=n;i++){
scanf("%d",&val[i]);b[i]=val[i];
last[i]=i;pre[i]=i-1;nxt[i]=i+1;
if(i!=1)q.push((node){i-1,i,abs(val[i]-val[i-1])});
G.insert(val[i]);
}
sort(b+1,b+n+1);
for(int i=2;i<=n;i++)ans=min(ans,b[i]-b[i-1]);
G.insert(-inf);G.insert(inf);
while(m--){
scanf("%s",S);
if(S[0]=='I'){
scanf("%d%d",&x,&z);
y=last[x];
cnt++;val[cnt]=z;
if(x<n)pre[x+1]=cnt,nxt[cnt]=x+1;
last[x]=cnt;nxt[y]=cnt;pre[cnt]=y;

q.push((node){y,cnt,abs(val[y]-z)});
if(x<n)q.push((node){cnt,x+1,abs(val[x+1]-z)});

it=G.lower_bound(z);
if(*it!=-inf){
ans=min(ans,abs(*it-z));
--it;
if(*it!=-inf)ans=min(ans,abs(*it-z));
}
it=G.upper_bound(z);
if(*it!=inf)ans=min(ans,abs(*it-z));
G.insert(z);
}
else if(S[4]=='G'){
while(!q.empty()){
k=q.top();
if(nxt[k.x]!=k.y)q.pop();
else {printf("%d\n",k.val);break;}
}
}
else printf("%d\n",ans);
}
}

int main()
{
freopen("pp.in","r",stdin);
freopen("pp.out","w",stdout);
work();
return 0;
}


posted @ 2017-12-14 22:51  PIPIBoss  阅读(102)  评论(0编辑  收藏  举报