Pyramids
InterviewTime limit1sMemory limit1024 MB
Given two equal-length ranges of arrays A and B, decide whether A[L..R] can be turned into B[X..Y] by moving stones between adjacent pyramids.
- Level
Easy3 of 10
- Topics
- Prefix sum, Array
- Solved
- No attempts yet
Problem
Everyone knows that Pharaoh Khufu was a great ruler, but many are unaware that he was also a fashion enthusiast. Long ago, he had pyramids numbered from to , where pyramid () consists of stones. He also had the latest catalogue of the most fashionable pyramids of the year. The catalogue has pyramids numbered from to , where pyramid () consists of stones.
For any and such that , a range of pyramids is the sequence . A range is defined the same way.
Every day, Khufu browses the catalogue and chooses two ranges and where . The values of , , , and may differ from day to day. He then wants to know whether the range can be transformed into the range . A transformation performs the following step any number of times: take one stone from a pyramid within the range and move it to an adjacent pyramid within the range.
Your task is to answer queries of the following form. Given four integers , , , and , determine whether can be transformed into . The number of stones in each pyramid does not change. Khufu only wants to know whether one range can be transformed into the other.
Constraints
Time limit: 1000 ms. Memory limit: 1024 MB.
In each call to can_transform: