# bzoj1058 [ZJOI2007]报表统计

【题解】

# include <set>
# include <queue>
# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 5e5 + 10;
const int mod = 1e9+7;

# define RG register
# define ST static

int n, q, a[M];
int beg[M], end[M];

multiset<int> num;
multiset<int> st2;
priority_queue<int, vector<int>, greater<int> > st1;

int x = 0, f = 1;
char ch = getchar();
while(!isdigit(ch)) {
if(ch == '-') f = -1;
ch = getchar();
}
while(isdigit(ch)) {
x = (x<<3)+(x<<1)+ch-'0';
ch = getchar();
}
return x*f;
}

inline void del(int x) {
st2.erase(st2.find(x));
}

multiset<int>::iterator it;

inline void push(int x) {
int ins = 1e9;
it = num.lower_bound(x);
if(it != num.end()) {
int y = *it;
ins = min(ins, y-x);
}
if(it != num.begin()) {
int y = *--it;
ins = min(ins, x-y);
}
st1.push(ins);
num.insert(x);
}
int main() {
cin >> n >> q;
for (int i=1, t; i<=n; ++i) {
//        printf("i = %d\n", i);
beg[i] = end[i] = t;
push(t);
if(i >= 2) st2.insert(abs(t - beg[i-1]));
}
char st[23];
int ps, d;
while(q--) {
scanf("%s", st);
if(st[0] == 'I') {
push(d);
if(ps != n) {
del(abs(end[ps] - beg[ps+1]));
st2.insert(abs(d - end[ps]));
st2.insert(abs(d - beg[ps+1]));
} else st2.insert(abs(d - end[ps]));
end[ps] = d;
} else if(st[4] == 'S') printf("%d\n", st1.top());
else printf("%d\n", *st2.begin());
}
return 0;
}
View Code

posted @ 2017-05-25 18:03  Galaxies  阅读(148)  评论(0编辑  收藏  举报