Submission #1210990

#TimeUsernameProblemLanguageResultExecution timeMemory
1210990madamadam3Port Facility (JOI17_port_facility)C++20
10 / 100
10 ms1036 KiB
#include <bits/stdc++.h>

using namespace std;

struct Event {
    int time, box_id;
    bool is_remove;

    const bool operator<(const Event &other) {
        return time < other.time;
    }
};

const int MAXN = 1'000'001;
const int MOD = 1'000'000'007;
int n;

int A[MAXN], B[MAXN];
vector<Event> events;

int dfs(int cur_time, vector<int> &left_stack, vector<int> &right_stack) {
    int our_box = events[cur_time].box_id;
    bool remove = events[cur_time].is_remove;

    if (remove && !(left_stack.back() == our_box || right_stack.back() == our_box)) {
        return 0;
    }

    int ways = 0;
    if (cur_time >= 2*n-1) {
        ways = remove ? 1: 0;
        return ways;
    }

    if (remove ) {
        bool is_left = false;
        if (left_stack.back() == our_box) {
            left_stack.pop_back();
            is_left = true;
        } else {
            right_stack.pop_back();
        }

        ways += dfs(cur_time + 1, left_stack, right_stack);
        (is_left ? left_stack : right_stack).push_back(our_box);
    } else {
        left_stack.push_back(our_box);
        ways += dfs(cur_time + 1, left_stack, right_stack);
        left_stack.pop_back();
        
        right_stack.push_back(our_box);
        ways += dfs(cur_time + 1, left_stack, right_stack);
        right_stack.pop_back();
    }

    return ways;
}

void brute() {
    vector<int> ls(1, -1), rs(1, -1);
    cout << dfs(0, ls, rs);
}

void solve() {
    int ways = 0;
    vector<vector<int>> adj(n, vector<int>());
    for (int i = 0; i < n; i++) {
        for (int j = i+1; j < n; j++) {
            int ci = i, cj = j;
            if (A[ci] > A[cj]) swap(ci, cj);
            if (A[ci] < A[cj] && A[cj] < B[ci] && B[ci] < B[cj]) {
                adj[ci].push_back(cj);
                adj[cj].push_back(ci);
            }
        }
    }

    int cmps = 0; bool bipartite = true;
    vector<int> colour(n, -1);
    queue<int> q;

    for (int i = 0; i < n; i++) {
        if (colour[i] != -1 || !bipartite) continue;
        cmps++;

        q.push(i);
        colour[i] = 1;

        while (!q.empty()) {
            int cur = q.front();
            q.pop();

            for (auto &el : adj[cur]) {
                if (colour[el] == colour[cur]) {
                    bipartite = false;
                    break;
                } else if (colour[el] == -1) {
                    colour[el] = colour[cur] == 1 ? 2 : 1;
                    q.push(el);
                }
            }

            if (!bipartite) break;
        }
    }

    ways = bipartite ? pow(2, cmps) : 0;
    cout << ways;
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> A[i] >> B[i];
    }

    for (int i = 0; i < n; i++) {
        events.push_back(Event {A[i], i, false});
        events.push_back(Event {B[i], i, true});
    }

    sort(events.begin(), events.end());

    solve();
    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...