제출 #607331

#제출 시각아이디문제언어결과실행 시간메모리
607331piOOE자리 배치 (IOI18_seats)C++17
0 / 100
499 ms81464 KiB
#include "seats.h"
#include <bits/stdc++.h>

using namespace std;

constexpr int Alp = 1;

struct node {
  int mn[Alp], cnt[Alp];

  node() {
    memset(cnt, 0, sizeof(cnt));
    memset(mn, 0x3f, sizeof(mn));
  }
};

void update2(int mn[Alp], int cnt[Alp], int x, int cntx) {
  if (cntx == 0) {
    return;
  }
  if (x < 0) {
    while (1) {

    }
    cout << x << " " << cntx << endl;
    exit(1);
  }
  if (mn[0] < 0) {
    while (1) {

    }
  }
  assert(mn[0] >= 0);
  for (int t = 0; t < Alp; ++t) {
    if (x < mn[t]) {
      if (t < Alp - 1) {
        rotate(mn + t, mn + Alp - 1, mn + Alp);
        rotate(cnt + t, cnt + Alp - 1, cnt + Alp);
      }
      mn[t] = x, cnt[t] = cntx;
      break;
    } else if (x == mn[t]) {
      cnt[t] += cntx;
      break;
    }
  }
}

node operator+(node a, node b) {
  node res = {};
  memset(res.mn, 0x3f, sizeof(res.mn));
  for (int t = 0; t < Alp; ++t) {
    update2(res.mn, res.cnt, a.mn[t], a.cnt[t]);
    update2(res.mn, res.cnt, b.mn[t], b.cnt[t]);
  }
  return res;
}

constexpr int N = 1 << 20, dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1}; //Up, Right, Down, Left

int tag[N << 1], sz = 1;
node t[N << 1];

void apply(int x, int v) {
  if (x >= N * 2) {
    while (1) {

    }
  }
  tag[x] += v;
  for (int i = 0; i < Alp; ++i) {
    t[x].mn[i] += v;
  }
}

void push(int x) {
  if (x >= N * 2) {
    while (1) {
      
    }
  }
  if (tag[x] == 0) {
    return;
  }
  apply(x << 1, tag[x]);
  apply(x << 1 | 1, tag[x]);
  tag[x] = 0;
}

void pull(int x) {
  t[x] = t[x << 1] + t[x << 1 | 1];
}

void modify(int l, int r, int val, int x = 1, int lx = 0, int rx = sz) {
  if (l >= rx || lx >= r) {
    return;
  }
  if (l <= lx && rx <= r) {
    apply(x, val);
    return;
  }
  int mid = (lx + rx) >> 1;
  push(x);
  modify(l, r, val, x << 1, lx, mid), modify(l, r, val, x << 1 | 1, mid, rx);
  pull(x);
}

node get(int l, int r, int x = 1, int lx = 0, int rx = sz) {
  if (l >= rx || lx >= r || l >= r) {
    return {};
  }
  if (x >= N * 2) {
    while (1) {

    }
  }
  if (l <= lx && rx <= r) {
    return t[x];
  }
  if (x >= N) {
    while (1) {

    }
  }
  int mid = (lx + rx) >> 1;
  push(x);
  return get(l, r, x << 1, lx, mid) + get(l, r, x << 1 | 1, mid, rx);
}


int n, m, S;
vector<int> R, C;
vector<vector<int>> p;

bool inside(int a, int b) {
  return a >= 0 && a < n && b >= 0 && b < m;
}

int prefix[N];

void update(int i, int val, bool O1 = false) {
  int x = R[i], y = C[i];
  int nx[4], ny[4], time[4];
  for (int j = 0; j < 4; ++j) {
    nx[j] = x + dx[j], ny[j] = y + dy[j];
    time[j] = inside(nx[j], ny[j]) ? p[nx[j]][ny[j]] : sz;
  }
  //URDL
//  cout << "id: " << i << endl;
  for (int j = 0; j < 4; ++j) {
    int r = min(time[j], time[(j + 1) % 4]);
//    cout << i << " " << min(time[j], time[(j + 1) % 4]) << endl;
    if (!O1) {
      if (i < r)
        modify(i, r, val);
    } else {
      prefix[i] += 1;
      prefix[max(i, r)] -= 1;
    }
  }
}

void give_initial_chart(int H, int W, vector<int> _R, vector<int> _C) {
  swap(R, _R), swap(C, _C);
  n = H, m = W;
  S = n * m;
  p.assign(n, vector<int>(m));
  for (int i = 0; i < S; ++i) {
    p[R[i]][C[i]] = i;
  }
  sz = 1;
  while (sz < S) sz <<= 1;
  if (sz > N) {
    while (1) {

    }
  }
  for (int i = 0; i < S; ++i) {
    update(i, 1, true);
  }
  for (int i = 0; i < S; ++i) {
    if (i > 0) {
      prefix[i] += prefix[i - 1];
    }
//    cout << prefix[i] << " ";
    t[i + sz].mn[0] = prefix[i];
    t[i + sz].cnt[0] = 1;
  }
//  cout << endl;
  memset(tag, 0, sizeof(int) * sz * 2);
  for (int i = sz - 1; i > 0; --i) {
    pull(i);
  }
//  auto ans = get(0, S);
//  cout << ans.mn[0] << endl;
//  assert(ans.mn[0] == 4);
//  cout << get(0, S).cnt[0] << endl;
}

int swap_seats(int a, int b) {
  if (a > b) {
    swap(a, b);
  }
  vector<int> yy = {a, b};
  for (int _ = 0; _ < 2; ++_) {
    for (int i = 0; i < 4; ++i) {
      int nx = R[a] + dx[i], ny = C[a] + dy[i];
      if (inside(nx, ny)) {
        yy.push_back(p[nx][ny]);
      }
    }
    swap(a, b);
  }
  sort(yy.begin(), yy.end());
  yy.resize(unique(yy.begin(), yy.end()) - yy.begin());
  for (int i: yy) {
    update(i, -1);
  }
  swap(R[a], R[b]), swap(C[a], C[b]);
  p[R[a]][C[a]] = a, p[R[b]][C[b]] = b;
  for (int i: yy) {
    update(i, 1);
  }
  node ans = get(0, S);
  if (ans.mn[0] != 4) {
    while (1) {

    }
  }
  assert(ans.mn[0] == 4);
  return ans.cnt[0];
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...