Submission #57667

#TimeUsernameProblemLanguageResultExecution timeMemory
57667BenqPort Facility (JOI17_port_facility)C++14
100 / 100
3491 ms752648 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; typedef complex<ld> cd; typedef pair<int, int> pi; typedef pair<ll,ll> pl; typedef pair<ld,ld> pd; typedef vector<int> vi; typedef vector<ld> vd; typedef vector<ll> vl; typedef vector<pi> vpi; typedef vector<pl> vpl; typedef vector<cd> vcd; template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; #define FOR(i, a, b) for (int i=a; i<(b); i++) #define F0R(i, a) for (int i=0; i<(a); i++) #define FORd(i,a,b) for (int i = (b)-1; i >= a; i--) #define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--) #define sz(x) (int)(x).size() #define mp make_pair #define pb push_back #define f first #define s second #define lb lower_bound #define ub upper_bound #define all(x) x.begin(), x.end() const int MOD = 1000000007; const ll INF = 1e18; const int MX = 1000001; int N; pi par[MX]; vi rap[MX][2]; map<int,int> M[MX]; vpi p; map<int,int> m; void input() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> N; p.resize(N); m.clear(); F0R(i,N) { F0R(j,2) rap[i][j].clear(); M[i].clear(); } F0R(i,N) { cin >> p[i].f >> p[i].s; } sort(all(p)); F0R(i,N) { par[i] = {i,0}; rap[i][0].pb(i); M[i][p[i].s] = i; } } void unite(int a, int b) { // cout << "AH " << a << " " << b << "\n"; if (par[a].f == par[b].f) { if (par[a].s == par[b].s) { cout << 0; exit(0); } return; } bool eq = (par[a].s == par[b].s); a = par[a].f, b = par[b].f; if (sz(rap[a][0])+sz(rap[a][1]) < sz(rap[b][0])+sz(rap[b][1])) swap(a,b); F0R(j,2) for (int i: rap[b][j]) { par[i] = {a,j^eq}; rap[a][j^eq].pb(i); M[a][p[i].s] = i; } F0R(j,2) rap[b][j].clear(); M[b].clear(); } void del(int mn) { int x = par[m.begin()->s].f; m.erase(m.begin()); auto IT = M[x].lb(mn); if (IT == M[x].end()) return; // cout << "OOPS " << x << " " << IT->f << " " << IT->s << "\n"; m[IT->f] = IT->s; } void check(vi v) { sort(all(v)); /*for (int i: v) cout << i << " * "; cout << "BAD\n"; for (int i: v) cout << p[i].f << " " << p[i].s << " | "; cout << "\n";*/ set<int> z; for (int i: v) { while (sz(z) && *z.begin() < p[i].f) z.erase(z.begin()); if (sz(z) && *z.begin() < p[i].s) { // cout << "\nAH " << i << " " << p[i].f << " " << p[i].s << " " << *z.begin() << " " << sz(z) << "\n"; cout << 0; exit(0); } z.insert(p[i].s); } } void solve() { input(); F0R(i,sz(p)) { while (sz(m) && m.begin()->f < p[i].f) del(p[i].f); vi to; for (auto a: m) { if (a.f < p[i].s) to.pb(a.s); else break; } // for (int j: to) cout << p[j].f << " " << p[j].s << " " << j << " " << i << "\n"; for (int j: to) { m.erase(p[j].s); unite(j,i); } to.pb(i); m[p[to.front()].s] = to.front(); } int ans = 1; F0R(i,N) if (par[i].f == i) { ans = 2*ans%MOD; check(rap[i][0]); check(rap[i][1]); } cout << ans << "\n"; } int main() { F0R(i,1) solve(); } /* Look for: * the exact constraints (multiple sets are too slow for n=10^6 :( ) * special cases (n=1?) * overflow (ll vs int?) * array bounds */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...