Maintain a sequence of prices under range-add and range-reverse, and after each update output the number of contiguous segments whose values are strictly increasing.
Hard9Segment treeArrayDynamic programmingImplementationNo attempts yetTime limit5sMemory limit512 MB
Shoe Wariwap is the most famous shoe boutique in the world. Every day the shop displays N of the finest pairs of shoes ever made. Each pair stands on its own pedestal, and the pedestals are arranged in a row. The pedestals are numbered from 1 to N, where pedestal 1 is the leftmost one.
When Shoe Wariwap opens, the pair on pedestal i is worth pi pesos. During the day the manager may raise prices or shuffle the display order so that the less appreciated pairs get more attention. At any moment the manager picks a set of pedestals
Si,j={i,i+1,…,j−1,j}
and does one of the following.
Today is the birthday of one of Shoe Wariwap's best customers, Imelda. If it were up to her, she would buy all N pairs. Her husband Marshall tells her they are deep in debt, so the two agree on a system that decides which pairs she buys.
The system works like this. Imelda picks two integers a and b with a≤b. Her chosen shoes are the ones that currently stand on pedestals a through b. Marshall adds one more condition, which the couple fondly calls Marshall's Law. Marshall's Law says that Imelda's chosen shoes, read from left to right, must increase in price. That is, if m<n, then the pair on pedestal n must be more expensive than the pair on pedestal m. If the chosen shoes break Marshall's Law, the couple goes home with nothing and Marshall vows never to return to Shoe Wariwap.
Imelda is still excited about her big shopping day. Every time the manager changes something, she wants to know how many ways she can fill her shopping bag while obeying Marshall's Law. She would never go home empty handed on her birthday, so she buys at least one pair. For example, consider N=5 pairs with the following prices.
| Shoe A | Shoe B | Shoe C | Shoe D | Shoe E |
|---|---|---|---|---|
| 69000 pesos | 1000 pesos | 1000 pesos | 2000 pesos | 3000 pesos |
| Pedestal 1 | Pedestal 2 | Pedestal 3 | Pedestal 4 | Pedestal 5 |
Here is everything Imelda's shopping bag might contain.
Every other combination breaks Marshall's Law, so there are 8 possibilities in total.
You are her loyal servant, and you must answer her question at any point during the day.
The first line contains a single integer T, the number of test cases.
The first line of each test case contains two space separated integers N, the number of pairs of shoes, and Q, the number of changes the manager makes. The next line contains N space separated integers p1,p2,…,pN, the initial prices.
The next Q lines describe the changes in chronological order, one per line. Each line has one of the following formats.
INC i j k describes an INCREASE operation as explained in the statement.REV i j describes a REVERSE operation as explained in the statement.Constraints
Every time the manager makes a change, whether to a price or to the order, print a line with a single integer X. X is the number of ways Imelda can fill her shopping bag right after that change while obeying Marshall's Law.