DMOPC '14 Contest 2 P4 - Deforestation

View as PDF

Submit solution

Points: 5 (partial)
Time limit: 2.5s
Memory limit: 256M

Problem type

The Logging Company has a long line of N (1 \le N \le 1\,000\,000) trees numbered from 0 to N-1. Each tree i has a mass m_i (0 \le m_i \le 2000). The Company wants to cut some of the trees, so they hired you to calculate the mass of all the wood they would get from cutting all the trees between positions a and b inclusive (0 \le a, b < N). In particular, they want you to answer Q (1 \le Q \le 1\,000\,000) such queries.

Input Specification

  • First line: N.
  • Lines 2 to N+1: line i+2 is the mass of tree i, m_i.
  • The line N+2 will contain the integer Q, the number of queries the logging company wants answered.
  • The next Q lines will contain the integers a and b.

Output Specification

For each query, print the total mass of the trees at position i such that a \le i \le b.


  • For 30% of the points, N, Q \le 1\,000.
  • For 50% of the points, N, Q \le 10\,000.
  • For the rest, N, Q \le 1\,000\,000.

Sample Input

0 4
1 3
2 2

Sample Output



  • 0
    adam_t27  commented on Nov. 19, 2020, 6:42 p.m. edit 2

    I am getting an arrayindexoutofbounds error on my program on the last couple of test cases and the first ones are working fine. Any help?

    edit: now the arrayindexoutofbounds error is gone but Im getting a TLE error. Plz help

  • 1
    cabbagecabbagecabbage  commented on Nov. 30, 2019, 5:46 p.m. edited

    with Python3 if u tle on the last 2 try using fast input

  • 2
    hmy20040204  commented on Sept. 22, 2019, 10:54 p.m.

    I got a TLE, not sure how to fix it, can anyone help me, thanx XD

    • 4
      Tzak  commented on Sept. 23, 2019, 8:13 a.m.

      This problem requires prefix sum arrays to handle queries in constant time.

      • 2
        hmy20040204  commented on Sept. 25, 2019, 7:11 p.m.

        Thanks so much

  • 0
    TypicalToxic  commented on May 12, 2016, 12:18 p.m.

    An internal error occurred while grading, and the DMOJ administrators have been notified. In the meantime, try resubmitting in a few seconds.

    • 4
      Xyene  commented on May 12, 2016, 2:33 p.m.

      Yes, we all get an email whenever an IE occurs. I've fixed the issue, and the problem is working now.

  • 8
    FatalEagle  commented on Oct. 9, 2015, 11:50 p.m.

    cin/cout might be too slow to pass. Try one of the tips!

    • -4
      Dan13llljws  commented on Jan. 11, 2020, 8:45 p.m.

      How come when I use fast I/O in my IDE it works but when I submit it TLE

    • 2
      UnknownSource  commented on Sept. 14, 2019, 1:11 p.m.

      You are a saint

    • -2
      Kirito  commented on May 14, 2016, 5:13 p.m.

      Is this question doable in Turing with the current time limit?

      • 2
        Xyene  commented on May 14, 2016, 5:23 p.m. edited

        The OpenTuring we use includes some benchmarks against Python in its README, which might be of help.


        Python is faster than Turing.

  • 12
    quantum  commented on Nov. 18, 2014, 11:22 p.m.

    Give PYPY a try. (Why must I rhyme?)

    • -5
      FatalEagle  commented on Nov. 20, 2014, 12:30 p.m.

      This comment is hidden due to too much negative feedback. Click here to view it.