Cod sursa(job #2908423)

Utilizator _andrei4567Stan Andrei _andrei4567 Data 3 iunie 2022 12:19:58
Problema Schi Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.35 kb
///mereu va fi al catalea zice din pozitiile ramase(incepem de la final)
#include <fstream>
#define lsb(x) x & (-x)
#pragma optimize GCC ("Ofast")

using namespace std;

class InParser {
private:
	FILE *fin;
	char *buff;
	int sp;

	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			fread(buff, 1, 4096, fin);
		}
		return buff[sp];
	}

public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}

	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}

	InParser& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
};
class OutParser {
private:
    FILE *fout;
    char *buff;
    int sp;

    void write_ch(char ch) {
        if (sp == 50000) {
            fwrite(buff, 1, 50000, fout);
            sp = 0;
            buff[sp++] = ch;
        } else {
            buff[sp++] = ch;
        }
    }


public:
    OutParser(const char* name) {
        fout = fopen(name, "w");
        buff = new char[50000]();
        sp = 0;
    }
    ~OutParser() {
        fwrite(buff, 1, sp, fout);
        fclose(fout);
    }

    OutParser& operator << (int vu32) {
        if (vu32 <= 9) {
            write_ch(vu32 + '0');
        } else {
            (*this) << (vu32 / 10);
            write_ch(vu32 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (long long vu64) {
        if (vu64 <= 9) {
            write_ch(vu64 + '0');
        } else {
            (*this) << (vu64 / 10);
            write_ch(vu64 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (char ch) {
        write_ch(ch);
        return *this;
    }
    OutParser& operator << (const char *ch) {
        while (*ch) {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
};
InParser cin ("schi.in");
OutParser cout ("schi.out");

const int N = 3e4;
int aib[N + 2], v[N + 2], sol[N + 2];

int n;
void update (int pos, int val)
{
    for (int i = pos; i <= n; i += lsb(i))
        aib[i] += val;
}

int query(int pos)
{
    int sum = 0;
    for (int i = pos; i >= 1; i -= lsb(i))
        sum += aib[i];
    return sum;
}

int cb (int st, int dr, int val)
{
    int med, last = -1;
    while (st <= dr)
    {
        med = (st + dr) >> 1;
        if (med - query(med) >= val)
        {
            last = med;
            dr = med - 1;
        }
        else
            st = med + 1;
    }
    return last;
}

int main()
{
    cin >> n;
    ios_base :: sync_with_stdio(false);
    for (int i = 1; i <= n; ++i)
    {
        cin >> v[i];
    }
    sol[v[n]] = n;
    update (v[n], 1);
    for (int i = n - 1; i >= 1; --i)
    {
        int pos = cb(1, n, v[i]);
        update (pos, 1);
        sol[pos] = i;
    }
    for (int i = 1; i <= n; ++i)
        cout << sol[i] << '\n';

    return 0;
}