#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
template<class T>
bool minimize(T &a, const T &b) {
if (a > b) return a = b, true;
return false;
}
template<class T>
bool maximize(T &a, const T &b) {
if (a < b) return a = b, true;
return false;
}
#define FOR(i,a,b) for(int i=(a); i<=(int)(b); ++i)
#define FORD(i,a,b) for(int i=(a); i>=(int)b; i--)
#define MASK(i) (1LL << (i))
#define BIT(S, i) (((S) >> (i)) & 1)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
const int MOD = 1e9 + 7;
const int oo = 1e9 + 29032008;
const ll inf = 1e18 + 29032008;
const int N = 1e6 + 5;
int n, T, q;
int m[N];
vector <int> A[N], B[N];
void init(void) {
cin >> n >> T;
FOR(i, 1, n - 1) {
cin >> m[i];
A[i].resize(m[i] + 2, 0);
B[i].resize(m[i] + 2, 0);
FOR(j, 1, m[i]) {
cin >> A[i][j] >> B[i][j];
}
}
cin >> q;
}
namespace sub1 {
ll Cost[2005][2005];
ll dp[2005][10];
void solve() {
#define bg array <ll, 3>
priority_queue <bg, vector <bg>, greater <bg>> pq;
FOR(i, 1, n) {
FOR(j, i + 1, n) Cost[i][j] = 1e18;
FOR(j, i, n) FOR(t, 0, 5) dp[j][t] = 1e18;
FOR(j, 1, m[i]) {
dp[i + 1][j] = B[i][j] - A[i][j];
pq.push({dp[i + 1][j], i + 1, j});
}
while (!pq.empty()) {
ll cost = pq.top()[0];
int u = pq.top()[1], id = pq.top()[2];
pq.pop();
Cost[i][u] = min(Cost[i][u], cost);
if (cost > dp[u][id] || u == n) continue;
FOR(j, 1, m[u]) {
if (A[u][j] >= B[u - 1][id]) {
if (minimize(dp[u + 1][j], cost + B[u][j] - B[u - 1][id])) {
pq.push({dp[u + 1][j], u + 1, j});
}
} else {
if (minimize(dp[u + 1][j], cost + T - B[u - 1][id] + B[u][j])) {
pq.push({dp[u + 1][j], u + 1, j});
}
}
}
}
}
while (q--) {
int l, r; cin >> l >> r;
cout << Cost[l][r] << "\n";
}
}
}
void process(void) {
return sub1 :: solve();
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define task "kieuoanh"
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int tc = 1;
// cin >> tc;
while (tc--) {
init();
process();
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:103:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
103 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:104:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |