Pagini recente » Cod sursa (job #509975) | Cod sursa (job #171473) | Cod sursa (job #1622196) | Cod sursa (job #968079) | Cod sursa (job #2134849)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
class parser{
public:
parser() {}
parser(const char *file_name){
input_file.open(file_name,ios::in | ios::binary);
input_file.sync_with_stdio(false);
index&=0;
input_file.read(buffer,SIZE);}
inline parser &operator >>(int &n){
for (;buffer[index]<'0' or buffer[index]>'9';inc());
n&=0;
//sign&=0;
//sign|=(buffer[index-1]=='-');
for (;'0'<=buffer[index] and buffer[index]<='9';inc())
n=(n<<1)+(n<<3)+buffer[index]-'0';
//n^=((n^-n)&-sign);
return *this;}
~parser(){
input_file.close();}
private:
fstream input_file;
static const int SIZE=0x40000; ///4MB
char buffer[SIZE];
int index/*,sign*/;
inline void inc(){
if(++index==SIZE)
index=0,input_file.read(buffer,SIZE);}
};
class writer{
public:
writer() {};
writer(const char *file_name){
output_file.open(file_name,ios::out | ios::binary);
output_file.sync_with_stdio(false);
index&=0;}
inline writer &operator <<(int target){
aux&=0;
sign&=0;
n=target;
if (n<0)
sign=1,
buffer[index]='-',
inc();
n^=((n^-n)&-sign);
if (!n)
nr[aux++]='0';
for (;n;n/=10)
nr[aux++]=n%10+'0';
for(;aux;inc())
buffer[index]=nr[--aux];
return *this;}
inline writer &operator <<(const char *target){
aux&=0;
while (target[aux])
buffer[index]=target[aux++],inc();
return *this;}
~writer(){
output_file.write(buffer,index);output_file.close();}
private:
fstream output_file;
static const int SIZE=0x20000; ///2MB
int index,aux,n,sign;
char buffer[SIZE],nr[24];
inline void inc(){
if(++index==SIZE)
index&=0,output_file.write(buffer,SIZE);}
};
parser f ("cautbin.in");
writer t ("cautbin.out");
int n;
vector <int> v;
int bin_search(int target){
vector <int>::iterator it=v.begin();
bool found=false,e=false;
int last_pos;
for (int x=1,range=n>>1;range;range>>=1){
it+=x*range;
if (*it==target)
found=true,
last_pos=it-v.begin(),
x=1;
else if (*it>target)
x=-1;
else
x=1;
if (range%2 and !e){
if (range==1)
e=true;
++range;
}
}
if (found)
return 1+last_pos;
else
return -1;
}
int upperbound(int target){
vector <int>::iterator it=v.begin();
int last_pos;
bool found=false,e=false;
for (int x=1,range=n>>1;range;range>>=1){
it+=x*range;
if (*it==target)
found=true,
last_pos=it-v.begin(),
x=1;
else if (*it>target)
x=-1;
else
x=1;
if (range%2 and !e){
if (range==1)
e=true;
++range;
}
if (!found)
last_pos=it-v.begin();
}
if (found)
return 1+last_pos;
else
return last_pos;
}
int lowerbound(int target){
vector <int>::iterator it=v.begin();
int last_pos;
bool found=false,e=false;
for (int x=1,range=n>>1;range;range>>=1){
it+=x*range;
if (*it==target)
found=true,
last_pos=it-v.begin(),
x=-1;
else if (*it>target)
x=-1;
else
x=1;
if (range%2 and !e){
if (range==1)
e=true;
++range;
}
if (!found)
last_pos=it-v.begin();
}
return 1+last_pos;
}
int main()
{
int m;
f>>n;
for (int x,i=0;i<n;++i)
f>>x,
v.push_back(x);
f>>m;
for (int x,q,i=0;i<m;++i){
f>>q>>x;
switch (q){
case 0:{t<<bin_search(x)<<"\n";break;}
case 1:{t<<upperbound(x)<<"\n";break;}
case 2:{t<<lowerbound(x)<<"\n";break;}
}
}
return 0;
}