Universal Cup Judging System

Universal Cup

Limite de temps : 4.0 s Limite de mémoire : 1024 MB Points totaux : 100 Hackable ✓
Statistiques

In the year 3142, deep within the rings of Titan, the last archive of pre-Collapse human memory is sealed inside the Chronos Vault. Its entrance is guarded by the Echo Dial, a quantum-entangled lock composed of $n$ chronal rings arranged in a single filament. Each ring displays a phase value in the range $0$ to $m - 1$, indicating its alignment with the cosmic rhythm. Initially, the $i$-th ring shows the value $a_i$.

The Echo Dial can only be adjusted through resonant tuning, governed by the following rules:

  • In a single operation, you may choose any phase value $x \in [0, m)$ and simultaneously tune all rings currently displaying $x$ either:
    • forward tuning: $x \to (x + 1) \bmod m$, or
    • backward tuning: $x \to (x - 1) \bmod m$.
  • This operation affects every ring in the entire dial that currently resonates at phase $x$, regardless of position.

The Oracle of Echoes, the vault's sentient guardian, issues $q$ diagnostic trials. Each trial is given as a query $(l, r, v)$ and asks for the minimum number of resonant tuning operations required to set all rings in positions $l$ through $r$ (inclusive) to phase $v$.

All trials are hypothetical. The Echo Dial always starts from its original initial state for every query, and no trial alters the actual configuration of the dial. As the last Quantum Archivist, your duty is to answer all $q$ queries efficiently.

Input

The first line contains three integers $n, q$ ($1 \le n, q \le 2 \times 10^5$), and $m$ ($1 \le m \le 10^9$).

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i < m$), denoting the initial phase of each ring.

Each of the next $q$ lines contains three integers $l, r$ ($1 \le l \le r \le n$) and $v$ ($0 \le v < m$), describing a diagnostic trial.

Output

For each query, output a line containing an integer, denoting the minimum number of operations needed to set all rings in positions $[l, r]$ to phase $v$, assuming the dial starts from its original state and remains unchanged between queries.

Examples

4 6 7
2 0 2 5
1 4 3
2 4 6
1 3 1
1 3 5
3 4 0
3 3 2
5
4
2
4
4
0

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.