Submission #365028

#TimeUsernameProblemLanguageResultExecution timeMemory
365028wiwiho자리 배치 (IOI18_seats)C++14
100 / 100
3903 ms126720 KiB
#include "seats.h"

#include <bits/stdc++.h>
#include <bits/extc++.h>

#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define iceil(a, b) (((a) + (b) - 1) / (b))
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
    if(pvaspace) b << " "; pvaspace=true;\
    b << pva;\
}\
b << "\n";}

using namespace std;
using namespace __gnu_pbds;

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

using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;

const ll MOD = 1000000007;
const ll MAX = 2147483647;

template<typename A, typename B>
ostream &operator<<(ostream &o, pair<A, B> p){
    return o << '(' << p.F << ',' << p.S << ')';
}

struct Node{
//    int l = -1, r = -1;
    int mn = 0, cnt = 0, tag = 0;

    int rmn(){
        return mn + tag;
    }
};

int n;

#define lc(id) ((id) * 2 + 1)
#define rc(id) ((id) * 2 + 2)

struct SegmentTree{

    vector<Node> st;
    int ts = 0;

    void build(int l = 0, int r = n - 1, int id = 0){
        st[id].cnt = r - l + 1;
        if(l == r) return;
        int m = (l + r) / 2;
        build(l, m, lc(id));
        build(m + 1, r, rc(id));
    }

    void pull(int id){
        int l = lc(id), r = rc(id);
        st[id].mn = min(st[l].rmn(), st[r].rmn());
        st[id].cnt = 0;
        if(st[l].rmn() == st[id].mn) st[id].cnt += st[l].cnt;
        if(st[r].rmn() == st[id].mn) st[id].cnt += st[r].cnt;
    }

    void modify(int l, int r, int v, int L = 0, int R = n - 1, int id = 0){
//        cerr << l << " " << r << " " << L << " " << R << "\n";
        if(l == L && r == R){
            st[id].tag += v;
            return;
        }
        int M = (L + R) / 2;
        if(r <= M) modify(l, r, v, L, M, lc(id));
        else if(l > M) modify(l, r, v, M + 1, R, rc(id));
        else{
            modify(l, M, v, L, M, lc(id));
            modify(M + 1, r, v, M + 1, R, rc(id));
        }
        pull(id);
    }

};

SegmentTree st;
vector<vector<int>> seat;
int h, w;

void add(int x, int y){
    vector<int> t;
    for(int i = 0; i <= 1; i++){
        for(int j = 0; j <= 1; j++){
            if(seat[x + i][y + j] != -1) t.eb(seat[x + i][y + j]);
        }
    }
    lsort(t);
    if(!t.empty()){
        int l = t[0];
        int r = t.size() >= 2 ? t[1] : h * w;
        r--;
        if(l <= r) st.modify(l, r, 1);
    }
    if(t.size() >= 3){
        int l = t[2];
        int r = t.size() >= 4 ? t[3] : h * w;
        r--;
        if(l <= r) st.modify(l, r, 1);
    }
}

void del(int x, int y){
    vector<int> t;
    for(int i = 0; i <= 1; i++){
        for(int j = 0; j <= 1; j++){
            if(seat[x + i][y + j] != -1) t.eb(seat[x + i][y + j]);
        }
    }
    lsort(t);
    if(!t.empty()){
        int l = t[0];
        int r = t.size() >= 2 ? t[1] : h * w;
        r--;
        if(l <= r) st.modify(l, r, -1);
    }
    if(t.size() >= 3){
        int l = t[2];
        int r = t.size() >= 4 ? t[3] : h * w;
        r--;
        if(l <= r) st.modify(l, r, -1);
    }
}

void update(int x, int y, int v){
    for(int i = -1; i <= 0; i++){
        for(int j = -1; j <= 0; j++){
            del(x + i, y + j);
        }
    }
    seat[x][y] = v;
    for(int i = -1; i <= 0; i++){
        for(int j = -1; j <= 0; j++){
            add(x + i, y + j);
        }
    }
}

int ans(){
    return st.st[0].rmn() == 4 ? st.st[0].cnt : 0;
}

vector<pii> pos;

void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C){
    h = H;
    w = W;
    seat.resize(h + 2, vector<int>(w + 2, -1));
    pos.resize(h * w);
    for(int i = 0; i < h * w; i++){
        seat[R[i] + 1][C[i] + 1] = i;
        pos[i] = mp(R[i] + 1, C[i] + 1);
    }
    st.st.resize(4 * H * W);
    n = H * W;
    st.build();
    for(int i = 0; i <= H; i++){
        for(int j = 0; j <= W; j++){
            add(i, j);
        }
    }
//    for(int i = 1; i <= h; i++){
//        for(int j = 0; j <= w; j++){
//            cerr << seat[i][j] << " ";
//        }
//        cerr << "\n";
//    }
//    cerr << ans() << "\n";
}

int swap_seats(int a, int b){
    update(pos[a].F, pos[a].S, b);
    update(pos[b].F, pos[b].S, a);
    swap(pos[a], pos[b]);
    return ans();
}
#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...