Submission #985552

#TimeUsernameProblemLanguageResultExecution timeMemory
985552maomao90Escape Route 2 (JOI24_escape2)C++17
100 / 100
1136 ms856572 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 unsigned long long ull;
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;
typedef tuple<ll, int, int> lii;
#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 = 100005;
const int MAXQ = 300005;
const int BLK = 183;

int n, t;
int m[MAXN];
vii ba[MAXN];
vector<vi> adj[MAXN];
vector<vector<lii>> big[MAXN];
int q;
vii mpl[MAXN];
ll ans[MAXQ];
ll dp[2][MAXN];

vi p[MAXN];
vll d[MAXN];
vi stk;
void dfs(int i, int j) {
    cerr << i << ' ' << j << ": " << d[i][j] << '\n';
    stk.pb(j);
    if (m[i] <= BLK) {
        for (auto [r, id] : mpl[i]) {
            int anc = stk[n - r];
            mnto(ans[id], d[i][j] - d[r - 1][anc] + ba[i][j].FI - ba[i][j].SE);
        }
    }
    for (auto [w, r, id] : big[i][j]) {
        int anc = stk[n - r];
        mnto(ans[id], d[i][j] - d[r - 1][anc] + w);
    }
    for (int k : adj[i][j]) {
        int w = ba[i][j].FI - ba[i - 1][k].FI;
        if (ba[i - 1][k].FI > ba[i][j].SE) {
            w += t;
        }
        d[i - 1][k] = d[i][j] + w;
        dfs(i - 1, k);
    }
    stk.pop_back();
}

int main() {
#ifndef DEBUG
    ios::sync_with_stdio(0), cin.tie(0);
#endif
    cin >> n >> t;
    REP (i, 1, n) {
        cin >> m[i];
        ba[i].resize(m[i]);
        adj[i].resize(m[i]);
        p[i].resize(m[i]);
        d[i].resize(m[i]);
        big[i].resize(m[i]);
        REP (j, 0, m[i]) {
            cin >> ba[i][j].SE >> ba[i][j].FI;
        }
        sort(ALL(ba[i]));
        int ptr = 0;
        REP (j, 0, m[i]) {
            while (ptr < SZ(ba[i - 1]) && ba[i - 1][ptr].FI <= ba[i][j].SE) {
                p[i - 1][ptr] = j;
                adj[i][j].pb(ptr++);
            }
        }
        while (ptr < SZ(ba[i - 1])) {
            p[i - 1][ptr] = 0;
            adj[i][0].pb(ptr++);
        }
    }
    cin >> q;
    REP (i, 0, q) {
        int l, r; cin >> l >> r;
        mpl[l].pb({r, i});
        ans[i] = LINF;
    }
    REP (i, 1, n) {
        sort(ALL(mpl[i]));
    }
    REP (i, 1, n) {
        if (m[i] <= BLK) {
            continue;
        }
        REP (j, 0, m[i]) {
            dp[i & 1][j] = ba[i][j].FI - ba[i][j].SE;
        }
        int u = i;
        int ptr = 0;
        while (m[u] > BLK) {
            ll res = LINF;
            REP (j, 0, m[u]) {
                mnto(res, dp[u & 1][j]);
            }
            while (ptr < SZ(mpl[i]) && mpl[i][ptr].FI - 1 == u) {
                ans[mpl[i][ptr++].SE] = res;
            }
            if (ptr == SZ(mpl[i])) {
                break;
            }
            REP (j, 0, m[u + 1]) {
                dp[u & 1 ^ 1][j] = LINF;
            }
            REP (j, 0, m[u]) {
                int pj = p[u][j];
                int w = ba[u + 1][pj].FI - ba[u][j].FI;
                if (ba[u][j].FI > ba[u + 1][pj].SE) {
                    w += t;
                }
                mnto(dp[u & 1 ^ 1][pj], dp[u & 1][j] + w);
            }
            u++;
        }
        while (ptr < SZ(mpl[i])) {
            auto [r, id] = mpl[i][ptr++];
            REP (j, 0, m[u]) {
                big[u][j].pb({dp[u & 1][j], r, id});
            }
        }
    }
    REP (j, 0, m[n - 1]) {
        dfs(n - 1, j);
    }
    REP (i, 0, q) {
        cout << ans[i] << '\n';
    }
    return 0;
}

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:138:22: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
  138 |                 dp[u & 1 ^ 1][j] = LINF;
      |                    ~~^~~
Main.cpp:146:27: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
  146 |                 mnto(dp[u & 1 ^ 1][pj], dp[u & 1][j] + w);
      |                         ~~^~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...