线段树,16pts,求调
查看原帖
线段树,16pts,求调
774204
A_chicken_boy楼主2024/4/26 22:31
#include <bits/stdc++.h>
using namespace std ;
#define f(i,a,b) for ( int i = ( a ) ; i <= ( b ) ; ++i )
#define N 200005
struct S{
	int l , r ;
	int dat , lm , rm ;
}t[N<<2];
int a[N] ;
void box ( int q ){
	t[q].lm = t[q<<1].lm  ;
	if ( a[t[q<<1].r] ^ a[t[q<<1|1].l] ){
		t[q].lm = max ( t[q<<1].lm , t[q<<1].dat + t[q<<1|1].lm ) ;
	}
	t[q].rm = t[q<<1|1].rm ;
	if ( a[t[q<<1].r] ^ a[t[q<<1|1].l] ){
		t[q].rm = max ( t[q<<1|1].rm , t[q<<1|1].dat + t[q<<1].rm ) ;
	}
	t[q].dat = max ( t[q<<1].dat , t[q<<1|1].dat ) ;
	if ( a[t[q<<1].r] ^ a[t[q<<1|1].l] ){
		t[q].dat = max ( t[q<<1].rm + t[q<<1|1].lm , t[q].dat ) ;
	}
}
void built ( int q , int l , int r ){
	t[q].l = l ;
	t[q].r = r ;
	if ( l == r ){
		t[q].dat = t[q].lm = t[q].rm = 1 ;
		return ;
	}
	int mid = (l +r) >> 1 ;
	built ( q << 1 , l , mid ) ;
	built ( q << 1 | 1 , mid + 1 , r ) ;
	box ( q ) ;
}
void change ( int q , int x ){
	if ( t[q].l == t[q].r ){
		a[t[q].l] = a[t[q].l] * ( -1 ) + 1 ;
		t[q].dat = t[q].lm = t[q].rm = 1 ;
		return ;
	}
	int mid = ( t[q].l + t[q].r) >> 1 ;
	if ( x <= mid ) change ( q << 1 , x ) ;
	if ( x > mid ) change ( q << 1 | 1 , x ) ;
	box ( q ) ;
}
int n , q ;
int main ( ){
	cin >> n >> q ;
	f(i,1,n){
		a[i] = 1 ;//L
	}
	built ( 1 , 1 , n ) ;
	int x ;
	f(i,1,q){
		cin >> x ;
		change ( 1 , x ) ;
		cout << t[1].dat << endl ;
	}
	return 0 ;
}
	 
2024/4/26 22:31
加载中...