Submission #1105459

# Submission time Handle Problem Language Result Execution time Memory
1105459 2024-10-26T12:57:17 Z jerzyk Mosaic (IOI24_mosaic) C++17
100 / 100
147 ms 63868 KB
#include <bits/stdc++.h>
#include "mosaic.h"

using namespace std;
#define pb push_back
#define st first
#define nd second
typedef long long ll;
typedef long double ld;
const ll I = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL;
const int II = 2 * 1000 * 1000 * 1000;
const ll M = 1000LL * 1000LL * 1000LL + 7LL;
const int N = 1000 * 1000 + 7;
ll tab[N][5], sum[N][5];
ll ar[N], rev[N];

bool F(int a, int b)
{
    return !(a | b);
}

void DoBas(int n)
{
    int r = min(3, n);
    for(int j = 2; j <= r; ++j)
    {
        tab[n - j + 1][j] = F(tab[n - j + 1][j - 1], tab[n - j + 3][j - 1]);
        for(int i = n - j; i >= 1; --i)
            tab[i][j] = F(tab[i + 1][j], tab[i][j - 1]);
        for(int i = n - j + 2; i <= 2 * (n - j + 1) - 1; ++i)
            tab[i][j] = F(tab[i - 1][j], tab[i + 2][j - 1]);
    }
    for(int j = 1; j <= r; ++j)
        for(int i = 1; i <= 2 * (n - j + 1) - 1; ++i)
            sum[i][j] = tab[i][j] + sum[i - 1][j];

    /*for(int j = 1; j <= 3; ++j)
    {
        for(int i = 1; i <= 2 * (n - j + 1) - 1; ++i)
            cerr << tab[i][j] << " ";
        cerr << "\n";
    }*/

    if(r < 3) return;

    for(int i = 1; i <= 2 * n - 5; ++i)
        ar[i] = ar[i - 1] + (ll)i * (ll)tab[i][3];
    for(int i = 1; i <= 2 * n - 5; ++i)
        rev[i] = rev[i - 1] + (ll)(-i) * (ll)tab[i][3];

}

inline ll S(int a, int b, int r)
{
    return sum[b][r] - sum[a - 1][r];
}

ll A(int a, int b, int s)
{
    ll ans = ar[b] - ar[a - 1];
    ans += (ll)(s - a) * (ll)(sum[b][3] - sum[a - 1][3]);
    return ans;
}

ll R(int a, int b, int s)
{
    ll ans = rev[b] - rev[a - 1];
    ans += (ll)(s + b) * (ll)(sum[b][3] - sum[a - 1][3]);
    return ans;
}

ll WS(int a, int b, int d)
{
    ll ans = A(a, a + d - 2, 1);
    ans += R(b - d + 2, b, 1);
    ans += (ll)d * (ll)(sum[b - d + 1][3] - sum[a + d - 2][3]);
    return ans;
}

ll Query(int n, int i1, int i2, int j1, int j2)
{
    ll ans = 0LL;
    if(i1 == 1)
    {
        ans += S(n + j1 - 1, n + j2 - 1, 1);
        i1 = 2;
    }
    //cerr << j1 << " " << j2 << " Xd " << ans << "\n";
    if(i1 > i2) return ans;
    if(j1 == 1)
    {
        ans += S(n - i2 + 1, n - i1 + 1, 1);
        j1 = 2;
    }
    //cerr << j1 << " " << j2 << " Xd " << ans << "\n";
    if(j1 > j2) return ans;
    //cerr << "??? " << i2 << "\n";
    if(i1 == 2)
    {
        ans += S(n - 1 + j1 - 2, n - 1 + j2 - 2, 2);
        i1 = 3;
    }
    //cerr << sum[n - 1 + j1 - 2][2] << " " << sum[n - 1 + j2 - 2][2] << "\n";
    //cerr << n - 1 + j1 - 2 << " " << n - 1 + j2 - 2 << " Xd " << ans << "\n";
    if(i1 > i2) return ans;
    
    if(j1 == 2)
    {
        ans += S(n - 1 - i2 + 2, n - 1 - i1 + 2, 2);
        j1 = 3;
    }
    if(j1 > j2) return ans;
    //cerr << "First " << ans << " " << i1 << " " << i2 << " " << j1 << " " << j2 << "\n";
    int d = i1 - 3, r = (i2 - i1);
    j1 -= d; j2 -= d;
    ans += WS(n - 2 + j1 - 3 - r, n - 2 + j2 - 3, min(j2 - j1 + 1, i2 - i1 + 1));
    return ans;
}

vector<long long> mosaic(vector<int> X, vector<int> Y, vector<int> T, vector<int> B, vector<int> L, vector<int> R)
{
    vector<ll> ans;
    int n = X.size(), q = T.size();
    for(int i = 1; i <= n; ++i)
        tab[i][1] = Y[n - i];
    for(int i = n + 1; i <= 2 * n - 1; ++i)
        tab[i][1] = X[i - n];
    DoBas(n);
    for(int i = 0; i < q; ++i)
        ans.pb(Query(n, T[i] + 1, B[i] + 1, L[i] + 1, R[i] + 1));
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 1 ms 2384 KB Output is correct
3 Correct 1 ms 2384 KB Output is correct
4 Correct 1 ms 2384 KB Output is correct
5 Correct 1 ms 2384 KB Output is correct
6 Correct 1 ms 2640 KB Output is correct
7 Correct 2 ms 2384 KB Output is correct
8 Correct 1 ms 2384 KB Output is correct
9 Correct 1 ms 2384 KB Output is correct
10 Correct 1 ms 2384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 1 ms 2384 KB Output is correct
3 Correct 1 ms 2384 KB Output is correct
4 Correct 1 ms 2384 KB Output is correct
5 Correct 1 ms 2384 KB Output is correct
6 Correct 1 ms 2640 KB Output is correct
7 Correct 2 ms 2384 KB Output is correct
8 Correct 1 ms 2384 KB Output is correct
9 Correct 1 ms 2384 KB Output is correct
10 Correct 1 ms 2384 KB Output is correct
11 Correct 1 ms 2384 KB Output is correct
12 Correct 1 ms 2384 KB Output is correct
13 Correct 1 ms 2496 KB Output is correct
14 Correct 2 ms 2384 KB Output is correct
15 Correct 1 ms 2384 KB Output is correct
16 Correct 1 ms 2384 KB Output is correct
17 Correct 2 ms 2384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 117 ms 61628 KB Output is correct
2 Correct 114 ms 61628 KB Output is correct
3 Correct 99 ms 61116 KB Output is correct
4 Correct 91 ms 61564 KB Output is correct
5 Correct 89 ms 39152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 1 ms 2384 KB Output is correct
3 Correct 1 ms 2384 KB Output is correct
4 Correct 1 ms 2384 KB Output is correct
5 Correct 1 ms 2384 KB Output is correct
6 Correct 1 ms 2640 KB Output is correct
7 Correct 2 ms 2384 KB Output is correct
8 Correct 1 ms 2384 KB Output is correct
9 Correct 1 ms 2384 KB Output is correct
10 Correct 1 ms 2384 KB Output is correct
11 Correct 1 ms 2384 KB Output is correct
12 Correct 1 ms 2384 KB Output is correct
13 Correct 1 ms 2496 KB Output is correct
14 Correct 2 ms 2384 KB Output is correct
15 Correct 1 ms 2384 KB Output is correct
16 Correct 1 ms 2384 KB Output is correct
17 Correct 2 ms 2384 KB Output is correct
18 Correct 62 ms 16324 KB Output is correct
19 Correct 60 ms 16140 KB Output is correct
20 Correct 90 ms 15756 KB Output is correct
21 Correct 74 ms 15824 KB Output is correct
22 Correct 76 ms 15880 KB Output is correct
23 Correct 67 ms 15724 KB Output is correct
24 Correct 62 ms 15724 KB Output is correct
25 Correct 63 ms 15732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 13244 KB Output is correct
2 Correct 121 ms 63420 KB Output is correct
3 Correct 130 ms 62920 KB Output is correct
4 Correct 129 ms 62776 KB Output is correct
5 Correct 125 ms 63232 KB Output is correct
6 Correct 129 ms 63616 KB Output is correct
7 Correct 134 ms 63612 KB Output is correct
8 Correct 113 ms 63548 KB Output is correct
9 Correct 91 ms 40552 KB Output is correct
10 Correct 85 ms 40680 KB Output is correct
11 Correct 93 ms 40552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 135 ms 63416 KB Output is correct
2 Correct 127 ms 63612 KB Output is correct
3 Correct 132 ms 61884 KB Output is correct
4 Correct 132 ms 61880 KB Output is correct
5 Correct 147 ms 63868 KB Output is correct
6 Correct 110 ms 40764 KB Output is correct
7 Correct 79 ms 31684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 117 ms 61628 KB Output is correct
2 Correct 114 ms 61628 KB Output is correct
3 Correct 99 ms 61116 KB Output is correct
4 Correct 91 ms 61564 KB Output is correct
5 Correct 89 ms 39152 KB Output is correct
6 Correct 135 ms 63416 KB Output is correct
7 Correct 127 ms 63612 KB Output is correct
8 Correct 132 ms 61884 KB Output is correct
9 Correct 132 ms 61880 KB Output is correct
10 Correct 147 ms 63868 KB Output is correct
11 Correct 110 ms 40764 KB Output is correct
12 Correct 79 ms 31684 KB Output is correct
13 Correct 112 ms 63424 KB Output is correct
14 Correct 109 ms 63420 KB Output is correct
15 Correct 110 ms 63744 KB Output is correct
16 Correct 107 ms 62012 KB Output is correct
17 Correct 106 ms 63108 KB Output is correct
18 Correct 104 ms 63548 KB Output is correct
19 Correct 83 ms 36032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 1 ms 2384 KB Output is correct
3 Correct 1 ms 2384 KB Output is correct
4 Correct 1 ms 2384 KB Output is correct
5 Correct 1 ms 2384 KB Output is correct
6 Correct 1 ms 2384 KB Output is correct
7 Correct 1 ms 2640 KB Output is correct
8 Correct 2 ms 2384 KB Output is correct
9 Correct 1 ms 2384 KB Output is correct
10 Correct 1 ms 2384 KB Output is correct
11 Correct 1 ms 2384 KB Output is correct
12 Correct 1 ms 2384 KB Output is correct
13 Correct 1 ms 2384 KB Output is correct
14 Correct 1 ms 2496 KB Output is correct
15 Correct 2 ms 2384 KB Output is correct
16 Correct 1 ms 2384 KB Output is correct
17 Correct 1 ms 2384 KB Output is correct
18 Correct 2 ms 2384 KB Output is correct
19 Correct 117 ms 61628 KB Output is correct
20 Correct 114 ms 61628 KB Output is correct
21 Correct 99 ms 61116 KB Output is correct
22 Correct 91 ms 61564 KB Output is correct
23 Correct 89 ms 39152 KB Output is correct
24 Correct 62 ms 16324 KB Output is correct
25 Correct 60 ms 16140 KB Output is correct
26 Correct 90 ms 15756 KB Output is correct
27 Correct 74 ms 15824 KB Output is correct
28 Correct 76 ms 15880 KB Output is correct
29 Correct 67 ms 15724 KB Output is correct
30 Correct 62 ms 15724 KB Output is correct
31 Correct 63 ms 15732 KB Output is correct
32 Correct 72 ms 13244 KB Output is correct
33 Correct 121 ms 63420 KB Output is correct
34 Correct 130 ms 62920 KB Output is correct
35 Correct 129 ms 62776 KB Output is correct
36 Correct 125 ms 63232 KB Output is correct
37 Correct 129 ms 63616 KB Output is correct
38 Correct 134 ms 63612 KB Output is correct
39 Correct 113 ms 63548 KB Output is correct
40 Correct 91 ms 40552 KB Output is correct
41 Correct 85 ms 40680 KB Output is correct
42 Correct 93 ms 40552 KB Output is correct
43 Correct 135 ms 63416 KB Output is correct
44 Correct 127 ms 63612 KB Output is correct
45 Correct 132 ms 61884 KB Output is correct
46 Correct 132 ms 61880 KB Output is correct
47 Correct 147 ms 63868 KB Output is correct
48 Correct 110 ms 40764 KB Output is correct
49 Correct 79 ms 31684 KB Output is correct
50 Correct 112 ms 63424 KB Output is correct
51 Correct 109 ms 63420 KB Output is correct
52 Correct 110 ms 63744 KB Output is correct
53 Correct 107 ms 62012 KB Output is correct
54 Correct 106 ms 63108 KB Output is correct
55 Correct 104 ms 63548 KB Output is correct
56 Correct 83 ms 36032 KB Output is correct
57 Correct 144 ms 63420 KB Output is correct
58 Correct 108 ms 63232 KB Output is correct
59 Correct 134 ms 63420 KB Output is correct
60 Correct 141 ms 62908 KB Output is correct
61 Correct 139 ms 62908 KB Output is correct
62 Correct 111 ms 63164 KB Output is correct
63 Correct 92 ms 40556 KB Output is correct
64 Correct 79 ms 31420 KB Output is correct