Submission #533635

# Submission time Handle Problem Language Result Execution time Memory
533635 2022-03-06T18:03:31 Z CaroLinda Teams (CEOI11_tea) C++14
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>
 
#define mk make_pair
#define lp(i,a,b) for(int i = a ; i < b; i++ )
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
 
const int MAX = 1e6+5 ;

using namespace std ;

struct Seg{

	pair<int,int> tree[MAX*4] , _default =make_pair(MAX*10, -1) ;

	int m(int l, int r ) { return (l+r)>>1 ; }

	void build(int pos, int l, int r){

		tree[pos] = _default ;

		if(l == r ) return ;

		build(pos<<1 , l , m(l,r)) ;
		build(pos<<1|1 , m(l,r)+1 , r ) ;
	}

	pair<int,int> qry(int pos, int l, int r, int beg, int en){
		if(l > en || r < beg ) return _default ;
		if(l >= beg && r <= en ) return tree[pos] ;

		pair<int,int> al = qry(pos<<1 , l  , m(l,r ) , beg, en ) ;
		pair<int,int> ar = qry(pos<<1|1 , m(l,r)+1, r, beg, en ) ;

		return min(al, ar) ;
	}

	void upd(int pos, int l, int r, int id, pair<int,int> p ){
		
		tree[pos] = min(tree[pos] , p ) ;

		if(l == r) return ;

		if(id <= m(l,r)) upd(pos<<1 , l , m(l,r) , id,p) ;
		else upd(pos<<1|1 , m(l,r)+1, r, id, p ) ;
	}

	void clean(int pos, int l, int r){

		if( tree[pos] == _default ) return ;

		tree[pos] = _default ;

		if(l == r ) return ;

		clean(pos<<1 , l , m(l,r)) ;
		clean(pos<<1|1 , m(l,r)+1, r ) ;

	}

} seg[2] ;
 
int main(){
	ios_base::sync_with_stdio(false );
	cin.tie(0) ;
 
	int N ;
	cin >> N ;
	vector<int> arr(N) , idx(N) ; iota(all(idx) , 0) ;
	vector<int> corte(N) , mx(N) , tam(N) ;
	for(auto &e : arr ) cin >> e ;
	sort(all(idx),[&](int a, int b) { return arr[a] < arr[b] ; }) ;
	vector<int> dp(N) ;

	for(int i = 0 ; i < N ; i++){

		tam[i] = i+1 ;
		corte[i] = -1 ;

		if( i+1 < arr[idx[i]] ){
			dp[i] = mx[i] = 0 ;
			if(i) mx[i] = max(mx[i] , mx[i-1]) ;
		}
		else{
			dp[i] = mx[i] = max(1, mx[i-arr[idx[i]]]+1) ;
			if(i) mx[i] = max(mx[i] , mx[i-1]) ;
		}
	}

	vector<vector<int> > sweep(N+1, vector<int>()) ;
	for(int i = 0 ; i < N ; i++ ) sweep[dp[i]].push_back(i) ;

	for(int i = 1 ; i <= N ; i++ ){
		sort(all(sweep[i]) , [&](int a, int b){
			return a-arr[idx[a]] < b-arr[idx[b]] ;
		}) ;
	}

	auto getPair = [&](int x){

		return make_pair( tam[x]+x , x-tam[x] ) ;

	} ;

	for(int i = 2 ; i <= N ; i++ ){

		int ptr = 0 ;
	
		sort(all(sweep[i-1])) ;

		seg[0].clean(1,1,2*N) ;
		seg[1].clean(1,1,2*N ) ;

		for(auto e : sweep[i] ) {
			
			while(ptr < sz(sweep[i-1]) && sweep[i-1][ptr] <= e-arr[idx[e]] ){
				pair<int,int> p = getPair(sweep[i-1][ptr]) ;
				int id = sweep[i-1][ptr++] ;
				seg[0].upd(1,1,2*N , p.first, mk(-p.first-p.second,id) ) ;
				seg[1].upd(1,1,2*N , p.first, mk(p.first-p.second, id)) ;
			}

			tam[e] = N*2 ;

			pair<int,int> p[2] = { seg[0].qry( 1 , 1, 2*N , 1 , e  ), seg[1].qry(1,1,2*N , e , 2*N) } ;
			sort(p, p+2) ;

			tam[e] = (e-p[0].first)>>1 ;
			corte[e] = p[0].second ;
		}
	}

	//lp(i,0,N) cout << dp[i] << " " << tam[i] << " " << corte[i] << endl ;

	vector<vector<int> > ans ;

	int x = N-1 ;

	while(x != -1){
		ans.push_back(vector<int>()) ;
		for(int i = corte[x]+1 ; i <= x ; i++ ) ans.back().push_back(idx[i]+1) ;
		x = corte[x] ;
	}

	cout << sz(ans) <<"\n" ;

	for(auto e : ans ){
		cout << sz(e) <<" " ;
		for(auto ee : e ) cout << ee <<" " ;
			cout << "\n" ;
	} 
}

Compilation message

Compilation timeout while compiling tea