Submission #1158931

#TimeUsernameProblemLanguageResultExecution timeMemory
1158931Zbyszek99Joker (BOI20_joker)C++20
14 / 100
2092 ms4188 KiB
#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;
pii 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].ff == v) return {v,0};
    pii nxt = find_(next_[v].ff);
    return {nxt.ff,nxt.ss^next_[v].ss};
}

vector<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)
    {
        if(ra.ss == rb.ss) 
        {
            opers.pb({{a,b},-1});
            bad_edges++;
        }
        else
        {
            opers.pb({{a,b},0});
        }
    }
    else
    {
        opers.pb({{ra.ff,rb.ff},1});
        sub[ra.ff] += sub[rb.ff];
        next_[rb.ff].ff = ra.ff;
        next_[rb.ff].ss = 1;
        if(ra.ss != rb.ss) next_[rb.ff].ss = 0;
    }
}

void undo_oper()
{
    pair<pii,int> t = opers.back();
    opers.pop_back();
    int a = t.ff.ff;
    int b = t.ff.ss;
    if(t.ss <= 0)
    {
        if(t.ss == -1) bad_edges--;
    }
    else
    {
        sub[a] -= sub[b];
        next_[b] = {b,0};
    }
}

void solve(int l, int r, int a, int b)
{
    if(l > r) return;
    if(a > b)
    {
        rep2(i,l,r)
        {
            ans[i] = 1e9;
        }
        return;
    }
    int mid = (l+r)/2;
    int res = mid-1;
    rep2(i,l,mid-1)
    {
        union_(edges[i].ff,edges[i].ss);
    }
    if(bad_edges > 0)
    {
        rep2(i,mid,r) ans[i] = 1e9;
        rep2(i,l,mid-1) undo_oper();
        solve(l,mid-1,a,b);
        return;
    }
    for(int i = b; i >= max(a,mid); i--)
    {
        union_(edges[i].ff,edges[i].ss);
        if(bad_edges > 0)
        {
            undo_oper();
            res = i;
            break;
        }
    }
    ans[mid] = res;
    rep2(i,res+1,b) undo_oper();
    rep2(i,l,mid-1) undo_oper();
    rep2(i,res+1,b) union_(edges[i].ff,edges[i].ss);
    solve(l,mid-1,a,res);
    rep2(i,res+1,b) 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,0};
        sub[i] = 1;
    }
    rep(i,m)
    {
        int a,b;
        cin >> a >> b;
        edges.pb({a,b});
        union_(a,b);
    }
    if(bad_edges != 0)
    {
        rep(i,m) undo_oper();
        solve(0,m-1,0,m-1);
    }
    else
    {
        rep(i,m)
        {
            ans[i] = i-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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...