CF1810D.Climbing the Tree

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

The snails are climbing a tree. The tree height is hh meters, and snails start at position 00.

Each snail has two attributes aa and bb (a>ba \gt b). Starting from the 11-st day, one snail climbs the tree like this: during the daylight hours of the day, he climbs up aa meters; during the night, the snail rests, and he slides down bb meters. If on the nn-th day, the snail reaches position hh for the first time (that is, the top of the tree), he will finish climbing, and we say that the snail spends nn days climbing the tree. Note that on the last day of climbing, the snail doesn't necessarily climb up aa meters, in case his distance to the top is smaller than aa.

Unfortunately, you don't know the exact tree height hh at first, but you know that hh is a positive integer. There are qq events of two kinds.

  • Event of type 11: a snail with attributes aa, bb comes and claims that he spent nn days climbing the tree. If this message contradicts previously adopted information (i. e. there is no tree for which all previously adopted statements and this one are true), ignore it. Otherwise, adopt it.
  • Event of type 22: a snail with attributes aa, bb comes and asks you how many days he will spend if he climbs the tree. You can only give the answer based on the information you have adopted so far. If you cannot determine the answer precisely, report that.

You need to deal with all the events in order.

蜗牛正在爬一棵树。树的高度为 hh 米,蜗牛从位置 00 开始爬行。

每只蜗牛有两个属性 aa 和 bb(满足 a>ba > b)。从第 11 天起,一只蜗牛按如下方式爬树:在当天的白天,它向上爬 aa 米;在当夜,它休息并下滑 bb 米。若在第 nn 天,该蜗牛首次到达位置 hh(即树顶),则它完成爬树过程,此时称该蜗牛共花费了 nn 天爬树。注意:在爬树的最后一天,蜗牛不一定恰好向上爬 aa 米——若其与树顶的距离小于 aa,则只需爬完剩余距离即可。

不幸的是,你最初并不知道树的确切高度 hh,但你知道 hh 是一个正整数。接下来将发生 qq 个事件,分为两类:

  • 类型 11 的事件:一只属性为 aa、bb 的蜗牛到来,并声称它爬树共花费了 nn 天。若该声明与此前已采纳的所有信息矛盾(即:不存在任何满足所有已采纳陈述及该新陈述的树高 hh),则忽略该事件;否则,采纳该陈述。
  • 类型 22 的事件:一只属性为 aa、bb 的蜗牛到来,并询问:若它爬这棵树,将花费多少天?你只能依据目前已采纳的信息作答。若无法精确确定答案,则报告无法确定。

你需要按顺序处理全部 qq 个事件。

输入格式

Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases. Then follows their description.

The first line of each test case contains one integer qq (1≤q≤2⋅1051\le q \le 2\cdot 10^5) — the number of events.

For the following qq lines, the first integer of each line is either 11 or 22, denoting the event type.

If the event type is 11, then three integers aa, bb, and nn (1≤a,b,n≤1091\le a,b,n \le 10^9, a>ba \gt b) follow.

If the event type is 22, then two integers aa and bb (1≤a,b≤1091\le a,b \le 10^9, a>ba \gt b) follow.

It is guaranteed that the sum of qq over all test cases does not exceed 2⋅1052\cdot 10^5.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 qq(1≤q≤2⋅1051\le q \le 2\cdot 10^5),表示事件数量。

接下来的 qq 行中,每行的第一个整数为 11 或 22,表示事件类型。

若事件类型为 11,则随后跟三个整数 aa、bb 和 nn(1≤a,b,n≤1091\le a,b,n \le 10^9,且 a>ba \gt b)。

若事件类型为 22,则随后跟两个整数 aa 和 bb(1≤a,b≤1091\le a,b \le 10^9,且 a>ba \gt b)。

保证所有测试用例的 qq 值之和不超过 2⋅1052\cdot 10^5。

输出格式

For each test case, output qq integers in one line, one for each event, in order. Specifically,

  • for each event of type 11, if you adopt the message, output 11; if you ignore it, output 00;
  • for each event of type 22, output an integer denoting the number of days that the snail will spend. If you cannot determine it, output −1-1.

对于每个测试用例,在一行中输出 qq 个整数,依次对应每个事件。具体而言:

  • 对于每个类型为 11 的事件,若你采纳该消息,则输出 11;若忽略该消息,则输出 00;
  • 对于每个类型为 22 的事件,输出一个整数,表示蜗牛将花费的天数;若无法确定该天数,则输出 −1-1。

输入输出样例

  • 输入#1

    5
    3
    1 3 2 5
    2 4 1
    2 3 2
    3
    1 6 5 1
    2 3 1
    2 6 2
    3
    1 4 2 2
    1 2 1 3
    2 10 2
    9
    1 7 3 6
    1 2 1 8
    2 5 1
    1 10 9 7
    1 8 1 2
    1 10 5 8
    1 10 7 7
    2 7 4
    1 9 4 2
    9
    1 2 1 6
    1 8 5 6
    1 4 2 7
    2 9 1
    1 5 1 4
    1 5 2 7
    1 7 1 9
    1 9 1 4
    2 10 8

    输出#1

    1 2 5
    1 -1 1
    1 0 1
    1 0 -1 0 0 0 1 8 0
    1 0 0 1 0 0 0 0 1

说明/提示

In the first test case, we can determine h=7h=7 through the first message, so we know the second snail and the third snail need to spend 22 and 55 days respectively to reach the top.

Let's show how the second snail climbs:

  • During the daylight hours of the 11st day: climbs up 44 meters, now at position 44.
  • During the night of the 11st day: slides down 11 meters, now at position 33.
  • During the daylight hours of the 22nd day: climbs up 44 meters, now at position 77 (reaches the top).

In the third test case, the second snail's message contradicts the first snail's, because the second snail says he spent 33 days, and he can climb at most 1+1+2=41+1+2=4 meters in the first 33 days. However, the first snail only needs 11 day to climb 44 meters.

在第一个测试用例中,我们可以通过第一条消息确定 h=7h=7,因此我们知道第二只蜗牛和第三只蜗牛分别需要 22 天和 55 天才能到达顶端。

下面我们展示第二只蜗牛的攀爬过程:

  • 第 11 天白天:向上爬 44 米,此时位于位置 44。
  • 第 11 天夜晚:向下滑 11 米,此时位于位置 33。
  • 第 22 天白天:向上爬 44 米,此时位于位置 77(到达顶端)。

在第三个测试用例中,第二只蜗牛的消息与第一只蜗牛的消息矛盾,因为第二只蜗牛声称自己花费了 33 天,而它在前 33 天最多只能爬升 1+1+2=41+1+2=4 米。然而,第一只蜗牛仅需 11 天就能爬升 44 米。

输入解题思路,AI测评打分。不知道怎么写?

首页