Submission #293113

#TimeUsernameProblemLanguageResultExecution timeMemory
293113SamAndSeats (IOI18_seats)C++17
0 / 100
454 ms90748 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;

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

int** a;

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

ban t[N * 4];

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, 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 j)
{
    if (a[1][j] == n * m + 5)
        return;
    int q = 0;
    if (a[1][j - 1] > a[1][j])
        ++q;
    else
        --q;
    if (a[1][j + 1] > a[1][j])
        ++q;
    else
        --q;
    ubd(1, n * m, a[1][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 j = 1; j <= m; ++j)
    {
        ubdd(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;
    ubdd(y[u1] - 1);
    ubdd(y[u1]);
    ubdd(y[u1] + 1);
    ubdd(y[u1] - 1);
    ubdd(y[u1]);
    ubdd(y[u1] + 1);
    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...