結果

問題 No.2292 Interval Union Find
ユーザー prism17prism17
提出日時 2023-07-20 14:41:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,734 ms / 5,000 ms
コード長 3,724 bytes
コンパイル時間 2,327 ms
コンパイル使用メモリ 185,656 KB
実行使用メモリ 91,288 KB
最終ジャッジ日時 2023-10-20 15:24:30
合計ジャッジ時間 46,938 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1,033 ms
19,444 KB
testcase_05 AC 1,026 ms
19,444 KB
testcase_06 AC 1,018 ms
19,444 KB
testcase_07 AC 1,312 ms
19,444 KB
testcase_08 AC 1,644 ms
91,252 KB
testcase_09 AC 1,650 ms
91,252 KB
testcase_10 AC 1,621 ms
91,252 KB
testcase_11 AC 1,638 ms
91,252 KB
testcase_12 AC 1,607 ms
91,252 KB
testcase_13 AC 1,520 ms
91,252 KB
testcase_14 AC 1,734 ms
91,252 KB
testcase_15 AC 1,638 ms
91,252 KB
testcase_16 AC 1,587 ms
91,252 KB
testcase_17 AC 1,656 ms
91,252 KB
testcase_18 AC 173 ms
91,288 KB
testcase_19 AC 357 ms
91,288 KB
testcase_20 AC 358 ms
91,288 KB
testcase_21 AC 896 ms
91,252 KB
testcase_22 AC 889 ms
91,252 KB
testcase_23 AC 907 ms
91,252 KB
testcase_24 AC 903 ms
91,252 KB
testcase_25 AC 879 ms
91,252 KB
testcase_26 AC 888 ms
91,252 KB
testcase_27 AC 865 ms
91,252 KB
testcase_28 AC 886 ms
91,252 KB
testcase_29 AC 900 ms
91,252 KB
testcase_30 AC 898 ms
91,252 KB
testcase_31 AC 886 ms
91,252 KB
testcase_32 AC 870 ms
91,252 KB
testcase_33 AC 854 ms
91,252 KB
testcase_34 AC 875 ms
91,252 KB
testcase_35 AC 877 ms
91,252 KB
testcase_36 AC 857 ms
91,252 KB
testcase_37 AC 857 ms
91,252 KB
testcase_38 AC 871 ms
91,252 KB
testcase_39 AC 864 ms
91,252 KB
testcase_40 AC 875 ms
91,252 KB
testcase_41 AC 66 ms
10,708 KB
testcase_42 AC 72 ms
10,708 KB
testcase_43 AC 97 ms
10,708 KB
testcase_44 AC 352 ms
10,708 KB
testcase_45 AC 407 ms
10,708 KB
testcase_46 AC 416 ms
10,708 KB
testcase_47 AC 580 ms
10,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// Problem: No.2292 Interval Union Find No.2292 区间并集查找
// Contest: yukicoder
// URL: https://yukicoder.me/problems/no/2292
// Memory Limit: 512 MB
// Time Limit: 5000 ms

#include <bits/stdc++.h>

#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define dbg(x) cout << #x << " = " << (x) << "\n";
#define popcount(x) __builtin_popcountll((x))
#define all(v) (v).begin(), (v).end()
#define pb emplace_back
#define x first
#define y second

using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;

const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int N = 8e5 + 7;

struct sgt {
#define lson (id << 1)
#define rson (id << 1 | 1)
#define mid (st[id].l + st[id].r >> 1)
  int index;
  struct node {
    int l, r, lz;
    ll sum, ls, rs;
  } st[N << 2];
  void pushnow(int id, int val) {
    // 给自己打lazy标记
    st[id].rs = st[id].ls = st[id].sum = val ? (st[id].r - st[id].l + 1) : 0;
    st[id].lz = val;
  }
  void pushup(int id) { st[id].sum = st[lson].sum + st[rson].sum; }
  void pushdown(int id) {
    if (st[id].lz != -1) {
      pushnow(lson, st[id].lz);
      pushnow(rson, st[id].lz);
      st[id].lz = -1;
    }
  }
  void build(int id, int l, int r) {
    st[id].l = l;
    st[id].r = r;
    if (l == r) {
      st[id].lz = -1;
      st[id].sum = 0;
      return;
    }
    build(lson, l, mid);
    build(rson, mid + 1, r);
    pushup(id);
  }
  void update(int id, int l, int r, int val) {
    if (st[id].l > r || st[id].r < l) return;
    if (st[id].l >= l && st[id].r <= r) {
      pushnow(id, val);
      return;
    }
    pushdown(id);
    update(lson, l, r, val);
    update(rson, l, r, val);
    pushup(id);
  }
  ll query(int id, int l, int r) {
    if (st[id].l > r || st[id].r < l) return 0;
    if (st[id].l >= l && st[id].r <= r) {
      return st[id].sum;
    }
    pushdown(id);
    return query(lson, l, r) + query(rson, l, r);
  }
} st;

int n, q;

void solve() {
  cin >> n >> q;
  vector<int> op(q), l(q), r(q);
  vector<int> m;
  for (int i = 0; i < q; i++) {
    cin >> op[i];
    if (op[i] == 4) {
      cin >> l[i];
      m.pb(l[i]);
    } else {
      cin >> l[i] >> r[i];
      m.pb(l[i]);
      m.pb(l[i] - 1);
      m.pb(r[i]);
      m.pb(r[i] - 1);
    }
  }
  m.pb(1);
  m.pb(n - 1);
  sort(all(m));
  m.erase(unique(all(m)), m.end());
  st.build(1, 1, m.size());
  auto get = [&](int x) { return lower_bound(all(m), x) - m.begin() + 1; };
  for (int i = 0; i < q; i++) {
    if (op[i] == 1) {
      --r[i];
      st.update(1, get(l[i]), get(r[i]), 1);
    } else if (op[i] == 2) {
      --r[i];
      st.update(1, get(l[i]), get(r[i]), 0);
    } else if (op[i] == 3) {
      if (l[i] == r[i]) {
        cout << "1\n";
        continue;
      }
      if (l[i] > r[i]) swap(l[i], r[i]);
      --r[i];
      cout << ((st.query(1, get(l[i]), get(r[i])) == get(r[i]) - get(l[i]) + 1)
                   ? 1
                   : 0)
           << "\n";
    } else {
      int ansL = 0, ansR = 0;
      int L = get(1), R = get(l[i] - 1);
      while (L < R) {
        int mm = L + R >> 1;
        if (st.query(1, mm, get(l[i] - 1)) == get(l[i] - 1) - mm + 1)
          R = mm;
        else
          L = mm + 1;
      }
      ansL = L;
      if (st.query(1, ansL, ansL) == 0) ++ansL;
      L = get(l[i]), R = get(n - 1);
      while (L < R) {
        int mm = L + R + 1 >> 1;
        if (st.query(1, get(l[i]), mm) == mm - get(l[i]) + 1)
          L = mm;
        else
          R = mm - 1;
      }
      ansR = L;
      if (st.query(1, ansR, ansR) == 0) --ansR;
      cout << max(1, m[ansR - 1] - m[ansL - 1] + 2) << "\n";
    }
  }
}

int main() {
  fastio;
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
0