A. Hasan the lazy judge

#include <bits/stdc++.h>
using namespace std;

inline int read(){
    int x=0; bool f=0; char ch=getchar();
    while (ch<'0' || '9'<ch) f|=ch=='-', ch=getchar();
    while ('0'<=ch && ch<='9') x=x*10+ch-'0', ch=getchar();
    return f?-x:x;
}

const int maxn = 1e5 + 10;
int T, n, m;

int c[maxn];
int limit;

int lowbit(int x) {
  return x & (-x);
}

void update(int p, int v) {
  for(int i = p; i <= limit; i = i + lowbit(i)) {
    c[i] += v;
  }
}

int sum(int p) {
  int ans = 0;

  while(p >= 1) {
    ans += c[p];
    p -= lowbit(p);
  }

  return ans;
}

struct H { int x1, x2, y; } h[maxn];
struct V { int y1, y2, x; } v[maxn];

struct F {int x, y; } in[maxn], out[maxn];

bool cmp(const V& v1, const V& v2) {
  return v1.x < v2.x;
}

bool cmpf(const F& v1, const F& v2) {
  return v1.x < v2.x;
}

bool check(int len) {

  bool res = 0;

  int in_index = 0;
  int out_index = 0;

  for(int i = 1; i <= n; i ++) {
    if(h[i].x2 - h[i].x1 < 2 * len) continue;

    in_index ++;
    in[in_index].x = h[i].x1 + len;
    in[in_index].y = h[i].y;

    out_index ++;
    out[out_index].x = h[i].x2 - len;
    out[out_index].y = h[i].y;
  }

  sort(in + 1, in + in_index + 1, cmpf);
  sort(out + 1, out + out_index + 1, cmpf);

  int in_i = 1, out_i = 1;

  for(int j = 1; j <= m; j ++) {

    if(v[j].y2 - v[j].y1 < 2 * len) continue;

    while(in_i <= in_index && in[in_i].x <= v[j].x) {
      update(in[in_i].y, 1);
      in_i ++;
    }

    while(out_i <= out_index && out[out_i].x < v[j].x) {
      update(out[out_i].y, -1);
      out_i ++;
    }

    if(sum(v[j].y2 - len) - sum(v[j].y1 + len - 1) != 0) res = 1;
  }

  while(in_i <= in_index) {
    update(in[in_i].y, 1);
    in_i ++;
  }

  while(out_i <= out_index) {
    update(out[out_i].y, -1);
    out_i ++;
  }

  return res;
}

int main() {
  scanf("%d", &T);
  for(int cas = 1; cas <= T; cas ++) {

    int L = 0, R = 0, ans = 0;
    limit = 0;

    scanf("%d%d", &n, &m);

    for(int i = 1; i <= n; i ++) {
      h[i].x1 = read();
      h[i].x2 = read();
      h[i].y = read();

      if(h[i].x2 < h[i].x1) swap(h[i].x1, h[i].x2);
      R = max(R, (h[i].x2 - h[i].x1) / 2);
      limit = max(limit, h[i].x2);
    }

    for(int j = 1; j <= m; j ++) {
      v[j].y1 = read();
      v[j].y2 = read();
      v[j].x = read();

      if(v[j].y2 < v[j].y1) swap(v[j].y1, v[j].y2);
      R = max(R, (v[j].y2 - v[j].y1) / 2);
      limit = max(limit, v[j].x);
    }

    sort(v + 1, v + m + 1, cmp);

    while(L <= R) {
      int mid = (L + R) / 2;
      if(check(mid)) {
        ans = mid;
        L = mid + 1;
      } else {
        R = mid - 1;
      }
    }

    printf("%d\n", ans);

  }
  return 0;
}
View Code

相关文章:

  • 2021-07-09
  • 2021-05-17
  • 2021-05-26
  • 2022-01-20
  • 2022-12-23
  • 2021-09-04
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-02
  • 2021-11-08
  • 2022-12-23
相关资源
相似解决方案