結果
| 問題 |
No.2292 Interval Union Find
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-03-30 02:08:23 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 194 ms / 5,000 ms |
| コード長 | 1,653 bytes |
| コンパイル時間 | 1,969 ms |
| コンパイル使用メモリ | 175,508 KB |
| 実行使用メモリ | 12,928 KB |
| 最終ジャッジ日時 | 2024-09-22 02:30:49 |
| 合計ジャッジ時間 | 10,255 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 44 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
struct Problem_D{
set<pair<int,int>> S;
Problem_D(int N) { S.insert({N + 1, N + 1}); }
void insert(int L, int R){
int l, r;
auto it = S.lower_bound({L, -1});
while(it->second <= R){
tie(r, l) = *it;
S.erase(it);
L = min(L, l);
R = max(R, r);
it = S.lower_bound({L, -1});
}
S.insert({R, L});
}
void erase(int L, int R){
int l, r;
auto it = S.lower_bound({L + 1, -1});
while(it->second < R){
tie(r, l) = *it;
S.erase(it);
if(l < L) S.insert({L, l});
if(R < r) S.insert({r, R});
it = S.lower_bound({L + 1, -1});
}
}
bool same(int v, int u){
if(v == u) return true;
if(u < v) swap(u, v);
auto it = S.lower_bound({v, -1});
return (it->second <= v && u <= it->first);
}
int size(int v){
auto it = S.lower_bound({v, -1});
if(it->second <= v && v <= it->first) return it->first - it->second + 1;
return 1;
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int N, Q;
cin >> N >> Q;
Problem_D uf(N);
int type, L, R, v, u;
while(Q--){
cin >> type;
if(type <= 2){
cin >> L >> R;
if(type == 1) uf.insert(L, R);
else uf.erase(L, R);
}else if(type == 3){
cin >> u >> v;
cout << (uf.same(u, v) ? 1 : 0) << '\n';
}else{
cin >> v;
cout << uf.size(v) << '\n';
}
}
}