Submission #1156356

#TimeUsernameProblemLanguageResultExecution timeMemory
1156356The_SamuraiMosaic (IOI24_mosaic)C++20
12 / 100
98 ms31800 KiB
#include "mosaic.h"
#include "bits/stdc++.h"
using namespace std;
using ll = long long;

void maxs(int &x, int y) {
    x = max(x, y);
}

vector<long long> mosaic(vector<int> X, vector<int> Y, vector<int> T, 
                        vector<int> B, vector<int> L, vector<int> R) {

    int n = X.size();
    vector a(n, vector(min(n, 3), false));
    vector pref(5, vector(n, 0));
    a[0] = vector(n, false);
    for (int i = 0; i < n; i++) {
        a[0][i] = X[i]; a[i][0] = Y[i];
    }
    for (int i = 1; i < n; i++) {
        if (i <= 2) a[i].resize(n);
        for (int j = 1; j < a[i].size(); j++) {
            a[i][j] = !(a[i - 1][j] | a[i][j - 1]);   
        }
    }
    for (int i = 0; i < min(n, 3); i++) for (int j = 0; j < n; j++) {
        pref[i + 1][j + 1] = a[i][j] + pref[i][j + 1] + pref[i + 1][j] - pref[i][j];
    }
    auto get = [&](int x, int y) -> int {
        assert(min(x, y) >= 2);
        int d = min(x, y) - 2;
        x -= d, y -= d;
        if (y == 2) {
            return n - 1 - x;
        }
        return n - 3 + (y - 2);
    };

    vector<int> pref2(n * 3);
    if (n >= 3) {
        int z = 0;
        for (int i = n - 1; i >= 2; i--) {
            pref2[z] = a[i][2];
            if (z > 0) pref2[z] = pref2[z - 1];
            z++;
        }
        for (int i = 3; i < n; i++) {
            pref2[z] = pref2[z - 1];
            pref2[z] += a[2][i];
            z++;
        }
    }

    vector<ll> ans(T.size());
    for (int i = 0; i < T.size(); i++) {
        int now = 0;
        for (int x = T[i]; x <= B[i]; x++) {
            // cout << x << ' ' << now << endl;
            auto [y1, y2] = make_pair(L[i], R[i]);
            if (x <= 2) {
                y1++, y2++;
                now += pref[x+1][y2] - pref[x+1][y1 - 1] - pref[x][y2] + pref[x][y1 - 1];
                continue;
            }
            for (int i = y1; i <= min(y2, 1); i++) now += a[x][i];
            maxs(y1, 2);
            if (y1 > y2) continue;
            int l = get(x, y1), r = get(x, y2);
            now += pref2[r];
            if (l > 0) now -= pref2[l - 1];
        }
        ans[i] = now;
    }

    // for (int i = 0; i < n; i++) {
    //     for (int j = 0; j < a[i].size(); j++) cout << a[i][j] << ' ';
    //     cout << endl;
    // }
    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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...