:heavy_check_mark: test/datastructure/interval_set/yuki_674.test.cpp

Depends on

Code

// verification-helper: PROBLEM https://yukicoder.me/problems/no/674

#include "src/datastructure/interval_set.hpp"

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
	long long d, q;
	cin >> d >> q;

	long long ans = 0;
	IntervalSet<long long> is;
	while (q--) {
		long long a, b;
		cin >> a >> b;

		is.insert(a, b);
		auto it = is.find(a);
		ans = max(ans, it->second - it->first + 1);

		cout << ans << endl;
	}

	return 0;
}
#line 1 "test/datastructure/interval_set/yuki_674.test.cpp"
// verification-helper: PROBLEM https://yukicoder.me/problems/no/674

#line 1 "src/datastructure/interval_set.hpp"



#include <cassert>
#include <cstdint>
#include <iterator>
#include <limits>
#include <numeric>
#include <set>
#include <utility>

template<typename T>
class IntervalSet {
private:
	static constexpr T MIN = std::numeric_limits<T>::min();
	static constexpr T MAX = std::numeric_limits<T>::max();

	std::set<std::pair<T, T>> intervals;
	T adjacent_offset;

public:
	using iterator = typename std::set<std::pair<T, T>>::iterator;

	IntervalSet(bool enable_merge_adjacent = true) :
		intervals{
			{MIN, MIN},
            {MAX, MAX}
    },
		adjacent_offset(enable_merge_adjacent ? 1 : 0) {}

	iterator begin() const { return std::next(intervals.begin()); }

	iterator end() const { return std::prev(intervals.end()); }

	[[nodiscard]] iterator find(T x) const {
		auto it = std::prev(intervals.upper_bound({x, MAX}));
		return it->first <= x && x <= it->second ? it : end();
	}

	std::uint64_t insert(T x) { return insert(x, x); }

	std::uint64_t insert(T l, T r) { // [l, r]
		assert(l <= r);

		auto left_it = intervals.upper_bound({l, MAX});
		auto right_it = intervals.upper_bound({r + adjacent_offset, MAX});
		if (left_it != intervals.begin() &&
			l - adjacent_offset <= std::prev(left_it)->second)
			--left_it;

		std::uint64_t count =
			r - l + 1 -
			std::accumulate(left_it, right_it, std::uint64_t{0}, [](auto acc, auto x) {
				return acc + x.second - x.first + 1;
			});

		T ll = left_it->first;
		T rr = std::prev(right_it)->second;
		intervals.erase(left_it, right_it);
		if (ll < l) {
			count += l - ll;
			l = ll;
		}
		if (r < rr) {
			count += rr - r;
			r = rr;
		}

		intervals.emplace(l, r);
		return count;
	}

	std::uint64_t erase(T x) { return erase(x, x); }

	std::uint64_t erase(T l, T r) { // [l, r]
		assert(l <= r);

		auto left_it = intervals.upper_bound({l, MAX});
		auto right_it = intervals.upper_bound({r, MAX});
		if (left_it != intervals.begin() && l <= std::prev(left_it)->second) --left_it;
		if (left_it == right_it) return 0;

		std::uint64_t count =
			std::accumulate(left_it, right_it, std::uint64_t{0}, [](auto acc, auto x) {
				return acc + x.second - x.first + 1;
			});

		T ll = left_it->first;
		T rr = std::prev(right_it)->second;
		intervals.erase(left_it, right_it);
		if (ll < l) {
			count -= l - ll;
			intervals.emplace(ll, l - 1);
		}
		if (r < rr) {
			count -= rr - r;
			intervals.emplace(r + 1, rr);
		}

		return count;
	}

	[[nodiscard]] bool covered(T x) const { return covered(x, x); }

	[[nodiscard]] bool covered(T l, T r) const { // [l, r]
		assert(l <= r);
		auto it = std::prev(intervals.upper_bound({r, MAX}));
		return it->first <= l && r <= it->second;
	}

	[[nodiscard]] T mex(T x = 0) const {
		auto it = find(x);
		return it == end() ? x : it->second + 1;
	}
};


#line 4 "test/datastructure/interval_set/yuki_674.test.cpp"

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
	long long d, q;
	cin >> d >> q;

	long long ans = 0;
	IntervalSet<long long> is;
	while (q--) {
		long long a, b;
		cin >> a >> b;

		is.insert(a, b);
		auto it = is.find(a);
		ans = max(ans, it->second - it->first + 1);

		cout << ans << endl;
	}

	return 0;
}

Test cases

Env Name Status Elapsed Memory
g++ sample1.txt :heavy_check_mark: AC 12 ms 18 MB
g++ sample2.txt :heavy_check_mark: AC 12 ms 18 MB
g++ sample3.txt :heavy_check_mark: AC 12 ms 18 MB
g++ test1.txt :heavy_check_mark: AC 11 ms 18 MB
g++ test10.txt :heavy_check_mark: AC 21 ms 19 MB
g++ test11.txt :heavy_check_mark: AC 96 ms 21 MB
g++ test12.txt :heavy_check_mark: AC 134 ms 21 MB
g++ test13.txt :heavy_check_mark: AC 95 ms 21 MB
g++ test14.txt :heavy_check_mark: AC 124 ms 21 MB
g++ test15.txt :heavy_check_mark: AC 134 ms 21 MB
g++ test16.txt :heavy_check_mark: AC 112 ms 21 MB
g++ test17.txt :heavy_check_mark: AC 117 ms 21 MB
g++ test2.txt :heavy_check_mark: AC 12 ms 18 MB
g++ test3.txt :heavy_check_mark: AC 11 ms 18 MB
g++ test4.txt :heavy_check_mark: AC 12 ms 18 MB
g++ test5.txt :heavy_check_mark: AC 12 ms 18 MB
g++ test6.txt :heavy_check_mark: AC 12 ms 18 MB
g++ test7.txt :heavy_check_mark: AC 12 ms 18 MB
g++ test8.txt :heavy_check_mark: AC 11 ms 18 MB
g++ test9.txt :heavy_check_mark: AC 25 ms 19 MB
clang++ sample1.txt :heavy_check_mark: AC 11 ms 14 MB
clang++ sample2.txt :heavy_check_mark: AC 11 ms 14 MB
clang++ sample3.txt :heavy_check_mark: AC 11 ms 14 MB
clang++ test1.txt :heavy_check_mark: AC 12 ms 16 MB
clang++ test10.txt :heavy_check_mark: AC 23 ms 14 MB
clang++ test11.txt :heavy_check_mark: AC 77 ms 18 MB
clang++ test12.txt :heavy_check_mark: AC 80 ms 20 MB
clang++ test13.txt :heavy_check_mark: AC 76 ms 18 MB
clang++ test14.txt :heavy_check_mark: AC 132 ms 18 MB
clang++ test15.txt :heavy_check_mark: AC 91 ms 16 MB
clang++ test16.txt :heavy_check_mark: AC 121 ms 18 MB
clang++ test17.txt :heavy_check_mark: AC 80 ms 18 MB
clang++ test2.txt :heavy_check_mark: AC 11 ms 16 MB
clang++ test3.txt :heavy_check_mark: AC 11 ms 18 MB
clang++ test4.txt :heavy_check_mark: AC 11 ms 14 MB
clang++ test5.txt :heavy_check_mark: AC 11 ms 12 MB
clang++ test6.txt :heavy_check_mark: AC 11 ms 18 MB
clang++ test7.txt :heavy_check_mark: AC 11 ms 14 MB
clang++ test8.txt :heavy_check_mark: AC 10 ms 14 MB
clang++ test9.txt :heavy_check_mark: AC 19 ms 18 MB
Back to top page