Olympiad in Informatics

[Fortuna OJ] Aug 12th - Group A Problem-Solving Report

A - Maze

This problem is pretty good. It showed me that segment trees can pull off moves like this.

Consider a segment tree that maintains, for a range, the shortest paths from the row entrances to the row exits; the way it is maintained is, roughly, a lot like Floyd. There’s not much to say, just read the code.

// A.cpp
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define lson (p << 1)
#define rson ((p << 1) | 1)
#define mid ((l + r) >> 1)

using namespace std;

const int MAX_N = 2e5 + 1, INF = 0x3f3f3f3f;

int n, m, q, org[6][MAX_N];

struct node
{
    int mp[6][6];
} nodes[MAX_N << 2];

void pushup(node &p, node &ls, node &rs)
{
    memset(p.mp, 0x3f, sizeof(p.mp));
    for (int k = 1; k <= n; k++)
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                p.mp[i][j] = min(p.mp[i][j], ls.mp[i][k] + rs.mp[k][j]);
    return;
}

void build(int l, int r, int p)
{
    memset(nodes[p].mp, 0x3f, sizeof(nodes[p].mp));
    if (l == r)
    {
        for (int i = 1; i <= n; i++)
            if (org[i][l])
                for (int j = i; j <= n; j++)
                    if (org[j][l])
                        nodes[p].mp[i][j] = nodes[p].mp[j][i] = j - i + 1;
                    else
                        break;
        return;
    }
    build(l, mid, lson), build(mid + 1, r, rson);
    pushup(nodes[p], nodes[lson], nodes[rson]);
}

void update(int qx, int l, int r, int p)
{
    memset(nodes[p].mp, 0x3f, sizeof(nodes[p].mp));
    if (l == r && l == qx)
    {
        for (int i = 1; i <= n; i++)
            if (org[i][l])
                for (int j = i; j <= n; j++)
                    if (org[j][l])
                        nodes[p].mp[i][j] = nodes[p].mp[j][i] = j - i + 1;
                    else
                        break;
        return;
    }
    if (qx <= mid)
        update(qx, l, mid, lson);
    else
        update(qx, mid + 1, r, rson);
    pushup(nodes[p], nodes[lson], nodes[rson]);
}

node query(int ql, int qr, int l, int r, int p)
{
    if (ql <= l && r <= qr)
        return nodes[p];
    if (qr <= mid)
        return query(ql, qr, l, mid, lson);
    if (mid < ql)
        return query(ql, qr, mid + 1, r, rson);
    node fa, lft = query(ql, qr, l, mid, lson), rgt = query(ql, qr, mid + 1, r, rson);
    pushup(fa, lft, rgt);
    return fa;
}

int main()
{
    scanf("%d%d%d", &n, &m, &q);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            scanf("%d", &org[i][j]);
    build(1, m, 1);
    while (q--)
    {
        int opt, a, b, c, d;
        scanf("%d%d%d", &opt, &a, &b);
        if (opt == 1)
            org[a][b] ^= 1, update(b, 1, m, 1);
        else
        {
            scanf("%d%d", &c, &d);
            if (b > d)
                puts("-1");
            else
            {
                node nd = query(b, d, 1, m, 1);
                if (nd.mp[a][c] == INF)
                    puts("-1");
                else
                    printf("%d\n", nd.mp[a][c] - 1);
            }
        }
    }
    return 0;
}

B - Monster Hunter World

This problem is really nasty.

We classify the points with bottles and the points without bottles as black points and white points respectively. The whole problem then becomes: on a graph, mark directions on some undirected edges, then find a scheme that gives the most one-way triangles, where we only need to find the maximum (of course, the minimum is found in a very similar way).

We notice that the direction of an edge whose endpoints have different colors depends on the Manhattan distance and the parameter d, so we can try to precompute an array cover[i] meaning the number of points whose Manhattan distance to point \(i\) is less than \(d\). We can extend this a bit and let cover[i, j] be the number of points whose Manhattan distances to both points \(i, j\) are less than \(d\). Finally, the answer for the maximum is clearly:

\[ max_ans = \sum_{i, j \in White} max\{ cover[i] - cover[i, j], cover[j] - cover[i. j] \} \]

Apply some transformation:

\[ max_ans = \sum_{i, j\in White} max\{ cover[i], cover[j] \} - cover[i, j] \]

Notice that \(cover[i, j]\) is really, for each black point, the number of white points it can choose whose Manhattan distance is less than \(d\):

\[ max_ans = \sum_{i, j\in White} max\{ cover[i], cover[j] \} - \sum_{u \in Black} {cover[u] \choose 2} \]

So now we need to compute \(cover[]\) to carry out this calculation. Directly counting the points whose Manhattan distance is less than \(d\) is a bit hard, so we consider “folding” a point’s coordinate system, from \((x, y) \to (x + y, x - y)\). We find that after folding the coordinate system, considering the Manhattan distance between two points becomes considering the “Chebyshev distance” between them (\(max\{|x_i - x_j|, |y_i - y_j|\}\), and after transforming the coordinate system, substituting in satisfies the absolute value inequality, which is exactly the definition of Manhattan distance). So, working with the new coordinates, we find that \(cover[i]\) equals the number of points inside the square centered at point \(i\) with radius \(D\). This is the classic sweep-line problem (POJ 2482); just plug it in.

// B.cpp
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define ll long long
#define lson (p << 1)
#define rson ((p << 1) | 1)
#define mid ((l + r) >> 1)

using namespace std;

const int MAX_N = 1e5 + 200;

int n, m, d, tree[MAX_N << 2];
long long min_ans = 0, max_ans = 0, minusComb = 0;

vector<ll> mapping, xlist;

template <class T>
inline T read(T &x)
{
    T data = 0;
    int w = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-')
            w = -1;
        ch = getchar();
    }
    while (isdigit(ch))
        data = 10 * data + ch - '0', ch = getchar();
    return x = data * w;
}

inline int lowbit(int x) { return x & -x; }

void add(int x, int d)
{
    int siz = mapping.size();
    for (; x <= siz; x += lowbit(x))
        tree[x] += d;
}

int query(int x)
{
    int ans = 0;
    for (; x; x -= lowbit(x))
        ans += tree[x];
    return ans;
}

struct point
{
    ll x, y;
    bool operator<(const point &pt) const { return x < pt.x || (x == pt.x && y < pt.y); }
} pts1[MAX_N], pts2[MAX_N];

struct quad
{
    ll x, y, aff;
    bool operator<(const quad &qd) const { return x < qd.x; }
};
vector<quad> vec;

int cover[MAX_N];

int ripe(ll x) { return lower_bound(mapping.begin(), mapping.end(), x) - mapping.begin() + 1; }

void solve()
{
    memset(tree, 0, sizeof(tree));
    vec.clear(), xlist.clear(), mapping.clear();
    for (int i = 1; i <= m; i++)
    {
        vec.push_back(quad{pts2[i].x - d, pts2[i].y, 1});
        vec.push_back(quad{pts2[i].x + d + 1, pts2[i].y, -1});

        xlist.push_back(pts2[i].x - d), xlist.push_back(pts2[i].x + d + 1);

        mapping.push_back(pts2[i].y);
    }

    for (int i = 1; i <= n; i++)
    {
        xlist.push_back(pts1[i].x);
        mapping.push_back(pts1[i].y - d - 1), mapping.push_back(pts1[i].y + d);
    }

    sort(xlist.begin(), xlist.end()), sort(mapping.begin(), mapping.end());
    xlist.erase(unique(xlist.begin(), xlist.end()), xlist.end());
    mapping.erase(unique(mapping.begin(), mapping.end()), mapping.end());

    sort(vec.begin(), vec.end());
    int cur1 = 0, cur2 = 1;
    for (ll x : xlist)
    {
        while (cur1 < vec.size() && vec[cur1].x == x)
            add(ripe(vec[cur1].y), vec[cur1].aff), cur1++;
        while (cur2 <= n && pts1[cur2].x == x)
            cover[cur2] = query(ripe(pts1[cur2].y + d)) - query(ripe(pts1[cur2].y - d - 1)), cur2++;
    }

    sort(cover + 1, cover + 1 + n);

    for (int i = 1; i <= n; i++)
    {
        min_ans += 1LL * (n - i) * cover[i];
        max_ans += 1LL * (i - 1) * cover[i];
        minusComb += 1LL * (1LL * cover[i] * (cover[i] - 1) >> 1);
    }
}

int main()
{
    /*
    freopen("mhw.in", "r", stdin);
    freopen("mhw.out", "w", stdout);
    */
    read(n), read(m), read(d);
    for (int i = 1, x, y; i <= n; i++)
    {
        read(x), read(y);
        pts1[i].x = x + y, pts1[i].y = x - y;
    }
    for (int i = 1, x, y; i <= m; i++)
    {
        read(x), read(y);
        pts2[i].x = x + y, pts2[i].y = x - y;
    }
    sort(pts1 + 1, pts1 + 1 + n);
    sort(pts2 + 1, pts2 + 1 + m);

    solve();
    swap(n, m), swap(pts1, pts2);

    solve();

    printf("%lld %lld", min_ans - minusComb, max_ans - minusComb);
    return 0;
}

Comments 1

  1. ZiRo

    %%%