线段树求调
查看原帖
线段树求调
612044
wangyishan楼主2023/11/28 20:32

rt,只能过subtask#1 似乎是退房和买房的 updata 函数有问题。

#include <bits/stdc++.h>
using namespace std;
int n, m;
struct tr {
  int lmax, rmax;
  int mx, lazy, len;  // 1 ->装满  2->清空
} a[50050 << 3];
#define ls (p << 1)
#define rs ((p << 1) | 1)
//#define DEBUG
void pushup(int p) {
  a[p].lmax = (a[ls].mx == a[ls].len) ? a[ls].mx + a[rs].lmax : a[ls].lmax;
  a[p].rmax = (a[rs].mx == a[rs].len) ? a[rs].mx + a[ls].rmax : a[rs].rmax;
  a[p].mx = max(max(a[ls].mx, a[rs].mx), a[ls].rmax + a[rs].lmax);
}
void pushdown(int p) {
  if (a[p].lazy) {
    a[ls].lmax = a[ls].rmax = a[ls].mx = a[p].lazy == 1 ? 0 : a[ls].len;
    a[rs].lmax = a[rs].rmax = a[rs].mx = a[p].lazy == 1 ? 0 : a[rs].len;
    a[ls].lazy = a[rs].lazy = a[p].lazy;
  }
  a[p].lazy = 0;
}
void build(int p, int l, int r) {
  if (l == r) {
    a[p].lmax = a[p].rmax = a[p].mx = r - l + 1;
    a[p].lazy = 0;
    return;
  }
  a[p].lmax = a[p].rmax = a[p].mx = r - l + 1;
  a[p].lazy = 0;
  int mid = (l + r) >> 1;
  build(ls, l, mid);
  build(rs, mid + 1, r);
}
int query(int p, int fx, int l, int r) {
    #ifdef DEBUG
  cout << "query(" << p << "," << fx << "," << l << "," << r << ")" << endl;
#endif
  if (l == r) return l;
  pushdown(p);
  // if(a[p].mx<fx)return 0;
  int mid = (l + r) >> 1;
  if (a[ls].mx >= fx) return query(ls, fx, l, mid);
  if (a[ls].rmax + a[rs].lmax >= fx) return mid - a[ls].rmax +1;
  if (a[rs].mx >= fx) return query(rs, fx, mid + 1, r);
  return 0;
}
void updata(int type, int p, int l, int r, int L, int R) {
    if (l > R || r < L) return;
#ifdef DEBUG
  cout << "updata(" << type << "," << p << "," << l << "," << r << "," << L
       << "," << R << ")" << endl;
#endif
  pushdown(p);
  if (L <= l && r <= R) {
    a[p].lazy = type;
    a[p].mx = a[p].lmax = a[p].rmax = (type == 1) ? 0 : a[p].len;
    return;
  }
  int mid = (l + r) >> 1;
  if(L<=mid)updata(type, ls, l, mid, L, R);
  if(mid<R)updata(type, rs, mid + 1, r, L, R);
  pushup(p);
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  build(1, 1, n);
  while (m--) {
    int op, x, y;
    cin >> op >> x;
    if (op == 1) {
      int t = query(1, x, 1, n);
      cout << t << endl;
      if (t != 0) updata(1, 1, 1, n, t, t + x - 1);
    }
    if (op == 2) {
      cin >> y;
      updata(2, 1, 1, n, x, x + y - 1);
    }
  }
  return 0;
}


2023/11/28 20:32
加载中...