CSES - Datatähti 2022 alku - Results
Submission details
Task:Spiraali
Sender:xenial
Submission time:2021-10-05 16:35:01 +0300
Language:C++ (C++17)
Status:READY
Result:35
Feedback
groupverdictscore
#1ACCEPTED15
#2ACCEPTED20
#30
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1details
#2ACCEPTED0.01 s2details
#3--3details

Compiler report

input/code.cpp: In function 'void set_io(std::__cxx11::string)':
input/code.cpp:16:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         freopen((filename + ".in").c_str(), "r", stdin);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
input/code.cpp:17:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         freopen((filename + ".out").c_str(), "w", stdout);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
input/code.cpp: In function 'int main()':
input/code.cpp:64:12: warning: 'start' may be used uninitialized in this function [-Wmaybe-uninitialized]
         ll ans = start;
            ^~~

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
#define sz size
#define rsz resize

void set_io(string filename = "") {
    ios::sync_with_stdio(0);
	cin.tie(0);

    if (filename != "") {
        freopen((filename + ".in").c_str(), "r", stdin);
        freopen((filename + ".out").c_str(), "w", stdout);
    }
}

int main() {
    set_io("");

    ll n, t; cin >> n >> t; 
    int mid = n / 2;
    
    while (t--) {
        int y, x; cin >> y >> x;
        int xdist = n - x + 1;
        int ydist = n - y + 1;
        int ring = 4 * (n - 1), rindex = min(min(x, xdist), min(y, ydist)), dx, dy, offset;

        if (x <= mid) {
            if (y <= mid) {
                dx = x - min(x, y) + 1; dy = y - min(x, y) + 1;
                if (dx != 1) offset = -8;
                else offset = 0;
            } else {
                offset = -2;
                dx = x - min(x, ydist) + 1; dy = y + min(x, ydist) - 1;
            }
        } else {
            if (y <= mid) {
                offset = -6;
                dx = x + min(xdist, y) - 1; dy = y - min(xdist, y) + 1;
            } else {
                offset = -4;
                dx = x + min(xdist, ydist) - 1; dy = y + min(xdist, ydist) - 1;
            }
        }

        int start;
        if (dx == 1) {
            start = dy;
        } else if (dx == n) {
            start = 2 * n + n - 1 - dy;
        } else if (dy == 1) {
            start = ring + 2 - dx;
        } else if (dy == n) {
            start = n - 1 + dx;
        }

        int base = ring + offset;
        ll ans = start;
        for (int i = 0; i < rindex - 1; i++) {
            ans += base - (i * 8);
        }

        cout << ans << endl;
    }
}

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
...
Truncated

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
...
Truncated

Test 3

Group: 3

Verdict:

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

correct output
571375684522141210
967321186816598569
762879105851175000
370065046779516790
936897883750373771
...

user output
(empty)