Tom and Jerry
Practice
4.3 (3 votes)
Advanced data structures
Data structures
Lazy propagation in interval/segment trees
Problem
36% Success 245 Attempts 50 Points 5s Time Limit 256MB Memory 1024 KB Max Code

Tom and Jerry are everyone's favorite. Jerry who loves cheese the most, was given a track of \(N\) steps. Each step of the track contains a piece of cheese which has a happiness level of \(A_i\) for \(i^{th}\) step. Tom wants to test Jerry's intelligence and love for the cheese. To test Jerry, Tom will change his track and will ask him where he would like to start. If Jerry starts with step x then he can't go at steps less than x, more formally he would have to move in the forward direction only, additionally, he can't jump/skip a step i.e. he needs to move continuously. He can leave the track whenever he wants. Jerry wants to be happy as much as he can be by eating the pieces of cheese. Tom will change the track by replacing the pieces of cheese with new ones.

You will be asked \(3\) types of queries as follows:

\(1\space L\space R\space X\) \(-\) Tom changes the track from \(L\) to \(R\) by replacing the piece of cheese with a new piece with happiness equal to \(X\). (formally \(A_i = X\) for all \(L \le i \le R\))  

\(2 \space L\space R \) \(-\) Jerry needs to answer the maximum happiness he can achieve by selecting any starting point in the track. More formally, if he starts at step \(i\) and leaves the track at step \(j\) then the answer is the sum of all happiness of cheese i.e. \(\sum_{k = i}^{j} A_k\). Jerry would try to maximize the happiness that he may get. (where \(L \le i \le j \le R\))

Note: Jerry may have negative happiness too. (i.e. he should eat at least one cheese).

Input format:

  • The first line of input contains an integer \(T\) (the number of test cases). 
  • The first line of each test case contains an integer \(N\) (the number of steps at the track).
  • The second line contains \(N\) integers \(A_1, \space A_2 \space, A_3, ..... \space A_N\) (where the \(i^{th}\) element denotes the happiness of the cheese placed on the \(i^{th}\) step of the track.
  • The third line contains an integer \(Q\) (the number of queries).
  • Next \(Q\) lines contain space-separated integers either \(1\space L \space R \space X\) or \(2 \space L \space R\) denoting the query asked by Tom.
  • Additionally, it's guaranteed that there will be at least one query of type \(2\).

Output format:

For each test case, for each query of type \(2\), print a single integer denoting the answer to that query.

Constraints:

\(1 \le T \le 10 \\  1 \le N, Q \le 2 * 10^5 \\ -10^9 \le A_i, X\le 10^9 \\ 1 \le L \le R \le N\)

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:50
Tags:
Hard
Points:50
7 votes
Tags:
Advanced Data StructuresData StructuresXORHashing algorithmLazy Propagation in Interval/Segment TreesNumber theory
Points:50
8 votes
Tags:
StringLazy Propagation in Interval/Segment TreesData StructuresAdvanced Data Structures
Editorial

Login to unlock the editorial