Cod sursa(job #2439199)

Utilizator AlexandruLuchianov1Alex Luchianov AlexandruLuchianov1 Data 15 iulie 2019 13:06:06
Problema Walls Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.54 kb
#include <iostream>
#include <fstream>
#include <map>

using namespace std;

ifstream in ("walls.in");
ofstream out ("walls.out");

#define ll long long
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))

int const nmax = 100000;
map<int, int> hits[1 + nmax];

ll wall[1 + nmax][3];///start, length and height

class SegmenTree{
private:
  int n;
  int aint[1 + 4 * nmax];
  void update(int node, int from, int to, int pos, int val){
    if(from < to){
      int mid = (from + to) / 2;
      if(pos <= mid)
        update(node * 2, from, mid, pos, val);
      else
        update(node * 2 + 1, mid + 1, to, pos, val);
      aint[node] = MAX(aint[node * 2], aint[node * 2 + 1]);
    } else
      aint[node] = val;

  }
  int query(int node, int from, int to, int x, int val){ ///return last position with val <= v[i] && i <= x
    if(aint[node] < val)
      return 0;
    else if(from < to){
      int mid = (from + to) / 2;
      if(mid < x){
        int result = query(node * 2 + 1, mid + 1, to, x, val);
        if(0 < result)
            return result;
      }
      return query(node * 2, from, mid, x, val);
    } else
      return from;
  }
public:
  void init(int n){
    this->n = n;
  }
  void update(int pos, int val){
    update(1, 1, n, pos, val);
  }
  int query(int pos, int val){
    return query(1, 1, n, pos, val);
  }
};

int binarysearch(int from, int to, ll pos){
  if(from < to){
    int mid = (from + to + 1) / 2;
    if(wall[mid][0] <= pos)
      return binarysearch(mid, to, pos);
    else
      return binarysearch(from, mid - 1, pos);
  } else
    return from;
}

int main()
{
  int n, q;
  in >> n;
  ll start = 1;
  SegmenTree aint;
  aint.init(n);
  for(int i = 1;i <= n; i++){
    int l, height;
    in >> l >> height;
    wall[i][0] = start;
    wall[i][1] = l;
    wall[i][2] = height;
    start += l + 1;
    aint.update(i, height);
  }
  in >> q;

  for(int i = 1;i <= q; i++){
    int x, y;
    in >> x >> y;
    if(y == 0){
      out << "MISS\n";
      continue;
    }

    int pos = binarysearch(1, n, x);
    int pos2 = aint.query(pos, y);
    if(pos2 == 0)
      out << "MISS\n";
    else {
      hits[pos2][y]++;
      int realpos = wall[pos2][0] + wall[pos2][1] - hits[pos2][y];
      out << "HIT " << realpos << " " << pos2 << " " ;
      if(hits[pos2][y] == wall[pos2][1]) {
        out << "YES\n";
        aint.update(pos2, y - 1);
      } else {
        out << "NO\n";
      }
    }
  }
  return 0;
}