Submission #447684

#TimeUsernameProblemLanguageResultExecution timeMemory
447684MilosMilutinovicSeats (IOI18_seats)C++14
0 / 100
314 ms53904 KiB
#include "seats.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long unsigned long ull;
typedef double long ld;

int h, w, r[1000005], c[1000005];

pair<int, int> pref_mx[1000005], pref_mn[1000005];
int small_change = 0;

void give_initial_chart(int H, int W, vector<int> R, vector<int> C) {
  h = H; w = W;
  for (int i = 0; i < h * w; i++) r[i] = R[i];
  for (int i = 0; i < h * w; i++) c[i] = C[i];

  pref_mx[0] = pref_mn[0] = {r[0], c[0]};
  for (int i = 1; i < h * w; i++) {
    pref_mx[i].first = max(pref_mx[i - 1].first, r[i]);
    pref_mx[i].second = max(pref_mx[i - 1].second, c[i]);
    pref_mn[i].first = min(pref_mn[i - 1].first, r[i]);
    pref_mn[i].second = min(pref_mn[i - 1].second, c[i]);
  }
  for (int i = 0; i < h * w; i++) {
    if ((pref_mx[i].first - pref_mn[i].first + 1) * (pref_mx[i].second - pref_mn[i].second + 1) == i + 1) {
      small_change++;
    }
  }
}

int swap_seats(int a, int b) {
  if (h * w <= 10000) {
    int ans = 0;
    pair<int, int> mn, mx;
    mx = mn = {r[0], c[0]};
    for (int i = 0; i < h * w; i++) {
      mx.first = max(mx.first, r[i]);
      mx.second = max(mx.second, c[i]);
      mn.first = min(mn.first, r[i]);
      mn.second = min(mn.second, c[i]);
      if ((mx.first - mn.first + 1) * (mx.second - mn.second + 1) == i + 1) {
        ans++;
     }
    }
    return ans;
  }
  if (abs(a - b) <= 10000) {
    if (a > b) swap(a, b);
    for (int i = a; i <= b; i++) {
      if ((pref_mx[i].first - pref_mn[i].first + 1) * (pref_mx[i].second - pref_mn[i].second + 1) == i + 1) {
        small_change--;
      }
    }
    swap(r[a], r[b]);
    swap(c[a], c[b]);
    for (int i = a; i <= b; i++) {
      if (i == 0) pref_mx[i] = pref_mn[i] = {r[i], c[i]};
      else {
        pref_mx[i].first = max(pref_mx[i - 1].first, r[i]);
        pref_mx[i].second = max(pref_mx[i - 1].second, c[i]);
        pref_mn[i].first = min(pref_mn[i - 1].first, r[i]);
        pref_mn[i].second = min(pref_mn[i - 1].second, c[i]);
      }
    }
    for (int i = a; i <= b; i++) {
      if ((pref_mx[i].first - pref_mn[i].first + 1) * (pref_mx[i].second - pref_mn[i].second + 1) == i + 1) {
        small_change++;
      }
    }
  }
  return 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...