Submission #1102054

# Submission time Handle Problem Language Result Execution time Memory
1102054 2024-10-17T11:16:46 Z Berryisbetter Nile (IOI24_nile) C++17
100 / 100
170 ms 30548 KB
#include <bits/stdc++.h>
#include "nile.h"
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using vvll = vector<vll>;
const ll INF = 1e9;
vll EMPTY = {};
vll a, b, w;
struct Sum {
	ll n;
	vll sum;
	//Sum();
	Sum(vll& a) :n(a.size()), sum(n + 1) {
		for (ll i = 0; i < n; i++) {
			sum[i + 1] = sum[i] + a[i];
		}
	}
	ll qu(ll l, ll r) {
		return sum[r] - sum[l];
	}
};
struct Seg {
	ll n;
	vll a;
	//Seg();
	Seg(vll& v) :n(v.size()), a(2 * n) {
		for (ll i = 0; i < n; i++) {
			a[i + n] = v[i];
		}
		for (ll i = n - 1; i > 0; i--) {
			a[i] = max(a[i * 2], a[i * 2 + 1]);
		}
	}
	void up(ll i, ll x) {
		i += n;
		a[i] = x;
		while (i > 1) {
			i /= 2;
			a[i] = max(a[i * 2], a[i * 2 + 1]);
		}
	}
	ll qu(ll l, ll r) {
		ll ans = -INF;
		for (l += n, r += n; l < r; l /= 2, r /= 2) {
			if (l % 2) {
				ans = max(ans,a[l++]);
			}
			if (r % 2) {
				ans = max(ans,a[--r]);
			}
		}
		return ans;
	}
};
Sum sumall(EMPTY);
Seg segall(EMPTY), segeven(EMPTY), segodd(EMPTY);
ll calc(ll l, ll r) {
	ll sub = 0;
	if ((r - l) % 2 == 1) {
		if (l % 2 == 0) {
			sub = max(segall.qu(l, r), segeven.qu(l, r));
		}
		else {
			sub = max(segall.qu(l, r), segodd.qu(l, r));
		}
	}
	return sumall.qu(l, r) - sub;
}


std::vector<long long> calculate_costs(std::vector<int> W, std::vector<int> A, std::vector<int> B, std::vector<int> E) {
	ll n = A.size(), q = E.size();
	vector<pair<ll, pll>> ord(n);
	for (ll i = 0; i < n; i++) {
		ord[i] = { W[i],{A[i],B[i]} };
	}
	sort(ord.begin(), ord.end());
	//a = A, b = B, w = W; in correct order:
	a = b = w = vll(n);
	for (ll i = 0; i < n; i++) {
		w[i] = ord[i].first;
		a[i] = ord[i].second.first;
		b[i] = ord[i].second.second;
	}

	//setup rmq rsq:
	sumall = Sum(b);
	vll temp, rem=b;
	for (ll i = 0; i < n; i++) {
		rem[i] -= a[i];
	}
	temp=rem;
	segall = Seg(temp);
	for (ll i = 0; i < n; i += 2) {
		temp[i] = -INF;
	}
	segodd = Seg(temp);
	temp = rem;
	for (ll i = 1; i < n; i += 2) {
		temp[i] = -INF;
	}
	segeven = Seg(temp);
	temp = rem;

	//setup ev:
	vector<pair<ll, pll>> ev;// {len,{type,inx}}. type: 0 - query,1 - next disconnect,2 - disconnect two steps
	for (ll i = 0; i < q; i++) {
		ev.push_back({ E[i]+1,{0,i} });//?
	}
	for (ll i = 0; i < n - 1; i++) {
		ev.push_back({ w[i + 1] - w[i],{1,i} });//+1?
	}
	for (ll i = 0; i < n - 2; i++) {
		ev.push_back({ w[i + 2] - w[i],{2,i} });//+1?
	}
	sort(ev.begin(), ev.end());
	reverse(ev.begin(), ev.end());

	// solve:
	vll ans(q);
	ll sum = calc(0, n);
	set<pll> segs = { { 0ll,n } };
	for (auto k : ev) {
		ll type = k.second.first, inx = k.second.second;
		if (type == 0) {
			ans[inx] = sum;
		}
		if (type == 1) {
			auto p = segs.upper_bound({ inx,INF });
			p--;
			ll l = (*p).first, r = (*p).second;
			sum -= calc(l, r);
			segs.erase(p);
			sum += calc(l, inx + 1);
			sum += calc(inx + 1, r);
			segs.insert({ l, inx + 1 });
			segs.insert({inx + 1, r});
		}
		if (type == 2) {
			auto p = segs.upper_bound({ inx,INF });
			p--;
			ll l = (*p).first, r = (*p).second;
			sum -= calc(l, r);
			segall.up(inx+1, -INF);//?+1
			sum += calc(l, r);
		}
	}
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 848 KB Output is correct
2 Correct 3 ms 944 KB Output is correct
3 Correct 3 ms 856 KB Output is correct
4 Correct 3 ms 852 KB Output is correct
5 Correct 2 ms 940 KB Output is correct
6 Correct 2 ms 848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 123 ms 26908 KB Output is correct
2 Correct 116 ms 26816 KB Output is correct
3 Correct 129 ms 26800 KB Output is correct
4 Correct 123 ms 26804 KB Output is correct
5 Correct 114 ms 26820 KB Output is correct
6 Correct 108 ms 26940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 109 ms 26800 KB Output is correct
2 Correct 104 ms 26804 KB Output is correct
3 Correct 126 ms 26720 KB Output is correct
4 Correct 122 ms 26800 KB Output is correct
5 Correct 98 ms 26724 KB Output is correct
6 Correct 118 ms 26812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 848 KB Output is correct
2 Correct 3 ms 944 KB Output is correct
3 Correct 3 ms 856 KB Output is correct
4 Correct 3 ms 852 KB Output is correct
5 Correct 2 ms 940 KB Output is correct
6 Correct 2 ms 848 KB Output is correct
7 Correct 2 ms 848 KB Output is correct
8 Correct 2 ms 848 KB Output is correct
9 Correct 3 ms 1356 KB Output is correct
10 Correct 2 ms 848 KB Output is correct
11 Correct 2 ms 848 KB Output is correct
12 Correct 3 ms 848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 848 KB Output is correct
2 Correct 3 ms 944 KB Output is correct
3 Correct 3 ms 856 KB Output is correct
4 Correct 3 ms 852 KB Output is correct
5 Correct 2 ms 940 KB Output is correct
6 Correct 2 ms 848 KB Output is correct
7 Correct 123 ms 26908 KB Output is correct
8 Correct 116 ms 26816 KB Output is correct
9 Correct 129 ms 26800 KB Output is correct
10 Correct 123 ms 26804 KB Output is correct
11 Correct 114 ms 26820 KB Output is correct
12 Correct 108 ms 26940 KB Output is correct
13 Correct 109 ms 26800 KB Output is correct
14 Correct 104 ms 26804 KB Output is correct
15 Correct 126 ms 26720 KB Output is correct
16 Correct 122 ms 26800 KB Output is correct
17 Correct 98 ms 26724 KB Output is correct
18 Correct 118 ms 26812 KB Output is correct
19 Correct 2 ms 848 KB Output is correct
20 Correct 2 ms 848 KB Output is correct
21 Correct 3 ms 1356 KB Output is correct
22 Correct 2 ms 848 KB Output is correct
23 Correct 2 ms 848 KB Output is correct
24 Correct 3 ms 848 KB Output is correct
25 Correct 108 ms 26748 KB Output is correct
26 Correct 110 ms 26892 KB Output is correct
27 Correct 116 ms 26812 KB Output is correct
28 Correct 116 ms 26804 KB Output is correct
29 Correct 111 ms 26808 KB Output is correct
30 Correct 125 ms 26728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 109 ms 26800 KB Output is correct
2 Correct 104 ms 26804 KB Output is correct
3 Correct 126 ms 26720 KB Output is correct
4 Correct 122 ms 26800 KB Output is correct
5 Correct 98 ms 26724 KB Output is correct
6 Correct 118 ms 26812 KB Output is correct
7 Correct 133 ms 30384 KB Output is correct
8 Correct 129 ms 30460 KB Output is correct
9 Correct 143 ms 30328 KB Output is correct
10 Correct 166 ms 30380 KB Output is correct
11 Correct 138 ms 30376 KB Output is correct
12 Correct 139 ms 30376 KB Output is correct
13 Correct 147 ms 30376 KB Output is correct
14 Correct 137 ms 30380 KB Output is correct
15 Correct 146 ms 30520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 3 ms 848 KB Output is correct
3 Correct 3 ms 944 KB Output is correct
4 Correct 3 ms 856 KB Output is correct
5 Correct 3 ms 852 KB Output is correct
6 Correct 2 ms 940 KB Output is correct
7 Correct 2 ms 848 KB Output is correct
8 Correct 123 ms 26908 KB Output is correct
9 Correct 116 ms 26816 KB Output is correct
10 Correct 129 ms 26800 KB Output is correct
11 Correct 123 ms 26804 KB Output is correct
12 Correct 114 ms 26820 KB Output is correct
13 Correct 108 ms 26940 KB Output is correct
14 Correct 109 ms 26800 KB Output is correct
15 Correct 104 ms 26804 KB Output is correct
16 Correct 126 ms 26720 KB Output is correct
17 Correct 122 ms 26800 KB Output is correct
18 Correct 98 ms 26724 KB Output is correct
19 Correct 118 ms 26812 KB Output is correct
20 Correct 2 ms 848 KB Output is correct
21 Correct 2 ms 848 KB Output is correct
22 Correct 3 ms 1356 KB Output is correct
23 Correct 2 ms 848 KB Output is correct
24 Correct 2 ms 848 KB Output is correct
25 Correct 3 ms 848 KB Output is correct
26 Correct 108 ms 26748 KB Output is correct
27 Correct 110 ms 26892 KB Output is correct
28 Correct 116 ms 26812 KB Output is correct
29 Correct 116 ms 26804 KB Output is correct
30 Correct 111 ms 26808 KB Output is correct
31 Correct 125 ms 26728 KB Output is correct
32 Correct 133 ms 30384 KB Output is correct
33 Correct 129 ms 30460 KB Output is correct
34 Correct 143 ms 30328 KB Output is correct
35 Correct 166 ms 30380 KB Output is correct
36 Correct 138 ms 30376 KB Output is correct
37 Correct 139 ms 30376 KB Output is correct
38 Correct 147 ms 30376 KB Output is correct
39 Correct 137 ms 30380 KB Output is correct
40 Correct 146 ms 30520 KB Output is correct
41 Correct 147 ms 30472 KB Output is correct
42 Correct 148 ms 30384 KB Output is correct
43 Correct 145 ms 30376 KB Output is correct
44 Correct 148 ms 30380 KB Output is correct
45 Correct 145 ms 30548 KB Output is correct
46 Correct 142 ms 30336 KB Output is correct
47 Correct 153 ms 30376 KB Output is correct
48 Correct 128 ms 30380 KB Output is correct
49 Correct 170 ms 30376 KB Output is correct