QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#183398#6744. SquareBoaHancockAC ✓53ms3796kbC++147.8kb2023-09-19 14:43:562023-09-19 14:43:57

Judging History

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

  • [2023-09-19 14:43:57]
  • 评测
  • 测评结果:AC
  • 用时:53ms
  • 内存:3796kb
  • [2023-09-19 14:43:56]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long
class fastIO {
private:
    char ibuf[50007], *p1 = ibuf, *p2 = ibuf, obuf[50007], *p3 = obuf, sta[50];
    bool file_end = false;
    char get() {
        return p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, 50007, stdin), p1 == p2) ? (file_end = true), char(EOF): *p1++;
    }
    void put(const char x) {
        p3 - obuf < 50007 ? *p3 ++ = x : (fwrite(obuf, p3 - obuf, 1, stdout), p3 = obuf, *p3++ = x);
    }
public:
    explicit operator bool() { return !file_end; }
    size_t flush() {
        size_t f = fwrite(obuf, p3 - obuf, 1, stdout);
        p3 = obuf;
        *p3 = 0;
        return f;
    }
    fastIO &operator>>(char &t) {
        for (t = get(); !isgraph(t); t = get());
        return *this;
    }
    template<typename any>
    typename enable_if<is_same<any, char>::value, any>::type tpval() {
        char t;
        for (t = get(); !isgraph(t); t = get());
        return t;
    }
    fastIO &operator>>(char *t) {
        char c;
        for (c = get(); !isgraph(c); c = get());
        for (; isgraph(c); c = get())*t = c, t++;
        *t = 0;
        return *this;
    }
    fastIO &operator>>(string &t) {
        t.clear();
        char c;
        for (c = get(); !isgraph(c); c = get());
        for (; isgraph(c); c = get())t += c;
        return *this;
    }
    template<typename any>
    typename enable_if<is_same<any, string>::value, any>::type tpval() {
        string t;
        char c;
        for (c = get(); !isgraph(c); c = get());
        for (; isgraph(c); c = get())t += c;
        return t;
    }
    template<typename any>
    typename enable_if<
            (is_signed<any>::value && is_integral<any>::value && !is_same<any, char>::value) ||
            is_same<any, __int128_t>::value, fastIO>::type &operator>>(any &t) {
        t = 0;
        bool y = 0;
        char c = get();
        for (; !isdigit(c); c = get())if (c == 45)y = true;
        for (; isdigit(c); c = get())t = t * 10 + c - 48;
        if (y == 1)t = -t;
        return *this;
    }
    template<typename any>
    typename enable_if<
            (is_signed<any>::value && is_integral<any>::value && !is_same<any, char>::value) ||
            is_same<any, __int128_t>::value, any>::type tpval() {
        any t = 0;
        bool y = 0;
        char c = get();
        for (; !isdigit(c); c = get())if (c == 45)y = true;
        for (; isdigit(c); c = get())t = t * 10 + c - 48;
        if (y == 1)t = -t;
        return t;
    }
    template<typename any>
    typename enable_if<
            (is_unsigned<any>::value && is_integral<any>::value && !is_same<any, char>::value) ||
            is_same<any, __uint128_t>::value, fastIO>::type &operator>>(any &t) {
        t = 0;
        char c = get();
        for (; !isdigit(c); c = get());
        for (; isdigit(c); c = get())t = t * 10 + c - 48;
        return *this;
    }
    template<typename any>
    typename enable_if<
            (is_unsigned<any>::value && is_integral<any>::value && !is_same<any, char>::value) ||
            is_same<any, __uint128_t>::value, any>::type tpval() {
        any t = 0;
        char c = get();
        for (; !isdigit(c); c = get());
        for (; isdigit(c); c = get())t = t * 10 + c - 48;
        return t;
    }
    template<typename any1, typename any2>
    fastIO &operator>>(pair<any1, any2> &t) { return *this >> t.first >> t.second; }
    template<typename any1, typename any2>
    pair<any1, any2> tpval() { return pair<any1, any2>(tpval<any1>(), tpval<any2>()); }
    template<typename any>
    fastIO &read(any &t) { return *this >> t; }
    fastIO &read(char *t) {
        char c;
        for (c = get(); !isgraph(c); c = get());
        for (; isgraph(c); c = get())*t = c, t++;
        *t = 0;
        return *this;
    }
    template<typename any, typename...args>
    fastIO &read(any &t1, args &...t2) { return (*this >> t1).read(t2...); }
    fastIO &operator<<(const char t) {
        put(t);
        return *this;
    }
    fastIO &operator<<(const char *t) {
        for (; *t; t++)put(*t);
        return *this;
    }
    fastIO &operator<<(const string &t) {
        for (const char it: t)put(it);
        return *this;
    }
    template<typename any>
    typename enable_if<
            (is_signed<any>::value && is_integral<any>::value && !is_same<any, char>::value) ||
            is_same<any, __int128_t>::value, fastIO>::type &operator<<(any t) {
        if (!t) {
            put(48);
            return *this;
        }
        int len = 0;
        if (t < 0)t = -t, put(45);
        while (t)sta[len++] = char(t % 10 + 48), t /= 10;
        while (len--)put(sta[len]);
        return *this;
    }
    template<typename any>
    typename enable_if<
            (is_unsigned<any>::value && is_integral<any>::value && !is_same<any, char>::value) ||
            is_same<any, __uint128_t>::value, fastIO>::type &operator<<(any t) {
        if (!t) {
            put(48);
            return *this;
        }
        int len = 0;
        while (t)sta[len++] = char(t % 10 + 48), t /= 10;
        while (len--)put(sta[len]);
        return *this;
    }
    template<typename any1, typename any2>
    fastIO &operator<<(const pair<any1, any2> &t) { return *this << t.first << ' ' << t.second; }
    template<typename any>
    fastIO &write(const any &t) { return *this << t; }
    template<typename any, typename...args>
    fastIO &write(const any &t1, const args &...t2) { return (*this << t1).write(t2...); }

    ~fastIO() { fwrite(obuf, p3 - obuf, 1, stdout); }
}FastIO;
#define cin FastIO
#define cout FastIO
int f(int x) {
    return x + (int)round(sqrt(2 * x) + 1);
}
bool check(int x, int y, int n) {
    int x1 = f(x);
    int d = x1 - x - 1;
    int a0 = x - d;
    int cnt = a0 + d * (n + 1) + (1 + n) * (n) / 2;
    if(cnt >= y) return true;
    else {
        return false;
    }
}
int cal(int x, int y, int n) {
    int x1 = f(x);
    int d = x1 - x - 1;
    int a0 = x - d;
    int cnt = a0 + d * (n + 1) + (1 + n) * (n) / 2;
    return cnt;
}
int x, y;
bool check1(int n) {
    int r = f(n) - n;
    if (r >= f(x) - x - 1) {
        return true;
    }
    return false;
}

bool check2(int n) {
    int r = f(n) - n;
    if (r >= f(x) - x) {
        return false;
    }
    return true;
}
void solve() {
    cin >> x >> y;
    if(x >= y) {
        cout << x - y << '\n';
    }
    else {
        int res = 1e18;
        int l = 0, r = 2e9;
        while(l < r) {
            int mid = (l + r) >> 1;
            if(check(x, y, mid)) {
                r = mid;
            }
            else {
                l = mid + 1;
            }
        }
        int sum1 = l + cal(x, y, l) - y;
        l = 0, r = x - 1;
        while (r > l) {
            int mid = (l + r) / 2;
            if (check1(mid)) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        r = x - 1;
        while (r > l) {
            int mid = (l + r + 1) / 2;
            if (check2(mid)) {
                l = mid;
            } else {
                r = mid - 1;
            }
        }
        int d = x - l;
        x = l;
        l = 0, r = 2e9;
        while(l < r) {
            int mid = (l + r) >> 1;
            if(check(x, y, mid)) {
                r = mid;
            }
            else {
                l = mid + 1;
            }
        }
        res = min(sum1, d + l + cal(x, y, l) - y);
        cout << res << '\n';
    }
}
int32_t main() {
#ifdef ONLINE_JUDGE
#else
    freopen("FuDiWeiU.in", "r", stdin);
    freopen("FuDiWeiU.out", "w", stdout);
#endif
    int T = 1;
    cin >> T;
    while(T --) {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3644kb

input:

2
5 1
1 5

output:

4
3

result:

ok 2 number(s): "4 3"

Test #2:

score: 0
Accepted
time: 53ms
memory: 3796kb

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