#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
int bad_edges = 0;
int next_[200'001];
int sub[200'001];
int ans[200'001];
vector<pii> edges;
int n,m,q;
pii find_(int v)
{
if(next_[v] == v) return {v,0};
pii nxt = find_(next_[v]);
return {nxt.ff,nxt.ss^1};
}
stack<pair<pii,int>> opers;
void union_(int a, int b)
{
if(sub[a] < sub[b]) swap(a,b);
pii ra = find_(a);
pii rb = find_(b);
if(ra.ff == rb.ff)
{
opers.push({{a,b},0});
if(ra.ss == rb.ss) bad_edges++;
}
else
{
opers.push({{ra.ff,rb.ff},1});
sub[a] += sub[b];
next_[b] = a;
}
}
void undo_oper()
{
pair<pii,int> t = opers.top();
opers.pop();
int a = t.ff.ff;
int b = t.ff.ss;
if(t.ss == 0)
{
pii ra = find_(a);
pii rb = find_(b);
if(ra.ss == rb.ss) bad_edges--;
}
else
{
sub[a] -= sub[b];
next_[b] = b;
}
}
void solve(int l, int r, int a, int b)
{
//cout << l << " " << r << " " << a << " " << b << " solve\n";
if(l > r) return;
if(a > b)
{
rep2(i,l,r)
{
ans[i] = -1e9;
}
return;
}
int mid = (l+r)/2;
int res = -1;
rep2(i,l,mid-1)
{
union_(edges[i].ff,edges[i].ss);
}
if(bad_edges > 0)
{
rep2(i,l,mid) ans[i] = -1e9;
rep2(i,l,mid-1) undo_oper();
solve(l,mid-1,a,b);
return;
}
for(int i = b; i >= a; i--)
{
union_(edges[i].ff,edges[i].ss);
if(bad_edges > 0)
{
undo_oper();
res = i;
break;
}
}
ans[mid] = res;
rep2(i,res+1,r) undo_oper();
rep2(i,l,mid-1) undo_oper();
rep2(i,res+1,r) union_(edges[i].ff,edges[i].ss);
solve(l,mid-1,a,res);
rep2(i,res+1,r) undo_oper();
rep2(i,l,mid) union_(edges[i].ff,edges[i].ss);
solve(mid+1,r,res,b);
rep2(i,l,mid) undo_oper();
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//random_start();
cin >> n >> m >> q;
rep2(i,1,n)
{
next_[i] = i;
sub[i] = 1;
}
rep(i,m)
{
int a,b;
cin >> a >> b;
edges.pb({a,b});
}
solve(0,m-1,0,m-1);
rep(i,q)
{
int l,r;
cin >> l >> r;
l--;
r--;
if(ans[l] <= r) cout << "NO\n";
else cout << "YES\n";
}
}
# | 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... |