CodeForces 1326E - Bombs

2020-04-10 19:27 CST

Solution

#include <iostream>
#include <cstring>
using namespace std;

template <class T>
class Tree {
private:
T *lazy, *data;
void pushup(int id) {
data[id] = max(data[id << 1], data[id << 1 | 1]);
}
void pushdown(int id) {
if (lazy[id] != 0) {
lazy[id << 1] += lazy[id];
lazy[id << 1 | 1] += lazy[id];
data[id << 1] += lazy[id];
data[id << 1 | 1] += lazy[id];
lazy[id] = 0;
}
}
public:
explicit Tree(int len) {
lazy = new T[len << 2];
data = new T[len << 2];
memset(lazy, 0, sizeof(T) * (len << 2));
memset(data, 0, sizeof(T) * (len << 2));
}
void build(int id, int l, int r) {
lazy[id] = 0;
if (l == r) {
data[id] = 0;
} else {
int m = (l + r) >> 1;
build(id << 1, l, m);
build(id << 1 | 1, m + 1, r);
pushup(id);
}
}
void update(int id, int l, int r, int ql, int qr, T val) {
if (ql <= l and r <= qr) {
lazy[id] += val;
data[id] += val;
} else {
pushdown(id);
int m = (l + r) >> 1;
if (ql <= m) update(id << 1, l, m, ql, qr, val);
if (m + 1 <= qr) update(id << 1 | 1, m + 1, r, ql, qr, val);
pushup(id);
}
}
T query(int id, int l, int r, int ql, int qr) {
if (ql <= l and r <= qr) {
return data[id];
} else {
pushdown(id);
int m = (l + r) >> 1;
T ret = -0x3f3f3f3f;
if (ql <= m) ret = max(ret, query(id << 1, l, m, ql, qr));
if (m + 1 <= qr) ret = max(ret, query(id << 1 | 1, m + 1, r, ql, qr));
return ret;
}
}
};

int n = 0, temp = 0;
int p[300005] = {};
int q[300005] = {};

int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &temp);
p[temp] = i;
}
for (int i = 1; i <= n; ++i) scanf("%d", q+i);

int ans = n;
Tree<int> t(n + 1);
t.build(1, 1, n);
t.update(1, 1, n, 1, p[n], 1);
printf("%d", ans);
for (int i = 1; i <= n - 1; ++i) {
t.update(1, 1, n, 1, q[i], -1);
while (t.query(1, 1, n, 1, n) <= 0) {
--ans;
t.update(1, 1, n, 1, p[ans], 1);
}
printf(" %d", ans);
}
return 0;
}

<EOF>