PROBLEM LINK:Author: Sergey Nagin Tester: Jingbo Shang Editorialist: Hanlin Ren DIFFICULTY:EasyMedium PREREQUISITES:Fenwick tree, difference array PROBLEM:You're given an array $a$ consisting of $n$ zeros initially. You need to perform two kinds of commands:
After performing these commands, output the final array. QUICK EXPLANATION:For each command, we need to count how many times it's executed during the whole process, denoted by $cnt[i]$. We can iterate the commands backwards, and every time we meet a command $2\ l\ r$ which is executed $k$ times, we add $k$ to $cnt[l\sim r]$. When we know $cnt[i]$ for every command $i$ of type $1$, we can easily figure out the answer by maintaining the difference array of $a$. EXPLANATION:subtask 1brute forceWe have $n,m\le 10$ and we can use brute force. We just write a procedure
a faster algorithmLet $f_{i,j}$ be the number of time that $a[j]$ was increased when performing operation $i$. For the first type, $f_{i,j}$ is very easy to compute: if $l_i\le j\le r_i$, then $f_{i,j}=1$, otherwise $f_{i,j}=0$. For the second type, a single operation $i$ should be equivalent to the sum of all operations indexed in $[l_i,r_i]$. So for any $j$, $f_{i,j}$ is the sum of $f_{k,j}$'s where $l_i\le k\le r_i$. Once we know for every command, how many contribution it does to every element in array, we can compute the answer. Pseudocode:
The total time complexity is $O(nm^2)$. subtask 2Let $cnt[i]$ be the number of times that command $i$ is executed. If we know $cnt[1\sim m]$, this problem will become much easier. How to compute $cnt[]$? We don't know $cnt[1]$ but we know $cnt[m]$ must be $1$. If the command $m$ is of the form $2\ l_m\ r_m$, then $cnt[l_m\sim r_m]$ will increase by $1$. Then we look at command $m1$: originally its $cnt$ is $1$, however if it's executed by command $m$ then its $cnt$ should be $2$. Also this command might influence other $cnt[]$'s as well: if it's of the form $2\ l_{m1}\ r_{m1}$, then $cnt[l_{m1}\sim r_{m1}]$ will increase by $cnt[m1]$. Next we can consider $cnt[m2]$ and the same things happen again and again... This gives us an algorithm to compute $cnt[]$. Initially all $cnt[i]$'s should be $1$. Let's then iterate the commands backwards. When we process a command $i$, if it's of the form $2\ l\ r$, we add $cnt[i]$ to $cnt[l\sim r]$. Pseudocode:
Once we know all $cnt[]$'s, for any command $i$ of the form $1\ l\ r$, we simply add $cnt[i]$ to $a[l\sim r]$. The overall time complexity is $O(nm)$. subtask 3We need to optimize the code above. When we need to add the same value on a segment, we may consider maintaining its difference sequence. Formally, let $dcnt[i]=cnt[i]cnt[i+1]$, then adding $c$ to $cnt[l\sim r]$ is equivalent to:
When we're dealing with command $i$, $cnt[i\sim M]$ is fixed(there won't be modifications on them anymore). Thus we can calculate $cnt[i]$ immediately, using the formula $cnt[i]=dcnt[i]+cnt[i+1]$. As we obtained $cnt[i]$, we can update the array $dcnt[]$ when $i$th operation is type $2$. Pseudocode:
This gives an $O(m)$ algorithm for computing $cnt[]$. We can use the same trick to obtain the final array: let $da[i]=a[i]a[i+1]$, then adding $c$ to $a[l\sim r]$ is equivalent to:
After all modifications are done, we calculate the suffix sum of $da$, and that's the array $a$ we want. Pseudocode:
The overall complexity is $O(n+m)$. ALTERNATIVE SOLUTION:To maintain $cnt[]$, you need to support two operations: adding on a segment and querying on one position. Since this problem has some special structure, it can be done in linear time. Generally such kind of problems can be solved in time $O(m(\log m+\log n))$, if you use data structures such as segment trees or Fenwick trees. If your solution is different with ours, feel free to leave a comment. AUTHOR'S AND TESTER'S SOLUTIONS:Tester's solution can be found here.
This question is marked "community wiki".
asked 17 Aug '17, 18:06

I used an entirely different technique, no Trees, just using sum arrays and difference arrays. First i used a loop from last command to first, only processing query type 2, maintaining a record how many times a query of type 1 is to be applied. For maintaining a record, i used a difference array, which you may see from my code. After that, it's a simple matter of applying query x of type 1, adding n[x] to range[left[x]] and subtracting n[x] from range[right[x]+1], where n[x] is the number of times a query is applied found in first loop and left[x] and right[x] are the left and right of given command. The answer is simply a sum array of difference array made in second loop. I know i have not explained this very well, so, feel free to ask me anything. Please upvote if u find this helpful. Here's a link to my Code answered 11 Sep '17, 15:55
Hii! Can u please elaborate why did u do the following steps for type 2:
Thanks :)
(15 Sep '17, 23:07)
Here, variable t is keeping the track how many times this particular command is to be executed within the previously processed commands of type 2. In the problem, we are supposed to execute all the commands exactly once, so 1 is added for that execution in third loop. Please UPVOTE, much needed... Hope its clear now... Feel free to ask anything..
(16 Sep '17, 00:00)
1
For example N = 3, M = 3 1 1 1 2 1 1 2 1 2 initially range array 0 0 0 0 During first loop when i == 3 t is 0 we added (t+1) = 1 to range[2] subtracted 1 from range[11] range array becomes 1 0 1 0 i == 2 t = 1 (0 + range[2]) we added (t+1) = 2 to range[1] subtracted 2 from range[0] range array becomes 3 2 1 0 ignore i == 1 final range array 3 2 1 0 sum array 0 3 1 0 (using reverse loop) From here, we can ignore commands of type 2 and see, command 1 is to be executed 3 times because of other commands + 1 time for its own, as i mentioned above. Answer array is 4 0 0
(16 Sep '17, 00:02)
Thanks for the detailed explanation. But can u pls tell me how u came up with such a solution. I mean is this any standard algorithm? If yes, pls link some material where I can read more about it.
(16 Sep '17, 15:16)
You can read more about difference arrays at http://www.geeksforgeeks.org/prefixsumarrayimplementationapplicationscompetitiveprogramming/ example problem mentioned on that page is just the same...
(17 Sep '17, 05:50)

@shubham_raj199 You just had to make a few changes. Hint : ( A  B ) % MOD = ( A % MOD  B % MOD + MOD ) % MOD . I've made a few changes to your code , here : https://www.codechef.com/viewsolution/15406455 . I've myself solved this problem using Fenwick Trees and I was also doing this same mistake. answered 11 Sep '17, 22:32

actually we need to know that for operations of both types answered 07 Sep '17, 20:40

I did this question using frequency array for the commands. I used squareroot decomposition to create the frequency array. To add 1 to a specific range (for Query 1), I used a O(1) method. It is prefix array. The logic is  To add 1 in the range of l...r > Add 1 at index l, subtract 1 from index r+1. Example : Array : 0 0 0 0 0 l = 2, r = 4. After operation : Array : 0 1 0 0 1 Prefix Sum array : 0 1 1 1 0 (Can be calculated at last) l = 1, r = 4. After operation : Array : 1 1 0 0 2 Prefix Sum array : 1 2 2 2 0 This way we can do the Query 1 operation in constant time. You can visit this article for more applications on Prefix Sum Array : http://www.geeksforgeeks.org/prefixsumarrayimplementationapplicationscompetitiveprogramming/ The solution is here. answered 11 Sep '17, 18:14

I would be really thankful if someone tells me what is wrong with my code Code : https://ideone.com/FUTR56 answered 11 Sep '17, 19:30

https://www.codechef.com/viewsolution/15316099 PLEASE CHECK MY CODE IT SHOWS RUNTIME ERROR AFTER 50 PTS. I AM NOT ABLE TO GET ANY IDEA. answered 12 Sep '17, 00:08

Can someone help me out and tell me what is wrong with my code.What I did was I iterated through all the commands backwards and for any command of type 2,first I did the query operation on the segment tree to find how many times that command was getting executed and then I performed an update operation on the segment tree in the range l to r of the type 2 command.And for all the type 1 commands if it was from l to r , all I did was first the query operation to get how many times it was getting executed(val) and then I was able to pass only subtask 1. Here's my code: answered 12 Sep '17, 01:56
@harsh24 I have used the same approach and got 100 points. I have added explanation just above, alongwith a link to my code. Ask me anything if required.. Hers's the link https://discuss.codechef.com/questions/108189/seacoeditorial/110993 Please upvote if u find that helpful..
(13 Sep '17, 12:42)
I saw your code . The last part of your approach is the same as mine.But I haven't yet found out the mistake in my code.Please tell me if you find any bug in my code. Thank you
(13 Sep '17, 18:57)

Can anyone show some light on why we need the difference array? Thanks in advance!! answered 12 Sep '17, 08:02
Because the alternative is to update the actual array, which will result in O (N) complexity per command, thus O (N^2) comolexity for whole task, giving TLE. using difference array, u apply update in constant time. I have used difference arrays only and got 100 points. I have added explanation just above, alongwith a link to my code. Ask me anything if required.. Hers's the link https://discuss.codechef.com/questions/108189/seacoeditorial/110993 Please upvote if u find that helpful..
(13 Sep '17, 12:47)

Can someone tell me what's wrong in my code? I used a 2d array to store the array after each command. The command 1 executes normally. But for command 2, (2 l r) the array after last executed command is added with the array after command number 'r' and then subtracted with the array before command number 'l', hence adding the result of all the commands executing in [l, r] index to the current array. I know using a 2d array won't work for higher test cases in c++, and not using a difference array would maybe take too much time but I couldn't get my program to work for even the second sub task (I got WA) while it worked fine for first subtask. I just need to know what's wrong with this approach. And just for clarity, I also tried this approach with difference array. Still didn't work.
answered 12 Sep '17, 10:04

I was getting RZEC in python while the same logic in c++ was passing the first 2 subtasks... answered 12 Sep '17, 11:50

Hi ,if any one knows why submission for SEACO is not allowed now? answered 12 Sep '17, 19:54
The contest has ended. Now submission is not allowed for contest. U may solve the question in practice section using following link This is the contest problem link. Codechef.com/SEPT17/problems/SEACO Just remove the contest name and ull be able to submit solution.. Following link
(13 Sep '17, 12:50)

can anyone provide an easy editorial for this problem... answered 13 Sep '17, 18:58

I did this problem in a different way. I used two segment trees with lazy propagation on both of them. My solution passed in 0.30 secs.
segment tree for queries  segQ answered 13 Sep '17, 21:40

If for commands we build the difference sequence in backward order, as the editorial does, but for the array we build it in forward order, then it is sufficient to just keep the running totals (without actually restoring the corresponding sum sequences). Here is python solution. answered 15 Sep '17, 23:14
Well, the essential point is given in the question itself, "It's guaranteed that r is strictly less than the enumeration/number of the current command." So, we run a loop in backward order, keeping track which command is to be executed how many times... Well, a restore to actual sum sequence is required because after that, we are to provide for command type 1 also. I have discussed my implementation in detail in the following link https://discuss.codechef.com/questions/108189/seacoeditorial/110993 Please UPVOTE if you find this helpful...
(15 Sep '17, 23:31)
I am not sure what you are trying to say. You can see my code that just uses running totals of the difference arrays. Both command types are handled in a single pass starting from the last command.
(16 Sep '17, 00:42)
Well, I don't really understand what you are missing here, because i code in java and not used to C language, so don't understand your code... I think it depends upon the approach we are following. In your approach, as your code proved, There's no need to restore sum sequences... But in my approach, you need to restore sum array atleast once to find number of executions of command 1... So, in short, restoring sum array is dependent upon approach followed. Hope that helps.... Please UPVOTE... Feel free to ask anything...
(16 Sep '17, 01:24)
Illiteracy in popular programming languages does not justify your condescending attitude. ;)
(16 Sep '17, 09:02)
Well, I'm a new programmer with just one year in java programming mate... I meant to cooperate as much as i can... I tried my best explaining to you my code, the query you asked as well as the difference in our approaches... But when i cannot understand a code, how can i explain what's going on in the loop and statements... Well, if you found my explanation / approach / effort or code, anything, Please Upvote... Feel free to ask anything.... I'll be pleased to clarify your doubts... :)
(17 Sep '17, 00:41)
May be it is some kind of misunderstanding, but I honestly don't care about your code and have never asked you to explain to me anything about it. This is the editorial thread, in case you have not noticed. :)
(17 Sep '17, 01:54)
Oh Sure, I merely tried to help.. :)
(17 Sep '17, 05:54)
showing 5 of 7
show all

can someone explain how to solve this using fenwick tree? answered 18 Sep '17, 01:04

can anyone explain hw dis can be solved using seg trees ?? answered 18 Sep '17, 01:38

Sqrt decomposition technique works fine...AC in 0.33s answered 20 Sep '17, 18:24

Solution using BIT. https://www.codechef.com/viewsolution/15528033 answered 26 Sep '17, 20:02

Finally was able to solve this after learning from the editorial about difference array. Here is my solution: Yay! AC <3 answered 03 Oct '17, 01:53

Please, someone tells how to solve this problem using fenwick tree(Binary Index Tree). Share methods, not code. :) answered 13 Oct '17, 22:51

For each command, we need to count how many times it's executed during the whole process, denoted by cnt[i]. We can iterate the commands backwards, and every time we meet a command 2 l r which is executed k times, we add k to cnt[l∼r]. When we know cnt[i] for every command i of type 1, we can easily figure out the answer by maintaining the difference array of a. Can anyone explain this.I am completely unable to understand. answered 16 Nov '17, 05:26
