Deforestation

시간 제한2초메모리 제한2048 MB

요약
수직선 위의 점들과 각 구간마다 최소한 남아 있어야 하는 점의 개수를 정하는 제약이 주어질 때, 지울 수 있는 점의 최대 개수를 구한다.
난이도

어려움10점 중 8점

유형
그리디, 정렬, 누적 합, 구현
정답자
아직 제출이 없습니다

문제

Farmer John is expanding his farm! He has identified the perfect location in the Red-Black Forest, which consists of NN trees (1≤N≤1051 \le N \le 10^5) on a number line, with the ii-th tree at position x_ix\_i (−109≤x_i≤109-10^9 \le x\_i \le 10^9).

Environmental protection laws restrict which trees Farmer John can cut down to make space for his farm. There are KK restrictions (1≤K≤1051 \leq K \leq 10^5) specifying that there must always be at least t_it\_i trees (1≤t_i≤N1 \leq t\_i \leq N) in the line segment \[l_i,r_i]\[l\_i, r\_i], including the endpoints (−109≤l_i≤r_i≤109-10^9 \le l\_i \leq r\_i \le 10^9). It is guaranteed that the Red-Black Forest initially satisfies these restrictions.

Farmer John wants to make his farm as big as possible. Please help him compute the maximum number of trees he can cut down while still satisfying all the restrictions!

입력

Each input consists of TT (1≤T≤101 \le T \le 10) independent test cases. It is guaranteed that the sums of all NN and of all KK within an input each do not exceed 3⋅1053 \cdot 10^5.

The first line of input contains TT. Each test case is then formatted as follows:

  • The first line contains integers NN and KK.
  • The next line contains the NN integers x_1,…,x_Nx\_1, \dots, x\_N.
  • Each of the next KK lines contains three space-separated integers: l_il\_i, r_ir\_i and t_it\_i.

출력

For each test case, output a single line with an integer denoting the maximum number of trees Farmer John can cut down.

힌트

For the first test case, Farmer John can cut down the first 44 trees, leaving trees at x_i=2,6,7x\_i = 2, 6, 7 to satisfy the restriction.

For the second test case, the additional restriction does not affect which trees Farmer John can cut down, so he can cut down the same trees and satisfy both restrictions.

For the third test case, Farmer John can only cut down at most 33 trees because there are initially 77 trees but the second restriction requires him to leave at least 44 trees uncut.

예제1

  1. 예제 1

    입력
    3
    7 1
    8 4 10 1 2 6 7
    2 9 3
    7 2
    8 4 10 1 2 6 7
    2 9 3
    1 10 1
    7 2
    8 4 10 1 2 6 7
    2 9 3
    1 10 4
    
    예상 출력
    4
    4
    3