Cod sursa(job #3150641)

Utilizator victorzarzuZarzu Victor victorzarzu Data 17 septembrie 2023 19:38:47
Problema Cautare binara Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <queue>
#include <bitset>

using namespace std;
#define zeros(x) ((x ^ (x - 1)) & x)

ifstream f("cautbin.in");
ofstream g("cautbin.out");
int n, m;
int arr[100001];

void read() {
  f>>n;
  for(int i = 1;i <= n;++i) {
    f>>arr[i];
  }
}

void solve() {
  int rep;
  for(rep = 1;rep <= n;rep <<= 1);
  f>>m;
  int x, y;
  for(int i = 1;i <= m;++i) {
    f>>x>>y;
    int pos;
    if(x < 2) {
      for(int rep1 = rep, pos = 0;rep1;rep1 >>= 1) {
        if(pos + rep1 <= n && arr[pos + rep1] <= y) {
          pos += rep1;
        }
      }

      if(x == 0 && arr[pos] != y) {
        g<<-1<<'\n';
      } else {
        g<<pos<<'\n';
      }
      continue;
    }

    for(int rep1 = rep,pos = n;rep1;rep1 >>= 1) {
      if(pos - rep1 >= 1 && arr[pos - rep1] >= y) {
        pos -= rep1;
      }
    }
    g<<pos<<'\n';
  }
}

int main() {
  read();
  solve();
  return 0;
}