Submission #529896

# Submission time Handle Problem Language Result Execution time Memory
529896 2022-02-24T00:29:20 Z i_am_noob Colors (RMI18_colors) C++17
100 / 100
609 ms 71540 KB
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,unroll-loops")

#define ll long long
//#define int ll
#define ull unsigned ll
#define ld long double
#define rep(a) rep1(i,a)
#define rep1(i,a) rep2(i,0,a)
#define rep2(i,b,a) for(int i=(b); i<((int)(a)); i++)
#define rep3(i,b,a) for(int i=(b); i>=((int)(a)); i--)
#define chkmin(a,b) (a=min(a,b))
#define chkmax(a,b) (a=max(a,b))
#define all(a) a.begin(),a.end()
#define pii pair<int,int>
#define pb push_back
#define inf 1010000000
//#define inf 4000000000000000000
#define eps 1e-9
#define sz(a) ((int)a.size())
#define pow2(x) (1ll<<(x))
#define ceiling(a,b) (((a)+(b)-1)/(b))
#define print0(a) cout << (a) << ' '
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#ifdef i_am_noob
#define bug(...) cerr << "#" << __LINE__ << ' ' << #__VA_ARGS__ << "- ", _do(__VA_ARGS__)
template<typename T> void _do(vector<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(set<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(unordered_set<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(T && x) {cerr << x << endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr << x << ", "; _do(y...);}
#else
#define bug(...) 777771449
#endif
template<typename T> void print(T && x) {cout << x << "\n";}
template<typename T, typename... S> void print(T && x, S&&... y) {cout << x << ' ';print(y...);}

const int Mod=1000000007,Mod2=998244353;
const int MOD=Mod2;
template <int mod>
struct Modint{
    int val;
    Modint(int _val=0){val=_val%mod;if(val<0) val+=mod;}
    Modint operator +(const Modint& o) const {
        Modint res;
        res.val=val+o.val;
        if(res.val>=mod) res.val-=mod;
        return res;
    }
    Modint operator +(const int& o) const {return Modint(val+o);}
    Modint operator -() const {
        Modint res;
        res.val=-val;
        if(res.val<0) res.val+=mod;
        return res;
    }
    Modint operator -(const Modint& o) const {
        Modint res;
        res.val=val-o.val;
        if(res.val<0) res.val+=mod;
        return res;
    }
    Modint operator -(const int& o) const {return Modint(val-o);}
    Modint operator *(const Modint& o) const {return Modint(val*o.val);}
    Modint operator *(const int& o) const {return Modint(val*(o%mod));}
    Modint operator +=(const Modint& o){*this=(*this)+o;return *this;}
    Modint operator -=(const Modint& o){*this=(*this)-o;return *this;}
    Modint operator *=(const Modint& o){*this=(*this)*o;return *this;}
    Modint Pow(int b) const {
        Modint tmp(val),ret(1);
        while(b){
            if(b&1) ret*=tmp;
            b>>=1;tmp*=tmp;
        }
        return ret;
    }
    Modint Pow(const Modint& a, int b) const {return a.Pow(b);}
    inline Modint inv() const {return (*this).Pow(mod-2);}
    Modint operator /(const Modint& o) const {return *this*o.inv();}
    Modint operator /(const int& o) const {return *this*Modint(o).inv();}
    bool operator ==(const Modint& o) const {return val==o.val;}
};
template<int mod>
ostream& operator << (ostream& o, Modint<mod> x){return o << x.val;}
template<int mod>
Modint<mod> operator +(const int& x, const Modint<mod>& y){return Modint<mod>(x+y.val);}
template<int mod>
Modint<mod> operator -(const int& x, const Modint<mod>& y){return Modint<mod>(x-y.val);}
template<int mod>
Modint<mod> operator *(const int& x, const Modint<mod>& y){return Modint<mod>(x%mod*y.val);}
#define modint Modint<MOD>
vector<modint> inv,fac,invfac;
void init_comb(int N){
    inv.resize(N),fac.resize(N),invfac.resize(N);
    inv[1]=1,fac[0]=1,invfac[0]=1;
    rep2(i,2,N) inv[i]=inv[MOD%i]*(MOD-MOD/i);
    rep2(i,1,N) fac[i]=fac[i-1]*i;
    rep2(i,1,N) invfac[i]=invfac[i-1]*inv[i];
}
inline modint C(int n, int m){return m>n||m<0?modint(0):fac[n]*invfac[m]*invfac[n-m];}
inline modint H(int n, int m){return C(n+m-1,n);}

const int maxn=150005,maxm=5,maxk=7777714;

//i_am_noob
#define wiwihorz  
int par[maxn],siz[maxn];
vector<pii> ops;
int Find(int x){return x==par[x]?x:Find(par[x]);}
void Union(int x, int y){
    x=Find(x),y=Find(y);
    if(x==y){
        ops.pb({-1,-1});
        return;
    }
    if(siz[x]<siz[y]) swap(x,y);
    par[y]=x;
    siz[x]+=siz[y];
    ops.pb({x,y});
}
void undo(){
    if(ops.empty()) return;
    if(ops.back().first==-1){
        ops.pop_back();
        return;
    }
    par[ops.back().second]=ops.back().second;
    siz[ops.back().first]-=siz[ops.back().second];
    ops.pop_back();
}

vector<vector<pii>> node(maxn*4);

void build(int k, int l, int r){
    node[k].clear();
    if(l==r) return;
    int mid=l+r>>1;
    build(k<<1,l,mid),build(k<<1|1,mid+1,r);
}

void Insert(int k, int l, int r, int ql, int qr, int u, int v){
    if(l>qr||r<ql) return;
    if(ql<=l&&qr>=r){
        node[k].pb({u,v});
        return;
    }
    int mid=l+r>>1;
    Insert(k<<1,l,mid,ql,qr,u,v),Insert(k<<1|1,mid+1,r,ql,qr,u,v);
}

int n,m,l[maxn],r[maxn];
vector<vector<int>> in(maxn),out(maxn);
bool flag;

void solve(int k, int l, int r){
    for(auto [x,y]: node[k]) Union(x,y);
    if(l==r){
        vector<int> vec;
        for(auto i: out[l]) vec.pb(Find(i));
        sort(all(vec));
        vec.resize(unique(all(vec))-vec.begin());
        for(auto i: in[l]) if(!binary_search(all(vec),Find(i))) flag=0;
        rep(sz(node[k])) undo();
        return;
    }
    int mid=l+r>>1;
    solve(k<<1,l,mid),solve(k<<1|1,mid+1,r);
    rep(sz(node[k])) undo();
}

void orzck(){
    cin >> n >> m;
    rep(n) cin >> l[i];
    rep(n) cin >> r[i];
    rep(n) l[i]=n-l[i],r[i]=n-r[i];
    rep(n) in[i].clear(),out[i].clear();
    rep(n) par[i]=i,siz[i]=1;
    rep(n) in[r[i]].pb(i),out[l[i]].pb(i);
    build(1,0,n-1);
    rep(m){
        int u,v;
        cin >> u >> v;
        u--,v--;
        int L=max(l[u],l[v]),R=min(r[u],r[v]);
        if(L<=R) Insert(1,0,n-1,L,R,u,v);
    }
    rep(n) if(l[i]>r[i]){
        print(0);
        return;
    }
    flag=1;
    solve(1,0,n-1);
    print(flag);
}

signed main(){
    ios_base::sync_with_stdio(0),cin.tie(0);
    // #ifdef i_am_noob
    // freopen("input1.txt","r",stdin);
    // freopen("output1.txt","w",stdout);
    // freopen("output2.txt","w",stderr);
    // #endif
    cout << fixed << setprecision(15);
    int t;
    #ifdef wiwihorz
    cin >> t;
    #else
    t=1;
    #endif
    ld start=clock();
    while(t--) orzck();
    bug((clock()-start)/CLOCKS_PER_SEC);
    return 0;
}

Compilation message

colors.cpp: In function 'void build(int, int, int)':
colors.cpp:138:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  138 |     int mid=l+r>>1;
      |             ~^~
colors.cpp: In function 'void Insert(int, int, int, int, int, int, int)':
colors.cpp:148:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  148 |     int mid=l+r>>1;
      |             ~^~
colors.cpp: In function 'void solve(int, int, int)':
colors.cpp:167:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  167 |     int mid=l+r>>1;
      |             ~^~
colors.cpp: In function 'int main()':
colors.cpp:34:18: warning: statement has no effect [-Wunused-value]
   34 | #define bug(...) 777771449
      |                  ^~~~~~~~~
colors.cpp:213:5: note: in expansion of macro 'bug'
  213 |     bug((clock()-start)/CLOCKS_PER_SEC);
      |     ^~~
colors.cpp:211:8: warning: unused variable 'start' [-Wunused-variable]
  211 |     ld start=clock();
      |        ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 69 ms 21512 KB Output is correct
2 Correct 33 ms 21580 KB Output is correct
3 Correct 13 ms 21764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 21612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 21492 KB Output is correct
2 Correct 33 ms 21452 KB Output is correct
3 Correct 14 ms 21640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 21492 KB Output is correct
2 Correct 33 ms 21452 KB Output is correct
3 Correct 14 ms 21640 KB Output is correct
4 Correct 169 ms 21636 KB Output is correct
5 Correct 345 ms 37016 KB Output is correct
6 Correct 609 ms 56388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 21512 KB Output is correct
2 Correct 33 ms 21580 KB Output is correct
3 Correct 13 ms 21764 KB Output is correct
4 Correct 73 ms 21492 KB Output is correct
5 Correct 33 ms 21452 KB Output is correct
6 Correct 14 ms 21640 KB Output is correct
7 Correct 72 ms 21452 KB Output is correct
8 Correct 43 ms 21540 KB Output is correct
9 Correct 15 ms 21692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 126 ms 21524 KB Output is correct
2 Correct 359 ms 38176 KB Output is correct
3 Correct 337 ms 47280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 21580 KB Output is correct
2 Correct 20 ms 21964 KB Output is correct
3 Correct 17 ms 21708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 21512 KB Output is correct
2 Correct 33 ms 21580 KB Output is correct
3 Correct 13 ms 21764 KB Output is correct
4 Correct 62 ms 21612 KB Output is correct
5 Correct 73 ms 21492 KB Output is correct
6 Correct 33 ms 21452 KB Output is correct
7 Correct 14 ms 21640 KB Output is correct
8 Correct 169 ms 21636 KB Output is correct
9 Correct 345 ms 37016 KB Output is correct
10 Correct 609 ms 56388 KB Output is correct
11 Correct 72 ms 21452 KB Output is correct
12 Correct 43 ms 21540 KB Output is correct
13 Correct 15 ms 21692 KB Output is correct
14 Correct 126 ms 21524 KB Output is correct
15 Correct 359 ms 38176 KB Output is correct
16 Correct 337 ms 47280 KB Output is correct
17 Correct 41 ms 21580 KB Output is correct
18 Correct 20 ms 21964 KB Output is correct
19 Correct 17 ms 21708 KB Output is correct
20 Correct 90 ms 21764 KB Output is correct
21 Correct 336 ms 41184 KB Output is correct
22 Correct 573 ms 71540 KB Output is correct