Submission #1089755

# Submission time Handle Problem Language Result Execution time Memory
1089755 2024-09-17T05:48:03 Z ymm Dancing Elephants (IOI11_elephants) C++17
100 / 100
1605 ms 25116 KB
#include "elephants.h"
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;

int L;

struct Part {
	vector<int> Ps;
	vector<pii> ans;

	void calc_ans() {
		ans.resize(Ps.size());
		if (ans.empty())
			return;
		ans.back() = {1, Ps.back() + L};
		size_t ptr = ans.size();
		LoopR (i,0,(ll)ans.size()-1) {
			while (Ps[ptr-1] - Ps[i] >= L)
				--ptr;
			ans[i].first = ptr == ans.size()? 1: ans[ptr].first + 1;
			ans[i].second = ptr == ans.size()? Ps[i] + L: ans[ptr].second;
		}
	}

	// vec must be sorted
	// O(vec.size())
	Part(const vector<int> &vec) {
		Ps = vec;
		cache = -1;
		calc_ans();
	}

	ssize_t cache;
	auto lower_bound(int x) {
		if (
			cache != -1
		   	&& (cache == 0 || Ps[cache-1] < x)
		    	&& (cache == Ps.size() || Ps[cache] >= x)
		) {
			return Ps.begin() + cache;
		}
		cache = std::lower_bound(Ps.begin(), Ps.end(), x) - Ps.begin();
		return Ps.begin() + cache;
	}

	// O(log(Ps.size()))
	pii get(int p) {
		auto it = lower_bound(p);
		if (it == Ps.end())
			return {0, p};
		return ans[it - Ps.begin()];
	}

	// O(Ps.size())
	void put(int x) {
		auto it = lower_bound(x);
		Ps.insert(it, x);
		cache = -1;
		calc_ans();
	}

	// O(Ps.size())
	void remove(int x) {
		auto it = lower_bound(x);
		assert(it != Ps.end() && *it == x);
		Ps.erase(it);
		cache = -1;
		calc_ans();
	}
};

struct PartList {
	vector<Part> parts;
	vector<int> bounds;
	map<int, int> pos_cnt;
	static constexpr int S = 768;

	// O(nlog(n))
	void init(const vector<int> &vec) {
		pos_cnt.clear();
		for (int x : vec)
			pos_cnt[x]++;
		vector<int> Ps;
		for (auto [x, _] : pos_cnt)
			Ps.push_back(x);
		repart(Ps);
	}

	// Ps must be sorted
	// O(Ps.size())
	void repart(const vector<int> &Ps) {
		bounds.clear();
		parts.clear();
		for (size_t i = 0; i < Ps.size(); i += S) {
			size_t j = min(i + S, Ps.size());
			if (j != Ps.size())
				bounds.push_back(Ps[j]);
			parts.emplace_back(vector(Ps.begin() + i, Ps.begin() + j));
		}
	}

	// O(n)
	void repart() {
		vector<int> vec;
		for (auto &p : parts)
			vec.insert(vec.end(), p.Ps.begin(), p.Ps.end());
		repart(vec);
	}

	// O(log(n) + S) + amortized O(n/S)
	void move(int x, int y) {
		auto &cntx = pos_cnt[x];
		auto &cnty = pos_cnt[y];
		if (!--cntx) {
			int i = upper_bound(bounds.begin(), bounds.end(), x) - bounds.begin();
			parts[i].remove(x);
		}
		if (!cnty++) {
			int i = upper_bound(bounds.begin(), bounds.end(), y) - bounds.begin();
			parts[i].put(y);
			if (parts[i].Ps.size() > 2*S)
				repart();
		}
	}

	// O(n/S * log(S))
	int calc() {
		int ans = 0;
		int pnt = 0;
		for (auto &p : parts) {
			auto [x, y] = p.get(pnt);
			//cerr << "Ps = ";
			//for (auto x : p.Ps)
			//	cerr << x << ", ";
			//cerr << "\npnt = " << pnt << ", x = " << x << ", y = " << y << '\n';
			ans += x;
			pnt = y;
		}
		return ans;
	}
};

const int N = 150'010;
PartList part_list;
int pos[N];
int n;

void init(int n_, int L_, int X[])
{
	n = n_;
	L = L_ + 1;
	Loop (i,0,n)
		pos[i] = X[i];
	part_list.init(vector(pos, pos + n));
}

int update(int i, int y)
{
	part_list.move(pos[i], y);
	pos[i] = y;
	return part_list.calc();
}

// move = O(log(n) + S) + amortized O(n/S)
// calc = O(n/S * log(S))
// q log(n) + qS + nq/S + nq/S log(S)
// qS + nq/S log(S)
// S^2 / log(S) = n
// S = 1280

Compilation message

elephants.cpp: In member function 'auto Part::lower_bound(int)':
elephants.cpp:43:18: warning: comparison of integer expressions of different signedness: 'ssize_t' {aka 'long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |        && (cache == Ps.size() || Ps[cache] >= x)
      |            ~~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 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 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 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 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 159 ms 4944 KB Output is correct
8 Correct 163 ms 5460 KB Output is correct
9 Correct 164 ms 6836 KB Output is correct
10 Correct 135 ms 8548 KB Output is correct
11 Correct 155 ms 8556 KB Output is correct
12 Correct 410 ms 8512 KB Output is correct
13 Correct 175 ms 8304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 159 ms 4944 KB Output is correct
8 Correct 163 ms 5460 KB Output is correct
9 Correct 164 ms 6836 KB Output is correct
10 Correct 135 ms 8548 KB Output is correct
11 Correct 155 ms 8556 KB Output is correct
12 Correct 410 ms 8512 KB Output is correct
13 Correct 175 ms 8304 KB Output is correct
14 Correct 118 ms 5616 KB Output is correct
15 Correct 237 ms 7460 KB Output is correct
16 Correct 617 ms 9532 KB Output is correct
17 Correct 596 ms 11160 KB Output is correct
18 Correct 704 ms 11508 KB Output is correct
19 Correct 294 ms 11928 KB Output is correct
20 Correct 670 ms 11676 KB Output is correct
21 Correct 622 ms 11636 KB Output is correct
22 Correct 265 ms 11424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 159 ms 4944 KB Output is correct
8 Correct 163 ms 5460 KB Output is correct
9 Correct 164 ms 6836 KB Output is correct
10 Correct 135 ms 8548 KB Output is correct
11 Correct 155 ms 8556 KB Output is correct
12 Correct 410 ms 8512 KB Output is correct
13 Correct 175 ms 8304 KB Output is correct
14 Correct 118 ms 5616 KB Output is correct
15 Correct 237 ms 7460 KB Output is correct
16 Correct 617 ms 9532 KB Output is correct
17 Correct 596 ms 11160 KB Output is correct
18 Correct 704 ms 11508 KB Output is correct
19 Correct 294 ms 11928 KB Output is correct
20 Correct 670 ms 11676 KB Output is correct
21 Correct 622 ms 11636 KB Output is correct
22 Correct 265 ms 11424 KB Output is correct
23 Correct 1000 ms 21024 KB Output is correct
24 Correct 902 ms 20512 KB Output is correct
25 Correct 648 ms 19484 KB Output is correct
26 Correct 787 ms 25116 KB Output is correct
27 Correct 859 ms 24876 KB Output is correct
28 Correct 877 ms 9716 KB Output is correct
29 Correct 787 ms 9164 KB Output is correct
30 Correct 867 ms 9556 KB Output is correct
31 Correct 795 ms 9020 KB Output is correct
32 Correct 667 ms 24380 KB Output is correct
33 Correct 305 ms 17196 KB Output is correct
34 Correct 508 ms 24636 KB Output is correct
35 Correct 239 ms 17608 KB Output is correct
36 Correct 55 ms 7760 KB Output is correct
37 Correct 274 ms 17608 KB Output is correct
38 Correct 836 ms 23836 KB Output is correct
39 Correct 844 ms 24684 KB Output is correct
40 Correct 791 ms 24032 KB Output is correct
41 Correct 1414 ms 23300 KB Output is correct
42 Correct 1605 ms 23872 KB Output is correct