Embercode
✕
Segment TreesCoding problems

Segment Trees: coding problems

Write a program that reads the input and prints the answer. Run checks it against every test, including hidden ones.

0 of 7 solved

  • A shop writes down its sales for every day of the year, in one long list. The owner asks many questions of the form "how much did we sell from day l to day r?". Each answer is a sum over a slice of the list.

    Read the list once, then answer every question. Add the daily sales with long long, because a long year can pass the range of int. Day numbers start at 0.

    **Constraints:** 1 to 800 days. Each day's sales are from 0 to 1000000. 1 to 200 questions. Each question has 0 <= l <= r < n.

    Input. Line 1: `n`, the number of days. Line 2: `n` integers, the sales of day 0, day 1, and so on. Line 3: `q`, the number of questions. Then `q` lines, each with `l` and `r`.

    Output. For each question, one line with the total sales from day `l` to day `r`, both included.

    Example 1 input

    5
    3 1 4 1 5
    3
    0 4
    1 2
    3 3
    

    Output

    14
    5
    1
    

    Example 2 input

    8
    120 0 75 300 0 50 980 10
    4
    0 7
    2 5
    6 6
    1 3
    

    Output

    1535
    425
    980
    375