Cod sursa(job #2761293)

Utilizator Virgil993Virgil Turcu Virgil993 Data 1 iulie 2021 14:58:01
Problema Order Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <iostream>
#include<bits/stdc++.h>

using namespace std;

int arb[130000],pas,n;

ifstream fin("order.in");
ofstream fout("order.out");

void creare_arbore(int nod, int st, int dr)
{
    if(st==dr)
        arb[nod] = 1;
    else
    {
        int mij = (st+dr)/2;
        creare_arbore(nod*2,st,mij);
        creare_arbore(nod*2+1,mij+1,dr);
        arb[nod]= arb[nod*2]+arb[nod*2+1];
    }
}

void update(int nod, int st, int dr, int p)
{
    if(st==dr)
    {
        arb[nod] = 0;
        fout<<st<<" ";
    }
    else
    {
        int mij = (st+dr)/2;
        if(arb[nod*2]!=0)
        {
            if(p<=arb[nod*2])
            {
                update(nod*2,st,mij,p);
                arb[nod] = arb[nod*2]+arb[nod*2+1];
            }
            else
            {
                p = p-arb[nod*2];
                update(nod*2+1,mij+1,dr,p);
                arb[nod] = arb[nod*2]+arb[nod*2+1];
            }
        }
        else
        {
            update(nod*2+1,mij+1,dr,p);
            arb[nod] = arb[nod*2]+arb[nod*2+1];
        }
    }
}



int main()
{
    fin>>n;
    creare_arbore(1,1,n);
    pas = 2;
    cout<<arb[1];
    while(arb[1]>0)
    {
        while(pas>=arb[1])
            pas=pas-arb[1];
        update(1,1,n,pas);
        pas++;


    }

	return 0;
}