Under construction

Still being built. Things may change

Comparing Plants

Time limit4sMemory limit1024 MB

Summary
Given k and the counts r[i] of taller plants among the next k-1 plants in a circle, decide for each query pair whether one plant is always taller than the other.
Level

Hard10 of 10

Topics
Prefix sum, Segment tree, Graph
Solved
No attempts yet

Problem

Hazel the botanist visited a special exhibition in the Singapore Botanical Gardens. The exhibition has nn plants of distinct heights placed in a circle. The plants are labelled from 00 to n−1n-1 in clockwise order, with plant n−1n-1 beside plant 00.

For each plant ii (0≤i≤n−10 \leq i \leq n-1), Hazel compared plant ii with each of the next k−1k-1 plants in clockwise order and wrote down r[i]r[i], the number of these k−1k-1 plants that are taller than plant ii. Each r[i]r[i] therefore depends on the relative heights of kk consecutive plants.

For example, let n=5n=5, k=3k=3, and i=3i=3. The next k−1=2k-1=2 plants clockwise from plant 33 are plants 44 and 00. If plant 44 is taller than plant 33 and plant 00 is shorter than plant 33, Hazel writes r[3]=1r[3]=1.

You may assume Hazel recorded every r[i]r[i] correctly. At least one arrangement of distinct heights matches these values.

You need to compare the heights of qq pairs of plants. You cannot enter the exhibition. Your only information is the value kk and the sequence r[0],…,r[n−1]r[0], \ldots, r[n-1] in Hazel's notebook.

For each pair of different plants xx and yy, decide which of the following holds:

  • Plant xx is definitely taller than plant yy: in every arrangement of distinct heights h[0],…,h[n−1]h[0], \ldots, h[n-1] consistent with rr, h[x]>h[y]h[x] > h[y].
  • Plant xx is definitely shorter than plant yy: in every consistent arrangement, h[x]<h[y]h[x] < h[y].
  • The comparison is inconclusive: neither of the above holds.

Constraints

2≤k≤n≤2000002 \leq k \leq n \leq 200000

1≤q≤2000001 \leq q \leq 200000

0≤r[i]≤k−10 \leq r[i] \leq k-1 (for all 0≤i≤n−10 \leq i \leq n-1)

0≤x<y≤n−10 \leq x < y \leq n-1

At least one arrangement of distinct heights is consistent with rr.

Examples1

  1. Example 1

    Input
    2 2
    1 0
    1
    0 1
    
    Expected output
    -1