Submission #1125101

#TimeUsernameProblemLanguageResultExecution timeMemory
1125101hainam2k9Joker (BOI20_joker)C++20
100 / 100
624 ms10192 KiB
// Why so serious
#include <bits/stdc++.h>
#define tt cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0)
#define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define db long double
#define sz(a) ((int)(a).size())
#define pb emplace_back
#define pf emplace_front
#define pob pop_back
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define ins emplace
#define mp make_pair
using namespace std;
const int MOD = 1e9+7, MAXN = 2e5+5;
const string NAME = "";
int cnt=0;
struct DSU{
    pair<int,bool> p[MAXN];
    int sz[MAXN];
    bool bipartite[MAXN];
    stack<pair<int,pair<int,bool>>> historyA,historyB;
    void save(int a, int b){
        historyA.ins(a,mp(sz[a],bipartite[a]));
        historyB.ins(b,p[b]);
    }
    void rollback(){
        if(historyA.empty()) return;
        sz[historyA.top().fi]=historyA.top().se.fi;
        cnt-=bipartite[historyA.top().fi]^1;
        bipartite[historyA.top().fi]=historyA.top().se.se;
        cnt+=bipartite[historyA.top().fi]^1;
        p[historyB.top().fi]=historyB.top().se;
        historyA.pop(), historyB.pop();
    }
    void init(int n){
        for(int i = 1; i<=n; ++i)
            p[i]=mp(i,0);
        fill(sz+1,sz+n+1,1);
        fill(bipartite+1,bipartite+n+1,1);
    }
    pair<int,int> Find(int u){
        bool color=0;
        while(u!=p[u].fi){
            color^=p[u].se;
            u=p[u].fi;
        }
        return mp(u,color);
    }
    void Union(int a, int b){
        pair<int,int> parentA=Find(a), parentB=Find(b);
        a=parentA.fi, b=parentB.fi;
        if(sz[a]<sz[b]) swap(a,b);
        save(a,b), cnt-=(bipartite[a]^1);
        if(a==b&&parentA.se==parentB.se) bipartite[a]=0;
        else if(a!=b){
            p[b]=mp(a,parentA.se^parentB.se^1);
            sz[a]+=sz[b], bipartite[a]&=bipartite[b];
        }
        cnt+=bipartite[a]^1;
    }
}dsu;
int n,m,q,last[MAXN],L,R;
pair<int,int> edge[MAXN];
void dnc(int l, int r, int optl, int optr){
    if(l>r) return;
    int mid=(l+r)>>1, curL=L, curR=R;
    while(L<mid-1) ++L, dsu.Union(edge[L].fi,edge[L].se);
    while(R>optr+1) --R, dsu.Union(edge[R].fi,edge[R].se);
    while(cnt==0&&R>max(L,optl-1)+1) --R, dsu.Union(edge[R].fi,edge[R].se);
    last[mid]=R-1;
    while(R<optr+1) ++R, dsu.rollback();
    if(mid+1<=r) dnc(mid+1,r,last[mid],optr);
    if(l<=mid-1){
        while(R<curR) ++R, dsu.rollback();
        while(L+1>(l+mid-1)>>1) --L, dsu.rollback();
        dnc(l,mid-1,optl,last[mid]);
    }
    while(R<curR) ++R, dsu.rollback();
    while(L>curL) --L, dsu.rollback();
}
int main()
{
    tt;
    if(fopen((NAME + ".INP").c_str(), "r")) fo;
    cin >> n >> m >> q;
    for(int i = 1; i<=m; ++i)
        cin >> edge[i].fi >> edge[i].se;
    dsu.init(n);
    L=0, R=m+1;
    dnc(1,m,1,m);
    while(q--){
        int l,r;
        cin >> l >> r;
        if(r<=last[l]) cout << "YES\n";
        else cout << "NO\n";
    }
}

Compilation message (stderr)

Joker.cpp: In function 'int main()':
Joker.cpp:4:19: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    4 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
      |            ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:91:45: note: in expansion of macro 'fo'
   91 |     if(fopen((NAME + ".INP").c_str(), "r")) fo;
      |                                             ^~
Joker.cpp:4:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    4 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
      |                                                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:91:45: note: in expansion of macro 'fo'
   91 |     if(fopen((NAME + ".INP").c_str(), "r")) fo;
      |                                             ^~
#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...