tinycodinglm / misc /misc-0202.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 16)
d517dc9 verified
|
Raw History Blame Contribute Delete
3.78 kB
opencode trace
System You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code. When solving problems, explain your reasoning step by step before providing...

You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code. When solving problems, explain your reasoning step by step before providing the final solution.

User

Note: The time limit for this problem is 6s, three times the default. The
memory limit for this problem is 512MB, twice the default.

Bessie is a hungry cow. Each day, for dinner, if there is a haybale in the barn,
she will eat one haybale. Farmer John does not want Bessie to starve, so some
days he sends a delivery of haybales, which arrive in the morning (before
dinner). In particular, on day $d_i$, Farmer John sends a delivery of $b_i$
haybales ($1\leq d_i \leq 10^{14}$, $0\leq b_i \leq 10^9$).

Process $U$ ($1\le U\le 10^5$) updates as follows: Given a pair $(d, b)$,
update the number of haybales arriving on day $d$ to $b$. After each update,
output the sum of all days on which Bessie eats haybales modulo
$10^9+7$.

INPUT FORMAT (input arrives from the terminal / stdin):
$U$, followed by $U$ lines containing the updates.

OUTPUT FORMAT (print output to the terminal / stdout):
The sum after each update modulo $10^9+7$.

SAMPLE INPUT:
3
4 3
1 5
1 2
SAMPLE OUTPUT:
15
36
18

Answers after each update:

4+5+6=15
1+2+3+4+5+6+7+8=36
1+2+4+5+6=18

SAMPLE INPUT:
9
1 89
30 7
101 26
1 24
5 1
60 4
5 10
101 0
1 200
SAMPLE OUTPUT:
4005
4656
7607
3482
3507
3753
4058
1107
24531

SCORING:
Input 3: $U\le 5000$Inputs 4-10: Updates only increase the number of haybales arriving on day
$d$.Inputs 11-22: No additional constraints.

Problem credits: Brandon Wang and Benjamin Qi

Assistant

import bisect

MOD = 10**9 + 7

def main():
import sys
U = int(sys.stdin.readline())
sorted_days = []
H = []

markdown
for _ in range(U):
    d, b = map(int, sys.stdin.readline().split())
    idx = bisect.bisect_left(sorted_days, d)
    if idx < len(sorted_days) and sorted_days[idx] == d:
        H[idx] = b
    else:
        bisect.insort(sorted_days, d)
        H.insert(idx, b)
    
    # Recompute everything from scratch
    current_R = 0
    current_day = 1
    total = 0
    
    for i in range(len(sorted_days)):
        D = sorted_days[i]
        h = H[i]
        
        # Process the interval between current_day and D
        days_between = D - current_day
        if days_between > 0:
            eaten = min(current_R, days_between)
            start = current_day
            end = start + eaten - 1
            interval_sum = (start + end) * eaten // 2
            total += interval_sum
            current_R = max(current_R - eaten, 0)
            current_day = D
        
        # Process the day itself
        hay = current_R + h
        if hay >= 1:
            total += D
            current_R = hay - 1
        else:
            current_R = 0
        current_day += 1
    
    # Final contribution after last delivery
    final_R = current_R
    if final_R > 0:
        final_start = current_day
        final_end = final_start + final_R - 1
        final_sum = (final_start + final_end) * final_R // 2
        total += final_sum
    
    print(total % MOD)

if name == "main":
main()