Pagini recente » Cod sursa (job #916418) | Cod sursa (job #1716670) | Cod sursa (job #823968) | Cod sursa (job #353172) | Cod sursa (job #1042676)
#include <fstream>
#include <vector>
#include <string>
#include <math.h>
#include <set>
#include <sstream>
#include <algorithm>
using namespace std;
const int inf = (1LL << 31) - 1;
const int nmax = int(1e5) + 2;
int aint[nmax*4];
int a[nmax];
int n;
int query(int node,int l,int r) {
if (l == r) {
return l;
}
int leftSon = node << 1;
int rightSon = leftSon + 1;
return a[aint[leftSon]] < a[aint[rightSon]] ? aint[leftSon] : aint[rightSon];
}
void update(int node,int l,int r,int pos) {
if (l == r) {
return;
}
int leftSon = node << 1;
int rightSon = leftSon + 1;
int mid = (l + r) >> 1;
if (pos <= mid) update(leftSon,l,mid,pos);
else
update(rightSon,mid + 1,r,pos);
aint[node] = a[aint[leftSon]] < a[aint[rightSon]] ? aint[leftSon] : aint[rightSon];
}
void buildTree(int node,int l,int r) {
if (l == r) {
aint[node] = l;
return;
}
int leftSon = node << 1;
int rightSon = leftSon + 1;
int mid = (l + r) >> 1;
buildTree(leftSon,l,mid);
buildTree(rightSon,mid + 1,r);
aint[node] = a[aint[leftSon]] < a[aint[rightSon]] ? aint[leftSon] : aint[rightSon];
}
void readData(istream& cin) {
cin >> n;
for (int i = 1;i <= n;i++) {
cin >> a[i];
}
}
int main()
{
ifstream cin("algsort.in");
ofstream cout("algsort.out");
readData(cin);
buildTree(1,1,n);
for (int i = 1;i <= n;i++) {
int pos = query(1,1,n);
cout << a[pos] << " ";
a[pos] = inf;
update(1,1,n,pos);
}
return 0;
}