제출 #293146

#제출 시각아이디문제언어결과실행 시간메모리
293146SamAnd자리 배치 (IOI18_seats)C++17
100 / 100
2145 ms157256 KiB
#include "seats.h"
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
typedef long long ll;
const int N = 1000006;
const int xx[9] = {0, 1, 0, 1, -1, 0, 1, -1, -1};
const int yy[9] = {0, 0, 1, 1, 0, -1, -1, 1, -1};

int n, m;
int x[N], y[N];

int** a;

struct ban
{
    pair<int, int> s, minu;
    int q;
    ban()
    {
        s = minu = m_p(0, 0);
        q = 0;
    }
    ban(pair<int, int> x)
    {
        s = x;
        minu = x;
        q = 1;
    }
};

ban t[N * 4];

pair<int, int> operator+(const pair<int, int>& a, const pair<int, int>& b)
{
    return m_p(a.fi + b.fi, a.se + b.se);
}

ban mer(const ban& l, const ban& r)
{
    ban res;
    res.s = l.s + r.s;
    if (l.minu < l.s + r.minu)
    {
        res.minu = l.minu;
        res.q = l.q;
    }
    else if (l.minu > l.s + r.minu)
    {
        res.minu = l.s + r.minu;
        res.q = r.q;
    }
    else
    {
        res.minu = l.minu;
        res.q = l.q + r.q;
    }
    return res;
}

void ubd(int tl, int tr, int x, pair<int, int> y, int pos)
{
    if (tl == tr)
    {
        t[pos] = ban(y);
        return;
    }
    int m = (tl + tr) / 2;
    if (x <= m)
        ubd(tl, m, x, y, pos * 2);
    else
        ubd(m + 1, tr, x, y, pos * 2 + 1);
    t[pos] = mer(t[pos * 2], t[pos * 2 + 1]);
}

void ubdd(int i, int j)
{
    if (a[i][j] == n * m + 5)
        return;
    pair<int, int> q = m_p(0, 0);
    for (int sx = i - 1; sx <= i; ++sx)
    {
        for (int sy = j - 1; sy <= j; ++sy)
        {
            int qq = 0;
            for (int ii = 0; ii < 4; ++ii)
            {
                int hx = sx + xx[ii];
                int hy = sy + yy[ii];
                if (a[hx][hy] <= a[i][j])
                    ++qq;
            }
            if (qq == 1)
                q.se++;
            else if (qq == 2)
                q.se--;
            else if (qq == 3)
                q.fi++;
            else
                q.fi--;
        }
    }
    ubd(1, n * m, a[i][j], q, 1);
}

void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C)
{
    n = H;
    m = W;
    for (int i = 0; i < n * m; ++i)
    {
        x[i + 1] = R[i] + 1;
        y[i + 1] = C[i] + 1;
    }
    a = new int*[n + 5];
    for (int i = 0; i < n + 5; ++i)
    {
        a[i] = new int[m + 5];
        for (int j = 0; j < m + 5; ++j)
            a[i][j] = n * m + 5;
    }
    for (int i = 1; i <= n * m; ++i)
    {
        a[x[i]][y[i]] = i;
    }
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= m; ++j)
        {
            ubdd(i, j);
        }
    }
}

int swap_seats(int u1, int u2)
{
    ++u1;
    ++u2;
    swap(x[u1], x[u2]);
    swap(y[u1], y[u2]);
    a[x[u1]][y[u1]] = u1;
    a[x[u2]][y[u2]] = u2;
    for (int i = 0; i < 9; ++i)
    {
        int hx = x[u1] + xx[i];
        int hy = y[u1] + yy[i];
        ubdd(hx, hy);
    }
    for (int i = 0; i < 9; ++i)
    {
        int hx = x[u2] + xx[i];
        int hy = y[u2] + yy[i];
        ubdd(hx, hy);
    }
    return t[1].q;
}
#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...