#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 = 1e5 + 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];
}
}
A[n].resize(m[n - 1] + 2, 0);
B[n].resize(m[n - 1] + 2, 0);
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";
}
}
}
namespace sub2 {
bool checksub() {
FOR(i, 1, n - 1) if (m[i] > 1) return 0;
return 1;
}
int record = 0;
struct Node {
ll cost;
int head, tail;
Node() {
cost = 0;
head = tail = - 1;
}
friend Node operator +(Node a, Node b) {
if (b.head == - 1) return a;
if (a.head == - 1) return b;
Node ans;
ans.head = a.head;
ans.tail = b.tail;
ans.cost = a.cost + b.cost;
if (B[a.tail][1] <= A[b.head][1]) {
ans.cost+= A[b.head][1] - B[a.tail][1];
} else {
ans.cost+= T - B[a.tail][1] + A[b.head][1];
}
return ans;
}
};
Node st[4 * N];
void build(int id, int l, int r) {
if (l == r) {
st[id].cost = B[l][1] - A[l][1];
st[id].head = st[id].tail = l;
return;
}
int mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
st[id] = st[id << 1] + st[id << 1 | 1];
}
Node get(int id, int l, int r, int u, int v) {
if (l > v || r < u) return Node();
if (l >= u && r <= v) return st[id];
int mid = (l + r) >> 1;
return get(id << 1, l, mid, u, v) + get(id << 1 | 1, mid + 1, r, u, v);
}
void solve() {
A[n][1] = A[n - 1][1];
B[n][1] = B[n - 1][1];
build(1, 1, n);
while (q--) {
int l, r; cin >> l >> r;
cout << get(1, 1, n, l, r - 1).cost << "\n";
}
}
}
namespace sub3 {
struct Node {
ll cost[10][10];
int head, tail;
Node() {
FOR(i, 1, 8) FOR(j, 1, 8) cost[i][j] = 1e18;
head = tail = - 1;
}
friend Node operator +(Node a, Node b) {
if (b.head == - 1) return a;
if (a.head == - 1) return b;
Node ans;
ans.head = a.head;
ans.tail = b.tail;
FOR(x, 1, m[a.head]) FOR(t, 1, m[b.tail]) {
FOR(y, 1, m[a.tail]) FOR(z, 1, m[b.head]) {
if (B[a.tail][y] <= A[b.head][z]) {
minimize(ans.cost[x][t], a.cost[x][y] + b.cost[z][t] + A[b.head][z] - B[a.tail][y]);
} else {
minimize(ans.cost[x][t], a.cost[x][y] + b.cost[z][t] + A[b.head][z] - B[a.tail][y] + T);
}
}
}
return ans;
}
};
Node st[4 * N];
void build(int id, int l, int r) {
if (l == r) {
FOR(i, 1, m[l]) st[id].cost[i][i] = B[l][i] - A[l][i];
st[id].head = st[id].tail = l;
return;
}
int mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
st[id] = st[id << 1] + st[id << 1 | 1];
}
Node get(int id, int l, int r, int u, int v) {
if (l > v || r < u) return Node();
if (l >= u && r <= v) return st[id];
int mid = (l + r) >> 1;
return get(id << 1, l, mid, u, v) + get(id << 1 | 1, mid + 1, r, u, v);
}
void solve() {
build(1, 1, n);
while (q--) {
int l, r; cin >> l >> r;
ll ans = 1e18;
Node res = get(1, 1, n, l, r - 1);
FOR(i, 1, m[l]) FOR(j, 1, m[r - 1]) minimize(ans, res.cost[i][j]);
cout << ans << "\n";
}
}
}
void process(void) {
if (n <= 2000) return sub1 :: solve();
if (sub2 :: checksub()) return sub2 :: solve();
return sub3 :: 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:226:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
226 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:227:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
227 | 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... |