結果

問題 No.2292 Interval Union Find
ユーザー ecotteaecottea
提出日時 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;
      |                                          ^~~~

ソースコード

diff #
プレゼンテーションモードにする

#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^18int -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
#else
inline 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/E
auto 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_VER
friend 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_d
auto 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_d
auto 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_g
if (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_VER
friend 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);
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0