×

# SEACO - Editorial

Practice

Contest

Author: Sergey Nagin

Tester: Jingbo Shang

Editorialist: Hanlin Ren

Easy-Medium

# 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:

• Add $1$ to $a[l\sim r]$;
• Perform all commands whose indices are in $[l,r]$, where $r<$(the index of current command)

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:

### brute force

We have $n,m\le 10$ and we can use brute force. We just write a procedure op(i) that performs the $i$-th operation, and do what the problem asks us to do:

op(i) : if type[i] == 1 then for j = l[i] to r[i] do a[j] += 1 else //type[i] == 2 for j = l[i] to r[i] do op(j)

### a faster algorithm

Let $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:

f = [empty array of m*n] for i = 1 to m do if type[i] == 1 then for j = l[i] to r[i] do f[i][j] = 1 else for k = l[i] to r[i] do for j = 1 to n do f[i][j] = f[i][j] + f[k][j] //perform this operation for j = 1 to n do a[j] = a[j] + f[i][j]

The total time complexity is $O(nm^2)$.

Let $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 $m-1$: 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_{m-1}\ r_{m-1}$, then $cnt[l_{m-1}\sim r_{m-1}]$ will increase by $cnt[m-1]$. Next we can consider $cnt[m-2]$ 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:

cnt = [array of 1's] for i = m downto 1 do if type[i] == 2 then for j = l[i] to r[i] do cnt[j] += cnt[i]

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)$.

We 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:

• $dcnt[r] \gets dcnt[r]+c$;
• $dcnt[l-1]\gets dcnt[l-1]-c$.

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:

dcnt = [array of 0's] cnt[m + 1] = 1 for i = m downto 1 do cnt[i] = dcnt[i] + cnt[i + 1] if type[i] == 2 then dcnt[r[i]] += cnt[i] dcnt[l[i] - 1] -= cnt[i]

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:

• $da[r]\gets da[r]+c$;
• $da[l-1]\gets da[l-1]-c$.

After all modifications are done, we calculate the suffix sum of $da$, and that's the array $a$ we want. Pseudocode:

da, a = [array of 0's] for i = 1 to m do if type[i] == 1 then da[r[i]] += cnt[i] da[l[i] - 1] -= cnt[i] for i = n downto 1 do a[i] = a[i + 1] + da[i]

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.

# AUTHOR'S AND TESTER'S SOLUTIONS:

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

This question is marked "community wiki".

7★r_64
261924
accept rate: 16%

19.8k350498541

 3 The problem had extremely weak testcases.I initially wrote a brute force solution (O(n*m)) and it passed. code answered 11 Sep '17, 15:50 4★abx_2109 275●1●11 accept rate: 0%
 1 The $T\le3$ allowed me to use square root decomposition here. Loved the feeling of AC. My solution is here answered 11 Sep '17, 15:28 15.5k●1●20●66 accept rate: 18%
 1 @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 237●4 accept rate: 0%
 0 For each command of type 1, we need to count how many times it's executed, denoted by cnt[i] actually we need to know that for operations of both types answered 07 Sep '17, 20:40 204●8●24 accept rate: 12%
 0 You can also solve it by segment tree using lazy propagation for updating a range. answered 11 Sep '17, 15:08 108●6 accept rate: 12% 2 lazy propagation is an overkill here actually. Normal segtree would work. I used a segtree but now am realising that there was an even easier way using a difference array. (11 Sep '17, 15:13) 1 yes, i realised that while solving it. There was no way 2k people would be able to solve it using segtrees. (11 Sep '17, 15:21) Honestly I didnt expect it to cross 2k when I got AC for my sqrt decomposition method. (11 Sep '17, 16:40)
 0 Access denied for tester's and editorialist's solution @r_64 answered 11 Sep '17, 15:18 58●4 accept rate: 0% it usually happens they will just rectify it soon you must wait for sometime (11 Sep '17, 15:22) divik5444★
 0 Actually there is an O(M+N) solution. You can see my code here. answered 11 Sep '17, 15:56 6★phben 149●3 accept rate: 9% 1 The editorial solution is O(n+m). (12 Sep '17, 05:53) eugalt3★ Oh right. I misread the alternative solution. (12 Sep '17, 06:07) phben6★
 0 I did this question using frequency array for the commands. I used square-root 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/prefix-sum-array-implementation-applications-competitive-programming/ The solution is here. answered 11 Sep '17, 18:14 319●1●10 accept rate: 5%
 0 Can someone tell me ,why I am getting WA for last cases, I used BIT for both queries: SOLUTION answered 11 Sep '17, 19:05 62●1 accept rate: 0% You need to use modulo arithmetic when implementing BIT. (12 Sep '17, 06:02) eugalt3★ I used that but may not be correctly (12 Sep '17, 17:43)
 0 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 3★cope 1 accept rate: 0%
 0 can anyone tell me why is my code wrong (link) and this is right (link), there is only one minor change. answered 11 Sep '17, 21:31 4★gauss14 0 accept rate: 0%
 0 How to solve this question using segment tree ? answered 11 Sep '17, 23:22 1 accept rate: 0%
 0 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 1 accept rate: 0%
 0 Can anyone show some light on why we need the difference array? Thanks in advance!! answered 12 Sep '17, 08:02 2★kunnu120 518●9 accept rate: 5% 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/seaco-editorial/110993 Please upvote if u find that helpful.. (13 Sep '17, 12:47)
 0 Can someone tell me what's wrong in my code? I used a 2-d 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 2-d 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 sub-task. 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. #include #include #include #include #include using namespace std; #define max 1000000007 int main() { int t; scanf("%d", &t); for(int i=0; i>c>>a>>b; if(c==1) { if(j==0) { com[j][a-1]++; com[j][b]--; } else { for(int k=0; k
 0 I was getting RZEC in python while the same logic in c++ was passing the first 2 subtasks... answered 12 Sep '17, 11:50 3★viru_57 -3●1 accept rate: 0%
 0 Hi ,if any one knows why submission for SEACO is not allowed now? answered 12 Sep '17, 19:54 0●1 accept rate: 0% 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 Codechef.com/problems/SEACO (13 Sep '17, 12:50)
 0 I coded according to the editorial only but getting WA in subtask 2 and 3, Can anyone tell me why? Here is the link to my code - Code answered 13 Sep '17, 11:15 4★k1s6313 11●1 accept rate: 0%
 0 can anyone provide an easy editorial for this problem... answered 13 Sep '17, 18:58 10●2 accept rate: 0%
 0 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 segment tree for array - segA First, I updated segQ with a val=1 (as every operation will be executed at least once). Then I ran a loop from m-1 to 0 and updated all the queries of type 2. Like for example for the 3rd sample test case of the problem, if the query was 2 1 5, then first I got the initial value on that node by doing query on segQ to get the no. of times this operation will be executed (say x) in future then with that value I updated the operations from 1 to 5 (i.e. l and r of the query) which means operations from 1 to 5 will be called x more times so add. Then after that I ran a second loop from 0 to m-1 but this time for queries of type 1 and updated segA with the values for that particular command on my segQ. This is my full code answered 13 Sep '17, 21:40 1●1 accept rate: 0%
 0 can someone explain how to solve this using fenwick tree? answered 18 Sep '17, 01:04 1★nikmul19 32●5 accept rate: 0%
 0 can anyone explain hw dis can be solved using seg trees ?? answered 18 Sep '17, 01:38 1★msd_007 317●8 accept rate: 5%
 0 Can anyone give similar problem like this? answered 18 Sep '17, 17:37 12●3 accept rate: 0%
 0 Sqrt decomposition technique works fine...AC in 0.33s answered 20 Sep '17, 18:24 1 accept rate: 0%
 0 Solution using BIT. https://www.codechef.com/viewsolution/15528033 answered 26 Sep '17, 20:02 1 accept rate: 0%
 0 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 4★orlon 40●4 accept rate: 0%
 0 Please, someone tells how to solve this problem using fenwick tree(Binary Index Tree). Share methods, not code. :) answered 13 Oct '17, 22:51 1 accept rate: 0%
 0 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 37●5 accept rate: 0%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• 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,852
×1,723
×286
×171
×8

question asked: 17 Aug '17, 18:06

question was seen: 7,827 times

last updated: 16 Nov '17, 05:26