Cod sursa(job #1846355)

Utilizator stefanmereutaStefan Mereuta stefanmereuta Data 12 ianuarie 2017 16:22:43
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.47 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

#define P 100003

void add(vector<queue<int> > &h, int val) {
    int x;

    for (unsigned int i = 0; i < h[val % P].size(); i++) {
        x = h[val % P].front();

        if (x == val) {
            return;
        }

        h[val % P].pop();
        h[val % P].push(x);
    }

    h[val % P].push(val);
}

void del(vector<queue<int> > &h, int val) {
    int x;

    for (unsigned int i = 0; i < h[val % P].size(); i++) {
        x = h[val % P].front();

        if (x == val) {
            h[val % P].pop();
            return;
        }

        h[val % P].pop();
        h[val % P].push(x);
    }
}

int query(vector<queue<int> > &h, int val) {
    int x;

    for (unsigned int i = 0; i < h[val % P].size(); i++) {
        x = h[val % P].front();

        if (x == val) {
            return 1;
        }

        h[val % P].pop();
        h[val % P].push(x);
    }

    return 0;
}

int main()
{
    ifstream fin("hashuri.in");
    ofstream fout("hashuri.out");

    int n, op, x;
    vector<queue<int> > h (P);

    fin >> n;

    for (int i = 0; i < n; i++) {
        fin >> op >> x;

        if (op == 1) {
            add(h, x);
        } else if (op == 2) {
            del(h, x);
        } else {
            fout << query(h, x) << "\n";
        }
    }

    fin.close();
    fout.close();

    return 0;
}