Submission #411398

#TimeUsernameProblemLanguageResultExecution timeMemory
411398usachevd0Seats (IOI18_seats)C++14
25 / 100
1259 ms71504 KiB
#include <bits/stdc++.h>
#ifndef LOCAL
    #include "seats.h"
#endif

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define Time (clock() * 1.0 / CLOCKS_PER_SEC)
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using ld = long double;
template<typename T1, typename T2> bool chkmin(T1& x, T2 y) {
    return y < x ? (x = y, true) : false;
}
template<typename T1, typename T2> bool chkmax(T1& x, T2 y) {
    return y > x ? (x = y, true) : false;
}
void debug_out() {
    cerr << endl;
}
template<typename T1, typename... T2> void debug_out(T1 A, T2... B) {
    cerr << ' ' << A;
    debug_out(B...);
}
template<typename T> void mdebug_out(T* a, int n) {
    for (int i = 0; i < n; ++i)
        cerr << a[i] << ' ';
    cerr << endl;
}
#ifdef LOCAL
    #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
    #define mdebug(a, n) cerr << #a << ": ", mdebug_out(a, n)
#else
    #define debug(...) 1337
    #define mdebug(a, n) 1337
#endif
template<typename T> ostream& operator << (ostream& stream, const vector<T>& v) {
    for (auto& x : v)
        stream << x << ' ';
    return stream;
}
template<typename T1, typename T2> ostream& operator << (ostream& stream, const pair<T1, T2>& p) {
    return stream << p.first << ' ' << p.second;
}


const int INF32 = 1e9;
const int logS = 20;
const int sqrtS = 1e3 + 3;
const int maxS = 1e6 + 6;
int N, M, S;
int L[2];
int X[maxS], Y[maxS], P[maxS][2];

int line[2][sqrtS][sqrtS];
int minHere[2][sqrtS];
int minPref[2][sqrtS], minSuff[2][sqrtS];

struct maxSGT {
    static const int logN = 10;
    static const int N = 1 << logN;
    int n;
    int mx[2 * N];
    
    void rebuild(int* a, int _n) {
        n = _n;
        memset(mx, 255, sizeof mx);
        for (int i = 0; i < n; ++i)
            mx[i + N] = a[i];
        for (int i = N - 1; i >= 1; --i)
            mx[i] = max(mx[i << 1], mx[i << 1 | 1]);
    }
    
    int rmq(int l, int r) {
        int ans = -1;
        for (l += N, r += N; l <= r; l >>= 1, r >>= 1) {
            if (l & 1)  chkmax(ans, mx[l++]);
            if (~r & 1) chkmax(ans, mx[r--]);
        }
        return ans;
    }
} MX[2][sqrtS];

void give_initial_chart(int _N, int _M, vector<int> _X, vector<int> _Y) {
    N = _N, M = _M;
    S = N * M;
    L[0] = N, L[1] = M;
    for (int i = 0; i < S; ++i) {
        X[i] = P[i][0] = _X[i];
        Y[i] = P[i][1] = _Y[i];
        for (int t = 0; t < 2; ++t) {
            line[t][P[i][t]][P[i][t ^ 1]] = i;
        }
    }
    for (int t = 0; t < 2; ++t)
        for (int i = 0; i < L[t]; ++i) {
            minHere[t][i] = *min_element(line[t][i], line[t][i] + L[t ^ 1]);
            MX[t][i].rebuild(line[t][i], L[t ^ 1]);
        }
}

void rebuildMinPrefSuff() {
    for (int t = 0; t < 2; ++t) {
        minPref[t][0] = +INF32;
        for (int i = 0; i < L[t]; ++i)
            minPref[t][i + 1] = min(minPref[t][i], minHere[t][i]);
        minSuff[t][L[t]] = +INF32;
        for (int i = L[t] - 1; i >= 0; --i)
            minSuff[t][i] = min(minSuff[t][i + 1], minHere[t][i]);
    }
}

int swap_seats(int a, int b) {
    // swap(grid[X[a]][Y[a]], grid[X[b]][Y[b]]);
    swap(X[a], X[b]);
    swap(Y[a], Y[b]);
    for (int t = 0; t < 2; ++t)
        swap(P[a][t], P[b][t]);
    for (int i : {a, b})
        for (int t = 0; t < 2; ++t) {
            int x = P[i][t];
            line[t][x][P[i][t ^ 1]] = i;
            minHere[t][x] = *min_element(line[t][x], line[t][x] + L[t ^ 1]);
            MX[t][x].rebuild(line[t][x], L[t ^ 1]);
        }
    rebuildMinPrefSuff();
    
    int ans = 1;
    int C[2][2] = {{X[0], X[0]}, {Y[0], Y[0]}}; // {{minX, maxX}, {minY, maxY}}
    int mx = 0;
    while (true) {
        int mn = INF32;
        for (int t = 0; t < 2; ++t) {
            chkmin(mn, minPref[t][C[t][0]]);
            chkmin(mn, minSuff[t][C[t][1] + 1]);
        }
        if (mn == INF32) break;
        // debug(mn);
        
        for (int t = 0; t < 2; ++t) {
            while (C[t][0] > P[mn][t]) {
                chkmax(mx, MX[t][--C[t][0]].rmq(C[t ^ 1][0], C[t ^ 1][1]));
            }
            while (C[t][1] < P[mn][t]) {
                chkmax(mx, MX[t][++C[t][1]].rmq(C[t ^ 1][0], C[t ^ 1][1]));
            }
        }
        // debug(C[0][0], C[0][1], C[1][0], C[1][1], mx);
        if (mx + 1 == (C[0][1] - C[0][0] + 1) * (C[1][1] - C[1][0] + 1))
            ++ans;
    }
    return ans;
}

#ifdef LOCAL

int read_int() {
  int x;
  if (scanf("%d", &x) != 1) {
    fprintf(stderr, "Error while reading input\n");
    exit(1);
  }
  return x;
}

int32_t main() {
#ifdef LOCAL
    freopen("in", "r", stdin);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    
int H = read_int();
  int W = read_int();
  int Q = read_int();
  std::vector<int> R(H * W), C(H * W);
  for (int i = 0; i < H * W; ++i) {
    R[i] = read_int();
    C[i] = read_int();
  }
  std::vector<int> A(Q), B(Q);
  for (int j = 0; j < Q; ++j) {
    A[j] = read_int();
    B[j] = read_int();
  }

  give_initial_chart(H, W, R, C);
  for (int j = 0; j < Q; ++j) {
    int answer = swap_seats(A[j], B[j]);
    printf("%d\n", answer);
  }

    return 0;
}
#endif
#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...