Submission #914999

#TimeUsernameProblemLanguageResultExecution timeMemory
914999KiaRezPort Facility (JOI17_port_facility)C++17
100 / 100
2878 ms161684 KiB
/* IN THE NAME OF GOD */ #include <bits/stdc++.h> // #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") // #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef long double ld; #define F first #define S second #define Mp make_pair #define pb push_back #define pf push_front #define size(x) ((ll)x.size()) #define all(x) (x).begin(),(x).end() #define kill(x) cout << x << '\n', exit(0); #define fuck(x) cout << "(" << #x << " , " << x << ")" << endl #define endl '\n' const int N = 1e6+23, lg = 21; ll Mod = 1e9+7; //998244353; inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;} inline ll poww(ll a, ll b, ll mod=Mod) { ll ans = 1; a=MOD(a, mod); while (b) { if (b & 1) ans = MOD(ans*a, mod); b >>= 1; a = MOD(a*a, mod); } return ans; } int n, par[N], sz[N], val[N], vis[N], a[N], b[N]; int who[2*N], seg[2][5*N], cnt; int getPar(int v) {return (par[v]==0 ? v : getPar(par[v]));} int getVal(int v) {return (par[v]==0 ? val[v] : val[v]^getVal(par[v]));} void merge(int v, int u) { if(getPar(v)==getPar(u)) { if(getVal(v) == getVal(u)) kill(0); return; } int x = (getVal(v) == getVal(u)); v=getPar(v), u=getPar(u); if(sz[v] < sz[u]) swap(v, u); sz[v] += sz[u]; par[u]=v; if(x==1) val[u] ^= 1; cnt--; } void build(int ind=1) { if(ind >= (1<<lg)) { int id = ind-(1<<lg)+1; if(id <= 2*n) { if(a[who[id]]==id) seg[0][ind] = who[id]; else seg[1][ind] = who[id]; } return; } build(2*ind); build(2*ind+1); if(seg[0][2*ind]==0 || b[seg[0][2*ind]] < b[seg[0][2*ind+1]]) seg[0][ind] = seg[0][2*ind+1]; else seg[0][ind] = seg[0][2*ind]; if(seg[1][2*ind]==0 || (seg[1][2*ind+1]>0 && a[seg[1][2*ind]]>a[seg[1][2*ind+1]])) seg[1][ind] = seg[1][2*ind+1]; else seg[1][ind] = seg[1][2*ind]; } void upd0(int ind, int val) { if(ind == 0) return; if(ind >= (1<<lg)) { seg[0][ind] = val; } else { if(seg[0][2*ind]==0 || b[seg[0][2*ind]] < b[seg[0][2*ind+1]]) seg[0][ind] = seg[0][2*ind+1]; else seg[0][ind] = seg[0][2*ind]; } upd0(ind/2, val); } void upd1(int ind, int val) { if(ind == 0) return; if(ind >= (1<<lg)) { seg[1][ind] = val; } else { if(seg[1][2*ind]==0 || (seg[1][2*ind+1]>0 && a[seg[1][2*ind]]>a[seg[1][2*ind+1]])) seg[1][ind] = seg[1][2*ind+1]; else seg[1][ind] = seg[1][2*ind]; } upd1(ind/2, val); } int qry0(int l, int r, int ind=1, int lc=1, int rc=(1<<lg)+1) { if(l>=rc || lc>=r) return 0; if(lc>=l && rc<=r) return seg[0][ind]; int mid = (lc+rc)/2; int x=qry0(l, r, 2*ind, lc, mid), y=qry0(l, r, 2*ind+1, mid, rc); if(b[y] > b[x]) return y; return x; } int qry1(int l, int r, int ind=1, int lc=1, int rc=(1<<lg)+1) { if(l>=rc || lc>=r) return 0; if(lc>=l && rc<=r) return seg[1][ind]; int mid = (lc+rc)/2; int x=qry1(l, r, 2*ind, lc, mid), y=qry1(l, r, 2*ind+1, mid, rc); if(x==0 || (a[y] < a[x] && y>0)) return y; return x; } void dfs(int v) { vis[v] = 1; upd0(a[v]+(1<<lg)-1, 0); upd1(b[v]+(1<<lg)-1, 0); while(true) { int u = qry0(a[v], b[v]); if(u==0 || b[u]<b[v]) break; merge(v, u); dfs(u); } while(true) { int u = qry1(a[v], b[v]); if(u==0 || a[u]>a[v]) break; merge(v, u); dfs(u); } } int main () { ios_base::sync_with_stdio(false), cin.tie(0); cin>>n; cnt=n; fill(sz+1, sz+n+1, 1); for(int i=1; i<=n; i++) { cin>>a[i]>>b[i]; who[a[i]] = who[b[i]] = i; } build(1); for(int i=1; i<=n; i++) { if(vis[i] == 1) continue; dfs(i); } vector<pii> vec[2]; for(int i=1; i<=n; i++) { vec[getVal(i)].pb({a[i], b[i]}); } for(int i=0; i<2; i++) { sort(all(vec[i])); set<int> st; for(auto it : vec[i]) { auto it2 = st.lower_bound(it.F); if(it2!=st.end() && (*it2) < it.S) kill(0); st.insert(it.S); } } cout<<poww(2, cnt); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...