[Blog] [Docs] [Code] [Slides] [About]

CodeForces 1326E - Bombs

线段树

2020-03-21 21:40 CST

Problem

题目链接:CodeForces 1326E

不会做,看题解也要看半天。

简单的说就是要不断减小答案(答案是非严格单调递减的),直到所有位置上(含这个位置)右边比答案大的数不超过右边炸弹的个数。

利用区间更新线段树维护比答案大的数的数量和炸弹数量的差值,只要线段树最大值不超过0(所有值均不为正,上述不满足条件)就要继续降低答案。

忘了怎么写区间更新线段树了,丢人。

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>