Cod sursa(job #2231559)

Utilizator inquisitorAnders inquisitor Data 14 august 2018 22:15:03
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.44 kb
#include <cstdio>
#include <malloc.h>

#define min(x, y) ((y) ^ (((x) ^ (y)) & -((x) < (y))))

struct int_vector
{
    public:

        int_vector() : array_(0), size_(0), capacity_(0) {resize(16);}

        int_vector(unsigned n) : array_(0), size_(0), capacity_(0) {resize(n);}

        ~int_vector(){free((void*) array_);}

        __attribute__((always_inline)) void push_back(int val)
        {
            array_[size_++] = val;

            if(size_ >= capacity_) resize(size_ << 1);
        }

        __attribute__((always_inline)) void pop_back() {--size_;}

        __attribute__((always_inline)) int& operator[](unsigned n) {return array_[n];}

        __attribute__((always_inline)) unsigned size() {return size_;}

        void resize(unsigned n)
        {
            array_ = (int*)realloc(array_, n << 2);

            capacity_ = n;

            size_ = min(size_, capacity_);
        }

    private:

        int* array_;

        unsigned size_, capacity_;
};

#define MOD 8191
#define buffSize 65536

int_vector v[8192];

void Delete(int N)
{
    int h = N & MOD;

    for(int i = 0; i != v[h].size(); ++i)
    {
        if(v[h][i] == N)
        {
            v[h][i] = v[h][v[h].size() - 1];

            v[h].pop_back();

            return;
        }
    }
}

bool Find(int N)
{
    int h = N & MOD;

    for(int i = 0; i != v[h].size(); ++i)
    {
        if(v[h][i] == N)
        {
            return true;
        }
    }

    return false;
}

char buffer[buffSize];

int i = buffSize - 1;

__attribute__((always_inline)) int get_nr()
{
    int x = 0;

    while(buffer[i] < 48 | buffer[i] > 57)

        if(++i == buffSize) fread(buffer, 1, buffSize, stdin), i = 0;

    while(buffer[i] > 47 & buffer[i] < 58)
    {
        x = x * 10 + buffer[i] - 48;

        if(++i == buffSize) fread(buffer, 1, buffSize, stdin), i = 0;
    }

    return x;
}

int main()
{
    freopen("hashuri.in", "r", stdin);
    freopen("hashuri.out", "w", stdout);

    int T, Q, x, i = -1;

    char answer[2000000];

    T = get_nr() + 1;

    while(--T)
    {
        Q = get_nr();

        x = get_nr();

        if(Q == 1) v[x & MOD].push_back(x);

        else if(Q & 1)
        {
            answer[++i] = 48 + Find(x);

            answer[++i] = 10;
        }

        else Delete(x);

    }

    answer[++i] = 0;

    puts(answer);

    return 0;
}