Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs.
Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.
For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.
Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
Input
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105).
Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
Output
For each query, print its answer in a single line.
Examples
Input
2 1 4
1 5 3
3 3 10
7 10 2
6 4 8
Output
4
-1
8
-1
Input
1 5 2
1 5 10
2 7 4
Output
1
2
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are given a sequence s_i = A + (i-1)*B, with A,B positive integers up to 1e6.
We have queries: each query gives l, t, m. We need to find the largest r >= l such that the subarray [l, r] can be "eaten" by performing m-bite operation no more than t times.
Definition: An m-bite operation: we can decrease the height of at most m distinct not eaten Karafses (i.e., those with current height > 0) by 1 each. So each operation reduces the total sum of heights by at most m, but also we can only reduce each Karafs by at most 1 per operation. However, we can repeat operations over time. The condition: we want to reduce all heights to zero. The process: we have a multiset of positive integers (initial heights). In each operation (called m-bite), we choose up to m distinct items that are still positive and decrease each by 1. We are allowed at most t operations. Is it possible to reduce all to zero? That's the feasibility condition for a given set of heights.
We need to find the maximal r such that the sequence from l to r inclusive can be fully eaten within t operations.
We need to answer many queries (n up to 1e5). A, B up to 1e6, l, t, m up to 1e6.
We must derive a condition for feasibility.
Let’s formalize:
We have heights: a_1, a_2, ..., a_k (k = r-l+1). Initially s_i. We have to apply at most t operations. In each operation, we can subtract 1 from up to m distinct items. The operations are independent; we can choose any subset each time, as long as each item never goes negative (i.e., we cannot subtract from a zero Karafs because it's eaten, but we can avoid it). The goal is to reduce all to zero.
This is reminiscent of scheduling: we have to cover each height with a number of "bites" equal to its initial height. Each bite corresponds to subtracting 1 from that Karafs in some operation. The constraint: each operation can involve at most m Karafses (distinct). So effectively, we have t time slots (operations), each slot can accommodate up to m unit-decrements on distinct items. So we need to assign each unit of height (each "bite" needed) to one of the t operation slots, such that for each slot, at most m distinct items are assigned (since we can only apply one decrement per item per operation? Actually each operation we can decrement up to m distinct items by 1 each, so per operation, each item can be decremented at most once (since we can't decrement the same item twice in the same operation, as it says "at most m distinct not eaten Karafses"). So each operation gives us the ability to assign a "decrement" to at most m distinct items. Over t operations, an item can be decremented multiple times, but each time in a different operation. So total number of decrements we can assign is at most m * t? Actually careful: If each operation we can choose up to m items, then total number of decrements (unit reductions) we can apply across all operations is at most m * t. However, there is also a constraint per item: it cannot be decremented more than its height, but that's fine. So a necessary condition is sum_{i=l}^{r} s_i <= m * t? But is that sufficient? Not necessarily because of the per-operation distinctness constraint: we cannot apply more than one decrement to the same item in a single operation. But we can spread decrements across operations. So as long as we have enough total capacity (mt) and also the maximum height among the items is <= t (since an item can be decremented at most once per operation, so it needs at least its height number of operations to be fully eaten; i.e., t must be at least max s_i). Actually, you need to apply as many decrements to a single item as its height; each operation can decrement that item at most once, so you need at least s_i operations to reduce it to zero. So t >= max_{i=l..r} s_i. Also total sum condition: sum s_i <= m * t? Let's test: Suppose m=1, then we have one item per operation. Then the condition is sum s_i <= t? Actually, if m=1, each operation can only decrement one Karafs by 1. To eat all, we need total number of decrements = sum s_i <= t (since each operation yields one decrement). And also t >= max s_i, but that's implied if sum condition holds? Not necessarily: If one item has height > t, we can't reduce it to zero because even if we decrement it each operation, after t ops it still >0. So we need both: max s_i <= t and sum s_i <= mt. But are they sufficient? Let's consider a more complex scenario: The per-operation constraint of at most m distinct items might impose additional restrictions when some items require many decrements. But think of it as a scheduling problem: We have t time steps; at each time step we can select up to m items to decrement. This is exactly like a machine with m parallel processors? But here each item can be processed at most once per time step; we have to process each item for its height number of time steps. This is like a scheduling on m identical machines (or a multiprocessor) where each job (Karafs) has processing time equal to its height, and we have t time units. However, note that we can only assign at most one unit of processing to each job per time unit (since each operation can decrement each Karafs at most once). This is exactly like scheduling on m identical machines, with the additional twist that jobs can be preempted arbitrarily (since each operation we choose which to work on). So the condition for feasibility (existence of a schedule that completes all jobs within t time steps on m machines) is:
The total work (sum of processing times) <= m * t.
No single job's processing time > t.
These are well-known necessary and sufficient conditions for preemptive scheduling on identical machines (McNaughton's algorithm). Indeed, with preemptive scheduling on m identical machines, a set of jobs can be scheduled within a makespan T iff sum p_i <= m*T and max p_i <= T. Here T = t, m = number of machines. So indeed the conditions are both necessary and sufficient. The process described matches exactly: each operation (time step) we can work on up to m distinct jobs, each one gets one unit of processing. That's precisely m machines with preemptive scheduling. So the feasibility condition is:
Given a set of heights a_i = s_i, let T = t, M = m.
We require: (1) max(a_i) <= T. (2) sum(a_i) <= M * T.
Thus for a segment [l, r] we need to check these two conditions. And we need the largest r such that both hold.
Now s_i is arithmetic progression: s_i = A + (i-1)*B. So for a segment from l to r (both inclusive), the max is s_r (since it's increasing if B>0; B >=1, so strictly increasing). So condition (1): s_r <= t.
Condition (2): sum_{i=l}^{r} s_i <= m * t.
Sum of arithmetic progression: sum = (r-l+1)*(s_l + s_r)/2 = number_of_terms * (first + last)/2.
Let k = r-l+1. Then s_l = A + (l-1)B, s_r = A + (r-1)B = s_l + (k-1)B.
Sum = k * (2s_l + (k-1)B)/2 = (k/2)(2*s_l + (k-1)B).
We need: k * (2*s_l + (k-1)B) / 2 <= m * t.
Also s_r = s_l + (k-1)B <= t.
So for each query (l, t, m), we need to find the maximum k >= 1 such that both inequalities hold. Then r = l + k - 1.
If no k >= 1 satisfies, answer -1.
However, there is also the possibility that the segment may be empty? But r must be >= l, so at least one element. So we need at least k=1.
Now constraints: A, B up to 1e6, l, t, m up to 1e6, n up to 1e5.
We need to answer quickly per query.
We can treat k as variable. The conditions are:
(1) s_l + (k-1)B <= t -> (k-1) <= (t - s_l)/B -> k <= floor((t - s_l)/B) + 1.
Let max_k1 = floor((t - s_l)/B) + 1, if t >= s_l; else no solution.
(2) sum condition: k*(2s_l + (k-1)B) <= 2m*t.
This is quadratic in k: Bk^2 + (2s_l - B)k - 2m*t <= 0? Let's derive:
sum = k/2 * (2s_l + (k-1)B) = k/2 * (2s_l + Bk - B) = (1/2)(2s_lk + Bk^2 - Bk) = (1/2)(Bk^2 + (2*s_l - B)*k).
So we have quadratic inequality: B k^2 + (2s_l - B) k - 2m*t <= 0.
Since B>0, left side is a convex parabola. For k >=1 integer, the condition holds for k up to some maximal root.
We can solve for the positive root: k_max2 = floor( ( - (2s_l - B) + sqrt( (2s_l - B)^2 + 8 B m t ) ) / (2 B ) ). Actually using quadratic formula: k = [ -(2s_l - B) ± sqrt( (2s_l - B)^2 + 8 B m t ) ] / (2B). Since sqrt term > |2*s_l - B|, the positive root is with +. So:
k_max2 = floor( ( - (2s_l - B) + sqrt( (2s_l - B)^2 + 8 B m t ) ) / (2 B) ).
But careful: Our inequality is <=0, so k must be between the two roots (one negative, one positive). So valid k <= the positive root. So the integer maximal k satisfying is floor of the positive root.
Thus the answer k = min(max_k1, max_k2) provided that both are >=1. If this min is >=1 then r = l + k - 1. Else -1.
But we need to confirm the accuracy: The sum inequality uses the arithmetic sum formula. However, note that heights are integers and the m-bite operations may need to be integer. But the scheduling conditions are sufficient with preemption; the operations correspond exactly to time slots, and preemptive schedule exists as long as the conditions hold (McNaughton). But is there any nuance about the fact that we cannot split a unit of decrement? Actually, each operation decrements by exactly 1 per selected Karafs. So it's discrete time, but preemptive scheduling on unit-time slots is exactly the same; you can assign a job to a processor at each time slot. So it's fine.
Thus answer per query can be computed in O(1) using integer arithmetic, but careful with overflow because values can be large: l up to 1e6, but k could be large? Actually s_i grows linearly; t and m up to 1e6, but note that the condition sum <= mt bounds k. The sum condition limits k roughly to about sqrt(2 m t / B) perhaps, but could still be up to? Let's estimate worst-case: A,B can be 1, m t up to 1e12 (since m and t up to 1e6 each). So mt can be up to 1e12. s_l is up to A+(l-1)B, but l can be up to 1e6, so s_l up to about 1e6+1e6*1e6? Actually B up to 1e6, l up to 1e6 => s_l <= 1e6 + (1e6-1)*1e6 ≈ 1e12. But t also up to 1e6; condition (1) s_r <= t so s_r cannot exceed t <= 1e6. So actually condition (1) severely restricts r when t is small. But t can be up to 1e6, so s_r <= 1e6. Since s_i = A + (i-1)B, and B>=1, the index i such that s_i <= 1e6 is limited. For B=1, maximum i such that A+(i-1)*1 <= 1e6 gives i <= 1e6 - A +1 ~ 1e6. So r could be up to 1e6. For B large, i small. So overall r will not exceed about 1e6 (since s_i integer and t <= 1e6). Actually l can be up to 1e6, but r might be larger than 1e6? Because if l is high, s_l may already exceed t, then no solution. So feasible r is bounded by max index where s_i <= 1e6. That index is at most (t - A)/B + 1. Since t <= 1e6, and A>=1, B>=1, max index is at most t (if B=1) => up to 1e6. So r is at most about 1e6. So k is also at most about 1e6. So 64-bit integers are fine.
But we need to compute sqrt of a potentially large number: (2s_l - B)^2 + 8 B m t. s_l can be up to ~1e12? Actually if l=1e6, B=1e6 => s_l = A+(1e6-1)1e6 ≈ 1e12. So (2s_l)^2 ~ 4e24, which is beyond 64-bit (2^63 ~ 9.22e18). So we have overflow issues. But note that if s_l is large, condition (1) will fail because t is at most 1e6. So for the query to have any solution, s_l must be <= t. Because the first element must be eatable: s_l <= t. So for any feasible query, s_l <= t <= 1e6. Then s_l is at most 1e6. So in the feasible range, s_l is bounded by 1e6. But wait: l can be up to 1e6, but if B is large, s_l may exceed t, so answer is -1, and we don't need to compute sqrt. So we can first check condition (1) for the first element: if s_l > t, answer -1 directly. So for queries that might have answer, s_l <= t, so s_l <= 1e6. Then (2s_l - B)^2 <= (21e6)^2 = 4e12, still fits in 64-bit. 8 B m t: B up to 1e6, m up to 1e6, t up to 1e6 => product up to 8e18, which is near the limit of 64-bit (9e18). So 8 B m t can be up to 8e18, which fits within signed 64-bit? 2^63-1 = 9.22e18, so 8e18 is fine. But we also add (2s_l - B)^2 (max ~4e12), so total up to ~8e18, still within 64-bit. However, if B=1e6, m=1e6, t=1e6, then 81e61e61e6 = 8e18 exactly. That's okay. So we can compute using 128-bit? But in C++ we can use unsigned long long (64-bit) which max 1.84e19 unsigned, so signed might overflow if we go over 9e18. But 8e18 is safe for signed 64-bit if we are careful; but what about (2s_l - B)^2? That is at most (21e6-1)^2 ~ 4e12, negligible. So sum <= 8e18 + 4e12 < 9e18, so safe for signed 64-bit. But wait: B could be 1e6, m=1e6, t=1e6 => 81e18 = 8e18, plus 4e12 = 8.000000004e18 < 9.22e18, so fits in signed 64-bit. However, if we use signed 64-bit, 8e18 is less than 9.22e18, so ok. But we need to be careful: 8e18 is about 8*10^18, less than 2^63-1=9.22e18. So safe. But if we use unsigned long long (0 to 1.8e19), definitely safe. So we can compute sqrt using double? But double has 53-bit mantissa (~9e15 precise integer), so for numbers up to 8e18, double might not give exact integer sqrt, but we can compute floor sqrt using integer sqrt algorithms. Better to compute sqrt using binary search or using built-in sqrtl with long double, but to avoid precision issues, we can use integer binary search on 64-bit numbers. Since n up to 1e5, performing a binary search per query for sqrt might be okay if it's O(log value) ~ O(60). But we can also compute using sqrt function with double after confirming that the error is negligible? But we need exact floor of sqrt for the inequality; if we compute with double and floor, it might be off by 1 for large numbers due to rounding. Better to use integer sqrt.
But we can also solve for k_max2 via binary search directly on k because the range of k is limited (since k_max1 <= something like (t - s_l)/B + 1 <= maybe 1e6). Actually k_max1 <= (t - s_l)/B + 1. Since s_l <= t, and B>=1, (t - s_l) <= t <= 1e6, so k_max1 <= 1e6+1. So k is at most around 1e6. So we could simply binary search on k in [1, k_max1] to find the largest k satisfying sum condition. Since per query we could do O(log 1e6) ~ 20 steps, and n=1e5, total ~2e6 operations, which is fine. That might be simpler and avoid overflow and sqrt issues. However, we need to compute sum condition quickly per k. Sum can be computed using arithmetic series, but careful about overflow: k up to 1e6, s_l up to 1e6, B up to 1e6, then product could be up to about 1e12? Actually sum = (k/2)(2s_l+(k-1)B). k up to 1e6, (2*s_l+(k-1)B) up to about 2e6 + (1e6-1)1e6 ≈ 1e12, product kthat up to 1e18, dividing by 2 yields up to 5e17. That fits 64-bit. So we can compute using 64-bit unsigned or signed.
Thus we can do for each query:
If s_l > t: print -1; continue.
Else compute max possible k from condition (1): k1_max = floor((t - s_l)/B) + 1.
Now we need to find the largest k in [1, k1_max] that satisfies sum condition: sum_{i=0}^{k-1} (s_l + iB) <= mt.
Since sum = k * s_l + B * k*(k-1)/2 = ks_l + B(k*(k-1))/2.
Alternatively, sum = (k/2)(2s_l + (k-1)B). Both equivalent.
We can compute left side with 64-bit.
We can binary search for the maximum k such that condition holds.
Because the sum is increasing in k (since all positive). So we can binary search.
Edge: k may be 0? But we need at least 1. So if even k=1 fails sum condition (i.e., s_l > mt? Actually for k=1 sum = s_l, condition: s_l <= mt, which usually holds? Not necessarily; mt could be smaller than s_l. So it might fail. So if even k=1 fails, answer -1. But note s_l <= t from condition (1) and t <= 1e6, mt could be as low as 1? Actually m>=1, t>=1, m*t can be less than s_l. So we need to check.
Now binary search over k from 1 to max_k_from_t to find max k satisfying sum <= m*t.
left = 1, right = max_k_from_t.
While left <= right:
mid = (left+right)/2
compute sum = mid * s_l + B * mid * (mid-1) / 2.
if sum <= (long long) m * t: then update ans = mid, left = mid+1
else: right = mid-1
If ans exists (>=1), r = l + ans - 1; print r.
Else print -1.
But we must also ensure that the condition max(s_i) <= t is automatically enforced by k <= max_k_from_t? Because s_r = s_l + (k-1)B <= t if and only if k <= max_k_from_t. So yes, we incorporate it.
Thus we don't need separate check for max.
Time complexity: O(n log max_k) where max_k <= 1e6, so up to 20 iterations per query, total 2e6 iterations, fine.
However, we must consider when B is large and max_k_from_t could be small (like 1). Binary search still trivial.
One nuance: (t - s_l) / B is integer division; careful with negative? But s_l <= t, so t - s_l >=0. So fine.
Also, note that the sum may overflow 64-bit if we compute mid * s_l where mid up to 1e6, s_l up to 1e6 => product up to 1e12, fine. B * mid * (mid-1)/2: B up to 1e6, mid up to 1e6 => B * mid^2 ~ 1e18, /2 still up to 5e17, within 9e18. So safe in signed 64-bit if we use long long.
But we need to compute m*t as 64-bit as well, up to 1e12, fine.
Thus this approach is straightforward.
But wait: The constraints say l, t, m up to 10^6. But we assumed s_l <= t for feasible query. If s_l > t, answer -1. So fine.
But what about the case where the segment can be eaten only if we use exactly m-bite operations? The description: "by performing m-bite no more than t times" means at most t operations. So the schedule can use fewer than t operations. The condition we used is that there exists a schedule completing within t operations. That's exactly the makespan <= t. So correct.
But need to consider: The m-bite operation says "decreasing the height of at most m distinct not eaten Karafses by 1". It does not say we must decrease exactly m each time; we can choose fewer. So yes, it matches preemptive schedule with m machines but not required to use all machines each time slot.
Thus condition is correct.
Now test with provided examples:
Example1:
A=2, B=1.
s = 2,3,4,5,6,7,8,9,10,...
Query1: l=1, t=5, m=3.
s_l=2 <=5.
max_k1 = (5-2)/1+1 = 3+1=4? Actually (5-2)=3, /1=3, +1=4. So k <=4.
Now binary search for sum <= mt = 15.
Check k=4: sum = 42 + 143/2 = 8 + 6 = 14 <=15, so okay. So ans k=4 -> r=4. Output 4. matches.
But wait: Are there any tricky aspects? Let's consider the condition sum <= mt carefully. In preemptive scheduling on m machines, makespan T, necessary and sufficient: sum p_i <= mT and max p_i <= T. However, is it always possible to schedule when these hold? Yes, by McNaughton's algorithm: you can schedule jobs preemptively on m identical machines with makespan T if those conditions hold. The algorithm is: fill machines one by one, splitting a job across machines if needed. The schedule uses time slots, each machine can process at most one job per time unit. This exactly corresponds to m-bite operations: each time unit we can process up to m jobs (one per machine). So yes.
Thus solution stands.
Now performance: For each query we binary search on k from 1 to max_k1 (<= (t - s_l)/B + 1 <= (t)/1? worst-case B=1, t=1e6, s_l minimal: l=1, A could be 1 => s_l=1, max_k1 = (1e6-1)+1 = 1e6. So binary search over 1e6 range: about 20 iterations. Each iteration does a sum computation with a few multiplications. That's fine for 1e5 queries: 2e6 iterations. Should be okay in 1-2 seconds in C++.
But we can also compute the exact k_max2 via solving quadratic, which might be even faster per query. However, we have to be careful about overflow and sqrt precision. Since n is 1e5, O(1) per query is better but not required.
But we need to compute s_l = A + (l-1)*B. That can be large up to 1e12 but if it's > t we early exit. However, we must compute s_l as long long because (l-1)*B may be up to 1e12. But if s_l > t (with t up to 1e6), we can detect that without overflow? Actually (l-1)*B can be up to 1e12, which may overflow 32-bit but we use 64-bit. Since we need to compare to t (<=1e6), we can first compute s_l using long long. If it's > 1e6, we know it's > t because t <=1e6. But we still compute it. That's fine.
Now about max_k1: (t - s_l)/B. s_l can be large, but if s_l > t, we already exit. So within feasible queries, s_l <= t <= 1e6, so s_l <= 1e6, so (t - s_l) is at most 1e6. So computing max_k1 is safe with int. But if s_l is exactly t, then max_k1 = 0+1=1. So fine.
Now compute sum: use 64-bit. mid is int up to 1e6. s_l up to 1e6? Actually s_l can be up to t (1e6). So mids_l up to 1e12. B up to 1e6, mid(mid-1) up to 1e12, product B * that up to 1e18. So use unsigned long long or long long. Better to use unsigned long long for safety. But mt up to 1e12, which fits in signed 64-bit. Comparison between unsigned long long and signed? It's okay if we cast to same type. I'll use long long and ensure no overflow: e.g., compute sum = mid * s_l + B * mid * (mid-1) / 2. The term B * mid * (mid-1) could overflow 64-bit if computed as (B * mid) * (mid-1) because Bmid up to 1e12, times (mid-1) up to 1e6 gives 1e18, still okay. But better compute as (B * mid * (mid-1)) but multiplication order might cause intermediate overflow if done as (B * mid * (mid-1)) all in 64-bit? Actually B, mid, (mid-1) are up to 1e6, product of three numbers up to 1e18, which is within 2^63-1? 1e18 is less than 9.22e18, so okay. However, we then divide by 2, but integer division may truncate. But the sum formula is exact integer. Since B * mid * (mid-1) is always even? Not necessarily. But the division yields integer because the product is divisible by 2? Actually B * mid * (mid-1) is always even because either mid or mid-1 is even. So integer division yields correct result. So we can compute sum = mid * s_l + (B * mid * (mid-1)) / 2.
But careful: B * mid * (mid-1) could be as high as 1e6 * 1e6 * 1e6 = 1e18, which fits. But mid can be up to 1e6, s_l up to 1e6, mid*s_l up to 1e12. So sum up to ~1e18. So use unsigned long long or long long.
Now we also need to consider that for mid up to 1e6, Bmid(mid-1) may be computed as (B * mid) * (mid-1). B*mid up to 1e12, times (mid-1) up to 1e6 gives 1e18. That's okay.
But to avoid any overflow, we could compute sum using double? But better to stay integer.
One more nuance: The binary search upper bound: max_k1 might be large (like 1e6). But in some queries, the sum condition might allow a larger k than max_k1? Actually max_k1 is from max height condition, so the actual feasible k cannot exceed max_k1. So the upper bound for binary search is min(max_k1, something huge). But we only set right = max_k1.
But what if max_k1 is huge due to B=0? But B is positive integer: "1 ≤ A, B ≤ 10^6" so B >=1. So fine.
Now we must consider that the answer r might be larger than 1e6? Could it be larger? t is at most 1e6, s_i = A + (i-1)B. For i such that s_i <= t, i max = floor((t-A)/B)+1. Since A >=1, t <=1e6, B>=1, i max <= t (if B=1) = 1e6. So r cannot exceed 1e6. So r fits in int.
Now regarding input size: n up to 1e5, we need fast IO.
Now we should also consider if there are any queries where s_l <= t but still no k satisfies sum condition (i.e., even k=1 fails). Then answer -1. So we need to check after binary search that ans >=1.
Edge Cases:
If B=0? Not allowed; B>=1.
A and B up to 1e6.
l up to 1e6, but could l be 0? No, l >=1.
t, m >=1.
Could m be larger than number of Karafses? In condition, the m-bite operation can affect at most m distinct Karafses. In the segment of length k, if k < m, the operation can affect at most k items. The scheduling analogy: we have m machines, but we only have k jobs; you can't use more machines than jobs; effectively the parallelism is limited by min(m, k). However, our condition sum <= mt and max <= t still holds for scheduling with m machines but with only k jobs? Let's think: In the scheduling model, we have m identical machines; we can assign at most one job to each machine per time unit. If there are fewer than m jobs, we can still use only at most one per job, so total work per time unit cannot exceed number of jobs. But our condition sum <= mt might be too optimistic if k < m? Actually, suppose we have m=100, but only 1 job with processing time p=50, and t=1. Our condition: sum=50 <= 100*1=100, max=50 <=1? Actually max=50>1, fails condition. If max<=t, then p<=t. So if p=50, t=49 fails; if t=50, then sum=50<=100, max=50<=50 holds, and schedule exists: use one machine for 50 time units; we have 100 machines but only one job, it's fine. So condition works.
But what about case where k < m, sum <= m*t, max <= t. Is schedule always possible? Yes, because we can simply assign each job to a machine for its whole duration, no problem. So condition remains correct.
Thus our approach is correct.
Now we must double-check with some custom cases.
Case: A=10, B=10, l=1, t=15, m=1. s_l=10<=15.
max_k1 = (15-10)/10+1 = 0+1=1. So only k=1. Check sum: 10 <= 1*15=15? Yes. So r=1. Good.
Case: A=1, B=1, l=1, t=3, m=2. s_l=1.
max_k1 = (3-1)/1+1 =2+1=3. So possible k up to 3.
Check sum condition:
k=3: sum = 1+2+3=6 <= 2*3=6 => ok. So r=3. Schedule: items of heights 1,2,3. With 2 machines, makespan 3, can we schedule? Use McNaughton: total work 6, 2 machines => average load 3. Max job 3 <=3. Yes. So r=3.
Case: A=1, B=1, l=1, t=3, m=1. s_l=1.
max_k1 = 3.
sum condition: for k=3 sum=6 > 1*3=3 -> fails. k=2 sum=3 <=3? sum=1+2=3 <=3, ok. So r=2. Indeed with one machine, we need total work = sum, so sum <= t. So correct.
Case: A=100, B=1, l=1, t=100, m=100. s_l=100.
max_k1 = (100-100)/1+1=1. So only k=1. sum=100 <= 100*100=10000, ok. r=1. Good.
Case: A=1, B=2, l=1, t=5, m=2. s_l=1.
max_k1 = (5-1)/2+1 = 4/2=2+1=3. So possible k up to 3. s: 1,3,5. sum = 1+3+5=9. m*t=10, sum <=10. max =5 <=5. So r=3. Check schedule: heights 1,3,5, m=2, t=5. Can we schedule? Let's try: We need to process job1:1, job2:3, job3:5. On 2 machines over 5 time units. Total work 9, capacity 10. Max job 5 <=5. Feasible. e.g., machine1: job3 for all 5 units; machine2: job1 for 1 unit, job2 for 3 units, idle 1 unit. That works. So r=3.
Now test edge: s_l <= t, but k=1 fails sum? For example, A=100, B=1, l=1, t=100, m=1. s_l=100, max_k1=1, sum=100 <= 1100=100? Actually 100 <=100, ok. If m=0? m>=1. So failure unlikely? But could be if m=1, t=99, s_l=100? s_l > t then no solution. So only if s_l <= t but sum condition fails for k=1 would be when s_l > mt. Since s_l <= t, and m could be 1, then mt = t, so s_l > t? cannot happen because s_l <= t. So for k=1, sum condition is s_l <= mt. Since s_l <= t, and m>=1, it's possible that mt < s_l if m=0? But m>=1. For m=1, mt = t, so s_l <= t ensures s_l <= mt. So k=1 always satisfies sum condition when s_l <= t. Actually if m=1, mt = t, so s_l <= t implies s_l <= mt. So always true. For m>=2, mt >= 2t, which is >= t >= s_l, so also true. So k=1 always passes sum condition when s_l <= t. Wait, is it always? mt could be less than t? No, m>=1 => mt >= t, since t positive. So yes, m*t >= t >= s_l. So k=1 always feasible. Therefore if s_l <= t, there is always at least a solution with r = l (k=1). So answer is never -1 if s_l <= t? Let's check Example 1: query 2: l=3, s_l=4 > t=3, so -1. Query 4: l=6, s_l=7 > t=4, -1. So indeed if s_l <= t, we always can eat at least that single Karafs? Let's think: For a single Karafs of height s_l, can we eat it within t operations using at most m bites per operation? To eat it, we need to apply s_l decrements, each decrement requires an operation where that Karafs is selected. Since each operation can select it at most once, we need at least s_l operations. But we have t operations. Condition: s_l <= t. That's given. Also, we need to ensure that we don't exceed m per operation? That doesn't matter because we only need to select this one Karafs each operation, and we can select it each time (since it's distinct). There's no limit on how many times we can select the same Karafs across operations. So yes, we can eat it by simply biting it each operation until it becomes zero. So single Karafs always feasible if its height <= t. So answer always at least l. So the answer is never -1 for queries with s_l <= t? But the problem statement says: print -1 if there is no such r. But if at least l works, then there is some r (>=l). So -1 only occurs when s_l > t. So in examples, that's the case. So we can simplify: If s_l > t, answer -1; else we need to find the largest r such that both conditions hold, which will be at least l.
But wait: is there any scenario where s_l <= t but sum condition fails for k=1? As argued, sum condition for k=1 is s_l <= mt, which holds because mt >= t >= s_l (since m>=1). So indeed always true. So -1 only when s_l > t.
But double-check: What if m=0? Not allowed, m>=1. So fine.
Thus the answer always exists for s_l <= t.
But wait, are there any other constraints? Could there be a scenario where the process of m-bite might require that we never exceed m distinct per operation, and if we have many Karafses, we might need to schedule bites such that at each operation we don't exceed m. But for a single Karafs, we only need to select it each operation; that's 1 <= m, so fine. So indeed single always works.
Thus we can guarantee that for any query with s_l <= t, the answer is at least l.
Now we only need to compute the max r.
Given that, we can implement binary search as described.
Now we can also solve via quadratic to avoid binary search. But binary search is simpler.
But we must consider: The sum condition is monotonic increasing in k (since heights positive). So binary search works.
Now we need to be careful about overflow when computing sum for mid values. Use unsigned long long or __int128 if necessary. But as argued, it's safe with unsigned long long.
Let's compute the maximum possible sum for k = 1e6. s_l could be as high as t = 1e6 (since s_l <= t). Actually s_l could be as low as 1, but max case: s_l = 1e6, B = 1, k = 1e6. Then sum = ks_l + Bk*(k-1)/2 = 1e61e6 + 11e6999999/2 ≈ 1e12 + (1e61e6/2) ≈ 1e12 + 5e11 = 1.5e12. That's 1.5e12, far less than 9e18. Even if s_l = 1e6, B=1e6, k up to? But k is limited by max_k1 = (t - s_l)/B + 1. If s_l=1e6 and t=1e6, then max_k1 = 1. If s_l is smaller, say s_l = 1, B=1e6, t=1e6, then max_k1 = (1e6-1)/1e6 +1 = 0+1=1. So k won't be large when B is large. The worst case for k is when B is minimal (1) and s_l is minimal (1) and t is maximal (1e6). Then max_k1 = (1e6-1)/1 +1 = 999999+1=1e6. So k up to 1e6. In that case, s_l=1, B=1. So sum = 1e61 + 11e6999999/2 ≈ 1e6 + 5e11 ≈ 5e11, which is fine. If s_l were larger, say s_l=500000, B=1, t=1e6, then max_k1 = (1e6-500000)/1+1 = 500001? Actually (1e6-500000)=500000, +1 => 500001. So k ~ 5e5. sum ~ 5e55e5 + (1)5e5 (5e5-1)/2 ≈ 2.5e11 + 1.25e11 = 3.75e11. So still safe.
If B=1 and s_l is close to t, k is small. So sum never reaches near 1e18. Actually maximum sum possible under constraints: s_i are integers up to t (<=1e6). The maximum sum of a sequence of length k where each term <= t and increasing? But we have arithmetic progression with B=1, s_l at least 1, and k up to t. The maximum sum would be if we take l=1, s_1 = A (at most 1e6), but to maximize sum for given t and k, we want s_l as large as possible? Actually sum for given k is ks_l + k(k-1)/2. s_l is at most t - (k-1)B (since s_r = s_l+(k-1)B <= t). To maximize sum, given t and k, we maximize s_l = t - (k-1)B. So sum_max(k) = k(t - (k-1)B) + Bk(k-1)/2 = kt - Bk(k-1) + Bk(k-1)/2 = kt - Bk(k-1)/2. For B=1, sum_max(k) = kt - k(k-1)/2. This is a concave quadratic in k. Maximum occurs at derivative: t - (2k-1)/2 =0 => k ~ t+0.5? Actually solve t - (2k-1)/2 =0 => 2t = 2k-1 => k ≈ t+0.5. But k cannot exceed t because s_l positive? Actually if s_l = t - (k-1) must be >=1? Not necessarily required but s_l positive integer, so k <= t. So at k=t, s_l = t - (t-1)=1, sum = tt - t(t-1)/2 = t^2 - t(t-1)/2 = (2t^2 - t^2 + t)/2 = (t^2 + t)/2 ≈ 5e11 for t=1e6. That's ~5e11. So sum is at most ~5e11 for B=1. For B>1, k is smaller, sum even smaller. So sum is at most about 5e11, far less than 9e18. So we can safely use 64-bit.
Thus overflow not a concern.
So binary search approach is safe.
Now we need to implement fast. We'll read A, B, n. For each query, read l, t, m.
Compute s_l = A + (l-1)*B (as long long).
If s_l > t: print -1.
Else:
max_k = (t - s_l) / B + 1; // integer division
// binary search for largest k in [1, max_k] with sum <= m*t
long long max_work = m * t; // might be up to 1e12, fits in long long
int lo = 1, hi = max_k;
int ans = 1; // at least 1 works
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
// compute sum for k = mid
// Use long long for sum
long long sum = mid * s_l + (B * 1LL * mid * (mid - 1)) / 2;
// Note: B * mid * (mid-1) might overflow 32-bit, so cast to long long.
if (sum <= max_work) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
// answer r = l + ans - 1
// l is int (up to 1e6), ans up to 1e6, so r fits in int
cout << l + ans - 1 << '\n';
But need to consider that max_k might be huge? max_k <= 1e6 as argued. But we used int for lo, hi, ans, mid; that's fine.
But there is one more nuance: s_l computed as long long but could be up to 1e12 if l large, but we early exit if s_l > t. However, we compute s_l regardless; if l is 1e6, B=1e6, s_l = A + (1e6-1)*1e6 ≈ 1e12, which exceeds 2^31-1. So we need to store s_l in 64-bit. So we compute as long long s_l = A + (l-1) * 1LL * B.
Now for max_k = (t - s_l) / B + 1. But if s_l > t, we already exit, so s_l <= t, so t - s_l >=0. But s_l could be large? Actually if s_l <= t, then s_l <= 1e6, so it's small. So we could compute max_k as (t - (int)s_l) / B + 1 using ints. But to be safe, we can use long long but then cast to int after verifying it's small. But we can just compute with long long and then assign to hi, but hi must be int; if hi is up to 1e6 it's fine. But ensure that (t - s_l) / B + 1 fits in int; it will because t <= 1e6, B >=1, so max value = 1e6. So ok.
Now we have to think about potential off-by-one: max_k from condition s_r <= t: s_l + (k-1)*B <= t => k-1 <= (t - s_l)/B. Since integer division floors, we get k <= (t - s_l)/B + 1. That's correct.
Now check for extreme: if t = s_l exactly, then (t-s_l)=0, division gives 0, +1 => k=1. OK.
Now sum calculation: sum = k*s_l + B * k * (k-1) / 2. This is exactly the sum of arithmetic progression from s_l with difference B, length k.
But note: The term B * k * (k-1) / 2 may need to be computed carefully to avoid overflow in intermediate multiplication. However, as argued, it's safe within 64-bit. To be extra safe, we could compute using unsigned long long.
Now, we should verify with sample and some random tests.
But hold on: The scheduling condition we used is sum <= m*t and max <= t. But is there any other hidden condition? Possibly about the fact that we can only perform m-bite operation at most t times, but we cannot use more than t operations. In the scheduling analogy, each operation corresponds to one time unit. We assume that we can allocate at most m jobs per time unit. But is there any restriction that we must select exactly m distinct Karafses each operation? The statement says "decreasing the height of at most m distinct not eaten Karafses by 1". So we can choose fewer. So it's okay.
But there is also the concept of "not eaten" meaning once a Karafs height becomes zero, we cannot decrease it further. In scheduling, once a job is completed, it doesn't occupy a machine. That's captured.
Thus condition stands.
Now, we need to ensure that we are not missing the possibility that we might use fewer than t operations, but the condition sum <= m*t and max <= t is for makespan exactly t. But if we can finish earlier, that's fine. So necessary and sufficient.
Now, what about the condition that the sequence must be eaten by performing m-bite no more than t times. That means we must have a schedule with at most t operations. It doesn't require that we use exactly t operations; we can stop earlier. So condition max <= t and sum <= m*t are exactly that.
Thus correct.
Now we should also consider if B=0? Not allowed.
Now we can implement.
But wait, there is another subtle point: The problem statement says: "Karafs is considered as eaten when its height becomes zero." And we can only decrease height of not eaten Karafses. So once a Karafs reaches zero, we cannot further decrease it. That's captured.
Now, does the process require that we only decrease heights of Karafses that are currently not eaten? In scheduling, once a job is finished, we don't schedule it. So fine.
Thus solution seems solid.
Now we can code.
Potential pitfalls: The binary search upper bound max_k might be huge if B is 0, but B>=1. So fine.
Edge Cases: When B divides (t - s_l) exactly, max_k includes that.
Also, note that the sum condition might be satisfied for a k but not for larger, so binary search works.
Now about performance: 1e5 queries, each binary search at most 20 steps, each step does a few multiplications, fine.
Alternative: Could compute the exact k_max2 as floor( ( - (2s_l - B) + sqrt(D) ) / (2B) ) with integer sqrt. But then we need to compare with max_k1. That's also O(1). But binary search is simpler.
But we need to ensure that the binary search doesn't cause timeouts in Python? The problem is likely from Codeforces (maybe problem "Karafs"?). In C++ it's fine. But we are solving in general. Since the instructions say "write a solution", we can implement in C++.
Now let's check with a random test: Suppose A=1, B=2, l=2, t=9, m=2. s_l = 1+(2-1)2=3. max_k1 = (9-3)/2+1 = 6/2+1=3+1=4? Actually (9-3)=6, /2=3, +1=4. So k up to 4. Sequence: i=2:3, i=3:5, i=4:7, i=5:9. sum = 3+5+7+9=24. mt=18. So sum > 18, fails. Check k=3: sum=3+5+7=15 <=18, ok. So ans k=3 => r=4. That's correct.
Now consider case where max_k1 is 0? That would happen if s_l > t? Actually if s_l <= t, then (t - s_l) >=0, so max_k1 at least 1. So fine.
Now we must also consider that l might be very large, say l=1e6, B=1e6, s_l = A + (999999)*1e6 ~ 1e12 > t (since t<=1e6). So early exit -1.
Now we need to output r as integer.
Now implement.
But before finalizing, we should double-check the derivation of sum formula:
s_i = A + (i-1)B. For i from l to r inclusive, let k = r-l+1. Then sum = sum_{i=0}^{k-1} (s_l + i B) = k * s_l + B * (k-1)k/2. Yes.
Now compute sum <= m*t.
Now I'll consider if there is any issue when k is large and we compute B * k * (k-1) / 2, but the product might be odd? But it's even as argued.
Now check if we need to use 128-bit for computing sqrt? Not needed.
Now we should also think about potential pitfalls with integer division when computing max_k1: (t - s_l)/B. Since t and s_l are ints? s_l may be large if l is huge but we exit early; but we can compute using long long. However, we can compute max_k1 as int after confirming s_l <= t. Actually if s_l is <= t, then s_l is also <= 1e6 (since t <= 1e6). But wait, s_l could be larger than t? Not in this branch. But could s_l be >1e6 even if <= t? t <= 1e6, so s_l <= t implies s_l <= 1e6. So s_l is <=1e6. So we can safely treat s_l as int? But careful: s_l is computed as A + (l-1)*B. l can be up to 1e6, B up to 1e6, product up to 1e12. But if s_l <= t <= 1e6, then indeed s_l <= 1e6, so the product (l-1)*B must be <= 1e6 - A. That means if l is large, B must be very small. For example, l=1e6, B=1, then s_l = A + 999999 ≈ 1e6, which is <= 1e6. So s_l fits in int. So we could cast s_l to int after checking s_l <= t. However, better to keep as long long to avoid overflow before the check. But after check, we can treat as int if we want. For simplicity, keep as long long.
Now compute max_k1: since t and s_l are <=1e6, B is <=1e6, so (t - s_l)/B is at most 1e6, fit in int.
Now binary search uses int for mid.
Now we must be careful that in sum computation, we multiply mid (int) by s_l (long long). That yields long long. Then B * 1LL * mid * (mid-1) yields long long. So sum is long long.
Now check if m*t can be up to 1e12, which fits in long long.
Now we can implement.
Now let's test with some more corner cases:
Case: A=1, B=1, l=1, t=1000000, m=1000000.
s_l=1 <= t.
max_k1 = (1000000-1)/1+1 = 999999+1=1000000.
Binary search: find largest k such that sum <= 1e12.
sum for k=1000000 = 10000001 + 11000000999999/2 = 1e6 + (1e6999999)/2. 1e6*999999 = 999999000000, /2 = 499999500000. Sum = 500000500000? Actually 1e6 + 499999500000 = 500000500000. That's 5.000005e11, less than 1e12, so k=1000000 works. So r=1000000. That seems plausible.
Case: A=1, B=1, l=1, t=1000000, m=1.
max_k1=1000000. sum for k=1000000 = 500000500000 > mt=1000000, so fails. So need binary search to find max k satisfying sum <= 1e6. Solve k(k+1)/2 <= 1e6? Actually sum from 1 to k = k(k+1)/2. k approx sqrt(2e6) ≈ 1414. So r = 1414. That seems right.
Now test if our sum formula for l=1, s_l=1: sum = k1 + 1k*(k-1)/2 = k + k(k-1)/2 = k(k+1)/2. Yes.
Now ensure that binary search finds correct.
Now think about potential issue: The condition max(s_i) <= t is s_l + (k-1)B <= t. That is automatically enforced by k <= max_k1. But what about the possibility that the sum condition could be satisfied for a k > max_k1? But we restrict to max_k1, so we might miss a feasible r? But no, because if k > max_k1 then s_r > t, so max condition fails, so that segment cannot be eaten. So we must restrict to k <= max_k1. So using max_k1 as upper bound is correct.
Now one more nuance: The segment must consist of "not eaten Karafses" initially. But that's fine.
Now final answer: for each query, if s_l > t: print -1; else compute max_k1 and binary search.
But is there any scenario where s_l <= t but the answer is -1 because even l alone cannot be eaten? For a single Karafs, as argued, you need to apply s_l decrements. Each operation can decrement it at most once. Since s_l <= t, you have enough operations. Also you have m >=1, so you can always choose it (since it's distinct). So yes, always feasible. So answer always at least l.
Thus we don't need to check ans existence after binary search; ans will be at least 1 because we set ans=1 initially and we will at least confirm that k=1 satisfies sum condition? But we must be sure that k=1 sum condition holds. As argued, sum for k=1 = s_l <= t <= mt, so yes. So ans will be at least 1. So we can set ans=1 and then binary search. But careful: If for some reason the binary search never updates ans (if lo starts at 1 and hi= max_k, and we only update ans when sum <= max_work), but we have sum for mid might be > max_work for mid=1? That can't happen because sum for 1 is s_l <= t <= mt? Wait, is it always true that s_l <= mt? Since m>=1, mt >= t >= s_l. So yes, so condition holds for k=1. So when we evaluate mid=1, sum <= max_work true, ans will be set to 1. So ans will be at least 1. So safe.
Now complexity is fine.
Now let's consider if there is any tricky off-by-one in max_k1 calculation: Suppose s_l=1, B=2, t=2. Then max_k1 = (2-1)/2 +1 = 0+1=1. But is it possible to have k=2? Then s_r = 1+2=3 >2, so max condition fails. So correct.
Now suppose s_l=1, B=2, t=3. max_k1 = (3-1)/2 +1 = 2/2+1=1+1=2. So k=2 gives s_r=3 <=3, ok. So correct.
Now let's test with a random query in code to ensure binary search gives correct.
Now consider potential pitfalls with integer division in sum: B * mid * (mid-1) / 2. Because of integer division, ensure we do multiplication first, then division. But note that B * mid * (mid-1) might be odd? But we argued it's even. However, if B is odd and mid*(mid-1) is even? Actually mid*(mid-1) is always even because product of two consecutive integers. So product is even, thus divisible by 2. So integer division yields exact result. So fine.
Now we should also consider the possibility of using 64-bit for sum, but maybe some intermediate product (B * mid * (mid-1)) could exceed 2^63-1? As we argued, max is B=1e6, mid=1e6, (mid-1)=999999, product = 1e6 * 1e6 * 999999 ≈ 9.99999e17 < 9.22e18, safe. Actually compute: 1e6 * 1e6 = 1e12, times 999999 ≈ 9.99999e17. That's less than 9.22e18. So safe. If B were larger? B max is 1e6, so that's max. So safe.
Now we can implement.
But is there any scenario where we might get overflow in (t - s_l) if s_l is negative? No.
Now we can implement with C++.
Now we should also note that the input values can be up to 1e6, but A and B up to 1e6, l up to 1e6. So reading ints is fine.
Now code structure:
int main() {
int A, B, n;
cin >> A >> B >> n;
while (n--) {
int l, t, m;
cin >> l >> t >> m;
long long s_l = A + (l - 1LL) * B;
if (s_l > t) {
cout << -1 << "\n";
continue;
}
int max_k_from_t = (t - s_l) / B + 1; // since s_l <= t
long long max_allowed = 1LL * m * t;
int lo = 1, hi = max_k_from_t, ans = 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
// compute sum for this mid
long long sum = mid * s_l + (1LL * B * mid * (mid - 1)) / 2;
if (sum <= max_allowed) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
long long r = l + ans - 1;
cout << r << "\n";
}
return 0;
}
Now we need to verify with sample.
Now, what about the second sample: Should output 1 and 2. Let's simulate.
Now, note: In second query, s_l=6, max_k_from_t=1, but could we eat also the next one? s_3=11 > t=7, so no.
Thus works.
Now we should also test with a scenario where sum condition limits k before max condition. Example: earlier we had l=1, t=1000000, m=1, s_l=1, max_k_from_t=1000000, but sum condition restricts k to ~1414. So ans should be around 1414. Binary search will find that.
Now check with a bigger example: A=10, B=0? not allowed.
Now one more nuance: The problem statement says "at most m distinct not eaten Karafses". This means you can choose fewer than m. So yes.
Now, is there any condition that you must use exactly m bites each operation? No.
Now consider edge case: m could be larger than the number of Karafses in the segment. In that case, the per-operation limit is effectively the number of Karafses. But our condition sum <= mt may be too lax? Actually it's still correct because we cannot exceed m per operation. If there are k Karafses, then in each operation we can decrement at most min(m, k) of them. But if m > k, we can still decrement at most k per operation. So the effective parallelism is min(m, k). However, our condition sum <= mt might be easier to satisfy than the true capacity which is min(m,k)t. But is the condition sum <= mt sufficient when m > k? Let's examine: Suppose we have k=2, heights [5,5], m=100, t=1. Then sum=10, mt=100, max=5 <=1? Actually max=5 > t=1, so fails condition max <= t. So not a problem. Suppose t is large enough: k=2, heights [5,5], m=100, t=5. Then sum=10, mt=500, max=5 <=5. Our conditions say feasible. But can we eat them within 5 operations? We have 2 Karafses, each requires 5 decrements. In each operation, we can decrement at most 2 Karafses (since there are only 2 distinct). So maximum total decrements per operation is 2. Over 5 operations, max total decrements = 10. We need exactly 10. So it's feasible: each operation decrement both. So condition sum <= mt is actually sum <= min(m,k)t? But here min(m,k)t = 25=10, which equals sum. Since mt=500, our condition sum <= 500 is weaker but still satisfied; however, is it possible that sum <= mt but sum > min(m,k)t, causing infeasibility? That would require m > k and sum > kt but <= mt. Example: k=2, heights [100,1], m=100, t=50. Then sum=101, mt=5000, max=100 <=50? Actually max=100 >50, fails max condition. So must have max <= t. With max <= t, each job's processing time <= t. For k jobs, the total work sum must be <= k * t (since each job can be processed at most once per time unit, so total processing per time unit cannot exceed k). Actually with k jobs, the maximum number of decrements per operation is k (since only k distinct items). So capacity per time unit is min(m, k). If m >= k, capacity is k per time unit. Over t time units, max total decrements = k * t. Therefore the necessary condition is sum <= k * t. But is this automatically implied by max <= t and sum <= mt? Not necessarily. Consider m=100, k=2, t=3, heights [3,3]. Then sum=6, mt=300, max=3 <=3. Our conditions say feasible. But can we schedule? Each operation can decrement at most 2 Karafses (since only 2). Over 3 operations, max decrements = 2*3 = 6, sum=6, feasible. So it's okay.
Now consider heights [3,2] same t=3, sum=5 <=6, feasible.
Now consider heights [3,3,0] but k=2. So no issue.
What about a scenario where m > k, and the sum is between kt and mt, but max <= t? Is that possible? Since each job's max <= t, the total sum of k jobs each <= t can be at most kt. So sum cannot exceed kt. Because each job's height is <= t, so sum <= kt. So if max <= t, then automatically sum <= kt. And since m >= 1, mt >= t, but could be less than kt? If m < k, then the capacity per time unit is m, so the necessary condition becomes sum <= mt (which we already check). If m >= k, then sum <= kt is automatically true because each <= t, but we might not have explicitly checked it; but it's implied by max <= t. So the condition sum <= mt is necessary but not sufficient when m < k? Actually when m < k, the capacity per time unit is m, so sum must be <= mt. That's our condition. When m >= k, the capacity is k, but sum <= kt is automatically satisfied if each <= t. So condition sum <= mt is always sufficient? Let's examine: If m >= k, we need sum <= kt. But we only check sum <= mt, which is weaker (since mt >= kt). So it's possible that sum <= mt but sum > kt? But can that happen while max <= t? The maximum sum given max <= t and k items is kt (achieved when all items have height t). So sum cannot exceed kt. Therefore sum <= kt always holds if max <= t. So the condition sum <= mt is automatically satisfied because sum <= kt <= mt when m >= k. So checking sum <= mt is equivalent to checking sum <= min(m,k)t? Not exactly, but because sum <= kt, if m >= k, then sum <= kt <= mt, so condition holds automatically. So the only real check when m >= k is max <= t. But we also have sum condition but it's redundant. So overall, the pair of conditions (max <= t and sum <= mt) is necessary and sufficient? Let's test: Suppose m=1, k=2, t=3, heights [2,2]. sum=4, m*t=3, so fails sum condition. Indeed not feasible because one machine can only do one per time, total work 4 > 3. So correct.
Suppose m=2, k=3, t=2, heights [2,2,2]. sum=6, m*t=4, fails sum condition. Not feasible because 2 machines over 2 time units can do at most 4 units, need 6. Good.
Suppose m=2, k=3, t=3, heights [3,3,0]? Actually heights [3,3,1]? But max=3 <=3, sum=7, m*t=6, fails sum condition, not feasible.
Now consider m=2, k=3, t=3, heights [3,2,1] sum=6, m*t=6, passes sum condition, max=3 passes. Is it feasible? With 2 machines and 3 jobs, total work 6 over 3 time units. Can we schedule? Let's try: We need to schedule jobs with processing times 3,2,1. On two machines over 3 time slots. This is feasible: Machine1: job1 all 3 slots; Machine2: job2 for 2 slots, job3 for 1 slot. So yes.
Now consider m=2, k=3, t=3, heights [3,3,0]? Actually need sum=6, but heights [3,3,0] sum=6, but job3 height 0 is trivial. So feasible.
Now, is there any case where both conditions hold but schedule is impossible due to integer constraints? In preemptive scheduling on identical machines, with integer processing times, a schedule exists with makespan T (integer) if sum <= m*T and max <= T. The schedule can be constructed with preemption and uses integer time slots (McNaughton's algorithm yields schedule where each job is processed in at most two intervals, and the schedule fits within T). It uses T time slots, each slot unit length. So it's fine.
Thus our solution stands.
Now we can code.
But maybe the problem originally from Codeforces required 64-bit and careful about overflow? Our approach handles.
Now I'll think about potential pitfalls: The binary search hi initial value is max_k_from_t computed as (t - s_l) / B + 1. However, if B=0? not allowed.
But what if B does not divide (t - s_l) exactly? That's fine, floor.
Now what about the case where s_l <= t but max_k_from_t is huge (e.g., 1e6). That's fine.
Now could the sum for mid be larger than 2^63-1? No, as argued.
Now let's also test with A=1e6, B=1e6, l=1, t=1e6, m=1e6. s_l=1e6 <= t. max_k_from_t = (1e6-1e6)/1e6+1 = 0+1=1. So ans=1. Good.
Now test with A=1, B=1, l=500000, t=1e6, m=1. s_l = 500000 <= 1e6. max_k_from_t = (1e6-500000)/1+1 = 500000+1=500001? Actually 1e6-500000=500000, +1=500001. So hi=500001. sum for mid=500001? Let's approximate s_l=500000, k=500001, then s_r = s_l+(k-1)1 = 500000+500000=1e6 <= t, okay. sum = ks_l + k*(k-1)/2 = 500001500000 + 500001500000/2? Actually formula: sum = ks_l + k(k-1)/2 = 500001500000 + 500001500000/2 = 500001500000(1+0.5)=5000015000001.5 = approx 3.75e11. mt=1e6, so sum >> mt, so condition fails. So binary search will find a small k.
Now, is there any risk that sum computation for mid near hi could overflow? mid up to 500001, s_l=500000, product 2.5e11, plus Bmid(mid-1)/2 where B=1, product ~1.25e11, total ~3.75e11, fine.
Now if s_l is smaller, product even smaller.
Thus all good.
Now we should think about using 32-bit for sum? 3.75e11 > 2^31-1, so need 64-bit.
Thus we will use long long.
Now I'll produce final answer.
But hold on: The problem statement might have a twist: The m-bite operation decreases the height of at most m distinct not eaten Karafses by 1. But can we apply the operation to a Karafs that has already been eaten? No, only not eaten. So it's okay.
Now I recall a known Codeforces problem: "Karafs" from Codeforces Round #? It might be problem 372A? Actually I think it's Codeforces 374C? Not sure. The typical solution is binary search on r using the condition that sum of heights <= m*t and s_r <= t. So indeed similar.
Thus we are correct.
Now we should also consider that there might be a more direct formula, but binary search is enough.
Now potential issue: For binary search, the upper bound hi is max_k_from_t, which could be 0? But if s_l <= t, then max_k_from_t >=1, so fine.
Now implement.
But careful: In C++, integer division of negative numbers: but t - s_l is non-negative.
Now I'll code.
Now test with sample.
But before finalize, consider the possibility of using the sum formula incorrectly: Actually sum = ks_l + B * k(k-1)/2. Let's test with sample: l=1, k=4, s_l=2, B=1: sum = 42 + 14*3/2 = 8 + 6 =14, matches.
Now let's test with l=7, k=2, s_l=8, B=1: sum = 28 + 12*1/2 =16+1=17, matches earlier.
Now with l=2, A=1, B=5, k=1: sum=6.
Now seems correct.
Now we need to output r as integer. Ensure it's within 64-bit, but it's less than maybe 2e6? Actually maximum r could be up to l + k - 1, where l up to 1e6, k up to 1e6, so r up to 2e6? Wait, if l=1e6 and k=1e6, then r = 2e6 - 1? But can k be as high as 1e6 when l=1e6? s_l = A + (l-1)B. For l=1e6, s_l will be at least 1 + 999999*1 = 1e6. So s_l is at least 1e6. For t=1e6, s_l <= t implies s_l = 1e6 exactly (if B=1, A=1). Then max_k_from_t = (1e6-1e6)/1+1=1. So k=1. So r = 1e6. So r never exceeds about 1e6. Because to have k large, l must be small, so r = l + k - 1 ≤ k + (some small). Since k <= max index where s_i <= t. That max index is floor((t-A)/B)+1. With t=1e6, B=1, max index = 1e6. So r ≤ 1e6. So r fits in int.
Now for l up to 1e6 and k up to 1e6, r could be up to 2e6? Not if l=1e6, k cannot be >1, because s_l close to t. So r ≤ max index ≤ 1e6. So int fine.
Now we are ready.
Now we also note that the input size is moderate; we can use fast I/O.
Now final solution:
#include
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int A, B, n;
cin >> A >> B >> n;
while (n--) {
int l, t, m;
cin >> l >> t >> m;
long long s_l = A + (l - 1LL) * B;
if (s_l > t) {
cout << "-1\n";
continue;
}
int max_k = (t - s_l) / B + 1;
long long max_allowed = 1LL * m * t;
int lo = 1, hi = max_k, ans = 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
// compute sum for k = mid
long long sum = mid * s_l + (1LL * B * mid * (mid - 1)) / 2;
if (sum <= max_allowed) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
cout << l + ans - 1 << '\n';
}
return 0;
}
Now we need to produce final answer in required format: for each query print answer on its own line.
Now edge case: when m*t might overflow int, we used long long.
Now just to be thorough, let's test with random small values by brute force to verify condition.
We can write a small program to test for random A, B, small l, t, m and compare binary search result against brute force enumeration of r from l upward until condition fails. Since we cannot run here, but we can reason.
But I think it's correct.
Now there is one nuance: The condition sum <= mt is necessary but is it always sufficient with max <= t? Yes, as argued. However, I recall that in some scheduling problems, if the number of machines is less than the number of jobs, the condition is sum <= mt and max <= t is sufficient for preemptive scheduling. This is a classic result.
Thus final answer.
Now we must ensure that we used 64-bit for sum and max_allowed, but also for s_l we might have large values before the check. Use long long.
Now time complexity O(n log max_k) ≈ 2e6 operations, fine.
Now we can output.
But I recall that the original problem might have constraints where t and m up to 1e6 but the answer r might be up to 10^12? Actually the sequence is infinite, but because s_i increases, and t limits max height, the maximum index that can be included is about (t - A)/B + 1. Since t up to 1e6, that's at most 1e6. So r <= 1e6. So no issue.
But is there any scenario where A could be 0? No, A >=1.
Now we should also consider the possibility that B=0? Not allowed.
Now final code.
We might also consider optimizing further: Instead of binary search per query, we could precompute something? Not needed.
Now we also need to consider that there may be many queries where s_l <= t but max_k is small (like 1). Binary search overhead minimal.
Thus final answer.
Now, we should also handle the case where max_k might be huge but s_l is small; binary search will do log steps.
Now I'm ready to output final solution.
One more thought: Could there be an issue with integer division in max_k when B does not divide (t - s_l)? We used integer floor division; that's correct because k-1 must be integer <= (t - s_l)/B. So floor is appropriate.
Now final answer.
I'll now produce the final response with the solution. ```cpp
#include
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
code
int A, B, n;
cin >> A >> B >> n;
while (n--) {
int l, t, m;
cin >> l >> t >> m;
// height of the first karafs in the segment
long long s_l = A + (l - 1LL) * B;
// single karafs cannot be eaten
if (s_l > t) {
cout << "-1\n";
continue;
}
// maximum possible length from the max-height condition
int max_k = (t - s_l) / B + 1;
long long max_allowed = 1LL * m * t; // total available bites
// binary search the greatest k <= max_k satisfying sum <= max_allowed
int lo = 1, hi = max_k;
int ans = 1; // k = 1 always works (s_l <= t <= m*t)
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
// sum of arithmetic progression: s_l, s_l+B, ..., s_l+(mid-1)*B
long long sum = mid * s_l + (1LL * B * mid * (mid - 1)) / 2;
if (sum <= max_allowed) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
// answer r = l + ans - 1
cout << l + ans - 1 << '\n';
}
return 0;