Submission #969199

# Submission time Handle Problem Language Result Execution time Memory
969199 2024-04-24T16:26:13 Z happypotato Overtaking (IOI23_overtaking) C++17
19 / 100
3500 ms 1880 KB
#include "overtaking.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define pb push_back
ll L;
ll speed;
map<ll, pll> ans[1001];
void debugmp(int ptr) {
	cerr << "Outputting map " << ptr << endl;
	for (pair<ll, pll> cur : ans[ptr]) {
		cerr << "[" << cur.ff << ", " << cur.ss.ff << "] -> " << cur.ss.ss << endl;
	}
	cerr << "End of map " << ptr << endl << endl;
}
void init(int L_, int N, vector<long long> T, vector<int> W, int X, int m, vector<int> s) {
	L = L_; speed = X;
	vector<pll> blocks;
	for (int i = 0; i < N; i++) {
		if (W[i] <= speed) continue;
		blocks.pb({T[i], W[i]});
	}

	map<ll, pll> tmp;
	auto discretise = [&](int ptr) {
		tmp.clear();
		ll prev = -1e18; pll range = {-1e9, -1e9};
		for (map<ll, pll>::iterator it = ans[ptr].begin(); it != ans[ptr].end(); ++it) {
			if (it->ss.ss == prev) {
				tmp[range.ff].ff = it->ss.ff;
				range.ss = it->ss.ff;
				continue;
			}
			tmp[it->ff] = it->ss;
			prev = it->ss.ss;
		}
		ans[ptr] = tmp;
	};

	for (int i = 1; i < m; i++) {
		sort(blocks.begin(), blocks.end(), [&](const pll &lhs, const pll &rhs) {
			if (lhs.ff != rhs.ff) return lhs.ff < rhs.ff;
			return lhs.ss > rhs.ss;
		});
		ll dt = s[i] - s[i - 1];
		map<ll, ll> mp;
		for (auto& [t, w] : blocks) {
			map<ll, ll>::iterator it = mp.upper_bound(t);
			if (it != mp.begin() && (--it)->ss >= t + w * dt) {
				if (it->ff < t) t = it->ss;
				else t += w * dt;
				continue;
			}
			mp[t] = t + w * dt;
			t += w * dt;
		}
		// for (auto &[t, w] : blocks) cerr << t << ' ' << w << endl;
		for (map<ll, ll>::iterator it = mp.begin(); it != mp.end(); ++it) {
			// cerr << it->ff << ' ' << it->ss << endl;
			ll st = it->ff + 1, en = it->ss - speed * dt;
			map<ll, ll>::iterator it2 = it; ++it2;
			if (it2 != mp.end()) en = min(en, it2->ff);
			ans[i][st] = {en, it->ss};
		}
		discretise(i);
		// debugmp(i);
	}

	auto merge = [&](int l, int r) {
		int mid = (l + r) >> 1;
		int pl = l, pr = mid + 1;
		// cerr << l << ' ' << r << ' ' << pl << ' ' << pr << endl;
		// debugmp(pl); debugmp(pr);
		for (map<ll, pll>::iterator it = ans[pl].begin(); it != ans[pl].end(); ++it) {
			ll oldv = it->ss.ss;
			it->ss.ss += speed * (s[r] - s[mid]);
			map<ll, pll>::iterator it2 = ans[pr].upper_bound(oldv);
			if (it2 != ans[pr].begin()) {
				--it2;
				if (it2->ff <= oldv && oldv <= it2->ss.ff) {
					it->ss.ss = it2->ss.ss;
				}
			}
		}
		discretise(pl);
		// cerr << "RESULT 1:" << endl; debugmp(pl);
		for (map<ll, pll>::iterator it = ans[pr].begin(); it != ans[pr].end(); ++it) {
			ll tl = it->ff, tr = it->ss.ff;
			tl -= speed * (s[mid] - s[l - 1]);
			tr -= speed * (s[mid] - s[l - 1]);
			// cerr << tl << ' ' << tr << ' ' << it->ss.ss << endl;
			bool checked = false;
			while (true) {
				map<ll, pll>::iterator it2 = ans[pl].lower_bound(tl);
				if (!checked) {
					checked = true;
					if (it2 == ans[pl].begin()) continue;
					--it2;
				}
				if (it2 == ans[pl].end()) break;
				// cerr << it2->ff << ' ' << it2->ss.ff << ' ' << it2->ss.ss << endl;
				if (it2->ff > tr) break;
				if (it2->ss.ff < tl) continue;
				// have intersection
				// cerr << "CHECKING:\n";
				// cerr << tl << ' ' << tr << ' ' << it->ss.ss << endl;
				// cerr << it2->ff << ' ' << it2->ss.ff << ' ' << it2->ss.ss << endl;
				if (it2->ss.ss <= it->ss.ss) {
					if (it2->ss.ss < it->ss.ss) {
						it2->ss.ff = tl - 1;
					} else {
						tl = min(tl, it2->ff);
						tr = max(tr, it2->ss.ff);
						ans[pl].erase(it2);
					}
				} else {
					tr = min(tr, it2->ff - 1);
					break;
				}
			}
			// cerr << tl << ' ' << tr << endl;
			if (tl <= tr) {
				ans[pl][tl] = {tr, it->ss.ss};
			}
		}
		discretise(pl);
		// cerr << "RESULT 2:" << endl; debugmp(pl);
	};
	auto recur = [&](auto&& self, int l, int r) {
		if (l == r) return;
		int mid = (l + r) >> 1;
		self(self, l, mid); self(self, mid + 1, r);
		merge(l, r);
	};

	recur(recur, 1, m - 1);

	cerr << "FINAL:" << endl; debugmp(1);
	cerr << "END OF FINAL" << endl;
	return;
}

long long arrival_time(long long x) {
	map<ll, pll>::iterator it = ans[1].upper_bound(x);
	if (it != ans[1].begin()) {
		--it;
		if (it->ff <= x && x <= it->ss.ff) return it->ss.ss;
	}
	return x + L * speed;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 2 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 4 ms 604 KB Output is correct
7 Correct 8 ms 860 KB Output is correct
8 Correct 10 ms 860 KB Output is correct
9 Correct 10 ms 952 KB Output is correct
10 Correct 10 ms 860 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 500 KB Output is correct
2 Correct 1 ms 504 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 3 ms 348 KB Output is correct
6 Correct 6 ms 604 KB Output is correct
7 Correct 8 ms 604 KB Output is correct
8 Correct 7 ms 604 KB Output is correct
9 Correct 7 ms 696 KB Output is correct
10 Correct 9 ms 604 KB Output is correct
11 Correct 7 ms 600 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 2 ms 500 KB Output is correct
7 Correct 1 ms 504 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Execution timed out 3535 ms 1880 KB Time limit exceeded
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 4 ms 604 KB Output is correct
8 Correct 8 ms 860 KB Output is correct
9 Correct 10 ms 860 KB Output is correct
10 Correct 10 ms 952 KB Output is correct
11 Correct 10 ms 860 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 2 ms 500 KB Output is correct
14 Correct 1 ms 504 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Execution timed out 3535 ms 1880 KB Time limit exceeded
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 4 ms 604 KB Output is correct
8 Correct 8 ms 860 KB Output is correct
9 Correct 10 ms 860 KB Output is correct
10 Correct 10 ms 952 KB Output is correct
11 Correct 10 ms 860 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 2 ms 500 KB Output is correct
14 Correct 1 ms 504 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 3 ms 348 KB Output is correct
18 Correct 6 ms 604 KB Output is correct
19 Correct 8 ms 604 KB Output is correct
20 Correct 7 ms 604 KB Output is correct
21 Correct 7 ms 696 KB Output is correct
22 Correct 9 ms 604 KB Output is correct
23 Correct 7 ms 600 KB Output is correct
24 Correct 2 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Execution timed out 3535 ms 1880 KB Time limit exceeded
28 Halted 0 ms 0 KB -