Cod sursa(job #2908060)

Utilizator denisa0230Zarioiu Denisa denisa0230 Data 1 iunie 2022 11:35:03
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.9 kb
#include <fstream>

using namespace std;

#include <stdio.h>
#include <ctype.h>

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

#include <cstdio>
#include <cstring>

using namespace std;

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

const int mod=1000000;

struct nod{

    int val;
    nod *next;
};
nod *v[mod];

int parcurgere(int x)
{
    int index=x%mod;
    nod *head=v[index];
    while(head!=nullptr)
    {
        if(head->val==x)
            return 1;
        head=head->next;
    }
    return 0;
}

void insereaza(int x)
{
    if(parcurgere(x)==1)
        return ;
    int index=x%mod;
    if(v[index]==nullptr)
    {
        v[index]=new nod;
        v[index]->val=x;
        v[index]->next=nullptr;
    }
    else
    {
        nod *nou=new nod;
        nou->val=x;
        nou->next=v[index];
        v[index]=nou;
    }

}

void stergere(int x)
{
    if(parcurgere(x)==0)
        return ;
    int index=x%mod;
    nod *head=v[index],*before=nullptr,*after=nullptr;
    while(head!=nullptr)
    {
        if(head->val==x)
        {
            after=head->next;
            break;
        }
        before=head;
        head=head->next;
    }
    if(before!=nullptr)
        before->next=after;
    else
        v[index]=after;
    delete head;
}

int main()
{
    InParser fin("hashuri.in");
    OutParser fout("hashuri.out");
    int n;
    fin>>n;
    for(int i=1;i<=n;i++)
    {
        int op,x;
        fin>>op>>x;
        if(op==1)
            insereaza(x);
        if(op==2)
            stergere(x);
        if(op==3)
            fout<<parcurgere(x)<<'\n';
    }
    for(int i=0;i<mod;i++)
    {
        nod *head=v[i],*urm=nullptr;
        while(head!=nullptr)
        {
            urm=head->next;
            delete head;
            head=urm;
        }
    }
    return 0;
}