题目
题目链接:http://codeforces.com/problemset/problem/46/D
题目来源:某人总结的线段树合集
简要题意:要停一些车子,车子有长度,每个车子至少要距离前后的车子某距离,可以移走车子,求每次停车的位置。
题解
虽然这题是在一个线段树教程里的,不过数据范围是可以更暴力的。
作为一个懒人,我就暴力了一发。
维护俩数组分别是向前最近后向后最近的车子的位置。
每次查找这个数组去找到符合的位置,然后进行更新。
代码
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
// head
const int N = 1e5+5;
int l[N], r[N];
bool a[N];
PII q[N];
void update(int n) {
for (int i = 1; i <= n; i++) {
l[i] = a[i] ? i : l[i-1];
}
for (int i = n; i > 0; i--) {
r[i] = a[i] ? i : r[i+1];
}
}
int query(int n, int x, int b, int f) {
for (int i = 1; i <= n; i++) {
if (i - l[i] > f && r[i] - i >= x + b && x + i - 1 <= n) {
for (int j = 0; j < x; j++) {
a[i+j] = true;
}
return i;
}
}
return 0;
}
int main() {
int len, b, f, n, op, x;
scanf("%d%d%d%d", &len, &f, &b, &n);
l[0] = -1e9;
r[len+1] = 1e9;
update(len);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &op, &x);
if (op == 1) {
int ans = query(len, x, b, f);
q[i] = make_pair(ans, ans + x - 1);
printf("%d\n", ans-1);
} else {
for (int j = q[x].fi; j <= q[x].se; j++) {
a[j] = false;
}
}
update(len);
}
return 0;
}