Pagini recente » Cod sursa (job #210273) | Cod sursa (job #2005423)
#include <iostream>
#include <fstream>
using namespace std;
const int ct=1;
ifstream f("farfurii.in");
ofstream g("farfurii.out");
long long k,n,p,m;
long long arb[400005];
void build(int nod,int lt,int rt,int pos)
{
if(lt==rt)
{
arb[nod]=1;
return;
}
int md=(lt+rt)/2;
if(pos<=md) build(nod*2,lt,md,pos);
else build(nod*2+1,md+1,rt,pos);
arb[nod]=arb[nod*2]+arb[nod*2+1];
}
void query(int nod,int lt,int rt,int val)
{
if(lt==rt)
{
g<<lt<<' ';
arb[nod]=0;
return;
}
int md=(lt+rt)/2;
if(arb[nod*2]>=val) query(nod*2,lt,md,val);
else query(nod*2+1,md+1,rt,val-arb[nod*2]);
arb[nod]=arb[nod*2]+arb[nod*2+1];
}
void afisare()
{
int d=0;
for(int i=1;i<=4;++i)
{
for(int j=1;j<=(1<<(i-1));++j)
{
++d;
cout<<arb[d]<<' ';
}
cout<<endl;
}
}
void mlc()
{
p=n-1;
m=(n*(n-1)/2);
}
int main()
{
f>>n>>k;
mlc();
for(int i=1;i<=n;++i) build(1,1,n,i);
while(m-p>=k && arb[1])
{
query(1,1,n,ct);
m-=p;
--p;
}
m-=p;
if(arb[1]) query(1,1,n,k-m+ct);
while(arb[1])
{
query(1,1,n,arb[1]);
}
return 0;
}