前面的操作和 hotel一样,后面我没用线段树写,roll直接树桩数组各种搞。对于后面两个操作用vector 搞,但是不知道为毛没超时,erase的复杂度是o(n)吧。。
#include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <set> #include <queue> #include <stack> #include<math.h> #include<vector> using namespace std; #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 const int maxn = 55555; int color[maxn << 2], sum[maxn << 2], lsum[maxn << 2], rsum[maxn << 2]; struct Node { int a; int b; }; int a; vector<Node > q; void down(int rt, int m) { if (~color[rt]){ color[rt << 1] = color[rt << 1 | 1] = color[rt]; sum[rt << 1] = lsum[rt << 1] = rsum[rt << 1] = color[rt] ? 0 : (m - (m >> 1)); sum[rt << 1 | 1] = lsum[rt << 1 | 1] = rsum[rt << 1 | 1] = color[rt] ? 0 : (m >> 1); color[rt] = -1; } } void up(int rt, int m) { lsum[rt] = lsum[rt << 1]; rsum[rt] = rsum[rt << 1 | 1]; if (lsum[rt] == (m - (m >> 1))) lsum[rt] += lsum[rt << 1 | 1]; if (rsum[rt] == (m >> 1)) rsum[rt] += rsum[rt << 1]; sum[rt] = max(max(sum[rt << 1], sum[rt << 1 | 1]), rsum[rt << 1] + lsum[rt << 1 | 1]); } void build(int l, int r, int rt) { color[rt] = -1;sum[rt]=lsum[rt]=rsum[rt]=r-l+1; if(l==r) return ; int mid = (l + r) >> 1; build(lson); build(rson); } void update(int L, int R, int add, int l, int r, int rt) { if (L <= l&&r <= R){ sum[rt] = lsum[rt] = rsum[rt] = add ? 0 : (r - l + 1); color[rt] = add; return; } down(rt, r - l + 1); int mid = (l + r) >> 1; if (L <= mid) update(L, R, add, lson); if (R > mid) update(L, R, add, rson); up(rt, r - l + 1); } int ask(int key, int l, int r, int rt) { if (l == r) return l; down(rt, r - l + 1); int mid = (l + r) >> 1; if (key <= sum[rt << 1]) return ask(key, lson); if (rsum[rt << 1] + lsum[rt << 1 | 1] >= key) return mid - rsum[rt << 1] + 1; return ask(key, rson); } int erfen(int x) { int l = 0; int r = q.size() - 1; while (l <= r){ int mid = (l + r) >> 1; if (q[mid].a > x){ r = mid - 1; } else l = mid + 1; } return l; } int main() { int n, m; char str[100]; while (scanf("%d%d",&n,&m)!=EOF){ build(1, n, 1); q.clear(); for (int i = 0; i < m; i++){ scanf("%s", str); if (str[0] == 'R'){ update(1,n,0,1,n,1); q.clear();printf("Reset Now ");continue; } scanf("%d", &a); if (str[0] == 'N'){ if(sum[1]<a){ printf("Reject New ");continue; } int t = ask(a, 1, n, 1); int pos = erfen(t); Node gg; gg.a = t; gg.b = t + a - 1; q.insert(q.begin() + pos, gg); update(t, t + a - 1, 1, 1, n, 1); printf("New at %d ", t); } if (str[0] == 'F'){ int pos = erfen(a)-1; if (pos<=-1||q[pos].b < a){ printf("Reject Free "); continue; } update(q[pos].a, q[pos].b, 0, 1, n, 1); printf("Free from %d to %d ", q[pos].a, q[pos].b); q.erase(q.begin()+pos,q.begin()+pos+1); } if (str[0] == 'G'){ if (q.size() < a){ printf("Reject Get "); continue; } printf("Get at %d ", q[a-1].a); } } printf(" "); } return 0; }