結果
問題 | No.2292 Interval Union Find |
ユーザー |
|
提出日時 | 2023-05-06 00:20:19 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 472 ms / 5,000 ms |
コード長 | 14,949 bytes |
コンパイル時間 | 4,736 ms |
コンパイル使用メモリ | 270,276 KB |
最終ジャッジ日時 | 2025-02-12 20:18:15 |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 44 |
コンパイルメッセージ
main.cpp: In member function ‘void Interval_map<S, T>::set(S, S, T, std::vector<_Tp>*, std::vector<_Tp>*, std::vector<_ValT>*) [with S = int; T = int]’: main.cpp:165:42: warning: ‘nl_l’ may be used uninitialized [-Wmaybe-uninitialized] 165 | bool n_l_flag = false; S nl_l, nr_l; T nv_l; | ^~~~
ソースコード
#ifndef HIDDEN_IN_VS // 折りたたみ用// 警告の抑制#define _CRT_SECURE_NO_WARNINGS// ライブラリの読み込み#include <bits/stdc++.h>using namespace std;// 型名の短縮using ll = long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9)using pii = pair<int, int>; using pll = pair<ll, ll>; using pil = pair<int, ll>; using pli = pair<ll, int>;using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>;using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>;using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>;using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>;template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;using Graph = vvi;// 定数の定義const double PI = acos(-1);const vi DX = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)const vi DY = { 0, 1, 0, -1 };int INF = 1001001001; ll INFL = 4004004004004004004LL;double EPS = 1e-15;// 入出力高速化struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;// 汎用マクロの定義#define all(a) (a).begin(), (a).end()#define sz(x) ((int)(x).size())#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x))#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x))#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}#define YES(b) {cout << ((b) ? "YES\n" : "NO\n");}#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)#define repb(set, d) for(int set = 0; set < (1 << int(d)); ++set) // d ビット全探索(昇順)#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了// 汎用関数の定義template <class T> inline ll pow(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら trueを返す)template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら trueを返す)template <class T> inline T get(T set, int i) { return (set >> i) & T(1); }// 演算子オーバーロードtemplate <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }// 手元環境(Visual Studio)#ifdef _MSC_VER#include "local.hpp"// 提出用(gcc)#elseinline int popcount(int n) { return __builtin_popcount(n); }inline int popcount(ll n) { return __builtin_popcountll(n); }inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : -1; }inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; }inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }#define gcd __gcd#define dump(...)#define dumpel(v)#define dump_list(v)#define dump_list2D(v)#define input_from_file(f)#define output_to_file(f)#define Assert(b) { if (!(b)) while (1) cout << "OLE"; }#endif#endif // 折りたたみ用#if __has_include(<atcoder/all>)#include <atcoder/all>using namespace atcoder;//using mint = modint1000000007;using mint = modint998244353;//using mint = modint; // mint::set_mod(m);namespace atcoder {inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }}using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;#endif//【区間からの写像】/** Interval_map<S, T>(T nil) : O(1)* S の全ての値に nil を割り当てる.** void set(S l, S r, T v) : ならし O(log n)* 区間 [l..r) に値 v を割り当てる.** void set(S l, S r, T v, vS* ls, vS* rs, vT* vs) : ならし O(log n)* 区間 [l..r) に含まれる区間の左端[右端, 値] を ls[rs, vs] に格納する.* その後区間 [l..r) に値 v を割り当てる.** T get(S x) : O(log n)* x に割り当てられた値を返す.*/template <class S, class T>class Interval_map {map<pair<S, S>, T> lr_to_v; // 区間 [l[i], r[i]) → v[i]T nil;// x が含まれる区間を指すイテレータを返す(なければ lr_to_v.end() を返す)typename map<pair<S, S>, T>::iterator get_iter(S x) {auto it = lr_to_v.lower_bound({ x, numeric_limits<S>::max() });if (it == lr_to_v.begin()) return lr_to_v.end();it--;if (it->first.first <= x && x < it->first.second) return it;else return lr_to_v.end();}// x の 1 つ右にある区間を指すイテレータを返す(なければ lr_to_v.end() を返す)typename map<pair<S, S>, T>::iterator get_right_iter(S x) {return lr_to_v.lower_bound({ x, numeric_limits<S>::max() });}// x の 1 つ左にある区間を指すイテレータを返す(なければ lr_to_v.end() を返す)typename map<pair<S, S>, T>::iterator get_left_iter(S x) {auto it = lr_to_v.lower_bound({ x, numeric_limits<S>::max() });if (it == lr_to_v.begin()) return lr_to_v.end();it--;if (it->first.first <= x && x < it->first.second) {if (it == lr_to_v.begin()) return lr_to_v.end();it--;}return it;}public:// 全ての値に nil を割り当てる.Interval_map(T nil_) : nil(nil_) {}// x に割り当てられた値を返す.T get(S x) {// verify : https://codeforces.com/contest/1638/problem/Eauto it = get_iter(x);return it == lr_to_v.end() ? nil : it->second;}// 区間 [l..r) に値 v を割り当てる.// また [l..r) に含まれていた区間の情報を ls, rs, vs に格納する.void set(S l, S r, T v, vector<S>* ls = nullptr, vector<S>* rs = nullptr, vector<T>* vs = nullptr) {// verify : https://atcoder.jp/contests/abc255/tasks/abc255_h// 左端 l がぶつかる区間を調べる.bool n_l_flag = false; S nl_l, nr_l; T nv_l;auto it_l = get_iter(l);if (it_l != lr_to_v.end()) {n_l_flag = true;nl_l = it_l->first.first;nr_l = it_l->first.second;nv_l = it_l->second;}else {it_l = get_right_iter(l);}// 右端 r がぶつかる区間を調べる.bool n_r_flag = false; S nl_r, nr_r; T nv_r;auto it_r = get_iter(r - 1);if (it_r != lr_to_v.end()) {n_r_flag = true;nl_r = it_r->first.first;nr_r = it_r->first.second;nv_r = it_r->second;}else {it_r = get_left_iter(r - 1);}if (ls != nullptr) {ls->clear(); rs->clear(); vs->clear();}if (ls != nullptr && n_l_flag) {if (n_r_flag && it_l == it_r) {ls->push_back(l); rs->push_back(r); vs->push_back(nv_l);}else {ls->push_back(l); rs->push_back(nr_l); vs->push_back(nv_l);}}if (it_l != lr_to_v.end() && it_r != lr_to_v.end()) {if (ls != nullptr) {for (auto it = it_l; it != next(it_r); it++) {if (n_l_flag && it == it_l) continue;if (n_r_flag && it == it_r) continue;ls->push_back(it->first.first);rs->push_back(it->first.second);vs->push_back(it->second);}}lr_to_v.erase(it_l, next(it_r));}if (ls != nullptr && n_r_flag) {if (!(n_l_flag && it_l == it_r)) {ls->push_back(nl_r); rs->push_back(r); vs->push_back(nv_r);}}lr_to_v[{l, r}] = v;if (n_l_flag) lr_to_v[{nl_l, l}] = nv_l;if (n_r_flag) lr_to_v[{r, nr_r}] = nv_r;}typename map<pair<S, S>, T>::iterator begin() { return lr_to_v.begin(); }typename map<pair<S, S>, T>::iterator end() { return lr_to_v.end(); }#ifdef _MSC_VERfriend ostream& operator<<(ostream& os, const Interval_map& d) {repe(p, d.lr_to_v) os << p << " ";return os;}#endif};// クエリ 2 を [L..R] に属する頂点を端点にもつような辺の全削除だと誤読していた。void WA() {int n, q;cin >> n >> q;dsu d(q);Interval_map<int, int> IM(-1);vi cnt(q);IM.set(0, INF, -1);int col = 0;rep(hoge, q) {int tp;cin >> tp;if (tp == 1) {int l, r;cin >> l >> r;l--;vi ls, rs, cs;IM.set(l, r, col, &ls, &rs, &cs);int m = sz(ls);int sum = 0;rep(j, m) {if (cs[j] == -1) {sum += rs[j] - ls[j];}else {int ld = d.leader(cs[j]);sum += cnt[ld];cnt[ld] = 0;d.merge(cs[j], col);}}int ld = d.leader(col);cnt[ld] = sum;col++;}else if (tp == 2) {int l, r;cin >> l >> r;l--;vi ls, rs, cs;IM.set(l + 1, r - 1, -1, &ls, &rs, &cs);int m = sz(ls);rep(j, m) {if (cs[j] == -1) continue;int ld = d.leader(cs[j]);cnt[ld] -= rs[j] - ls[j];}}else if (tp == 3) {int u, v;cin >> u >> v;u--; v--;int colu = IM.get(u);int colv = IM.get(v);int res = 0;if (colu != -1 && colv != -1 && d.same(colu, colv)) res = 1;cerr << "ans:";cout << res << endl;}else {int v;cin >> v;v--;int colv = IM.get(v);int res = 1;if (colv != -1) res = cnt[d.leader(colv)];cerr << "ans:";cout << res << endl;}dump(IM); dump(cnt); dump(d);}}//【区間の集合】/** Interval_set(bool marge = true) : O(1)* 空で初期化する.ちょうど接する区間を併合するかを marge に渡す.** int size() : O(1)* 区間の数を返す.** pll get(ll x) : O(log n)* x が含まれる区間 [l, r) を返す(なければ {-1, -1} を返す)** pll get_right(ll x) : O(log n)* x の 1 つ右にある区間 [l, r) を返す(なければ {-1, -1} を返す)** pll get_left(ll x) : O(log n)* x の 1 つ左にある区間 [l, r) を返す(なければ {-1, -1} を返す)** insert(ll l, ll r) : ならし O(log n)* 区間 [l, r) を追加する.** erase(ll l, ll r) : ならし O(log n)* 区間 [l, r) を削除する.*/class Interval_set {bool marge;// x が含まれる区間を指すイテレータを返す(なければ lr.end() を返す)typename set<pll>::iterator get_iter(ll x) const {auto it = lr.lower_bound({ x, INFL });if (it == lr.begin()) return lr.end();it--;if (it->first <= x && x < it->second) return it;else return lr.end();}// x の 1 つ右にある区間を指すイテレータを返す(なければ lr.end() を返す)typename set<pll>::iterator get_right_iter(ll x) const {return lr.lower_bound({ x, INFL });}// x の 1 つ左にある区間を指すイテレータを返す(なければ lr.end() を返す)typename set<pll>::iterator get_left_iter(ll x) const {auto it = lr.lower_bound({ x, INFL });if (it == lr.begin()) return lr.end();it--;if (it->first <= x && x < it->second) {if (it == lr.begin()) return lr.end();it--;}return it;}typename set<pll>::iterator begin() const { return lr.begin(); }typename set<pll>::iterator end() const { return lr.end(); }public:set<pll> lr; // 区間 [l[i], r[i]) の昇順列// コンストラクタ(空で初期化)Interval_set(bool marge_ = true) : marge(marge_) {}// 区間の数を返す.int size() const { return sz(lr); }// x が含まれる区間 [l, r) を返す(なければ {-1, -1} を返す)pll get(ll x) const {// verify : https://atcoder.jp/contests/abc228/tasks/abc228_dauto it = get_iter(x);return it == lr.end() ? make_pair(-1LL, -1LL) : *it;}// x の 1 つ右にある区間 [l, r) を返す(なければ {-1, -1} を返す)pll get_right(ll x) const {// verify : https://atcoder.jp/contests/code-festival-2015-qualb/tasks/codefestival_2015_qualB_dauto it = get_right_iter(x);return it == lr.end() ? make_pair(-1LL, -1LL) : *it;}// x の 1 つ左にある区間 [l, r) を返す(なければ {-1, -1} を返す)pll get_left(ll x) const {auto it = get_left_iter(x);return it == lr.end() ? make_pair(-1LL, -1LL) : *it;}// 区間 [l, r) を追加する.void insert(ll l, ll r) {// verify : https://atcoder.jp/contests/abc254/tasks/abc254_gif (l >= r) return;auto it_l = get_iter(l - (int)marge);if (it_l == lr.end()) it_l = get_right_iter(l - (int)marge);auto it_r = get_iter(r - (int)(!marge));if (it_r == lr.end()) it_r = get_left_iter(r - (int)(!marge));if (it_l != lr.end() && it_r != lr.end()) {chmin(l, it_l->first);chmax(r, it_r->second);lr.erase(it_l, ++it_r);}lr.insert({ l, r });}// 区間 [l, r) を削除する.void erase(ll l, ll r) {if (l >= r) return;ll l2 = l, r2 = r;auto it_l = get_iter(l);if (it_l != lr.end()) l2 = it_l->first;else it_l = get_right_iter(l - 1);auto it_r = get_iter(r);if (it_r != lr.end()) r2 = it_r->second;else it_r = get_left_iter(r);if (it_l != lr.end() && it_r != lr.end()) {lr.erase(it_l, ++it_r);}if (l2 < l) lr.insert({ l2, l });if (r2 > r) lr.insert({ r, r2 });}#ifdef _MSC_VERfriend ostream& operator<<(ostream& os, const Interval_set& d) {repe(p, d.lr) os << "[" << p.first << "," << p.second << ") ";return os;}#endif};int main() {// input_from_file("input.txt");// output_to_file("output.txt");int n, q;cin >> n >> q;Interval_set IS;dump(IS);rep(hoge, q) {int tp;cin >> tp;if (tp == 1) {int l, r;cin >> l >> r;IS.insert(2 * l, 2 * r + 1);}else if (tp == 2) {int l, r;cin >> l >> r;IS.erase(2 * l + 1, 2 * r);}else if (tp == 3) {int u, v;cin >> u >> v;if (u == v) {cout << 1 << endl;continue;}if (u > v) swap(u, v);auto [l, r] = IS.get(2 * u);cerr << " ans:";cout << (int)(2 * v < r) << endl;}else {int v;cin >> v;auto [l, r] = IS.get(2 * v);cerr << " ans:";cout << max((r - l + 1) / 2, 1LL) << endl;}dump(IS);}}