CSES - Datatähti 2022 alku - Results
Submission details
Task:Spiraali
Sender:Mahtimursu
Submission time:2021-10-04 00:52:22
Language:C++17
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED15
#2ACCEPTED20
#3ACCEPTED65
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1details
#2ACCEPTED0.01 s2details
#3ACCEPTED0.01 s3details

Code

#include <bits/stdc++.h>

typedef long long ll;

#define M 1000000007
#define N (1 << 18)

using namespace std;

ll sumUpTo(ll x) {
    x /= 2;
    return x * (x + 1);
}

ll atob(ll a, ll b) {
    return ((b + b % 2) / 2) * ((b + b % 2) / 2) - ((a - a % 2) / 2) * ((a - a % 2) / 2);
}

ll calculate(ll n, ll y, ll x) {
    ll ans = 0;

    ll outside = min(min(x - 1, n - x), min(y - 1, n - y));

    ll maxVal = n - 1;
    ll minVal = maxVal - outside * 2 + 2;

    ll sm = atob(minVal, maxVal) * 4;
    /*for (int it = 0; it < outside; ++it) {
        ans += (maxVal) * 4;
        maxVal -= 2;
    }*/
    ans += sm;

    if (x == outside + 1) {
        // Going down
        ans += y - outside; 
    } else if (x < n - outside) {
        // In the middle. 

        // Left downwards
        ans += n - outside * 2;

        // Traveling above
        if (y <= n / 2) {
            // Lower part - left corner
            ans += n - outside * 2 - 1;
            // Right part - right down corner
            ans += n - outside * 2 - 1;
            // Going from left
            ans += n - x - outside;
        } else {
            // Traveling below
            ans += x - outside - 1;
        }
    } else {
        // Right going up
        // Left
        ans += n - outside * 2;
        // Lower part - left down corner
        ans += n - outside * 2 - 1;
        // up part
        ans += n - y - outside;
    }

    return ans;
}

int matrix[6][6] = {
    {1, 20, 19, 18, 17, 16} ,
    {2, 21, 32, 31, 30, 15} ,
    {3, 22, 33, 36, 29, 14} ,
    {4, 23, 34, 35, 28, 13} ,
    {5, 24, 25, 26, 27, 12} ,
    {6, 7, 8, 9, 10, 11}
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, t;
    cin >> n >> t;
    for (int i = 0; i < t; ++i) {
        ll x, y;
        cin >> y >> x;

        ll ans = calculate(n, y, x);

        cout << ans << "\n";
    } 

    /*for (int x = 1; x <= n; ++x) {
        for (int y = 1; y <= n; ++y) {
            ll ans = calculate(n, y, x);
            ll cor = matrix[y - 1][x - 1];
            if (ans != cor) {
                cout  << "FAIL" << endl;
                cout << x << ", " << y << " COR " << cor << " GOT " << ans << endl;
            }
        }
    }*/

    return 0;
}

Test details

Test 1

Group: 1

Verdict: ACCEPTED

input
10 100
1 1
1 2
1 3
1 4
...

correct output
1
36
35
34
33
...

user output
1
36
35
34
33
...

Test 2

Group: 2

Verdict: ACCEPTED

input
1000 1000
371 263
915 322
946 880
53 738
...

correct output
773533
312166
206053
200080
593922
...

user output
773533
312166
206053
200080
593922
...

Test 3

Group: 3

Verdict: ACCEPTED

input
1000000000 1000
177757853 827347032
409613589 419171337
739269360 256524697
328695530 896842209
...

correct output
571375684522141210
967321186816598569
762879105851175000
370065046779516790
936897883750373771
...

user output
571375684522141210
967321186816598569
762879105851175000
370065046779516790
936897883750373771
...