Problem link: http://www.spoj.com/problems/PATULJCI/

Solution link: http://ideone.com/NqSI3j

Verdict: TLE

I tried to use Mo's algorithm and segment tree to solve the problem. How do I further optimize my solution? Any help is really appreciated.

Before contest

Codeforces Round #393 (Div. 1) (8VC Venture Cup 2017 - Final Round Div. 1 Edition)

22:25:59

Register now »

Codeforces Round #393 (Div. 1) (8VC Venture Cup 2017 - Final Round Div. 1 Edition)

22:25:59

Register now »

*has extra registration

Before contest

Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition)

22:25:59

Register now »

Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition)

22:25:59

Register now »

*has extra registration

# | User | Rating |
---|---|---|

1 | tourist | 3739 |

2 | Petr | 3400 |

3 | moejy0viiiiiv | 3282 |

4 | W4yneb0t | 3141 |

5 | mnbvmar | 3122 |

6 | matthew99 | 3100 |

7 | ainta | 3051 |

8 | rng_58 | 3030 |

9 | LHiC | 2997 |

10 | V--o_o--V | 2980 |

# | User | Contrib. |
---|---|---|

1 | Errichto | 169 |

2 | Petr | 163 |

3 | rng_58 | 161 |

4 | Zlobober | 153 |

5 | Swistakk | 149 |

5 | zscoder | 149 |

7 | Um_nik | 147 |

8 | GlebsHP | 145 |

8 | csacademy | 145 |

10 | Edvard | 141 |

Problem link: http://www.spoj.com/problems/PATULJCI/

Solution link: http://ideone.com/NqSI3j

Verdict: TLE

I tried to use Mo's algorithm and segment tree to solve the problem. How do I further optimize my solution? Any help is really appreciated.

↑

↓

Codeforces (c) Copyright 2010-2017 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jan/21/2017 22:39:02 (c2).

Desktop version, switch to mobile version.
User lists

Name |
---|

You need O((n + q) log n) to get AC, or something very close to that.

Try to only use one segment tree, that gives you any "dominant color" in an interval, for which you then check if it appears enough times in the interval. The solution is completely online.

How to use segment tree in this case?

Notice that in a node of the segtree, its dominating color, if there is one, has to be a doninating color of one or both of its children. Then, you can store in each node the dominating color. To merge nodes, you can query the number of each dominating color in both nodes, and see if they can be dominating in the new node. You can do this with order statistic tree or BIT and get O(log^2 n) time per query.

To get log n time, I think you can have your segtree store the 2 colors with most number of occurrences, and the number of times they occur. Now observe that a dominating color on the new interval has to be among the colors in the top 2 for each node, so you can check all of those colors using segtree values.

Oh, sorry! My bad :)

About the log n approach, I don't think storing the 2 colors that appear the most is enough. For example, every color on a segment may appear only once. But the general idea seems correct, thanks for the anawer.

Yea the number of times each one appears should also be stored

An alternate solution is by using persistent segment trees. At a node if we have two subtrees with sum of frequencies as

S_{1}andS_{2}, our answer either exists in the subtree with greater sum or doesn't exist. This gets AC inO((N+Q) *logN).There is a solution with but I describe the one.

Let

occ(x,L,R) as a function that returns the number ofi's such thatL≤i<Randa_{i}=x. It's easy to implement, for each color likex, keep a vector of indexes thata_{i}=xthen answer is`lower_bound(vec[x].begin(), vec[x].end(), R) - lower_bound(vec[x].begin(), vec[x].end(), L)`

.Now use segment tree. Consider a node for segment [L, R), keep the color that appears in [L, R) strictly more than (

R-L) / 2 in that range, if there is no color satisfies the condition, save -1 there.It's easy to merge two nodes:

Here is my code : Link.

This problem can also be solved with a randomised algorithm. You can see my solution here. The details can be found out in the editorial of this problem on codechef which is a harder version of the above problem with point updates as well.