QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#128826#6744. Squareckiseki#AC ✓30ms3628kbC++201.8kb2023-07-21 16:04:472023-07-21 16:13:34

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-21 16:13:34]
  • 评测
  • 测评结果:AC
  • 用时:30ms
  • 内存:3628kb
  • [2023-07-21 16:04:47]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define all(x) begin(x), end(x)
#ifdef CKISEKI
#define safe cerr << __PRETTY_FUNCTION__ << " line " << __LINE__ << " safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
template <typename ...T>
void debug_(const char *s, T ...a) {
    cerr << "\e[1;32m(" << s << ") = (";
    int cnt = sizeof...(T);
    (..., (cerr << a << (--cnt ? ", " : ")\e[0m\n")));
}
template <typename I>
void orange_(const char *s, I L, I R) {
    cerr << "\e[1;32m[ " << s << " ] = [";
    while (L != R) cerr << " " << *L++;
    cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif


pair<int64_t,int64_t> getCoord(int64_t z) {
    int64_t X = 0;
    for (int s = 1<<30; s; s >>= 1) {
        if ((X + s) * (X + s + 1) / 2 <= z) {
            X += s;
        }
    }
    return {X, z - X * (X + 1) / 2};
}

void solve() {
    int64_t x, y;
    cin >> x >> y;
    if (x > y) {
        cout << x - y << '\n';
        return;
    }

    auto [sx, sy] = getCoord(x - 1);
    auto [tx, ty] = getCoord(y - 1);
    int64_t ans = 1e18;

    debug(sx, sy);
    debug(tx, ty);
    assert (sx <= tx);
    if (tx - ty >= sx - sy) {
        ans = min(ans, tx - sx + ((tx - ty) - (sx - sy)));
    }
    {
        ans = min(ans, tx - sx + 1 + (tx - ty) + (sy + 1));
    }

    cout << ans << '\n';
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    for (int x = 0; x <= 20; x++) {
        auto [X, Y] = getCoord(x);
        debug(X, Y, x + 1);
    }

    // int last = -1;
    // for (int i = 1; i <= 200; i++) {
    //     int z = floor(sqrt(i * 2) + 1.5);
    //     if (z != last)
    //         cout << '\n';
    //     cout << i << ' ';
    //     last = z;
    // }

    int T;
    cin >> T;
    while (T--)
        solve();

    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3628kb

input:

2
5 1
1 5

output:

4
3

result:

ok 2 number(s): "4 3"

Test #2:

score: 0
Accepted
time: 30ms
memory: 3468kb

input:

100000
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
1 26
1 27
1 28
1 29
1 30
1 31
1 32
1 33
1 34
1 35
1 36
1 37
1 38
1 39
1 40
1 41
1 42
1 43
1 44
1 45
1 46
1 47
1 48
1 49
1 50
1 51
1 52
1 53
1 54
1 55
1 56
1 57
1 58
1 59
1 60
1 ...

output:

0
2
1
4
3
2
6
5
4
3
8
7
6
5
4
10
9
8
7
6
5
12
11
10
9
8
7
6
14
13
12
11
10
9
8
7
16
15
14
13
12
11
10
9
8
18
17
16
15
14
13
12
11
10
9
20
19
18
17
16
15
14
13
12
11
10
22
21
20
19
18
17
16
15
14
13
12
11
24
23
22
21
20
19
18
17
16
15
14
13
12
26
25
24
23
22
21
20
19
18
1
0
2
2
1
3
4
3
2
4
6
5
4
3
5
...

result:

ok 100000 numbers