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

#FT0000231. Bài toán những viên kẹo (Many Sweets Problem)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Bài toán những viên kẹo (Many Sweets Problem)

Many Sweets Problem

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a sequence of positive integers A=(A1,A2,…,AN)A=(A_1,A_2,\ldots,A_N) representing the deliciousness of sweets, indexed from 11.

Process QQ queries in order. Each query consists of five integers c x l r k and executes two consecutive steps:

  1. Assign Ac=xA_c=x. This change affects the current and all subsequent queries.
  2. Consider the r−l+1r-l+1 sweets with deliciousness values Al,Al+1,…,ArA_l,A_{l+1},\ldots,A_r after the assignment. Choose a subset of these sweets, taking each sweet at most once, to reach a total deliciousness at least kk. Find the minimum number of sweets needed.

If even eating every sweet in the interval cannot reach kk, output -1. Choosing sweets does not remove them from the sequence: only the point assignment changes AA.

Input

  • The first line contains integers N,QN,Q, the sequence length and number of queries.
  • The second line contains exactly NN integers A1,A2,…,ANA_1,A_2,\ldots,A_N.
  • Each of the next QQ lines contains five integers c,x,l,r,kc,x,l,r,k, specifying the updated position, its new value, the inclusive query interval, and the required deliciousness.

Output

Print QQ lines. Line ii contains the minimum number of sweets to select after assignment ii, or exactly -1 if the required total is unreachable.

Subtasks

  • Subtask 1 (20 points): 1≤N≤81\le N\le8; 1≤Q≤351\le Q\le35; 1≤Ai,x≤1091\le A_i,x\le10^9; 1≤c≤N1\le c\le N; 1≤l≤r≤N1\le l\le r\le N; 1≤k≤10151\le k\le10^{15}.
  • Subtask 2 (30 points): 1≤N≤15001\le N\le1500; 1≤Q≤30001\le Q\le3000; 1≤Ai,x≤1091\le A_i,x\le10^9; 1≤c≤N1\le c\le N; 1≤l≤r≤N1\le l\le r\le N; 1≤k≤10151\le k\le10^{15}.
  • Subtask 3 (50 points): 1≤N,Q≤1051\le N,Q\le10^5; 1≤Ai,x≤1091\le A_i,x\le10^9; 1≤c≤N1\le c\le N; 1≤l≤r≤N1\le l\le r\le N; 1≤k≤10151\le k\le10^{15}.

Examples

Input

7 5
8 2 4 1 7 3 6
1 1 4 7 9
5 2 1 3 8
6 4 1 5 9
6 5 3 5 1
7 9 4 6 4

Output

2
-1
4
1
1

Explanation

The initial sequence is (8,2,4,1,7,3,6)(8,2,4,1,7,3,6), and every assignment happens before selecting sweets.

  1. Set A1=1A_1=1. Interval [4,7][4,7] is (1,7,3,6)(1,7,3,6). Its largest sweet has value 7<97<9, while choosing values 77 and 66 reaches 13≥913\ge9. Answer: 2.
  2. Set A5=2A_5=2. Interval [1,3][1,3] is (1,2,4)(1,2,4), whose total is only 7<87<8. Answer: -1.
  3. Set A6=4A_6=4. Interval [1,5][1,5] is (1,2,4,1,2)(1,2,4,1,2). The three largest values sum to 8<98<9; four sweets can sum to 99. Answer: 4.
  4. Set A6=5A_6=5. Interval [3,5][3,5] is (4,1,2)(4,1,2). One sweet is enough to reach k=1k=1. Answer: 1.
  5. Set A7=9A_7=9. Interval [4,6][4,6] is (1,2,5)(1,2,5). One sweet of value 55 reaches k=4k=4. Answer: 1.

Selecting sweets only computes the answer and never removes values from the array.