You are not logged in. Please login at www.codechef.com to post your questions!

×

OPRTNS - EDITORIAL

PROBLEM LINK:

Practice

Contest

Author: Shivam Manchanda

Tester: Shubham Singhal

Editorialist: Shivam Manchanda

DIFFICULTY:

EASY-MEDIUM

PREREQUISITES:

DP

QUICK EXPLANATION:

Use dynamic programming to pre-compute $Fun(r,k)$ for all 1≤r≤50000. Now during the input of operations, calculate how many operations are applied at each index. For every index $B[i]=B[i]+$ (no of operations[i] $*$ (Fun(A[i],k))).

EXPLANATION:

Computer P(r)

Let us maintain an array DP, where $DP[i]$ represents minimum fibonacci numbers that sum-up to i. You can now make a recursive definition:

for(i from 1 to 50000)
{
    if(i is fibonacci)
    {
        DP[i] = 1;
        Store i;
    }
    else
    {
        for(j in all fibonacci less than i)
        {
            DP[i] = minimum of all(DP[i-j]+1);
        }
    }
}

$NOTE:$ If you try to check all numbers (as j)less than i, you might get TLE. But as storing fibonacci numbers smaller than i will definitely get AC.

Applying operations

Take an array OPR, where $OPR[i]$ represents number of operations on index i. Initialize this array with 0. Now, for every operation performed from L to R, do this:

OPR[l]++;
OPR[r+1]--;

After performing all operations, take a cumulative sum of the array from index 1 to N, as follows:

for(i from 1 to N)
    OPR[i]+=OPR[i-1];

Finally, you have $OPR$ ready.

Answering Queries

As, we have computed the number of operations on each index OPR and P(r), thus query can be answered simply as:

B[x] = B[x] + ( OPR[x] * ( P[A[x]] less than k ? 1 : 0 ) );

TIME COMPLEXITY

$O(N+M+Q)$

ALTERNATIVE SOLUTION:

The part to compute number of operations applied can also be done using Segment Trees but in $O(n*logn)$.

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.
Tester's solution can be found here.

RELATED PROBLEMS:

This question is marked "community wiki".

asked 02 Oct '16, 10:41

torque's gravatar image

6★torque
4271111
accept rate: 17%

edited 02 Jan '17, 18:52

admin's gravatar image

0★admin ♦♦
19.7k350498541

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×15,629
×1,668
×137
×9
×1

question asked: 02 Oct '16, 10:41

question was seen: 472 times

last updated: 02 Jan '17, 18:52