UVA 12501: Bulky process of Reduction

UVA 12501: Bulky process of Reduction

Link: http://uva.onlinejudge.org/contests/310-4bd11ede/12501.html

Topic: Segment Tree with Lazy Propagation

This problem was given on BGC Trust University Programming Contest 12. But during the contest my team could not solve it. Later I tried this problem, got an analysis from Progkriya.com. But did not understand it properly, mayb I am newbie. 😦 Got TLE with that approach [ maybe implementation problem].

But after some googling, I have found this way. And this approach is easy to catch and a new trick to attack segment tree.

Problem Description Summary:

You will be given an array of N size, initialized with 100.

Then two of types of operation will be given.

1. change i j u

for this operation, you need to increase the numbers between index I and j by u.

for an example:

change 1 3 3

Index 1 2 3
Before Operation 100 100 100
After Operation 103 103 103

2. query i j

for this operation , you need to give some output according to the interval

for an example:

query 1 3

Index 1 2 3
Before Operation 100 106 104

Output will be

1*a[i]+ 2*a[i+1]+… +n*a[j]

So for this query output will be

1*100+2*106+3*104= 100+212+312=624

You need to print 624.

How to solve this problem??

After reading the problem, you might think, update can be done, but how to do the query. Crap!!

As we might think that, we need to traverse all the node between I and j. that would take O(nlogn). For n query, worst case will be O(n2logn).

That will give you TLE for sure.

Then how to solve it, any Mathematical formulation? So far I haven’t found any direct mathematical formula for this. But there is a Mathematical equation approach. Let’s find it out…

What if we initialize the array with this way.

Index 1 2 3 4 5
Primary Sum 100 100 100 100 100
Secondary Sum 100 200 300 400 500
Formula Index*100= 1*100 2*100 3*100 4*100 5*100

Remember this pattern of secondary sum, we need that later.

Hmm, now if a query comes for this interval [3, 5], output can be found with this formula

Secondary Sum of [3,5] – Primary sum of [3,5] * (3-1)

Generalized Equation:

For interval [ i , j ],

Output will be: Secondary sum of [ i , j] – Primary sum of [ i, j] * (i-1)

Solve this equation for arbitrary interval; check that you really get the correct output.

Now update process is kinda complex. For update process, you need to update both primary sum and secondary sum. Updating primary sum is trivial, but secondary sum is some Mathematical.

For secondary sum, we need to preserve the pattern property as we gave during the initialization.

If the operation comes like this way:

change 3 5 3

Index 1 2 3 4 5
Primary Sum Before Change 100 100 100 100 100
Secondary Sum before Change 100 200 300 400 500
Primary Sum After Change 100 100 103 103 103 
Secondary Sum after Change 100 100 Secondary Sum before Change+ index*u=300+ 3*3=309


400+4*3=412 500+5*3=515

So now its upon you that how do you handle the lazy propagation.

Again If you need the code, please let me know.  🙂



Problem Statement is here: http://www.spoj.pl/problems/YODANESS/

Summary:  at first you will be given an order of strings, and then you will be given another order of strings. You need to find that how many pairs of words in the order are not in relative order.

Algorithm: Segment Tree [http://wcipeg.com/wiki/Segment_tree ], BIT

This Problem is solvable with both Segment tree and BIT. But I don’t know BIT very well, that’s why I am describing the segment tree approach.

Let’s check the first sample case first line:

in the force strong you are

We can give the words some number id, so that we can identify them easily.

If id of the word in is 1, then id of the word the is 2 and so on, then we can rewrite the sentence with numbers like this way

1 2 3 4 5 6

Now let’s see the second line:

you are strong in the force

Now we can change this to integers with the value of the first order. Then we can rewrite it as-

5 6 4 1 2 3

As you word have id of 5, are word have id of 6 and so on.

Now what’s the benefit of this? Let’s check.

We will use Segment Tree now-

We will sort the value in Descending order with keeping the index information. Here’s the table will become-


6 5 4 3 2 1
Index 2 1 3 6 5 4

Now we will run loop on this sequence then do some query and update.

Initially we will assume that our array is empty.

First iteration:

I get value of 6 and index of 2. So we will do sum query on this [1, 2] interval, then add it with ultimate result. Now update the index 2 with 1. Array becomes


1 2 3 4 5 6
Value 1

Second Iteration:

I have value of 5, and index of 1, So again do a sum query on the interval [1,1] and add the result with ultimate result. And obviously update the index with 1. So the array becomes

Index 1 2 3 4 5 6
Value 1 1

Third Iteration:

Now Value is 4, index is 3, now do the query on [1,3], you will get 2, because before 4, we have 5,6 which are not in relative order with 4. So add 2 with the ultimate result. Update the index 3 with 1.

Index 1 2 3 4 5 6
Value 1 1 1

So on….

I am not showing the segment tree code… If you really need it… I will add it up…