Submission #1252255

#TimeUsernameProblemLanguageResultExecution timeMemory
1252255Bui_Quoc_CuongEscape Route 2 (JOI24_escape2)C++20
0 / 100
3111 ms259032 KiB
#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];
		}
	}
	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});
						}
					}
				}
			}
		}
		// cout << Cost[1][4] << "\n";
		while (q--) {
			int l, r; cin >> l >> r;
			cout << Cost[l][r] << "\n";
		}
	}
}

namespace sub2 {
	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";
		}
	}	
}

void process(void) {
	if (n <= 2000) sub1 :: solve();
	return sub2 :: 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;
}

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:163:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  163 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:164:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  164 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...