This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("Ofast,O3")
//#pragma GCC target("avx,avx2")
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debugger.cpp"
#else
#define debug(...)
#endif
template<class T>
int len(T &a){
return a.size();
}
using ll = long long;
using pii = pair<int,int>;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
string fileio = "";
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int MOD = 1e9 + 7;
const int inf = 1e9;
const ll infl = 1e18;
const int maxn = 1e5 + 1;
struct DSU_roll_back{
int n;
vector<int> p;
vector<int> sz;
vector<int> d;
vector<pair<int &, int>> his;
int bip = 1;
DSU_roll_back (int n) : n(n), p(n), sz(n), d(n){
for(int i = 0; i < n;i ++) p[i] = i, sz[i] = 1;
}
int get(int x, int &col){
col ^= d[x];
return p[x] == x ? x : get(p[x], col);
}
void unite(int a, int b){
if(bip == 0){
return;
}
int ca = 0, cb = 0;
a = get(a, ca), b = get(b, cb);
if(a == b){
if(ca == cb){
his.push_back({bip, bip});
bip = 0;
}
return;
}
if(sz[a] < sz[b]) swap(a, b);
his.push_back({sz[a], sz[a]});
his.push_back({p[b], p[b]});
his.push_back({d[b], d[b]});
d[b] = ca == cb;
sz[a] += sz[b];
p[b] = a;
}
int snap(){
return his.size();
}
void roll_back(int until){
while(len(his) > until){
his.back().ff = his.back().ss;
his.pop_back();
}
}
};
void solve(){
int n, m, q; cin >> n >> m >> q;
vector<int> a(m + 1), b(m + 1);
for(int i = 1; i <= m; i ++){
cin >> a[i] >> b[i]; a[i] --; b[i] --;
}
vector<vector<pair<int,int>>> qu(m + 1);
for(int i = 0; i < q; i++){
int l, r; cin >> l >> r;
qu[l].push_back({r, i});
}
DSU_roll_back ds(n);
vector<int> ans(q);
auto calc = [&](auto &calc, int l, int r, int optr)->void{
if(l > r) return;
int m = (l + r) / 2;
int lsz = ds.snap();
for(int i = l; i < m; i ++) ds.unite(a[i], b[i]);
int optm = optr;
while(optr >= m && ds.bip){
optm --;
ds.unite(a[optm], b[optm]);
}
for(auto [rig, j] : qu[m]){
ans[j] = rig < optm;
}
ds.roll_back(lsz);
for(int i = optr - 1; i >= optm; i --){
ds.unite(a[i], b[i]);
}
calc(calc, l, m - 1, optm);
ds.roll_back(lsz);
for(int i = l; i <= m; i ++){
ds.unite(a[i], b[i]);
}
calc(calc, m + 1, r, optr);
ds.roll_back(lsz);
};
calc(calc, 1, m, m + 1);
for(auto u : ans) cout << (u ? "YES\n" : "NO\n");
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
auto start = chrono::high_resolution_clock::now();
#ifndef LOCAL
if(fileio.size()){
freopen((fileio + ".in").c_str(), "r", stdin);
freopen((fileio + ".out").c_str(), "w", stdout);
}
#endif
int testcases = 1;
#ifdef Tests
cin >> testcases;
#endif
while(testcases --){
solve();
if(testcases) cout << '\n';
#ifdef LOCAL
else cout << '\n';
cout << "_________________________" << endl;
#endif
}
#ifdef LOCAL
auto duration = chrono::duration_cast<chrono::microseconds>(chrono::high_resolution_clock::now() - start);
cout << "time: " << (double)duration.count()/1000.0 << " milliseconds" << endl;
#endif
return 0;
}
Compilation message (stderr)
Joker.cpp: In function 'int main()':
Joker.cpp:136:10: warning: variable 'start' set but not used [-Wunused-but-set-variable]
136 | auto start = chrono::high_resolution_clock::now();
| ^~~~~
Joker.cpp:139:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
139 | freopen((fileio + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
140 | freopen((fileio + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |