Xenny and Formula 0
Practice
3.8 (103 votes)
Medium
Segment trees
Trees
Problem
76% Success 1040 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

Xenny drove in a popular racing championship called Formula 0. His long-distant friend Mr. Kmcode had made a racing track for him in RSQLand. To entertain the public, he used to keep a drag racing event at the Kmcode Track before every race. The Kmcode Track was circular and there were N grand stands along the track, numbered from 1 to N, separated by 1 angular unit each.
Initially, stand i contained Ai people.
In each drag race, he started from stand number X with initial angular velocity of magnitude w angular units per second. At the end of each second, his car's angular velocity decreased by a magnitude of d angular units per second. His car always came to a halt when its angular velocity reached equal to or below zero.
In each race, Xenny counted the total number of people in stands that he crossed in that race and reported its value modulo 109 + 7, to his team.
Whenever Xenny started his race from a particular stand X, at the end of the race, his team increased the number of passes for his races and hence, Y people were added to stand number X after the race.
Given the data for R races, print the number that Xenny reported to his team in each race.

Input

First line of input contains a single integer R - number of drag races.
Second line of input contains a single integer N - the number of grand stands.
Third line contains N space-separated integers - Ai denoting the number of people in ith stand.
R lines follow. Each line contains 4 space separated positive integers - X, w, d, Y - the starting stand, the initial angular velocity, the deceleration and the number of people that get added to stand X after each race.

Output

Print R lines, ith line containing the number of people Xenny crosses, modulo 109 + 7, in the ith race.

Constraints

1 ≤ R ≤ 105
1 ≤ N ≤ 105
1 ≤ Ai ≤ 109
1 ≤ X ≤ N
2 ≤ w ≤ 109
1 ≤ d ≤ 109
1 ≤ Y ≤ 100

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
6 votes
Tags:
Advanced Data StructuresData StructuresMediumSegment Trees
Points:30
27 votes
Tags:
Advanced Data StructuresAlgorithmsData StructuresDynamic ProgrammingFenwick TreeSegment Trees
Points:30
12 votes
Tags:
ApprovedData StructuresDynamic ProgrammingMediumSegment TreesSieveapproved