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

#PS0000223. Truy vấn tổng hình hộp bốn chiều (Four-dimensional Prefix Sum)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Truy vấn tổng hình hộp bốn chiều (Four-dimensional Prefix Sum)

Four-dimensional Prefix Sum

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given a static four-dimensional integer array of sizes n1,n2,n3,n4n_1,n_2,n_3,n_4, indexed from 1, each query gives eight integers l1,r1,l2,r2,l3,r3,l4,r4l_1,r_1,l_2,r_2,l_3,r_3,l_4,r_4. Sum entries whose coordinates satisfy lt≤it≤rtl_t\le i_t\le r_t for all four axes. All intervals are inclusive and nonempty.

Input

The first line contains n1,n2,n3,n4,Qn_1,n_2,n_3,n_4,Q. The next n1n2n3n_1n_2n_3 lines contain n4n_4 integers each, in increasing lexicographic order of (i1,i2,i3)(i_1,i_2,i_3), with i4i_4 increasing within a line. Then follow QQ lines of l1,r1,l2,r2,l3,r3,l4,r4l_1,r_1,l_2,r_2,l_3,r_3,l_4,r_4.

Output

Print QQ lines, one answer per query.

Subtasks

  • Subtask 1 (20 points): 1≤nt≤31\le n_t\le 3 (1≤t≤41\le t\le4); 1≤Q≤301\le Q\le 30; ∣ai1,i2,i3,i4∣≤109|a_{i_1,i_2,i_3,i_4}|\le10^9; 1≤lt≤rt≤nt1\le l_t\le r_t\le n_t (1≤t≤41\le t\le4).
  • Subtask 2 (30 points): 1≤nt≤201\le n_t\le 20 (1≤t≤41\le t\le4); 1≤Q≤1000001\le Q\le 100000; ∣ai1,i2,i3,i4∣≤109|a_{i_1,i_2,i_3,i_4}|\le10^9; 1≤lt≤rt≤nt1\le l_t\le r_t\le n_t (1≤t≤41\le t\le4); lt=rtl_t=r_t (1≤t≤41\le t\le4).
  • Subtask 3 (50 points): 1≤nt≤201\le n_t\le 20 (1≤t≤41\le t\le4); 1≤Q≤1000001\le Q\le 100000; ∣ai1,i2,i3,i4∣≤109|a_{i_1,i_2,i_3,i_4}|\le10^9; 1≤lt≤rt≤nt1\le l_t\le r_t\le n_t (1≤t≤41\le t\le4).

Example 1

Input

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

Output

36
14
3

Explanation

The first query includes all eight entries; the second includes 6 and 8; the third selects 3.

Example 2

Input

1 1 1 2 1
-5 2
1 1 1 1 1 1 1 2

Output

-3

Explanation

The two entries sum to −3.