QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#129788#6744. SquareUndertrainedOverpressure#AC ✓30ms3472kbC++204.7kb2023-07-22 23:58:302023-07-22 23:58:31

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-22 23:58:31]
  • 评测
  • 测评结果:AC
  • 用时:30ms
  • 内存:3472kb
  • [2023-07-22 23:58:30]
  • 提交

answer

/*
    author:  Maksim1744
    created: 22.07.2023 18:01:43
*/

#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;

#define mp   make_pair
#define pb   push_back
#define eb   emplace_back

#define sum(a)     ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a)    (*min_element((a).begin(), (a).end()))
#define maxe(a)    (*max_element((a).begin(), (a).end()))
#define mini(a)    ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a)    ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

template<typename T>             vector<T>& operator--            (vector<T> &v){for (auto& i : v) --i;            return  v;}
template<typename T>             vector<T>& operator++            (vector<T> &v){for (auto& i : v) ++i;            return  v;}
template<typename T>             istream& operator>>(istream& is,  vector<T> &v){for (auto& i : v) is >> i;        return is;}
template<typename T>             ostream& operator<<(ostream& os,  vector<T>  v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> pair<T,U>& operator--           (pair<T, U> &p){--p.first; --p.second;            return  p;}
template<typename T, typename U> pair<T,U>& operator++           (pair<T, U> &p){++p.first; ++p.second;            return  p;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U> &p){is >> p.first >> p.second;        return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U>  p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}

#ifdef HOME
#define SHOW_COLORS
#include "/mnt/c/Libs/tools/print.cpp"
#else
#define show(...) void(0)
#define debugf(fun)   fun
#define debugv(var)   var
#define mclock    void(0)
#define shows     void(0)
#define debug  if (false)
#define OSTREAM(...)    ;
#define OSTREAM0(...)   ;
#endif

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);

    auto msqrt = [&](ll x) -> ll {
        ll s = max(0ll, (ll)sqrt(x) - 3);
        while ((s + 1) * (s + 1) <= x) ++s;
        return s;
    };

    auto step = [&](ll x) {
        // floor(sqrt(2x) + 1.5)
        return (msqrt(x * 8) + 3) / 2;
    };

    auto mmul = [&](ll a, ll b) -> ll {
        if ((double)a * b > 6e18) return 6e18;
        return a * b;
    };

    auto jump = [&](ll x, ll cnt) {
        ll s = step(x);
        return x + mmul(cnt, s + s + cnt - 1) / 2;
    };

    auto prev_crit = [&](ll x) {
        assert(x >= 0);
        // max n(n+1)/2 + 1 <= x
        ll n = sqrt(x * 2) + 5;
        while (n * (n + 1) / 2 + 1 > x) --n;
        return n * (n + 1) / 2 + 1;
    };

    int t;
    cin >> t;
    while (t--) {
        ll x, y;
        cin >> x >> y;
        if (x >= y) {
            cout << x - y << '\n';
            continue;
        }
        ll R = 1;
        while (jump(x, R) < y) R *= 2;
        ll L = 0;
        while (R - L > 1) {
            ll C = (L + R) / 2;
            if (jump(x, C) < y) L = C;
            else R = C;
        }


        ll steps = R;
        ll ans = 0;
        while (true) {
            ll diff = jump(x, steps) - y;
            show(x, steps, diff, ans);
            if (diff == 0) break;
            assert(diff > 0);
            ll prev = prev_crit(x);
            show(prev);
            if (diff <= x - prev) {
                x -= diff;
                ans += diff;
                break;
            }
            ans += x - prev;
            x = prev;
            diff = jump(x, steps) - y;
            show(steps, diff, ans);
            if (diff == 0) break;
            if (steps + 1 <= diff) {
                --x;
                ++ans;
                continue;
            }
            ans += diff;
            break;
        }

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

    // for (ll x = 1; x <= 1e3; ++x) {
    //     // if (step(x + step(x)) != step(x) + 1)
    //     //     cerr << x << ' ' << step(x) << ' ' << step(x + step(x)) << endl;
    //     // if (x + step(x) + 1 != x + 1 + step(x + 1))
    //     //     cerr << x + 1 << endl;
    //     cerr << x << ' ' << '\t' << step(x) << endl;
    // }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
5 1
1 5

output:

4
3

result:

ok 2 number(s): "4 3"

Test #2:

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

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