PROBLEM LINK:Author: Lalit Kundu DIFFICULTY:MEDIUM PREREQUISITES:Segment Trees, Number Theory PROBLEM:Given a string of digits of length N<(10^{5}), do two kind of operations(total of M<(10^{5}):
QUICK EXPLANATION:Use segment trees. Store in node for each interval, Perform merge operations in constant time. EXPLANATION:We basically need to find number of subarrays in range L to R who sum is divisible by 3. This pseudo code will clear the things.
Note now, we also need to build both array count1 and count2 for the new interval.
So, complexity is: O(N log N) preprocessing and O(log N) per query. ALTERNATIVE SOLUTION:Suppose we keep in segment tree for each node, how many prefix sums in this interval are divisible by 0,1,2. For update query(mark A[x] = y), since we are keeping prefix sum modulo 3, for range [x, N], we increase/decrease each prefix sum by k(k<3). We can do this using lazy propagation. SOLUTIONS:
This question is marked "community wiki".
asked 12 Jan '15, 15:06

This is my favorite in this contest ;) My solution was in O(sqrt(N) * N)  simplified idea is to cut the array in sqrt(N) segments, segment can be updated in sqrt(N) time and query is in 3 * sqrt(N) (I handle first and last separately  2 * sqrt(N) and then I use precalculated values in all segments in between  last sqrt(N) ). Who is greedy can see my solution, I'll add detailed description a little later. answered 12 Jan '15, 15:17
why don't you use CHelper + IntelliJ IDEA?
(12 Jan '15, 15:35)
I'm an Eclipse guy. I tried IntelliJ IDEA and also NetBeans several times but I do not like those (maybe there is not rational reason for that)... What I do not like on IDEs (not sure now if it is the same in IntelliJ) the most is, that there have to be just one main project/class and especially for contests I'm using it different way (I have same experience with all C++ IDEs, also Eclipse for C/C++ one :( ) that's my biggest problem in using other IDEs... In Eclipse I'm using run this class as Java/JUnit and I'm happy...
(12 Jan '15, 15:41)
1
Ok. But, just so you know ... you can achieve the same in IntelliJ IDEA.
(12 Jan '15, 15:59)
Here is my code based on the above approach .. I also coded it during the contest itself .. :D
(14 Jan '15, 01:41)

Can someone please explain alternative solution for this problem, we have count of prefixes with remainder 0, 1 and 2 when divided by 3. Now how do we get number of substrings divisible by 3 using this information? I got that given two prefixes with equal remainder, we will have a substring which is divisible by 3. Hence if,
our answer should be s1(s11)/2 + s2(s21)/2 + s3*(s31)/2 ? For ex if the whole string is "133",
Our answer is 3*(2)/2 = 3. Not what editorial suggest, i.e., s1*s1 = 9. Am I missing or misunderstood something? answered 12 Jan '15, 23:23
yes, you are right, that was probably a typo. fixed that!
(14 Jan '15, 00:03)
@akumar3 : I saw your solution for this problem. https://www.codechef.com/viewsolution/5863202 Can you tell why have you incremented curr when i = 0 in processResult() ?
(25 Oct '15, 12:10)
1
That is because the case with 0 remainder is special. For every two prefixes with equal remainder, we have 1 substring which is divisible by 3; Now consider following string: 33 Prefixes: 3, 33 (both gives remainder 0 when divided by 3) If you apply above formula, you will get '1' substring which is divisible by 3. But here both the original prefixes are also divisible by 3. To account this fact, we have to update our formula for the case when remainder is 0, so if we have N prefixes which gives remainder 0; our answer will be: = (N(N1))/2 + N (original prefixes) = (N*(N+1))/2
(25 Oct '15, 15:23)
Thank you @akumar3.
(25 Oct '15, 20:14)

please explain node3.count1[i] = node1.count1[i] + node2.count1[3node1.total+i] answered 15 Jan '15, 20:36

Hello! I am posting the link to my solution for QSET... I am getting TLE for the subtask 4.. Can anyone please help me in optimizing my update_segment_tree function to avoid TLE???? An explanation added will be of very much help.. Thank you! http://www.codechef.com/viewsolution/5857954 answered 12 Jan '15, 17:25

Although I used the same idea as given here, I got TLE on both subtask 3 and 4. Can anyone tell why? http://www.codechef.com/viewsolution/5790168 answered 12 Jan '15, 20:17

This problem can be solved with TREAP , basicly whe storage the prefix sum mod 3 and we have 3 arrays that means T[ j ][ i ] = (AC[ i ] % 3 == j) where AC = prefix sum mod 3 and when we need to know the answer queries we need to know how many ones there has in T[ 0 ] , T[ 1 ] , T[ 2 ] in their respective intervals. Finally for update queries we only swap sufix of T . Sorry for my bad english. answered 13 Jan '15, 02:42

the links of Setter solution give xml error ... please correct answered 14 Jan '15, 22:25

The links to setters and tester's solution is showing xml error! Please check the links! answered 15 Jan '15, 00:27

Editorial is pretty much clear.. and explanatory.. but can anyone plzz help me how to built a query function.. means how can query for the range like 4 to 7 in segment tree when array size,n=8 only summing of node.ans is not enough so i am not able to manipulate how count1 and count2 is added.. or it is to be added to find ans in the same way like in merge operation explained above.. @darkshadows please explain it..Would be very much thankful if anybody can help me to understand..
link
This answer is marked "community wiki".
answered 15 Jan '15, 21:27

count1[], where count1[i] denotes number of prefixes of interval which modulo 3 give i.  count2[], where count2[i] denotes number of suffixes of interval which modulo 3 give i. what does this mean? answered 18 Jan '15, 18:50

why are we doing this
in the pseudo code ? answered 23 Jan '15, 08:27

how could i modify this to check if the substring between C and D inclusive is divisible by 3 ?? Or am i trying to explore a wrong approach for doing this ? answered 29 Jul '16, 22:26

include <stdio.h>int main() { int rows, cols, i, j;
} answered 30 Jul '16, 07:42

Someone please fix the "Access Denied" issue!