PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000316. Vượt qua bẫy (A Game with Traps)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Vượt qua bẫy (A Game with Traps)

A Game with Traps

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

In a game you command mm soldiers. Soldier ii has agility aia_i. The level is the segment from position 00 to n+1n+1; initially you and the soldiers are at 00.

There are kk traps. Trap jj is located at ljl_j, has danger level djd_j, and can be disabled instantly when you are at rjr_j. A soldier with agility below djd_j cannot enter ljl_j while this trap is active. Traps do not affect you.

Before starting, choose a squad. In one second, you can move one unit in either direction, alone or with the entire squad if you share its position. The squad cannot move independently. Disabling a trap takes no time. All positions remain integers after each action.

Find the largest squad that you can bring safely from 00 to n+1n+1 within tt seconds.

Input

  • The first line contains four integers m,n,k,tm,n,k,t: the soldier count, number of interior integer positions, trap count and time limit in seconds.
  • The second line contains mm integers a1,…,ama_1,\ldots,a_m, the soldiers’ agility values.
  • Each of the next kk lines contains three integers lj,rj,djl_j,r_j,d_j describing trap jj.

Output

Print one integer: the maximum number of soldiers that can reach the destination safely, or 0 if none can.

Subtasks

  • Subtask 1 (20 points): 1≤m,n,k,t≤201\le m,n,k,t\le20; n<tn<t; 1≤ai,dj≤201\le a_i,d_j\le20; 1≤lj≤rj≤n1\le l_j\le r_j\le n.
  • Subtask 2 (30 points): 1≤m,n,k,t≤2⋅1051\le m,n,k,t\le2\cdot10^5; n<tn<t; 1≤ai,dj≤2⋅1051\le a_i,d_j\le2\cdot10^5; 1≤lj=rj≤n1\le l_j=r_j\le n.
  • Subtask 3 (50 points): 1≤m,n,k,t≤2⋅1051\le m,n,k,t\le2\cdot10^5; n<tn<t; 1≤ai,dj≤2⋅1051\le a_i,d_j\le2\cdot10^5; 1≤lj≤rj≤n1\le l_j\le r_j\le n.

Examples

Input 1

5 6 4 14
1 2 3 4 5
1 5 2
1 2 5
2 3 5
3 5 3

Output 1

3

Explanation

Choose soldiers with agility 3, 4 and 5. Walk alone from 0 to 3, disable the two level-5 traps upon reaching positions 2 and 3, and return to 0. This takes 6 seconds; the squad then walks to 7 in 7 seconds, for a total of 13. A squad of four contains someone with agility at most 2. Dangerous intervals then cover positions 1 through 5; traversing these intervals alone and returning requires at least 10 extra steps in addition to the 7 squad steps. The total of at least 17 exceeds 14.