Submission #1129782

#TimeUsernameProblemLanguageResultExecution timeMemory
1129782not_amirEscape Route 2 (JOI24_escape2)C++20
6 / 100
3095 ms16044 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, ll> pii; int main() { cin.tie(nullptr); int n; ll t; cin >> n >> t; vector<vector<pii>> ranges(n - 1); for (int i = 0; i < n - 1; i++) { int m; cin >> m; vector<pii> tmp(m); for (auto& [A, B] : tmp) cin >> A >> B; sort(tmp.begin(), tmp.end()); int mx = -1; for (auto& [A, B] : tmp) { if (A <= mx) continue; mx = A; ranges[i].push_back({A, B}); } } vector<int> dpthf, dpthr; vector<pii> jmpf, jmpr, nxtf, nxtr; vector<int> idxf, idxr; vector<pii> prev; vector<vector<int>> endf(n); for (int i = 1, cnt = 0; i < n; i++) { vector<int> cidx, ends; vector<pii> tmp; for (int j = 0; j < ranges[i - 1].size(); j++) { auto [A, B] = ranges[i - 1][j]; cidx.push_back(cnt); idxf.push_back(i - 1); nxtf.push_back({cnt, 0}); dpthf.push_back(0); cnt++; idxf.push_back(i); nxtf.push_back({cnt - 1, B - A}); dpthf.push_back(dpthf[cnt - 1] + 1); tmp.push_back({B, cnt}); ends.push_back(cnt); cnt++; } endf[i] = ends; if (!prev.empty()) { bool cycled = false; for (int j = 0, it = prev.size() - 1; j < ranges[i - 1].size(); j++) { auto [A, B] = ranges[i - 1][j]; while (!cycled && prev[it].first > A) { if (it == 0) { it = prev.size() - 1; cycled = true; } else it--; } nxtf[cidx[j]] = {prev[it].second, (A - prev[it].first + t) % t}; dpthf[cidx[j]] = dpthf[prev[it].second] + 1; } } prev = tmp; } jmpf.resize(idxf.size()); for (int i = 0; i < idxf.size(); i++) { auto [p, w] = nxtf[i]; if (p == i) jmpf[i] = {i, 0}; else { if (dpthf[jmpf[p].first] - dpthf[p] == dpthf[jmpf[jmpf[p].first].first] - dpthf[jmpf[p].first]) jmpf[i] = {jmpf[jmpf[p].first].first, jmpf[p].second + jmpf[jmpf[p].first].second + w}; else jmpf[i] = {p, w}; } } int q; cin >> q; while (q--) { int l, r; cin >> l >> r; l--, r--; ll mina = LONG_MAX; for (int v : endf[r]) { ll sm = 0; while (idxf[v] > l) { if (idxf[jmpf[v].first] > l) { sm += jmpf[v].second; v = jmpf[v].first; } else { sm += nxtf[v].second; v = nxtf[v].first; } } mina = min(mina, sm); } cout << mina << '\n'; } }
#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...