Log inSign up
Problems / A023

Name tags

300 ptsCases 10Set by dongbinna
Added October 30.
1 solved1 attempted1 submissionssolvers took 1.0 tries on average

Statement

There is one long straight line. On this line the numbers from 1 to 300,000 are written in order from left to right. Below each number there is space where students can stick a name tag.

Each of the N students sticks their own name tag below every consecutive number from a start point (a number) of their choice up to an end point (a number) of their choice. The number of students N takes a value from 1 to 300,000, and for each student the length of the interval can take a value up to 300,000. Here the length of the interval means the number of name tags that student sticks.

Now suppose the information [start point t, interval length k] is given for each student. If a student's start point is 500 and the interval length is 10, they stick their name tag at every position from 500 to 509.

However, if the interval a student sets runs past the end of the range, no name tag is stuck at the positions beyond it. For example, think of a student who sets the start point to 299,995 and the interval length to 100. In that case they stick name tags at only 6 positions, 299,995, 299,996, 299,997, 299,998, 299,999 and 300,000, instead of 100 positions.

Once all N students have stuck their name tags, M queries are run. The number of queries M is at most 100,000. Each query has the form [start, end], which means adding up the number of name tags below every number from start to end. start is a natural number from 1 to 300,000. end is a natural number from start to 300,000. Write a program that computes, for each of the M queries, the total number of name tags that fall inside the interval, and returns the results as a one-dimensional array of size M.

For example, let N=5 and M=3, with the information on each student hanging their name tag on the line as in the table below.

Student numberNo. 1No. 2No. 3No. 4No. 5
Start point36213
Interval length23854

Suppose students 1 through 5 hung their name tags in order, as in the example above. Then the name tags end up hanging at each number (position) on the line as follows.

Position 1Position 2Position 3Position 4Position 5Position 6Position 7Position 8Position 9
No. 1No. 1No. 2No. 2No. 2
No. 3No. 3No. 3No. 3No. 3No. 3No. 3No. 3
No. 4No. 4No. 4No. 4No. 4
No. 5No. 5No. 5No. 5

Now suppose the information for the 3 queries is [3, 7], [7, 9] and [2, 5], in that order. Laying out the answer to each query in a table gives the following.

Query numberNo. 1No. 2No. 3
Start point372
End point795
Answer16513

So the answer in this example is [16, 5, 13].

Input and Output Examples

Example 1

Input

5, 3, [[3, 2], [6, 3], [2, 8], [1, 5], [3, 4]], [[3, 7], [7, 9], [2, 5]]

Output

[16, 5, 13]

Input

  • First the number of students N is given. N is a natural number from 3 to 300,000.

  • Next the number of queries M is given. M is a natural number from 1 to 100,000.

  • Next an array students of size N×2 is given, holding the information on each student hanging their name tag. Each element is given in the form [t, k], where the start point t is a natural number from 1 to 300,000. k means the length of the interval, a natural number from 1 to 300,000.

  • Next an array queries of size M×2 is given, holding the information on each query. Each element is given in the form [start, end], where start is a natural number from 1 to 300,000 and end is a natural number from start to 300,000.

Output

  • For each of the M queries, compute the total number of name tags that fall inside the interval and return the results as a one-dimensional array of size M.

Requirements

  • You must write a solution(N, M, students, queries) function and return the answer
  • Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
  • The languages you can use are Python and JavaScript
  • The memory limit is 256MB. The browser does not measure it, it only states it
  • The time limit is set by measuring the setter's reference solution on the same runtime
  • On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
  • The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
  • There are 10 cases, and 1 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
  • You have to pass all 10 cases to solve this.
Read the requirements carefully before solving. Tampering with the grading to make a submission look like a pass can get you sanctioned if caught. The code you submit is published on the leaderboard along with the result.

Examples

1 public cases. Judging also uses hidden cases. You have to pass all 10 cases to solve this.

Input
Expected output
5, 3, [[3, 2], [6, 3], [2, 8], [1, 5], [3, 4]], [[3, 7], [7, 9], [2, 5]]
[16, 5, 13]

Notes

Background that helps with solving. The rules you have to follow are in the input and output specs and the requirements above.

This is problem 8 of practice test 2 in the original.

Solution

Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.

0 chars1 lineMemory 256MB (recorded only)Reference 62ms

Submitting requires you to log in. Running against the public cases works without one.

You can test with 1 public cases. Submitting runs the hidden cases too.

People who solved this problem

The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.

1bampal21.32s361 charsPythonWindows NT 10.0 / Chrome/156.0.0.0 / 8코어 / 16GB

Input read time 1.56s (excluded from the ranking)

Log in and you can see the code once you solve this problem.