Cod sursa(job #3347597)

Utilizator rapidu36Victor Manz rapidu36 Data 17 martie 2026 14:38:17
Problema Hashuri Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 1.51 kb
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define N 1000000
#define MOD 666019
#define NIL -1

typedef struct
{
    int val, urm;
} element;

int poz_lst[MOD], nr;
element v[N];

bool exista(int x, int cat)
{
    for (int p = poz_lst[cat]; p != NIL; p = v[p].urm)
    {
        if (v[p].val == x)
        {
            return true;
        }
    }
    return false;
}

void adauga(int x)
{
    int categorie = x % MOD;
    if (exista(x, categorie))
    {
        return;
    }
    v[nr].val = x;
    v[nr].urm = poz_lst[categorie];
    poz_lst[categorie] = nr++;
}

void sterge(int x)
{
    int categorie = x % MOD;
    int p = poz_lst[categorie];
    while (p != NIL && v[p].val != x)
    {
        p = v[p].urm;
    }
    if (p == NIL)
    {
        return;
    }
    int poz_ultim = poz_lst[categorie];
    v[p].val = v[poz_ultim].val;
    poz_lst[categorie] = v[poz_ultim].urm;
}

int main()
{
    for (int i = 0; i < MOD; i++)
    {
        poz_lst[i] = NIL;
    }
    FILE *in, *out;
    in = fopen("hashuri.in", "r");
    out = fopen("hashuri.out", "w");
    int q;
    fscanf(in, "%d", &q);
    for (int i = 0; i < q; i++)
    {
        int tip, x;
        fscanf(in, "%d%d", &tip, &x);
        if (tip == 1)
        {
            adauga(x);
        }
        else if (tip == 2)
        {
            sterge(x);
        }
        else
        {
            fprintf(out, "%d\n", exista(x, x % MOD));
        }
    }
    return 0;
}