Submission #484378

#TimeUsernameProblemLanguageResultExecution timeMemory
484378xiaowuc1Examination (JOI19_examination)C++17
100 / 100
527 ms41440 KiB
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>

using namespace std;

// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
typedef vector<int> vi;
#define f first
#define s second
// END NO SAD

template<class Fun>
class y_combinator_result {
  Fun fun_;
public:
  template<class T>
  explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}

  template<class ...Args>
  decltype(auto) operator()(Args &&...args) {
    return fun_(std::ref(*this), std::forward<Args>(args)...);
  }
};

template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
  return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<vector<ll>> matrix;

struct koosaga {
  vector<vector<int>> ragetree;
  int SZ;
  koosaga(int n) {
    SZ = 1;
    while(SZ < n) SZ *= 2;
    ragetree.resize(2*SZ);
  }
  void upd(int idx, int val) {
    idx += SZ;
    while(idx) {
      ragetree[idx].pb(val);
      idx /= 2;
    }
  }
  int qry(const vector<int>& v, int x) {
    return ub(all(v), x) - v.begin();
  }
  int qry(int lhs, int rhs, int x) {
    int ret = 0;
    lhs += SZ;
    rhs += SZ;
    while(lhs <= rhs) {
      if(lhs%2) ret += qry(ragetree[lhs++], x);
      if(rhs%2==0) ret += qry(ragetree[rhs--], x);
      lhs /= 2;
      rhs /= 2;
    }
    return ret;
  }
};

void gen(koosaga& koo, vector<pii>& v) {
  sort(all(v));
  for(auto[y,x]: v) {
    koo.upd(x, y);
  }
}

void solve() {
  int n, q;
  cin >> n >> q;
  vector<pii> sump, yp;
  vector<int> xs;
  for(int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    xs.pb(x);
    sump.eb(x+y, x);
    yp.eb(y, x);
  }
  sort(all(xs));
  xs.erase(unique(all(xs)), xs.end());
  koosaga ykoo(sz(xs)), sumkoo(sz(xs));
  for(auto& [_, x]: sump) x = lb(all(xs), x) - xs.begin();
  for(auto& [_, x]: yp) x = lb(all(xs), x) - xs.begin();
  gen(ykoo, yp);
  gen(sumkoo, sump);
  while(q--) {
    int x, y, z;
    cin >> x >> y >> z;
    if(x+y >= z) {
      // the boring case
      int ret = n;
      int xidx = ub(all(xs), x-1) - xs.begin();
      ret -= ykoo.qry(0, xidx-1, 2e9);
      ret -= ykoo.qry(xidx, sz(xs)-1, y-1);
      cout << ret << "\n";
    }
    else {
      int ret = n;
      int xidx = ub(all(xs), x-1) - xs.begin();
      ret -= ykoo.qry(0, xidx-1, 2e9);
      int xidx2 = ub(all(xs), z-y) - xs.begin();
      ret -= sumkoo.qry(xidx, xidx2-1, z-1);
      ret -= ykoo.qry(xidx2, sz(xs)-1, y-1);
      cout << ret << "\n";
    }
  }
}

// what would chika do
// are there edge cases (N=1?)
// are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// integer overflow?
// DS reset properly between test cases
// are you doing geometry in floating points
// are you not using modint when you should

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...