Submission #916679

# Submission time Handle Problem Language Result Execution time Memory
916679 2024-01-26T09:27:50 Z mickey080929 None (JOI15_walls) C++17
100 / 100
317 ms 33572 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;

const ll inf = 2e18;

struct Wall{
	ll l, r, id;
	bool operator<(const Wall &other) const {
		if (r - l == other.r - other.l) return id < other.id;
		return r - l < other.r - other.l;
	}
};

int main() {
	ios_base :: sync_with_stdio(false); cin.tie(NULL);
	ll n, m;
	cin >> n >> m;
	vector<Wall> a(n);
	vector<ll> b(m);
	for (ll i=0; i<n; i++) {
		cin >> a[i].l >> a[i].r;
		a[i].id = i;
	}
	sort(a.begin(), a.end());
	ll mx = 0, mn = inf;
	for (ll i=0; i<m; i++) {
		cin >> b[i];
		mx = max(mx, b[i]);
		mn = min(mn, b[i]);
	}
	b.erase(unique(b.begin(), b.end()), b.end());
	m = b.size();
	vector<ll> t = {b[0]};
	for (ll i=1; i<m-1; i++) {
		if (b[i-1] > b[i] && b[i] < b[i+1])
			t.push_back(b[i]);
		else if (b[i-1] < b[i] && b[i] > b[i+1])
			t.push_back(b[i]);
	}
	if (m >= 2) t.push_back(b[m-1]);
	m = t.size();
	b = t;
	ll cur = 0;
	set<ll> s;
	priority_queue<array<ll,3>> pq;
	for (ll i=0; i<m; i++) s.insert(i);
	for (ll i=0; i<m-1; i++) {
		pq.push({-abs(b[i+1] - b[i]), i, i+1});
		cur += abs(b[i+1] - b[i]);
	}
	vector<ll> ans(n, 0);
	for (ll i=0; i<n; i++) {
		while (s.size() > 2 && !pq.empty() && -pq.top()[0] < a[i].r - a[i].l) {
			ll x = pq.top()[1];
			ll y = pq.top()[2];
			pq.pop();
			auto xit = s.lower_bound(x);
			if (xit == s.end() || *xit != x) continue;
			auto yit = next(xit);
			if (yit == s.end() || *yit != y) continue;

			cur -= abs(b[x] - b[y]);
			if (xit == s.begin()) {
				s.erase(xit);
				continue;
			}
			if (yit == prev(s.end())) {
				s.erase(yit);
				continue;
			}
			ll z = *prev(xit);
			ll w = *next(yit);
			cur -= abs(b[z] - b[x]);
			cur -= abs(b[y] - b[w]);
			s.erase(xit);
			s.erase(yit);
			cur += abs(b[z] - b[w]);
			pq.push({-abs(b[z] - b[w]), z, w});
		}
		if (mx - mn <= a[i].r - a[i].l) {
			if (a[i].r <= mx) ans[a[i].id] = mx - a[i].r;
			else if (a[i].l >= mn) ans[a[i].id] = a[i].l - mn;
			else ans[a[i].id] = 0;
			continue;
		}
		ll fi = *s.begin();
		ll se = *next(s.begin());
		if (b[fi] < b[se]) {
			if (a[i].l >= b[fi]) {
				ans[a[i].id] = a[i].l - b[fi] + cur - (a[i].r - a[i].l) * ((ll)s.size()-1);
			}
			else {
				ans[a[i].id] = b[se] - a[i].r + cur - (b[se] - b[fi]) - (a[i].r - a[i].l) * ((ll)s.size()-2);
			}
		}
		else {
			if (a[i].r <= b[fi]) {
				ans[a[i].id] = b[fi] - a[i].r + cur - (a[i].r - a[i].l) * ((ll)s.size()-1);
			}
			else {
				ans[a[i].id] = a[i].l - b[se] + cur - (b[fi] - b[se]) - (a[i].r - a[i].l) * ((ll)s.size()-2);
			}
		}
	}
	for (ll i=0; i<n; i++) {
		cout << ans[i] << '\n';
	}
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1112 KB Output is correct
2 Correct 101 ms 19720 KB Output is correct
3 Correct 135 ms 22728 KB Output is correct
4 Correct 108 ms 22728 KB Output is correct
5 Correct 125 ms 22900 KB Output is correct
6 Correct 84 ms 23752 KB Output is correct
7 Correct 15 ms 3676 KB Output is correct
8 Correct 17 ms 3932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 2900 KB Output is correct
2 Correct 152 ms 21140 KB Output is correct
3 Correct 67 ms 13124 KB Output is correct
4 Correct 288 ms 32104 KB Output is correct
5 Correct 203 ms 27592 KB Output is correct
6 Correct 287 ms 31008 KB Output is correct
7 Correct 190 ms 27224 KB Output is correct
8 Correct 279 ms 31172 KB Output is correct
9 Correct 73 ms 13600 KB Output is correct
10 Correct 170 ms 30404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1112 KB Output is correct
2 Correct 101 ms 19720 KB Output is correct
3 Correct 135 ms 22728 KB Output is correct
4 Correct 108 ms 22728 KB Output is correct
5 Correct 125 ms 22900 KB Output is correct
6 Correct 84 ms 23752 KB Output is correct
7 Correct 15 ms 3676 KB Output is correct
8 Correct 17 ms 3932 KB Output is correct
9 Correct 15 ms 2900 KB Output is correct
10 Correct 152 ms 21140 KB Output is correct
11 Correct 67 ms 13124 KB Output is correct
12 Correct 288 ms 32104 KB Output is correct
13 Correct 203 ms 27592 KB Output is correct
14 Correct 287 ms 31008 KB Output is correct
15 Correct 190 ms 27224 KB Output is correct
16 Correct 279 ms 31172 KB Output is correct
17 Correct 73 ms 13600 KB Output is correct
18 Correct 170 ms 30404 KB Output is correct
19 Correct 200 ms 28736 KB Output is correct
20 Correct 317 ms 33572 KB Output is correct
21 Correct 198 ms 27592 KB Output is correct
22 Correct 274 ms 31704 KB Output is correct
23 Correct 197 ms 29344 KB Output is correct
24 Correct 290 ms 32588 KB Output is correct
25 Correct 83 ms 15420 KB Output is correct
26 Correct 81 ms 16196 KB Output is correct
27 Correct 183 ms 32620 KB Output is correct