Cod sursa(job #2748628)

Utilizator valentinchipuc123Valentin Chipuc valentinchipuc123 Data 1 mai 2021 22:45:29
Problema Nums Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.02 kb
#include <bits/stdc++.h>
#define op(x) (x^(x-1))&x

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;
	}

	InParser& operator >> (string &s) {
		char c;
		c = read_ch();
		while (c >= '0' && c <= '9')
        {
            s += c;
            c = read_ch();
        }

		/*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;
	}
};

InParser f("nums.in");
ofstream g("nums.out");

int n,nr=0,tr=0,aib[100005];
int tip;
bool activ[100005];
string s;

map<string,int> poz;

struct operations
{
    int mod;
    string sir;
} v[100005];

struct numere
{
    string sir;
} elem[100005];

bool cmp(numere a,numere b)
{
    if( a.sir.size()==b.sir.size() )
        return a.sir<b.sir;
    return a.sir.size()<b.sir.size();
}

void adauga(int loc)
{
    for(int i=loc; i<=tr; i+=op(i)) aib[i]++;
}

int main()
{
    f>>n;

    while(n--)
    {
        f>>tip;
        f>>s;

        nr++;
        v[nr].mod=tip;
        v[nr].sir=s;

        if(tip==1)
        {
            tr++;
            elem[tr].sir=s;
        }
    }

    sort(elem+1,elem+tr+1,cmp);

    int cpy=tr;
    tr=0;

    for(int i=1; i<=cpy; i++)
    {
        if(elem[tr].sir!=elem[i].sir) elem[++tr].sir=elem[i].sir;
    }

    for(int i=1; i<=tr; i++)
    {
        poz[elem[i].sir]=i;
    }

    for(int i=1; i<=nr; i++)
    {
        if( v[i].mod==1 )
        {
            int loc=poz[v[i].sir];

            if(activ[loc]==1) continue;

            adauga(loc);
            activ[loc]=1;
            continue;
        }

        string s=v[i].sir;
        int k=0;
        for(int j=0; j<s.size(); j++)
            k=k*10+s[j]-'0';

        int suma=0,loc=0,pas=16;

        while(pas>=0)
        {
            if( loc+(1<<pas)<tr&&suma+aib[loc+(1<<pas)]<k )
            {
                loc+=(1<<pas);
                suma+=aib[loc];
            }
            pas--;
        }

        g<<elem[loc+1].sir<<'\n';
    }

}