#include <iostream>
#include <algorithm>
#include <tbb/parallel_reduce.h>
#include <tbb/blocked_range.h>
#include <tbb/task_scheduler_init.h>

#define N 20000001

using namespace std;
using namespace tbb;

class Task {
	private:
		int *a;

	public:
		int maxz;

		Task( int _a[] ) : a( _a ), maxz( 0 ) {}

		Task( Task &subtask, split )
		{
			a = subtask.a;
			maxz = 0;
		}

		void operator()( const blocked_range<int> &r ) {
			for ( int i=r.begin(); i!=r.end( ); ++i ) {
				maxz = max( maxz, a[i] );
			}
			printf("%08d - %08d : max = %08d\n",r.begin(),r.end(), maxz);

		}

		void join( const Task &subtask )
		{
			maxz = max( maxz, subtask.maxz );
		}
};

int par_max(int x, int y, int *a)
{
	Task task(a);

	parallel_reduce(blocked_range<int>(x,y), task, auto_partitioner());

	return task.maxz;
}

int main()
{
	task_scheduler_init init(2);

	int a[N];

	for ( int i=0; i<N; i++ ) {
		a[i] = (int) i;
	}

	random_shuffle(a+N/4,a+N);

	cout << par_max(0,N,a) << endl;

	return 0;
}

