Cod sursa(job #1609726)

Utilizator dumitrubogdanDumitru Bogdan Mihai dumitrubogdan Data 22 februarie 2016 23:00:52
Problema Hashuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.25 kb
#include <iostream>
#include <stdbool.h>
#include <stdio.h>
#define N 62500000
using namespace std;

typedef unsigned long ul;

ul b[N];

bool getb(ul &boolv, ul pos)
{
    return (boolv >> pos) & 1;
}

void setb(ul &boolv, ul pos, bool val)
{
    boolv ^= (-(val & 1) ^ boolv) & (1 << pos);
}

template <typename T>
void print(T n)
{
    cout << sizeof(n) * 8 << "b: ";
    for(int i = sizeof(n) * 8; i >= 0; i--)
    {
        cout << (n >> i & 1);
    }
    cout << endl;
}

int main()
{

    int opt;
    long nrOperatii;
    long nr;

    FILE * fread = fopen("hashuri.in","r");
    FILE * fwrite = fopen("hashuri.out","w");

    fscanf(fread, "%lu", &nrOperatii);

    for(long i = 0; i < nrOperatii; i++)
    {
        fscanf(fread, "%lu", &opt);
        fscanf(fread, "%lu", &nr);

        switch(opt)
        {
        case(1):
            setb(b[nr / N], nr, true);

            break;
        case(2):
            setb(b[nr / N], nr, false);

            break;
        case(3):
            bool exists = getb(b[nr / N], nr);
            fprintf(fwrite, "%d\n", exists);
            //cout << exists << endl;
            break;
        }
    }

    fclose(fread);
    fclose(fwrite);

    return 0;
}