首页 > 代码库 > 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;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。