#P2294. Ledger Consistency Check
Ledger Consistency Check
Ledger Consistency Check
A merchant has maintained a ledger recording his monthly income for n months. For each month i (where i = 1,2,...,n), the income is given by a_i. When a_i>0, it means the merchant made a profit of a_i yuan, and when a_i<0, it indicates a loss of |a_i| yuan. The total income over a period is defined as the sum of the monthly incomes over that period.
Due to the sensitive nature of the investigation, the inspector, Chà, secretly viewed the ledger on m separate occasions while working for the merchant. Each time she viewed the ledger, she only managed to see the records for a contiguous segment of months and she memorized the total income for that segment. Specifically, for the jth query she recalls that the sum of the incomes from month lj to month rj is sj (in other words, \(\sum_{i=l_j}^{r_j}a_i = s_j\)).
Your task is to determine whether there exists any sequence \(a_1,a_2,\ldots,a_n\) (with each \(a_i\) being an integer) satisfying all m records. If such a sequence exists, the ledger may be authentic; otherwise, it is definitely fake.
Input constraints:
- The first line contains two integers \(n\) and \(m\) separated by a space.
- Each of the following m lines contains three integers \(l_j\), \(r_j\) and \(s_j\) where \(1 \le l_j \le r_j \le n\) and \(s_j\) is the remembered total income for that period.
Note: You can assume that the values of the integers are such that a valid solution, if it exists, will have all intermediate calculations within the range of standard 32-bit or 64-bit integer arithmetic.
inputFormat
The first line contains two integers \(n\) and \(m\). Then, m lines follow. Each of these lines contains three integers l, r and s, representing that the total income from month l to month r is s.
outputFormat
Output a single line containing YES
if there exists an array \(a_1, a_2, \ldots, a_n\) satisfying the m records, and NO
otherwise.
sample
4 3
1 2 3
2 4 2
1 4 5
YES