有N個花園連續編號爲1到N,每個花園都有慈胡蘿蔔。 我們必須在陣列的每個可能的連續子序列的花園中存儲總胡蘿蔔的值。如何解決給定內存限制的給定場景?
現在我們已經得到的數組進行排序,並回答以下的問答查詢。在每個查詢中,我們想知道上面獲得的排序數組中L到R(包括兩個端點)的值的總和。
樣品測試案例
Input:
3 3 //First number is the total no. of gardens. Second is no. of queries
4 9 1 //No. of carrots in each of the gardens
1 6 // Query return sum from L to R.
2 4
3 3
Output:
51 23 9 // Respective Output for 3 queries.
說明
Gardens [1, 2, 3] has [4, 9, 1] carrots respectively.
All possible continuous gardens are { [1], [2], [3], [1, 2], [2, 3], [1, 2, 3] } .
Sum of carrots in each subgardens is {4, 9, 1, 13, 10, 14}
Sorted array is {1, 4, 9, 10, 13, 14} .
Now Queries for 1 6 sum is 1+4+9+10+13+14 which is 51,
next 2 4 so 4+9+10 hence 23, and 3 3 which is 9.
現在我已經解決了使用模擬/後綴總和,但原來的問題,這個問題有很大的約束
1 ≤ No. of gardens ≤ 2*10^5
1 ≤ Carrots in a particular garden ≤ 100
1 ≤ Li ≤ Ri ≤ N(N+1)/2
1 ≤ No. of queries ≤ 20
現在,當我嘗試創建N爲2 * 10^5的所有可能的連續子序列。我得到的連續子序列大約是10^10,這太大而不能存儲在數組中。
,這是什麼可能的解決方法,我該怎麼回答查詢,而無需實際存儲所有的連續序列的總和?
什麼是對查詢數量的限制? – Yerken
對查詢添加約束條件 –