[ZJOI2007]报表统计

题链

我们发现这是一道模板STL题。

边听歌边debug效率好低。

#include<bits/stdc++.h>
#define N 570007
using namespace std;
multiset<int> S1,S2;
char ch[18];
#define INF ((int)1e9)
#define sight(c) ('0'<=c&&c<='9')
inline void read(int &x){
    static char c;static int b;
    for(c=getchar(),b=1;!sight(c);c=getchar()) if (c=='-') b=-1;
    for(x=0;sight(c);c=getchar())x=x*10+c-48; x*=b;
}
void write(int x){if (x<10){ putchar(48+x);return;} write(x/10),putchar(48+x%10);}
inline void writeln(int x){if (x<0) putchar('-'),x*=-1; write(x); putchar('\n');}
int a[N],ed[N],ans,n,m,id,x;
multiset<int>::iterator it,itt;
void ins(int x){
    if (!ans) return;
    it=S1.upper_bound(x);
    if (it!=S1.end()) ans=min(ans,abs(x-*it));
    if (it!=S1.begin()) ans=min(ans,abs(*(--it)-x));
    S1.insert(x);
}
int main () {
//    freopen("a.in","r",stdin);
//    freopen("a.out","w",stdout);
    read(n); read(m); ans=1e9;
    S1.insert(-INF),S1.insert(INF);
    read(a[1]),S1.insert(a[1]);ed[1]=a[1];
    for (int i=2;i<=n;i++) 
      read(a[i]),ins(a[i]),ed[i]=a[i],S2.insert(abs(a[i]-a[i-1]));
    while (m--) {
        scanf("%s",ch);
        switch (ch[4]) {
            case 'R': read(id);read(x);ins(x);
            if (id!=n) { 
             S2.erase(S2.find(abs(ed[id]-a[id+1])));
             S2.insert(abs(x-a[id+1]));}
            S2.insert(abs(x-ed[id])); ed[id]=x;
            break;
            case 'G':
             writeln(*S2.begin());
             break;
            case 'S': 
             writeln(ans);
             break;
        }
    }
    
}

 

posted @ 2018-01-24 13:47  泪寒之雪  阅读(208)  评论(0编辑  收藏  举报