HDU 3308 LCIS(線段樹)

題目:

Description

Given n integers. 
You have two operations: 
U A B: replace the Ath number by B. (index counting from 0) 
Q A B: output the length of the longest consecutive increasing subsequence (LCIS) in [a, b]. 

Input

T in the first line, indicating the case number. 
Each case starts with two integers n , m(0<n,m<=10 5). 
The next line has n integers(0<=val<=10 5). 
The next m lines each has an operation: 
U A B(0<=A,n , 0<=B=10 5
OR 
Q A B(0<=A<=B< n). 

Output

For each Q, output the answer.

Sample Input

1
10 10
7 7 3 3 5 9 9 8 1 8 
Q 6 6
U 3 4
Q 0 1
Q 0 5
Q 4 7
Q 3 5
Q 0 2
Q 4 6
U 6 10
Q 0 9

Sample Output

1
1
4
2
3
1
2
5


題意:

給你n個數,每次有兩種操作 

U A B 把第A個數變成B(注意是從0開始)

Q A B 求區間【A,B】最長連續遞增區間


思路:線段樹,既然我們要求最長的遞增序列,那我們就要把每個區間的遞增序列的長度保留下來。定義一個max來存。

然後考慮到兩個區間組成一個新的區間的時候可能還可以進行合併,那麼我們就要把每個區間邊緣的數字(lnum,rnum)記錄下來,並且區間邊緣遞增的長度(lmax,rmax)記錄下來

當合並的時候新的區間的max 就是 左孩子區間的max,右孩子區間的max,如何左右孩子區間的可以合併(就是左孩子區間的右端和右孩子區間的左端是連續遞增的),就還要把他們兩端的長度加起來,三者中最大的就是新的區間的max。

每次同理也要更新 (lnum,rnum) lnum就是左孩子區間的lnum,rnum就是右孩子區間的rnum

更新(lmax,rmax ) 如果一個區間不是完全遞增的,那麼lmax就是左孩子區間的lmax ,rmax就是右孩子區間的rmax, 如果區間完全遞增就要加上另外一個區間的lmax或rmax


查詢的時候,如果剛好就是線段數的一個區間 就是該區間的max

否則 就是分成的左區間,右區間,以及可能合併的,三者的最大值。


代碼(代碼巨醜,就不要吐槽了):

#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
#define mido int mid=(tree[index].l+tree[index].r)/2;
struct node {
	int l;
	int r;
	int max;
	int lnum;
	int rnum;
	int lmax;
	int rmax;
};
int max(int a, int b) {
	if (a > b) return a;
	return b;
}

node tree[4 * 100005];
void build(int index, int l, int r) {
	tree[index].l = l;
	tree[index].r = r;
	mido;
	if (l == r) {
		int num;
		scanf("%d", &num);
		tree[index].max = 1;
		tree[index].lnum = num;
		tree[index].rnum = num;
		tree[index].lmax = 1;
		tree[index].rmax = 1;
		return;
	}
	build(index * 2, l, mid);
	build(index * 2 + 1, mid + 1, r);
	if (tree[index * 2].rnum < tree[index * 2 + 1].lnum) {
		tree[index].max = max(tree[index * 2].max, max(tree[index * 2 + 1].max, tree[index * 2].rmax + tree[index * 2 + 1].lmax));
	}
	else {
		tree[index].max = max(tree[index * 2].max, tree[index * 2 + 1].max);
	}
	tree[index].lnum = tree[index * 2].lnum;
	tree[index].rnum = tree[index * 2 + 1].rnum;
	if (tree[index * 2].lmax == tree[index * 2].r - tree[index * 2].l + 1 && tree[index * 2].rnum < tree[index * 2 + 1].lnum) {
		tree[index].lmax = tree[index * 2].lmax + tree[index * 2 + 1].lmax;
	}
	else tree[index].lmax = tree[index * 2].lmax;
	if (tree[index * 2 + 1].rmax == tree[index * 2 + 1].r - tree[index * 2 + 1].l + 1 && tree[index * 2 + 1].lnum>tree[index * 2].rnum) {
		tree[index].rmax = tree[index * 2 + 1].rmax + tree[index * 2].rmax;
	}
	else tree[index].rmax = tree[index * 2 + 1].rmax;
}
void update(int index,int l, int r, int q) {
	if (tree[index].l == l&&tree[index].r == r) {
		tree[index].lnum = q;
		tree[index].rnum = q;
		return;
	}
	mido;
	if (r <= mid) {
		update(index * 2, l, r, q);
	}
	else if (l>mid) {
		update(index * 2 + 1, l, r, q);
	}
	else {
		update(index * 2, l, mid, q);
		update(index * 2 + 1, mid + 1, r, q);
	}
	if (tree[index * 2].rnum < tree[index * 2 + 1].lnum) {
		tree[index].max = max(tree[index * 2].max, max(tree[index * 2 + 1].max, tree[index * 2].rmax + tree[index * 2 + 1].lmax));
	}
	else {
		tree[index].max = max(tree[index * 2].max, tree[index * 2 + 1].max);
	}
	tree[index].lnum = tree[index * 2].lnum;
	tree[index].rnum = tree[index * 2 + 1].rnum;
	if (tree[index * 2].lmax == tree[index * 2].r - tree[index * 2].l + 1 && tree[index * 2].rnum < tree[index * 2 + 1].lnum) {
		tree[index].lmax = tree[index * 2].lmax + tree[index * 2 + 1].lmax;
	}
	else tree[index].lmax = tree[index * 2].lmax;
	if (tree[index * 2 + 1].rmax == tree[index * 2 + 1].r - tree[index * 2 + 1].l + 1 && tree[index * 2 + 1].lnum>tree[index * 2].rnum) {
		tree[index].rmax = tree[index * 2 + 1].rmax + tree[index * 2].rmax;
	}
	else tree[index].rmax = tree[index * 2 + 1].rmax;
}
int query(int index, int a, int b) {
	if (tree[index].l == a&&tree[index].r == b) {
		return tree[index].max;
	}
	mido;
	if (b <= mid) {
		query(index * 2, a, b);
	}
	else if (a>mid) {
		query(index * 2 + 1, a, b);
	}
	else {
		int t1,t2,t3;
		if (tree[index * 2].rnum < tree[index * 2 + 1].lnum) {
			if (tree[index * 2].r - a + 1 >= tree[index * 2].rmax&&b - tree[index * 2 + 1].l+1 >= tree[index * 2 + 1].lmax) {
				t1 = tree[index * 2].rmax + tree[index * 2 + 1].lmax;
			}
			else if (tree[index * 2].r - a + 1 < tree[index * 2].rmax&&b - tree[index * 2 + 1].l+1 >= tree[index * 2 + 1].lmax) {
				t1= tree[index * 2].r - a + 1+ tree[index * 2 + 1].lmax;
			}
			else if (tree[index * 2].r - a + 1 >= tree[index * 2].rmax&&b - tree[index * 2 + 1].l+1 < tree[index * 2 + 1].lmax) {
				t1 = tree[index * 2].rmax + b - tree[index * 2 + 1].l + 1;
			}
			else {
				t1= tree[index * 2].r - a + 1 + b - tree[index * 2 + 1].l + 1;
			}
			t2 = query(index*2, a, mid);
			t3 = query(index * 2 + 1, mid + 1, b);
			return max(t1, max(t2, t3));
		}
		else {
			int t1, t2;
			t1= query(index * 2, a, mid);
			t2 = query(index * 2 + 1, mid + 1, b);
			return max(t1, t2);
		}
	}
}
int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		int n,m;
		scanf("%d%d", &n, &m);
		build(1, 1, n);
		while (m--) {
			char oper[2];
			int a, b;
			scanf("%s%d%d", oper, &a, &b);
			if (strcmp(oper, "U") == 0) {
				update(1, a+1, a+1, b);
			}
			else {
				printf("%d\n", query(1, a + 1, b + 1));
			}
		}
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章