#include "obstacles.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using pi = pair<int, int>;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ROF(i, a, b) for (int i = a; i >= b; i--)
#define each(a, x) for (auto &a : x)
#define all(x) (x).begin(), (x).end()
#define bg(x) (x).begin()
#define en(x) (x).end()
#define rev(x) reverse(all(x))
#define sz(x) int((x).size())
#define srt(x) sort(all(x))
#define cnt(a, x) count(all(x), a)
#define trace(x) each(a, (x)) cout << a << " "
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
struct DSU {
int n; vi par, siz;
DSU() {};
DSU(int N) {
n = N; par.resize(n); siz.assign(n, 1);
iota(all(par), 0);
}
int find(int v) {
if (par[v] == v) return v;
return par[v] = find(par[v]);
}
void unite(int a, int b) {
a = find(a); b = find(b);
if (a != b) {
if (siz[a] < siz[b]) swap(a, b);
par[b] = a;
siz[a] += siz[b];
}
}
};
struct SegTree {
int n; vector<int> arr, st;
SegTree() {};
SegTree(int N, vector<int> &Arr) {
n = N; arr = Arr; st.assign(4*n, INT_MIN);
build(0, 0, n);
}
int build(int i, int l, int r) {
if (l + 1 == r) return st[i] = arr[l];
int m = l + (r - l) / 2;
return st[i] = max(build(2*i+1, l, m), build(2*i+2, m, r));
}
int query(int i, int l, int r, int ql, int qr) {
if (r <= ql || qr <= l) return INT_MIN;
if (ql <= l && r <= qr) return st[i];
int m = l + (r - l) / 2;
return max(query(2*i+1, l, m, ql, qr), query(2*i+2, m, r, ql, qr));
}
int query(int l, int r) {
return query(0, 0, n, l, r);
}
};
int n, m;
DSU dsu;
SegTree rows, cols;
vi t, h;
// vvi id;
void quadratic() {
// id.assign(n, vi(m, -1));
// dsu = DSU(n*m);
dsu = DSU(m);
int cd = 0;
// FOR(i, 0, n) {
// FOR(j, 0, m) {
// id[i][j] = cd++;
// }
// }
// FOR(i, 0, n) {
FOR(j, 0, m) {
// if (T[i] <= h[j]) continue;
if (t[n-1] <= h[j]) continue;
// if (i > 0 && t[i-1] > h[j]) dsu.unite(id[i][j], id[i-1][j]);
// if (j > 0 && t[i] > h[j-1]) dsu.unite(id[i][j], id[i][j-1]);
if (j > 0 && t[n-1] > h[j-1]) dsu.unite(j, j-1);
// if (i < n - 1 && t[i+1] > h[j]) dsu.unite(id[i][j], id[i+1][j]);
// if (j < m - 1 && t[i] > h[j+1]) dsu.unite(id[i][j], id[i][j+1]);
if (j < m - 1 && t[n-1] > h[j+1]) dsu.unite(j, j+1);
}
// }
}
void initialize(vi T, vi H) {
t = T; h = H;
n = sz(T), m = sz(H);
// quadratic();
rows = SegTree(n, T); cols = SegTree(m, H);
return;
}
bool can_reach(int L, int R, int S, int D) {
if (S > D) swap(S, D);
int a = S, b = S;
while (a > 0 && t[0] > h[a - 1]) a--;
while (b < m - 1 && t[0] > h[b+1]) b++;
int c = D, d = D;
while (c > 0 && t[0] > h[c - 1]) c--;
while (d < m- 1 && t[0] > h[d+1]) d++;
if (b >= D) return true;
int cl = a; for (int i = a; i <= b; i++) if (h[i] < h[cl]) cl = i;
int cr = c; for (int i = c; i <= d; i++) if (h[i] < h[cr]) cr = i;
int k = 0; for (; k < n; k++) if (k == n - 1 || t[k + 1] <= h[cl] || t[k+1] <= h[cr]) break;
int br = rows.query(0, k+1);
int bc = cols.query(cl, cr+1);
return br > bc;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |