제출 #744481

#제출 시각아이디문제언어결과실행 시간메모리
744481maomao90Passport (JOI23_passport)C++17
100 / 100
1206 ms209892 KiB

// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h> 
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define RREP(i, s, e) for (int i = (s); i >= (e); i--)
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;

#ifndef DEBUG
#define cerr if (0) cerr
#endif

const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 200005;
const int MAXV = MAXN * 6;

#define OFFSET n * 4

int n;
vii adj[MAXV], radj[MAXV];
int q;

void addEdge(int u, int v, int p) {
    adj[u].pb({v, p});
    radj[v].pb({u, p});
}
#define MLR int mid = lo + hi >> 1, lc = u << 1, rc = u << 1 ^ 1
void init(int u = 1, int lo = 0, int hi = n - 1) {
    assert(u < OFFSET);
    if (lo == hi) {
        addEdge(u, OFFSET + n + lo, 0);
        return;
    }
    MLR;
    addEdge(u, lc, 0);
    addEdge(u, rc, 0);
    init(lc, lo, mid);
    init(rc, mid + 1, hi);
}
void upd(int s, int e, int c, int u = 1, int lo = 0, int hi = n - 1) {
    if (lo >= s && hi <= e) {
        addEdge(OFFSET + c, u, 0);
        return;
    }
    MLR;
    if (s <= mid) {
        upd(s, e, c, lc, lo, mid);
    }
    if (e > mid) {
        upd(s, e, c, rc, mid + 1, hi);
    }
}

ll d1[MAXV], dn[MAXV], ans[MAXV];
void dijkstra(int src, ll *d, vii *adj) {
    priority_queue<pll, vector<pll>, greater<pll>> pq;
    if (src != -1) {
        REP (i, 0, OFFSET + n + n) {
            d[i] = LINF;
        }
        d[src] = 0;
        pq.push({0, src});
    } else {
        REP (i, OFFSET, OFFSET + n + n) {
            pq.push({d[i], i});
        }
    }
    queue<pll> qu;
    while (!qu.empty() || !pq.empty()) {
        ll ud, u;
        if (!qu.empty()) {
            tie(ud, u) = qu.front(); qu.pop();
        } else {
            tie(ud, u) = pq.top(); pq.pop();
        }
        if (ud != d[u]) {
            continue;
        }
        for (auto [v, w] : adj[u]) {
            if (mnto(d[v], d[u] + w)) {
                if (w == 0) {
                    qu.push({d[v], v});
                } else {
                    pq.push({d[v], v});
                }
            }
        }
    }
}

int main() {
#ifndef DEBUG
    ios::sync_with_stdio(0), cin.tie(0);
#endif
    cin >> n;
    init();
    REP (i, 0, n) {
        int a, b; cin >> a >> b;
        a--; b--;
        addEdge(OFFSET + n + i, OFFSET + i, 1);
        upd(a, b, i);
    }
    dijkstra(OFFSET + n, d1, radj);
    dijkstra(OFFSET + n + n - 1, dn, radj);
    /*
    REP (i, 0, OFFSET + 1) {
        ans[i] = LINF;
    }
    */
    REP (i, 0, OFFSET + n + n) {
        ans[i] = d1[i] + dn[i];
    }
    dijkstra(-1, ans, radj);
    cin >> q;
    while (q--) {
        int x; cin >> x;
        x--;
        if (ans[OFFSET + n + x] >= LINF) {
            ans[OFFSET + n + x] = -1;
        }
        cout << ans[OFFSET + n + x] << '\n';
    }
    /*
    REP (i, 0, n) {
        if (ans[OFFSET + n + i] >= LINF) {
            ans[OFFSET + n + i] = -1;
        }
        cout << ans[OFFSET + n + i] << '\n';
    }
    */
    return 0;
}

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

passport.cpp: In function 'void init(int, int, int)':
passport.cpp:49:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   49 | #define MLR int mid = lo + hi >> 1, lc = u << 1, rc = u << 1 ^ 1
      |                       ~~~^~~~
passport.cpp:56:5: note: in expansion of macro 'MLR'
   56 |     MLR;
      |     ^~~
passport.cpp: In function 'void upd(int, int, int, int, int, int)':
passport.cpp:49:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   49 | #define MLR int mid = lo + hi >> 1, lc = u << 1, rc = u << 1 ^ 1
      |                       ~~~^~~~
passport.cpp:67:5: note: in expansion of macro 'MLR'
   67 |     MLR;
      |     ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...