Skip to content

Instantly share code, notes, and snippets.

@lukasa1993
Created April 25, 2012 15:43
Show Gist options
  • Save lukasa1993/2490761 to your computer and use it in GitHub Desktop.
Save lukasa1993/2490761 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
using namespace std;
void printTree(vector<int> & sgTree,int segSize)
{
int tmpSize = segSize;
for(int i =0; i<sgTree.size(); i++) {
cout<<sgTree[i]<<" ";
}
}
int segmentMax (vector<int> & t,int v, int tl, int tr, int l, int r) {
if (l > r)
return 0;
if (l == tl && r == tr)
return t[v];
int tm = (tl + tr) / 2;
return max(segmentMax(t,v*2, tl, tm, l, min(r,tm)),segmentMax(t,v*2+1, tm+1, tr, max(l,tm+1), r));
}
void build (vector<int> & a,vector<int> & t, int v, int tl, int tr) {
if (tl == tr)
t[v] = a[tl];
else {
int tm = (tl + tr) / 2;
build (a,t, v*2, tl, tm);
build (a,t, v*2+1, tm+1, tr);
t[v] = max(t[v*2],t[v*2+1]);
}
}
bool isPowerOfTwo (int x)
{
return ((x != 0) && ((x & (~x + 1)) == x));
}
int main()
{
vector<int> readouts;
int redout = 0 ,treeSize = 0,readoutsSize = 0,M = 0,counter = 0;;
cin>>M;
while(cin>>redout) {
if(redout < 0) {
break;
}
readouts.push_back(redout);
counter++;
}
int temp = readouts.size();
while( !isPowerOfTwo(readouts.size()) ) {
readouts.push_back(-1);
}
vector<int> sgTree(4*readouts.size(),-1);
build(readouts,sgTree,1,0,readouts.size() - 1);
//printTree(sgTree,readouts.size());
//cout<<"\n"<<segmentMax(sgTree,1,0,readouts.size() - 1,3,6)<<"\n";
for(int i = 0;i<readouts.size();i++)
{
if(i <=temp - M) {
cout<<segmentMax(sgTree,1,0,readouts.size() - 1,i,i+M-1)<<"\n";
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment