Line Segment tree (with deleted nodes)
The most dynamic value (minmax. Cpp/c/java)
(Space limit: 128 MB)
There is an array containing n elements, which requires the following operations:
DELETE k: Number of DELETE locations on k. Shift the number on the right to the left.
QUERY I j: QUERY position I ~ The minimum and maximum values of all numbers on j.
[Input] (minmax. in)
The first line contains two numbers n and m, indicating the number of elements and the number of operations in the original array. The second row contains n numbers, indicating the original array. The following m rows, each row in the format of 1 k or 2 I j, where the first number is 1 indicates the delete operation, and 2 indicates the query operation.
[Output] (minmax. out)
The output line contains two numbers, indicating the minimum and maximum values in the range.
[Example input]
10 4
1 5 2 6 7 4 9 3 1 5
2 2 8
1 3
1 6
2 2 8
[Sample output]
2 9
1 7
[Restrictions]
The absolute values of elements in the array cannot exceed 109
Group 1: n = 10, 1 minute, time limit: 0.1 s
Group 2: n = 333, 2 points, time limit: 0.1 s
Group 3: n = 4232,3, time limit: 0.1 s
Group 4: n = 6324, 4 points, time limit: 0.2 s
Group 5: n = 9999, 5 points, time limit: 2 s
Group 6: n = 100000, m = 300000, 10 points, time limit 2 s
Group 7: n = 300000, m = 422342, 13 points, time limit 3 s
Group 8: n = 500000, m = 651222, 18, time limit: 3 s
Group 9: n = 900000, m = 432122, 22, time 5S
Group 10: n = 999999, m = 999999,22, time 5S
/*********************** Author: Grant Yuan time: source: 11 second game algorithm: line Segment tree ************************/# include
# Include
# Include
# Include
# Include
# Define INF 0x7fffffff # define MAX 1000007 using namespace std; struct node {int l; int r; int Min; int Max; int num ;}; node tree [4 * MAX]; int n, m; int input [MAX]; int ansmin, ansmax; void build (int left, int right, int root) {tree [root]. l = left; tree [root]. r = right; if (left = right) {tree [root]. max = tree [root]. min = input [left]; tree [root]. num = 1; return;} int mid = (left + right)> 1; build (left, mid, root * 2); build (mid + 1, right, root * 2 + 1); tree [root]. max = max (tree [root * 2]. max, tree [root * 2 + 1]. max); tree [root]. min = min (tree [root * 2]. min, tree [root * 2 + 1]. min); tree [root]. num = tree [root * 2]. num + tree [root * 2 + 1]. num;} void query (int left, int right, int root) {if (left = 1 & (right = tree [root]. num) {ansmin = min (ansmin, tree [root]. min); ansmax = max (ansmax, tree [root]. max); return;} int n1 = tree [root * 2]. num; int n2 = tree [root * 2 + 1]. num; if (left <= n1 & right> n1) {query (Left, n1, root * 2); query (1, right-n1, root * 2 + 1);} else if (left <= n1 & right <= n1) query (left, right, root * 2); else if (left> n1 & right <= n1 + n2) query (left-n1, right-n1, root * 2 + 1 );} void Delete (int num, int root) {if (tree [root]. l = tree [root]. r) {tree [root]. num = tree [root]. num-1; tree [root]. min = INF; tree [root]. max =-INF; return;} int n1 = tree [root * 2]. num; int n2 = tree [root * 2 + 1]. num; if (num <= n1) Delete (num, root * 2); else Delete (n Um-n1, root * 2 + 1); tree [root]. num = tree [root * 2]. num + tree [root * 2 + 1]. num; tree [root]. max = max (tree [root * 2]. max, tree [root * 2 + 1]. max); tree [root]. min = min (tree [root * 2]. min, tree [root * 2 + 1]. min);} int main () {freopen ("minmax. in "," r ", stdin); freopen (" minmax. out "," w ", stdout); while (~ Scanf ("% d", & n, & m) {for (int I = 1; I <= n; I ++) {scanf ("% d", & input [I]) ;}for (int I = 1; I <= 4 * n; I ++) tree [I]. min = INF, tree [I]. max =-INF; build (1, n, 1); int a, B, c; for (int I = 1; I <= m; I ++) {scanf ("% d", & a); if (a = 1) {scanf ("% d", & B); Delete (B, 1 );} else if (a = 2) {ansmin = INF; ansmax =-INF; scanf ("% d", & B, & c); query (B, c, 1); printf ("% d \ n", ansmin, ansmax) ;}} return 0 ;}