AtCoder Regular Contest 155

B - Abs Abs Function

https://atcoder.jp/contests/arc155/tasks/arc155_b

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 500 points

Problem Statement

For a set S of pairs of non-negative integers, and a non-negative integer x, let fS(x) defined as fS(x)=min(a,b)S||xa|b|.

We have a set T of pairs of non-negative integers. Initially, T={(A,B)}.

Process Q queries. The i-th query gives you three non-negative integers ti, ai, and bi, and asks you to do the following.

  • If ti=1, add to T the pair (ai,bi) of non-negative integers.
  • If ti=2, print the minimum value of fT(x) for a non-negative integer x such that aixbi.

Constraints

  • 1Q2×105
  • 0A,B109
  • ti is 1 or 2.
  • 0ai,bi109
  • If ti=2, then aibi.
  • There is at least one query with ti=2.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

Q A B
t1 a1 b1
t2 a2 b2

tQ aQ bQ

Output

For each query with ti=2 in the given order, print the answer in its own line.

Sample Input 1

4 0 5
1 3 11
2 7 8
1 8 2
2 8 9

Sample Output 1

2
1

In the second query, T={(0,5),(3,11)}. For x=7, we have fT(7)=min{||70|5|,||73|11|}=min{2,7}=2. Similarly, fT(8)=3. Thus, the answer is min{2,3}=2.

In the fourth query, T={(0,5),(3,11),(8,2)}. In 8x9, fT(x) takes the minimum value fT(9)=1 at x=9.

Sample Input 2

2 1 2
1 2 3
2 2 6

Sample Output 2

0

Sample Input 3

20 795629912 123625148
2 860243184 892786970
2 645778367 668513124
1 531411849 174630323
1 635062977 195695960
2 382061637 411843651
1 585964296 589553566
1 310118888 68936560
1 525351160 858166280
2 395304415 429823333
2 583145399 703645715
2 97768492 218377432
1 707220749 459967102
1 210842017 363390878
2 489541834 553583525
2 731279777 811513313
1 549864943 493384741
1 815378318 826084592
2 369622093 374205455
1 78240781 821999998
2 241667193 243982581

Sample Output 3

26468090
3491640
25280111
9543684
0
22804896
20649370
19245624
4849993
484865

题解

核心思路

将函数 p(x):=||xa|b| 做变形:

p(x)=||xa|b|=min{|x(a+b)|,|x(ab)|}

函数图像如下:

通过变形,该函数的最小值点就很明确了,就是 a+bab,最小值为 0.

完整思路

我们将每对 (a,b) 对应的函数 p(x) 的最小值点记录下来,存在集合 S 中。即把 a+bab 储存在 S 内。

当要输出答案时,x 需要满足 axb

  • tS,atb,那么 xt 即可令某一个 ||xa|b|=0,取最小值后 fS(x)=0
  • tS,t<a  t>b,那么 x 取距离 a 或距离 b 最近的一个 t 即可令 ||xa|b| 最小,fS(x)=min{at1,t2b}

代码

#include <bits/stdc++.h>
#define endl '\n'
#define int long long

using namespace std;

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int Q, A, B;
    cin >> Q >> A >> B;
    set<int> s;
    s.insert(A + B);
    s.insert(A - B);
    while (Q--)
    {
        int t, a, b;
        cin >> t >> a >> b;
        if (t == 1)
        {
            s.insert(a + b);
            s.insert(a - b);
        }
        else
        {
            auto it = s.lower_bound(a);
            if (it == s.end())
                cout << a - *s.rbegin() << endl;
            else if (*it <= b)
                cout << 0 << endl;
            else if (it == s.begin())
                cout << *it - b << endl;
            else
                cout << min(*it - b, a - *prev(it)) << endl;
        }
    }
    return 0;
}