Koa and the Beach (Hard Version)
Contest: Codeforces Round 659 (Div. 2)
Difficulty rating: 2200
Tags: constructive algorithms, dp, greedy, implementation

The only difference between easy and hard versions is on constraints. In this version constraints are higher. You can make hacks only if all versions of the problem are solved.

Koa the Koala is at the beach!

The beach consists (from left to right) of a shore, $$$n+1$$$ meters of sea and an island at $$$n+1$$$ meters from the shore.

She measured the depth of the sea at $$$1, 2, \dots, n$$$ meters from the shore and saved them in array $$$d$$$. $$$d_i$$$ denotes the depth of the sea at $$$i$$$ meters from the shore for $$$1 \le i \le n$$$.

Like any beach this one has tide, the intensity of the tide is measured by parameter $$$k$$$ and affects all depths from the beginning at time $$$t=0$$$ in the following way:

- For a total of $$$k$$$ seconds, each second, tide increases all depths by $$$1$$$.
- Then, for a total of $$$k$$$ seconds, each second, tide decreases all depths by $$$1$$$.
- This process repeats again and again (ie. depths increase for $$$k$$$ seconds then decrease for $$$k$$$ seconds and so on ...).Formally, let's define $$$0$$$-indexed array $$$p = [0, 1, 2, \ldots, k - 2, k - 1, k, k - 1, k - 2, \ldots, 2, 1]$$$ of length $$$2k$$$. At time $$$t$$$ ($$$0 \le t$$$) depth at $$$i$$$ meters from the shore equals $$$d_i + p[t \bmod 2k]$$$ ($$$t \bmod 2k$$$ denotes the remainder of the division of $$$t$$$ by $$$2k$$$). Note that the changes occur instantaneously after each second, see the notes for better understanding.

At time $$$t=0$$$ Koa is standing at the shore and wants to get to the island. Suppose that at some time $$$t$$$ ($$$0 \le t$$$) she is at $$$x$$$ ($$$0 \le x \le n$$$) meters from the shore:

- In one second Koa can swim $$$1$$$ meter further from the shore ($$$x$$$ changes to $$$x+1$$$) or not swim at all ($$$x$$$ stays the same), in both cases $$$t$$$ changes to $$$t+1$$$.
- As Koa is a bad swimmer, the depth of the sea at the point where she is can't exceed $$$l$$$ at integer points of time (or she will drown). More formally, if Koa is at $$$x$$$ ($$$1 \le x \le n$$$) meters from the shore at the moment $$$t$$$ (for some integer $$$t\ge 0$$$), the depth of the sea at this point  — $$$d_x + p[t \bmod 2k]$$$  — can't exceed $$$l$$$. In other words, $$$d_x + p[t \bmod 2k] \le l$$$ must hold always.
- Once Koa reaches the island at $$$n+1$$$ meters from the shore, she stops and can rest.Note that while Koa swims tide doesn't have effect on her (ie. she can't drown while swimming). Note that Koa can choose to stay on the shore for as long as she needs and neither the shore or the island are affected by the tide (they are solid ground and she won't drown there).

Koa wants to know whether she can go from the shore to the island. Help her!

Input format:
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$)  — the number of test cases. Description of the test cases follows.

The first line of each test case contains three integers $$$n$$$, $$$k$$$ and $$$l$$$ ($$$1 \le n \le 3 \cdot 10^5; 1 \le k \le 10^9; 1 \le l \le 10^9$$$) — the number of meters of sea Koa measured and parameters $$$k$$$ and $$$l$$$.

The second line of each test case contains $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \le d_i \le 10^9$$$)  — the depths of each meter of sea Koa measured.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.

Output format:
For each test case:

Print Yes if Koa can get from the shore to the island, and No otherwise.

You may print each letter in any case (upper or lower).

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the following $$$s$$$ denotes the shore, $$$i$$$ denotes the island, $$$x$$$ denotes distance from Koa to the shore, the underline denotes the position of Koa, and values in the array below denote current depths, affected by tide, at $$$1, 2, \dots, n$$$ meters from the shore.

In test case $$$1$$$ we have $$$n = 2, k = 1, l = 1, p = [ 0, 1 ]$$$.

Koa wants to go from shore (at $$$x = 0$$$) to the island (at $$$x = 3$$$). Let's describe a possible solution:

- Initially at $$$t = 0$$$ the beach looks like this: $$$[\underline{s}, 1, 0, i]$$$.
- At $$$t = 0$$$ if Koa would decide to swim to $$$x = 1$$$, beach would look like: $$$[s, \underline{2}, 1, i]$$$ at $$$t = 1$$$, since $$$2 > 1$$$ she would drown. So Koa waits $$$1$$$ second instead and beach looks like $$$[\underline{s}, 2, 1, i]$$$ at $$$t = 1$$$.
- At $$$t = 1$$$ Koa swims to $$$x = 1$$$, beach looks like $$$[s, \underline{1}, 0, i]$$$ at $$$t = 2$$$. Koa doesn't drown because $$$1 \le 1$$$.
- At $$$t = 2$$$ Koa swims to $$$x = 2$$$, beach looks like $$$[s, 2, \underline{1}, i]$$$ at $$$t = 3$$$. Koa doesn't drown because $$$1 \le 1$$$.
- At $$$t = 3$$$ Koa swims to $$$x = 3$$$, beach looks like $$$[s, 1, 0, \underline{i}]$$$ at $$$t = 4$$$.
- At $$$t = 4$$$ Koa is at $$$x = 3$$$ and she made it!

We can show that in test case $$$2$$$ Koa can't get to the island.

Example input:
7
2 1 1
1 0
5 2 3
1 2 3 2 2
4 3 4
0 2 4 3
2 3 5
3 0
7 2 3
3 0 2 1 3 0 1
7 1 4
4 4 3 0 2 4 2
5 2 3
1 2 3 2 2

Example output:
Yes
No
Yes
Yes
Yes
No
No

Editorial:
Let's define positions $$$i$$$ such that ($$$1 \le i \le n$$$) and $$$d_i + k \le l$$$ as safe positions, also positions $$$0$$$ and $$$n + 1$$$ are safe too (ie. the shore and the island respectively). Remaining positions are unsafe.
Koa can wait indefinitely on safe positions without drowning, so she can reach the island (ie. position $$$n+1$$$) if and only if she can reach each safe position from the previous one.
Suppose Koa is at some safe position $$$i$$$ and wants to reach the next safe position $$$j$$$ ($$$0 \le i < j \le n + 1$$$):
A solution strategy for Koa is the following:
• If Koa is at an unsafe position $$$x$$$ at time $$$t_0$$$, she must swim to $$$x + 1$$$ as soon as she can, that is, at the first moment of time $$$t \ge t_0$$$ such that $$$d_{x + 1} + p[(t + 1) \bmod 2k] \le l$$$ (to not drown).
• If Koa is at a safe position $$$x$$$ at time $$$t_0$$$, she must wait to some moment of time $$$t_1$$$ such that tide is exactly at $$$k$$$ units. After that she must follow the unsafe positions strategy until the next safe position.
So a way to go from $$$i$$$ to $$$j$$$ would be, apply point $$$2$$$ on $$$i$$$, and apply point $$$1$$$ to reach each position $$$p$$$ such that ($$$i < p \le j$$$).
This works because:
• If there exists some position with $$$d_p$$$ greater than $$$l$$$ she would drown with any tide so let's assume that all positions are less or equal to $$$l$$$.
• Suppose Koa drowns at some position $$$p$$$, she can leave $$$i$$$ with some value of tide because $$$d_{i+1} \le l$$$ and as long as the tide is decreasing whether she chooses to wait or not she would be safe. So she must have drown with the tide increasing.
• If she leaves $$$i$$$ with other tide (different from $$$k$$$): suppose she would be able to reach position $$$p$$$, then the tide will have increased and it will be higher and therefore she would drown too, this is true because the tide never can be $$$k$$$ and start decreasing again between $$$i$$$ and $$$j$$$ because these positions are unsafe ones.
Time complexity: $$$O(n)$$$ per test case