#PS0000318. Chọn cấu trúc dữ liệu phù hợp (Choose the Data Structure)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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
SPARSEif there is a minimum query; otherwise choosePREFIX(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 , the number of operations in scenario .
- The next line contains exactly integers , 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): ; ; ; for . Each scenario has at least one code from 3 to 6 and never contains both codes 2 and 6. All operation codes belong to .
- Subtask 2 (30 points): ; ; ; for . Each scenario has at least one code from 3 to 6 and never contains both codes 2 and 6. All operation codes belong to .
- Subtask 3 (50 points): ; ; ; for . Each scenario has at least one code from 3 to 6 and never contains both codes 2 and 6. All operation codes belong to .
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 6contains only sum and XOR queries and no updates, so choosePREFIX. - Scenario 2:
5 4contains a minimum query with no updates, so chooseSPARSE. - Scenario 3:
2 1 3has two updates followed by a point query, so chooseDIFFERENCE. - Scenario 4:
3 1 3has an update after a query, so the offline difference rule does not apply; chooseFENWICK. - Scenario 5:
1 4adds at a point and queries a range sum, so chooseFENWICK. - Scenario 6:
3 2 3interleaves a range addition with point queries, so chooseFENWICK. - Scenario 7:
2 4combines range addition and range sum, so the exercise convention selectsSEGMENT. - Scenario 8:
1 5updates a point and asks for a minimum, so chooseSEGMENT. - Scenario 9:
1 6updates a point and asks for XOR, so chooseSEGMENT. - Scenario 10:
1 3 4contains a point update and point and range-sum queries; the sum query excludesDIFFERENCE, so chooseFENWICK.