Cask contrast queries
HardOnline Assessment
For each guest sample, find the barrel in a stretch of a winery's cellar that contrasts with it the most.
The cellar has n barrels in a single rack, numbered 1 to n. A stamping machine burned a flavour code into each one: barrel 1 has code seed, and every following barrel has code (previous * mult + add) % mod. A code is a bit pattern in which each bit stands for one tasting note, and more significant bits are more important notes. The contrast between two codes is their bitwise XOR, read as a number.
A guest brings a sample with code x and asks about the barrels numbered l to r inclusive. Among those barrels, find the greatest contrast with x, and name the barrel that provides it. If several barrels give the greatest contrast (their codes are equal), name the one with the smallest number.
Input: n, seed, mult, add, mod, and queries, a list of [l, r, x] with 1 <= l <= r <= n.
Output: a list with one entry [contrast, barrel] per query, in order.
The cellar can hold tens of thousands of barrels and there can be hundreds of questions, so checking every barrel for every question is too slow.
Example 1
- Input:
- [6,5,3,1,16,[[1,6,3],[2,4,6],[4,4,4]]]
- Output:
- [[14,5],[7,3],[0,4]]
- Explanation
- The codes are 5, 0, 1, 4, 13, 8. For
[1, 6, 3]the contrasts with3are 6, 3, 2, 7, 14 and 11; the greatest is 14, from barrel 5. For[2, 4, 6]the contrasts are 6, 7 and 2, so barrel 3 gives 7. A one-barrel range,[4, 4, 4], has contrast 0 with barrel 4.
Example 2
- Input:
- [8,1,1,4,12,[[2,8,2],[4,8,2],[1,3,8]]]
- Output:
- [[11,3],[11,6],[13,2]]
- Explanation
- The codes are 1, 5, 9, 1, 5, 9, 1, 5. For
[2, 8, 2]the contrasts of codes 5, 9 and 1 with2are 7, 11 and 3, so the best is 11. Codes 9 appear at barrels 3 and 6, and the smaller number, 3, is reported. For[4, 8, 2]the first barrel with code 9 inside the range is 6, not 3. For[1, 3, 8]the contrasts are 9, 13 and 1, so barrel 2 gives 13.
Example 3
- Input:
- [6,1000,3,1,1024,[[1,3,1023],[2,6,1023],[6,6,0]]]
- Output:
- [[211,3],[879,5],[433,6]]
- Explanation
- The codes are 1000, 953, 812, 389, 144, 433. A sample
x = 1023(all notes) contrasts with a code by its complement: 23, 70, 211, 634, 879 and 590. Over barrels 1 to 3 the greatest is 211, from barrel 3. Over barrels 2 to 6 it is 879, from barrel 5. For the single barrel in[6, 6, 0]the contrast is its own code, 433.
Constraints
1 <= n <= 60,0002 <= mod <= 65,536;0 <= seed, mult, add < mod;0 <= x < 65,5360 <= queries.length <= 900;1 <= l <= r <= n