Under construction

Still being built. Things may change

Pyramids

Interview

Time limit1sMemory limit1024 MB

Summary
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 NN pyramids numbered from 00 to N−1N - 1, where pyramid ii (0≤i<N0 \le i < N) consists of A[i]A[i] stones. He also had the latest catalogue of the most fashionable pyramids of the year. The catalogue has NN pyramids numbered from 00 to N−1N - 1, where pyramid ii (0≤i<N0 \le i < N) consists of B[i]B[i] stones.

For any xx and yy such that 0≤x≤y<N0 \le x \le y < N, a range of pyramids A[x..y]A[x..y] is the sequence A[x],A[x+1],…,A[y]A[x], A[x + 1], \dots, A[y]. A range B[x..y]B[x..y] is defined the same way.

Every day, Khufu browses the catalogue and chooses two ranges A[L..R]A[L..R] and B[X..Y]B[X..Y] where R−L=Y−XR - L = Y - X. The values of LL, RR, XX, and YY may differ from day to day. He then wants to know whether the range A[L..R]A[L..R] can be transformed into the range B[X..Y]B[X..Y]. 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 LL, RR, XX, and YY, determine whether A[L..R]A[L..R] can be transformed into B[X..Y]B[X..Y]. 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.

  • 1≤N≤1000001 \le N \le 100000
  • 1≤Q≤1000001 \le Q \le 100000
  • 1≤A[i]≤1091 \le A[i] \le 10^9
  • 1≤B[i]≤1091 \le B[i] \le 10^9

In each call to can_transform:

  • 0≤L≤R<N0 \le L \le R < N
  • 0≤X≤Y<N0 \le X \le Y < N
  • R−L=Y−XR - L = Y - X

Examples1

  1. Example 1

    Input
    1 1
    1
    1
    0 0 0 0
    
    Expected output
    YES