제출 #839305

#제출 시각아이디문제언어결과실행 시간메모리
839305fanwenPort Facility (JOI17_port_facility)C++17
100 / 100
1494 ms195116 KiB
/**
 *      author : pham van sam 
 *      created : 29.08.2023 21:47:12
 **/

#include <bits/stdc++.h>

using namespace std;

#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define FORE(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define debug(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }

#ifdef LOCAL 
    #include "debug.h"
#else 
    #define clog if(false) cerr
#endif

template <class A, class B> bool minimize(A &a, B b)  { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b)  { if (a < b) { a = b; return true; } return false; }

const int Mod = 1e9 + 7;

void add(int &x, int y) {
    if((x += y) >= Mod) x -= Mod;
    if(x < 0) x += Mod;
}

template < class T, T (*f) (T, T), T (*e) ()>
struct segment_tree {
    vector <T> tree;
    int n;

    void update(int u, int p) {
        u--;
        for(tree[u += n] += p; u >>= 1; ) {
            tree[u] = f(tree[u << 1], tree[u << 1 | 1]);
        }
    }

    void set(int u, int p) {
        u--;
        for (tree[u += n] = p; u >>= 1; ) {
            tree[u] = f(tree[u << 1], tree[u << 1 | 1]);
        }
    }

    T get(int p) {
        return tree[p += n - 1];
    }

    T get(int l, int r) {
        l--;
        T resl = e(), resr = e();
        for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if(l & 1) resl = f(resl, tree[l++]);
            if(r & 1) resr = f(tree[--r], resr);
        }
        return f(resl, resr);
    }

    segment_tree(int n = 0) : n(n), tree(n << 2 | 1) {
        fill(tree.begin(), tree.end(), e());
    }
};

namespace _max_ {
    int op(int a, int b) { return a > b ? a : b; }
    int e() { return numeric_limits<int>::min(); }
};

namespace _min_ {
    int op(int a, int b) { return a < b ? a : b; }
    int e() { return numeric_limits<int>::max(); }
};

const int MAXN = 1e6 + 5;

template <class T> using It_Max = segment_tree <T, _max_::op, _max_::e>;
template <class T> using It_Min = segment_tree <T, _min_::op, _min_::e>;

int N, dd[MAXN], pos[2 * MAXN];

struct Segment {
    int l, r;
    Segment(int _l = 0, int _r = 0) : l(_l), r(_r) {}
    bool operator < (const Segment &other) const { return l < other.l || (l == other.l and r < other.r); }
} A[MAXN];

void you_make_it(void) {
    cin >> N;
    FOR(i, 1, N) cin >> A[i].l >> A[i].r;
    sort(A + 1, A + N + 1);
    It_Max <int> itMax(2 * N);
    It_Min <int> itMin(2 * N);
    FOR(i, 1, N) {
        itMin.set(A[i].r, A[i].l);
        itMax.set(A[i].l, A[i].r);
        pos[A[i].l] = pos[A[i].r] = i;
    }
    memset(dd, -1, sizeof dd);

    function <void(int, int)> dfs = [&](int u, int color) {
        if(dd[u] != -1 and dd[u] != color) {
            cout << 0;
            exit(0);
        }
        clog << u << " ";
        dd[u] = color;
        itMax.set(A[u].l, _max_::e());
        itMin.set(A[u].r, _min_::e());

        while(itMax.get(A[u].l, A[u].r) > A[u].r) {
            int i = pos[itMax.get(A[u].l, A[u].r)];
            dfs(i, color ^ 1);
        }

        while(itMin.get(A[u].l, A[u].r) < A[u].l) {
            int i = pos[itMin.get(A[u].l, A[u].r)];
            dfs(i, color ^ 1);
        }
    };
    int ans = 1;
    FOR(i, 1, N) if(dd[i] == -1) {
        dfs(i, 0);
        add(ans, ans);
        clog << endl;
    }
    stack <int> check[2];
    FOR(i, 1, 2 * N) {
        int u = pos[i];
        if(i == A[u].l) check[dd[u]].push(u);
        else {
            if(check[dd[u]].top() != u) {
                cout << 0;
                return;
            }
            check[dd[u]].pop();
        }
    }
    cout << ans;
}

signed main() {

#ifdef LOCAL
    freopen("TASK.inp", "r", stdin);
    freopen("TASK.out", "w", stdout);
#endif
  	file("port_facility");
    auto start_time = chrono::steady_clock::now();

    cin.tie(0), cout.tie(0) -> sync_with_stdio(0);

    you_make_it();

    auto end_time = chrono::steady_clock::now();

    cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;

    return (0 ^ 0);
}

// Dream it. Wish it. Do it.

컴파일 시 표준 에러 (stderr) 메시지

port_facility.cpp: In instantiation of 'segment_tree<T, f, e>::segment_tree(int) [with T = int; T (* f)(T, T) = _max_::op; T (* e)() = _max_::e]':
port_facility.cpp:101:29:   required from here
port_facility.cpp:39:9: warning: 'segment_tree<int, _max_::op, _max_::e>::n' will be initialized after [-Wreorder]
   39 |     int n;
      |         ^
port_facility.cpp:38:16: warning:   'std::vector<int> segment_tree<int, _max_::op, _max_::e>::tree' [-Wreorder]
   38 |     vector <T> tree;
      |                ^~~~
port_facility.cpp:69:5: warning:   when initialized here [-Wreorder]
   69 |     segment_tree(int n = 0) : n(n), tree(n << 2 | 1) {
      |     ^~~~~~~~~~~~
port_facility.cpp: In instantiation of 'segment_tree<T, f, e>::segment_tree(int) [with T = int; T (* f)(T, T) = _min_::op; T (* e)() = _min_::e]':
port_facility.cpp:102:29:   required from here
port_facility.cpp:39:9: warning: 'segment_tree<int, _min_::op, _min_::e>::n' will be initialized after [-Wreorder]
   39 |     int n;
      |         ^
port_facility.cpp:38:16: warning:   'std::vector<int> segment_tree<int, _min_::op, _min_::e>::tree' [-Wreorder]
   38 |     vector <T> tree;
      |                ^~~~
port_facility.cpp:69:5: warning:   when initialized here [-Wreorder]
   69 |     segment_tree(int n = 0) : n(n), tree(n << 2 | 1) {
      |     ^~~~~~~~~~~~
port_facility.cpp: In function 'int main()':
port_facility.cpp:18:57: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
port_facility.cpp:157:4: note: in expansion of macro 'file'
  157 |    file("port_facility");
      |    ^~~~
port_facility.cpp:18:90: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
port_facility.cpp:157:4: note: in expansion of macro 'file'
  157 |    file("port_facility");
      |    ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...