Cod sursa(job #1698392)

Utilizator superstar1998Moldoveanu Vlad superstar1998 Data 4 mai 2016 11:43:58
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 3.46 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <thread>

#define pb push_back
#define mp make_pair
#define MAXN 100001
#define DIM 8192

using namespace std;

class Reader
{
public:
    Reader(const char* filename)
    {
        f.open(filename);
        pos = 0;
        f.read(buffer,DIM);
    }
    inline Reader& operator >>(int& x)
    {
        x=0;
        sgn=1;
        while((buffer[pos]<'0'||buffer[pos]>'9')&&buffer[pos]!='-')
            if(++pos==DIM)
                f.read(buffer,DIM),pos=0;
        if(buffer[pos]=='-')sgn=-1,pos++;
        while(buffer[pos]>='0'&&buffer[pos]<='9')
        {
            x=x*10+buffer[pos]-'0';
            if(++pos==DIM)f.read(buffer,DIM),pos=0;
        }
        x*=sgn;
        return (*this);
    }
    inline Reader& operator >>(char* c)
    {
        aux = 0;
        while(isspace(buffer[pos]))
            if(++pos==DIM)
                f.read(buffer,DIM),pos=0;
        while(!isspace(buffer[pos]))
        {
            c[aux++]=buffer[pos];
            if(++pos==DIM)f.read(buffer,DIM),pos=0;
        }
        return (*this);
    }
    ~Reader()
    {
        f.close();
    }
private:
    ifstream f;
    int pos,aux,sgn;
    char buffer[DIM];
};

class Writer
{
public:
    Writer(const char* filename)
    {
        g.open(filename);
        pos = 0;
    }
    inline Writer& operator <<(int xx)
    {
        aux = 0;
        x = xx;
        if(xx<0)x=-x;
        if(x==0)
            nr[aux++]='0';
        while(x)
        {
            nr[aux++]=x%10+'0';
            x/=10;
        }
        if(xx<0)nr[aux++]='-';
        while(aux>0)
        {
            buffer[pos++]=nr[--aux];
            if(pos==DIM)g.write(buffer,DIM),pos=0;
        }
        return (*this);
    }
    inline Writer& operator <<(const char* c)
    {
        aux = 0;
        while(c[aux])
        {
            buffer[pos++]=c[aux++];
            if(pos==DIM)g.write(buffer,DIM),pos=0;
        }
        return (*this);
    }
    ~Writer()
    {
        g.write(buffer,pos);
        g.close();
    }
private:
    ofstream g;
    int pos,aux,x;
    char buffer[DIM],nr[21];
};

Reader f("cautbin.in");
Writer g("cautbin.out");

int v[MAXN],n,cnt,i,j,x,y,m,tsk;
int* sol;

void tsk0(int i)
{
    x = upper_bound(v + 1, v + cnt + 1, y) - v - 1;
    if (x <= cnt && x >= 1 && v[x] == y)
        sol[i]=x;
    else
        sol[i]=-1;
}

void tsk1(int i)
{
    sol[i]=lower_bound(v + 1, v + cnt + 1, y + 1) - v - 1;
}

void tsk2(int i)
{
    sol[i] = upper_bound(v + 1, v + cnt + 1, y - 1) - v;
}

int main()
{
    ios_base::sync_with_stdio(false);
    n=thread::hardware_concurrency();
    thread* th = new thread[n];
    sol = new int[n];
    f>>cnt;
    for(i=1;i<=cnt;i++)
        f>>v[i];
    f>>m;
    for(i=0;i<m;i++)
    {
        f>>tsk>>y;
        if(tsk==0)
            th[i%n]=thread(tsk0,i%n);
        else if(tsk==1)
            th[i%n]=thread(tsk1,i%n);
        else
            th[i%n]=thread(tsk2,i%n);
        if(i%n==n-1)
        {
            for(j=0;j<n;j++)
                th[j].join();
            for(j=0;j<n;j++)
                g<<sol[j]<<"\n";
        }
    }
    if(i%n)
    {
        for(j=0;j<i%n;j++)
            th[j].join();
        for(j=0;j<i%n;j++)
            g<<sol[j]<<"\n";
    }
    return 0;
}