线段树+链表 警钟
查看原帖
线段树+链表 警钟
994729
JOKER_chu楼主2024/11/21 16:38
#include <bits/stdc++.h>

#define endl '\n'

#define ls(x) x << 1
#define rs(x) x << 1 | 1

using namespace std;

const int N = 5e5 + 5;

struct Block {
  int l, r, col, fr, ba;
} b[N];

int n, q, w[N], sum, t[N << 3];

void push_down( int x ) {
  if (t[x]) {
    t[ls(x)] = t[rs(x)] = t[x];
    t[x] = 0;
  }
}

void update( int l, int r, int nl, int nr, int p, int w ) {
  if (nl > nr || l > nr || r < nl) return ;
  push_down(p);
  if (nl >= l && nr <= r) {
    t[p] = w;
    return ;
  }
  int mid = nl + nr >> 1; 
  update(l, r, nl, mid, ls(p), w);
  update(l, r, mid + 1, nr, rs(p), w);
}

int query( int nl, int nr, int p, int pos ) {
  if (nl > nr || nl > pos || nr < pos) return 0;
  if (nl == nr) return t[p];
  push_down(p);
  int mid = nl + nr >> 1;
  int sum = query(nl, mid, ls(p), pos);
  sum = max(sum, query(mid + 1, nr, rs(p), pos));
  return sum;
}

int main() {
  ios :: sync_with_stdio(0), cin.tie(0);
	cin >> n >> q;
  for (int i = 1; i <= n; ++i) {
    w[i] = 1, b[++sum] = {i, i, i, i - 1, i + 1};
    update(i, i, 1, n, 1, i);
  }
  for (int _ = 1; _ <= q; ++_) {
    int op, x, c; cin >> op >> x;
    if (op == 1) {
      cin >> c;
      int pos = query(1, n, 1, x);
      w[c] += (b[pos].col != c) * (b[pos].r - b[pos].l + 1);
      w[b[pos].col] -= (b[pos].col != c) * (b[pos].r - b[pos].l + 1);
      b[pos].col = c;
      if (b[pos].fr && b[b[pos].fr].col == c) {
      	b[b[pos].ba].fr = b[pos].fr;
        b[b[pos].fr].r = b[pos].r, b[b[pos].fr].ba = b[pos].ba;
        pos = b[pos].fr;
      }
      if (b[pos].ba <= n && b[b[pos].ba].col == c) {
      	b[b[pos].fr].ba = b[pos].ba;
        b[b[pos].ba].l = b[pos].l, b[b[pos].ba].fr = b[pos].fr;
        pos = b[pos].ba;
      }
      update(b[pos].l, b[pos].r, 1, n, 1, pos);
    } else {
      cout << w[x] << endl;
    }
  }
	return 0;
}

这样是对的,而

#include <bits/stdc++.h>

#define endl '\n'

#define ls(x) x << 1
#define rs(x) x << 1 | 1

using namespace std;

const int N = 5e5 + 5;

struct Block {
  int l, r, col, fr, ba;
} b[N];

int n, q, w[N], sum, t[N << 2];

void push_down( int x ) {
  if (t[x]) {
    t[ls(x)] = t[rs(x)] = t[x];
    t[x] = 0;
  }
}

void update( int l, int r, int nl, int nr, int p, int w ) {
  if (nl > nr || l > nr || r < nl) return ;
  push_down(p);
  if (nl >= l && nr <= r) {
    t[p] = w;
    return ;
  }
  int mid = nl + nr >> 1; 
  update(l, r, nl, mid, ls(p), w);
  update(l, r, mid + 1, nr, rs(p), w);
}

int query( int nl, int nr, int p, int pos ) {
  if (nl > nr || nl > pos || nr < pos) return 0;
  if (nl == nr) return t[p];
  push_down(p);
  int mid = nl + nr >> 1;
  int sum = query(nl, mid, ls(p), pos);
  sum = max(sum, query(mid + 1, nr, rs(p), pos));
  return sum;
}

int main() {
  ios :: sync_with_stdio(0), cin.tie(0);
	cin >> n >> q;
  for (int i = 1; i <= n; ++i) {
    w[i] = 1, b[++sum] = {i, i, i, i - 1, i + 1};
    update(i, i, 1, n, 1, i);
  }
  for (int _ = 1; _ <= q; ++_) {
    int op, x, c; cin >> op >> x;
    if (op == 1) {
      cin >> c;
      int pos = query(1, n, 1, x);
      w[c] += (b[pos].col != c) * (b[pos].r - b[pos].l + 1);
      w[b[pos].col] -= (b[pos].col != c) * (b[pos].r - b[pos].l + 1);
      b[pos].col = c;
      if (b[pos].fr && b[b[pos].fr].col == c) {
      	b[b[pos].ba].fr = b[pos].fr;
        b[b[pos].fr].r = b[pos].r, b[b[pos].fr].ba = b[pos].ba;
        pos = b[pos].fr;
      }
      if (b[pos].ba <= n && b[b[pos].ba].col == c) {
      	b[b[pos].fr].ba = b[pos].ba;
        b[b[pos].ba].l = b[pos].l, b[b[pos].ba].fr = b[pos].fr;
        pos = b[pos].ba;
      }
      update(b[pos].l, b[pos].r, 1, n, 1, pos);
    } else {
      cout << w[x] << endl;
    }
  }
	return 0;
}

这样是错的

2024/11/21 16:38
加载中...