-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA_Text_Volume.cpp
81 lines (78 loc) · 1.76 KB
/
A_Text_Volume.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
const long double EPS = 1e-14;
const int maxn = 100500;
const int mod = 1e9 + 7;
int sum(int a, int n, int r) {
return n * (2 * a + (n - 1) * r) / 2;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int tc;
cin >> tc;
for(int T = 1; T <= tc; ++ T){
int l, r;
cin >> l >> r;
if(l < r) {
int diff = r - l, ans = 0, v = 0;
int lo = 0, hi = 1e9;
while(lo < hi) {
int mid = (lo + hi + 1) / 2;
if(sum(1, mid, 1) < diff) lo = mid;
else hi = mid - 1;
}
r -= sum(1, lo, 1);
ans = v = lo;
v++;
lo = 0, hi = 1e9;
while(lo < hi) {
int mid = (lo + hi + 1) / 2;
if(sum(v, mid, 2) <= r) lo = mid;
else hi = mid - 1;
}
ans += lo;
r -= sum(v, lo, 2);
lo = 0, hi = 1e9;
while(lo < hi) {
int mid = (lo + hi + 1) / 2;
if(sum(v + 1, mid, 2) <= l) lo = mid;
else hi = mid - 1;
}
ans += lo;
l -= sum(v + 1, lo, 2);
cout << "Case #" << T << ": " << ans << " " << l << " " << r << '\n';
} else {
int diff = l - r, ans = 0, v = 0;
int lo = 0, hi = 1e9;
while(lo < hi) {
int mid = (lo + hi + 1) / 2;
if(sum(1, mid, 1) <= diff) lo = mid;
else hi = mid - 1;
}
l -= sum(1, lo, 1);
ans = v = lo;
v++;
lo = 0, hi = 1e9;
while(lo < hi) {
int mid = (lo + hi + 1) / 2;
if(sum(v, mid, 2) <= l) lo = mid;
else hi = mid - 1;
}
ans += lo;
l -= sum(v, lo, 2);
lo = 0, hi = 1e9;
while(lo < hi) {
int mid = (lo + hi + 1) / 2;
if(sum(v + 1, mid, 2) <= r) lo = mid;
else hi = mid - 1;
}
ans += lo;
r -= sum(v + 1, lo, 2);
cout << "Case #" << T << ": " << ans << " " << l << " " << r << '\n';
}
}
}