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

#PS0000318. Chọn cấu trúc dữ liệu phù hợp (Choose the Data Structure)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Chọn cấu trúc dữ liệu phù hợp (Choose the Data Structure)

Choose the Data Structure

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

There are 10 independent array-processing scenarios. Each scenario gives only operation types and their order, not actual array values. Operation codes are:

  • 1: add a value to one element.
  • 2: add a value to every element in a range.
  • 3: query one element's value.
  • 4: query a range sum.
  • 5: query a range minimum.
  • 6: query a range XOR.

Each scenario contains at least one query (code 3 through 6). No scenario contains both types 2 and 6. Queries must receive results in operation order. For this exercise, a scenario is offline in the narrow sense if every update precedes every query.

Several structures may solve the same problem. For a unique answer, choose the label according to the following exercise-specific priority rules, taking the first applicable condition:

  • No updates: choose SPARSE if there is a minimum query; otherwise choose PREFIX (including prefix XOR).
  • There are updates, all updates precede all queries, and every query is a point query: choose DIFFERENCE.
  • There are updates, every query is a point or range-sum query, and range updates and range-sum queries do not both occur: choose FENWICK.
  • In every remaining scenario choose SEGMENT.

These are primary labels under this learning convention, not claims that other methods cannot work. Do not execute updates or compute query values.

Input

The input contains exactly 10 blocks, each consisting of two lines:

  • The first line contains integer mjm_j, the number of operations in scenario jj.
  • The next line contains exactly mjm_j integers tj,1,…,tj,mjt_{j,1},\ldots,t_{j,m_j}, the operation codes in execution order.

Output

Print exactly 10 lines, each containing one of PREFIX, DIFFERENCE, FENWICK, SPARSE, SEGMENT, in scenario order.

Subtasks

  • Subtask 1 (20 points): 1≤j≤101\le j\le10; 1≤mj≤2000001\le m_j\le200000; ∑j=110mj≤200000\sum_{j=1}^{10}m_j\le200000; 1≤tj,i≤61\le t_{j,i}\le6 for 1≤i≤mj1\le i\le m_j. Each scenario has at least one code from 3 to 6 and never contains both codes 2 and 6. All operation codes belong to {3,4,5,6}\{3,4,5,6\}.
  • Subtask 2 (30 points): 1≤j≤101\le j\le10; 1≤mj≤2000001\le m_j\le200000; ∑j=110mj≤200000\sum_{j=1}^{10}m_j\le200000; 1≤tj,i≤61\le t_{j,i}\le6 for 1≤i≤mj1\le i\le m_j. Each scenario has at least one code from 3 to 6 and never contains both codes 2 and 6. All operation codes belong to {1,3,4}\{1,3,4\}.
  • Subtask 3 (50 points): 1≤j≤101\le j\le10; 1≤mj≤2000001\le m_j\le200000; ∑j=110mj≤200000\sum_{j=1}^{10}m_j\le200000; 1≤tj,i≤61\le t_{j,i}\le6 for 1≤i≤mj1\le i\le m_j. Each scenario has at least one code from 3 to 6 and never contains both codes 2 and 6. All operation codes belong to {1,2,3,4,5,6}\{1,2,3,4,5,6\}.

Example 1

Input

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

Output

PREFIX
SPARSE
DIFFERENCE
FENWICK
FENWICK
FENWICK
SEGMENT
SEGMENT
SEGMENT
FENWICK

Explanation

  • Scenario 1: 4 6 contains only sum and XOR queries and no updates, so choose PREFIX.
  • Scenario 2: 5 4 contains a minimum query with no updates, so choose SPARSE.
  • Scenario 3: 2 1 3 has two updates followed by a point query, so choose DIFFERENCE.
  • Scenario 4: 3 1 3 has an update after a query, so the offline difference rule does not apply; choose FENWICK.
  • Scenario 5: 1 4 adds at a point and queries a range sum, so choose FENWICK.
  • Scenario 6: 3 2 3 interleaves a range addition with point queries, so choose FENWICK.
  • Scenario 7: 2 4 combines range addition and range sum, so the exercise convention selects SEGMENT.
  • Scenario 8: 1 5 updates a point and asks for a minimum, so choose SEGMENT.
  • Scenario 9: 1 6 updates a point and asks for XOR, so choose SEGMENT.
  • Scenario 10: 1 3 4 contains a point update and point and range-sum queries; the sum query excludes DIFFERENCE, so choose FENWICK.