Submission #799888

#TimeUsernameProblemLanguageResultExecution timeMemory
799888PixelCat자리 배치 (IOI18_seats)C++14
100 / 100
1976 ms104840 KiB
#include "seats.h"

#ifdef NYAOWO
#include "grader.cpp"
#endif

#include <bits/stdc++.h>
#define For(i, a, b) for(int i = a; i <= b; i++)
#define Forr(i, a, b) for(int i = a; i >= b; i--)
#define F first
#define S second
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) ((int)x.size())
using namespace std;
using i32 = int32_t;
using LL = long long;
using pii = pair<int, int>;

const int MAXN = 1'000'000;
const int MAXND = MAXN * 4;
const int INF = MAXN + 8;

#define L(id) ((id) * 2 + 1)
#define R(id) ((id) * 2 + 2)
struct SegTree {
    struct Node {
        int mn, cnt, add;
    } a[MAXND];
    void pull(int id) {
        a[id].mn = min(a[L(id)].mn, a[R(id)].mn);
        a[id].cnt = 0;
        if(a[L(id)].mn == a[id].mn) a[id].cnt += a[L(id)].cnt;
        if(a[R(id)].mn == a[id].mn) a[id].cnt += a[R(id)].cnt;
        a[id].mn += a[id].add;
    }
    void build(int id, int l, int r) {
        a[id].mn = 0;
        a[id].cnt = r - l + 1;
        a[id].add = 0;
        if(l == r) return;
        int m = (l + r) / 2;
        build(L(id), l, m);
        build(R(id), m + 1, r);
    }
    void add(int id, int l, int r, int L, int R, int val) {
        if(l >= L && r <= R) {
            a[id].mn += val;
            a[id].add += val;
            return;
        }
        int m = (l + r) / 2;
        if(L <= m) add(L(id), l, m, L, R, val);
        if(R > m)  add(R(id), m + 1, r, L, R, val);
        pull(id);
    }
    int query() {
        // assert(a[0].mn >= 4);
        if(a[0].mn != 4) return 0;
        return a[0].cnt;
    }
    void out(int id, int l, int r, int tot) {
        tot += a[id].add;
        if(l == r) {
            cerr << tot << " ";
            return;
        }
        int m = (l + r) / 2;
        out(L(id), l, m, tot);
        out(R(id), m + 1, r, tot);
        if(!id) cerr << "\n";
    }
} seg;

int h, w, hw;
vector<vector<int>> g;  // grid
int r[MAXN + 10];
int c[MAXN + 10];

inline void update_2x2(int i, int j, int val) {
    int vals[] = {g[i][j], g[i + 1][j], g[i][j + 1], g[i + 1][j + 1]};
    sort(vals, vals + 4);
    seg.add(0, 0, hw - 1, vals[0], vals[1] - 1, val);
    if(vals[2] != hw) seg.add(0, 0, hw - 1, vals[2], vals[3] - 1, 8 * val);
}

inline void update(int i, int j, int val) {
    update_2x2(i - 1, j - 1, val);
    update_2x2(i - 1, j    , val);
    update_2x2(i    , j - 1, val);
    update_2x2(i    , j    , val);
}

void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C) {
    h = H; w = W; hw = h * w;
    seg.build(0, 0, hw - 1);
    g.resize(h + 2);
    For(i, 0, h + 1) g[i].resize(w + 2, hw);
    For(i, 0, hw - 1) {
        r[i] = R[i] + 1;
        c[i] = C[i] + 1;
        g[r[i]][c[i]] = i;
    }
    For(i, 0, h) For(j, 0, w) {
        update_2x2(i, j, 1);
    }
    // seg.out(0, 0, hw - 1, 0);
    // For(i, 0, h + 1) For(j, 0, w + 1) {
    //     cerr << g[i][j] << " \n"[j == w + 1];
    // }
}

int swap_seats(int a, int b) {
    update(r[a], c[a], -1);
    update(r[b], c[b], -1);
    swap(g[r[a]][c[a]], g[r[b]][c[b]]);
    swap(r[a], r[b]);
    swap(c[a], c[b]);
    update(r[a], c[a], 1);
    update(r[b], c[b], 1);
    return seg.query();
}

/*

2 3 2
0 0
1 0
1 1
0 1
0 2
1 2
0 5
0 5

*/
#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...