首页 > 代码库 > CodeForces 46DParking Lot线段树

CodeForces 46DParking Lot线段树

 

 

#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;const int maxn = 111111;int sum[maxn << 2]; int lsum[maxn << 2], rsum[maxn << 2], color[maxn << 2];int q[maxn], q1[maxn];#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1void 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 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 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){color[rt] = add;        sum[rt] = lsum[rt] = rsum[rt] = color[rt] ? 0 : r - l + 1;         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;    int mid = (l + r) >> 1;    down(rt, r - l + 1);    if (sum[rt << 1] >= key) return ask(key, lson);    if (rsum[rt << 1] + lsum[rt << 1 | 1] >= key) return mid - rsum[rt << 1] + 1;    return ask(key, rson);}int main(){    int n, a, b, c, d, m;    scanf("%d%d%d", &n, &c, &d);    build(1, c + n + d, 1);    scanf("%d", &m);    for (int i = 1; i <= m; i++){        scanf("%d%d", &a, &b);        if (a == 1){            if (c + b + d > sum[1]){                printf("-1\n"); continue;            }            int t = ask(c + b + d, 1, c + n + d, 1);            printf("%d\n",t - 1);            update(c + t, c + t + b - 1, 1, 1, c + n + d, 1);            q[i] = t; q1[i] = b;        }        else{            update(c + q[b], c + q[b] + q1[b] - 1, 0, 1, c + n + d, 1);        }    }    return 0;}