제출 #394019

#제출 시각아이디문제언어결과실행 시간메모리
394019phathnv자리 배치 (IOI18_seats)C++11
100 / 100
3066 ms126632 KiB
#include <bits/stdc++.h>
#include "seats.h"

using namespace std;

struct Node{
    int sum, minVal, cntMin;
    Node(int x = 0, int cnt = 1){
        sum = minVal = x;
        cntMin = cnt;
    }
    Node operator + (const Node &oth){
        Node res;
        res.sum = sum + oth.sum;
        res.minVal = min(minVal, sum + oth.minVal);
        res.cntMin = cntMin * (minVal == res.minVal) + oth.cntMin * (sum + oth.minVal == res.minVal);
        return res;
    }
};

struct SegmentTree{
    int n;
    Node d[4000007];
    void init(int _n){
        n = _n;
    }
    void update(int id, int l, int r, int pos, int val){
        if (pos < l || r < pos)
            return;
        if (l == r){
            d[id].sum += val;
            d[id].minVal += val;
            return;
        }
        int mid = (l + r) >> 1;
        update(id << 1, l, mid, pos, val);
        update(id << 1 | 1, mid + 1, r, pos, val);
        d[id] = d[id << 1] + d[id << 1 | 1];
    }
    void update(int pos, int val){
        update(1, 0, n - 1, pos, val);
    }
    int getCnt(){
        return d[1].cntMin;
    }
} st;

int h, w, tmp[4], nTmp;
vector<vector<int>> chair;
vector<int> x, y;

bool inSide(int x, int y){
    return (0 <= x && x < h && 0 <= y && y < w);
}

void UpdateSquare(int x, int y, int type){
    nTmp = 0;
    for(int dx = 0; dx < 2; dx++)
        for(int dy = 0; dy < 2; dy++)
            if (inSide(x + dx, y + dy))
                tmp[nTmp++] = chair[x + dx][y + dy];
    sort(tmp, tmp + nTmp);
    for(int i = 0; i < nTmp; i++){
        st.update(tmp[i], type);
        type = -type;
    }
}

void give_initial_chart(int hh, int ww, vector<int> r, vector<int> c) {
    h = hh;
    w = ww;
    x = r;
    y = c;
    chair.assign(h, vector<int>(w, 0));
    for(int i = 0; i < w * h; i++)
        chair[r[i]][c[i]] = i;
    st.init(h * w);
    for(int i = -1; i < h; i++)
        for(int j = -1; j < w; j++)
            UpdateSquare(i, j, 1);
}

int swap_seats(int a, int b) {
    for(int dx = -1; dx < 1; dx++)
        for(int dy = -1; dy < 1; dy++){
            UpdateSquare(x[a] + dx, y[a] + dy, -1);
            UpdateSquare(x[b] + dx, y[b] + dy, -1);
        }
    swap(chair[x[a]][y[a]], chair[x[b]][y[b]]);
    swap(x[a], x[b]);
    swap(y[a], y[b]);
    for(int dx = -1; dx < 1; dx++)
        for(int dy = -1; dy < 1; dy++){
            UpdateSquare(x[a] + dx, y[a] + dy, +1);
            UpdateSquare(x[b] + dx, y[b] + dy, +1);
        }
    return st.getCnt();
}
#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...