Lazy Propagation লেবেলটি সহ পোস্টগুলি দেখানো হচ্ছে৷ সকল পোস্ট দেখান
Lazy Propagation লেবেলটি সহ পোস্টগুলি দেখানো হচ্ছে৷ সকল পোস্ট দেখান

মঙ্গলবার, ৮ ফেব্রুয়ারি, ২০১১

Codechef - Flipping coins

Problem statement

A relatively easier segment tree problem before going to sleep. I wanted to pass it by one try but one logical error was the reason for the incorrect submission. I kept a lazy value for each node.

Code

Mistakes:

1. Logical error : <to be completed>



PKU 2777 - Count Color

Problem statement

There are N (<=100000) strips of something (say paper). Initially all of them are painted white. You'll be given some queries, two types of them.

1. Paint a range (a, b) with color x (x<=30)
2. Print the number of different colors in range (a,b)

A really nice problem. Another segment tree problem which requires lazy propagation. I wrote a solution with keeping an array of bools for each color in each node. At first it got couple of WA: I didn't really read the input specification thoroughly. There can be some queries with a>b. It again got WA, I carefully checked the code and realized that my query function wasn't returning anything! (It was returning the correct output, thanks to the stack) A quick fix brought a TLE. I realized that the array of bools can be replaced by a bitmask of int (as there can be as many as 30 colors). That change finally made it AC.

Here is the code.

Mistakes:

1. Not reading the problem statement carefully.
2. Trying to finish the code in a hurry after getting the idea.
3. Forgetting to put return statement.

PKU 3468 - A Simple Problem with Integers

Problem statement


You'll be given an array of N<=100000 integers. And some M<=100000 queries. The queries are of two types: one asks to print the sum of values of a range, the other asks you to add some value to all the numbers in a range.

It's a standard application of lazy propagation or late update, whatever people prefer to say. It's a technique to update a range of values efficiently.

The following things are done when using late update: (Every time I use node as an array index, I mean it to be the current node)

1. During an update, every time a segment is fully inside the query interval, stop it right there. Update that interval. And remember that some value was added to the interval, like lazy[node] += x;

2. Otherwise, explore the left child and right child. Suppose you are at some node. If the value of lazy[node] is non zero, then it's known that the current node was updated but it's child wasn't. Update both child, and add the lazy value of current node to it's child nodes. Set the lazy[node] = 0;

3. Normal parent from child update follows.

4. During the retrieval only step number 2 should be taken care of.

Here is the code.

Mistakes I made:

1. Mixing index with endpoints, got two WAs, didn't even find the bug the first time :|