Submission #607269

#TimeUsernameProblemLanguageResultExecution timeMemory
607269piOOESeats (IOI18_seats)C++17
0 / 100
4062 ms109444 KiB
#include "seats.h"
#include <bits/stdc++.h>

using namespace std;

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

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

void update2(int mn[4], int cnt[4], int x, int cntx) {
  if (cntx == 0) {
    return;
  }
  for (int t = 0; t < 4; ++t) {
    if (x < mn[t]) {
      if (t < 3) {
        rotate(mn + t, mn + 3, mn + 4);
        rotate(cnt + t, cnt + 3, cnt + 4);
      }
      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 < 4; ++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 (v == 0) {
    return;
  }
  tag[x] += v;
  for (int i = 0; i < 4; ++i) {
    t[x].mn[i] += v;
  }
}

void push(int x) {
  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 (l <= lx && rx <= r) {
    return t[x];
  }
  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;
}

void update(int i, int val) {
  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;
  }
  for (int j = 0; j < 4; ++j) {
    modify(i, min(time[j], time[(j + 1) % 4]), val);
  }
}

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;
  }
  while (sz < S) sz <<= 1;
  for (int i = 0; i < S; ++i) {
    t[i + sz].mn[0] = 0;
    t[i + sz].cnt[0] = 1;
  }
  for (int i = sz - 1; i > 0; --i) {
    pull(i);
  }
  for (int i = 0; i < S; ++i) {
    update(i, 1);
  }
}

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);
  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...