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

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:65:13: warning: 'start' may be used uninitialized in this function [-Wmaybe-uninitialized]
         ans += (rindex - 1) * (base + base - (rindex - 2) * 8) / 2;
         ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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; 
    ll mid = n / 2;
    
    while (t--) {
        ll y, x; cin >> y >> x;
        ll xdist = n - x + 1;
        ll ydist = n - y + 1;
        ll 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;
            }
        }

        ll 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;
        }

        ll base = ring + offset;
        ll ans = start;
        ans += (rindex - 1) * (base + base - (rindex - 2) * 8) / 2;

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