Colorful Village
InterviewTime limit2sMemory limit512 MB
Maintain N houses under range repaint operations and answer queries counting how many of the T colors appear in a range.
- Level
Medium6 of 10
- Topics
- Segment tree, Bit manipulation, Linked list, Array
- Solved
- No attempts yet
Problem
Minho manages the country of Cheon, which has N houses. To keep track of them he calls the houses 1, 2, ... N.
One day Minho decided he wanted to repaint every house in a chosen range with a single color, and also to ask how many different colors appear in a chosen range.
There are two kinds of operations.
C x y z: paint house x, house y, and every house between them with color z.Q x y: print how many different colors appear on house x, house y, and every house between them.
Minho uses colors 1, 2, ... T, and at the start every house is painted with color 1.
Write a program that carries out Minho's operations in order.
Input
The first line contains N, T, and Q separated by spaces (, , ): the number of houses, the number of colors, and the number of operations.
Each of the next Q lines contains one operation, given as C x y z or Q x y, with , , and . The value of x may be larger than y. In that case the operation still applies to the range between the two numbers, that is, houses through .
Output
For each Q x y operation, print on its own line how many different colors appear on house x, house y, and every house between them.