:heavy_check_mark: test/datastructure/lazy_segment_tree/aoj_dsl_2_d.test.cpp

Depends on

Code

// verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/courses/library/3/DSL/2/DSL_2_D

#include "src/datastructure/lazy_segment_tree.hpp"

#include <iostream>
#include <limits>

using namespace std;

int main() {
	int n, q;
	cin >> n >> q;
	auto f = [](int, int) { return 0; };
	auto g = [](int, int b) { return b; };
	auto h = [](int, int b) { return b; };
	LazySegmentTree seg(f, g, h, numeric_limits<int>::max(), -1);
	seg.init(n);
	while (q--) {
		int com;
		cin >> com;
		if (com) {
			int i;
			cin >> i;
			cout << seg.at(i) << endl;
		} else {
			int s, t, x;
			cin >> s >> t >> x;
			seg.update(s, t + 1, x);
		}
	}

	return 0;
}
#line 1 "test/datastructure/lazy_segment_tree/aoj_dsl_2_d.test.cpp"
// verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/courses/library/3/DSL/2/DSL_2_D

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



#include <vector>

template<class T, class E, class F, class G, class H>
class LazySegmentTree { // 0-indexed
private:
	int n_{}, height{};
	std::vector<T> tree;
	std::vector<E> lazy;
	F f; // function<T(T, T)>
	G g; // function<T(T, E)>
	H h; // function<E(E, E)>
	T ti;
	E ei;

	inline T reflect(int k) { return (lazy[k] == ei ? tree[k] : g(tree[k], lazy[k])); }

	inline void eval(int k) {
		if (lazy[k] == ei) return;
		lazy[2 * k] = h(lazy[2 * k], lazy[k]);
		lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
		tree[k] = reflect(k);
		lazy[k] = ei;
	}

	inline void thrust(int k) {
		for (int i = height; i > 0; --i) eval(k >> i);
	}

	inline void recalc(int k) {
		while (k >>= 1) tree[k] = f(reflect(2 * k), reflect(2 * k + 1));
	}

public:
	LazySegmentTree(F f, G g, H h, T ti, E ei) : f(f), g(g), h(h), ti(ti), ei(ei) {}

	void init(int n) {
		n_ = 1, height = 0;
		while (n_ < n) n_ *= 2, ++height;
		tree.assign(2 * n_, ti);
		lazy.assign(2 * n_, ei);
	}

	void build(std::vector<T> const &v) {
		int const N = v.size();
		init(N);
		for (int i = 0; i < N; ++i) tree[n_ + i] = v[i];
		for (int i = n_ - 1; i > 0; --i) tree[i] = f(tree[2 * i], tree[2 * i + 1]);
	}

	void update(int s, int t, E const &x) { // [l, r)
		s += n_, t += n_;
		thrust(s), thrust(t - 1);
		int l = s, r = t;
		while (l < r) {
			if (l & 1) lazy[l] = h(lazy[l], x), ++l;
			if (r & 1) --r, lazy[r] = h(lazy[r], x);
			l >>= 1, r >>= 1;
		}
		recalc(s), recalc(t - 1);
	}

	T find(int s, int t) { // [l, r)
		s += n_, t += n_;
		thrust(s), thrust(t - 1);
		int l = s, r = t;
		T ll = ti, rr = ti;
		while (l < r) {
			if (l & 1) ll = f(ll, reflect(l++));
			if (r & 1) rr = f(reflect(--r), rr);
			l >>= 1, r >>= 1;
		}
		return f(ll, rr);
	}

	T at(int i) {
		i += n_;
		thrust(i);
		return reflect(i);
	}
};


#line 4 "test/datastructure/lazy_segment_tree/aoj_dsl_2_d.test.cpp"

#include <iostream>
#include <limits>

using namespace std;

int main() {
	int n, q;
	cin >> n >> q;
	auto f = [](int, int) { return 0; };
	auto g = [](int, int b) { return b; };
	auto h = [](int, int b) { return b; };
	LazySegmentTree seg(f, g, h, numeric_limits<int>::max(), -1);
	seg.init(n);
	while (q--) {
		int com;
		cin >> com;
		if (com) {
			int i;
			cin >> i;
			cout << seg.at(i) << endl;
		} else {
			int s, t, x;
			cin >> s >> t >> x;
			seg.update(s, t + 1, x);
		}
	}

	return 0;
}

Test cases

Env Name Status Elapsed Memory
g++ 00_sample_00.in :heavy_check_mark: AC 12 ms 7 MB
g++ 00_sample_01.in :heavy_check_mark: AC 12 ms 7 MB
g++ 01_rand_00.in :heavy_check_mark: AC 12 ms 7 MB
g++ 01_rand_01.in :heavy_check_mark: AC 12 ms 7 MB
g++ 01_rand_02.in :heavy_check_mark: AC 12 ms 7 MB
g++ 01_rand_03.in :heavy_check_mark: AC 13 ms 7 MB
g++ 01_rand_04.in :heavy_check_mark: AC 14 ms 7 MB
g++ 01_rand_05.in :heavy_check_mark: AC 25 ms 7 MB
g++ 02_corner_00.in :heavy_check_mark: AC 12 ms 7 MB
g++ 02_corner_01.in :heavy_check_mark: AC 12 ms 7 MB
g++ 03_large_00.in :heavy_check_mark: AC 34 ms 7 MB
g++ 03_large_01.in :heavy_check_mark: AC 28 ms 7 MB
g++ 03_large_02.in :heavy_check_mark: AC 42 ms 7 MB
g++ 03_large_03.in :heavy_check_mark: AC 30 ms 7 MB
g++ 04_maximum_00.in :heavy_check_mark: AC 196 ms 9 MB
g++ 04_maximum_01.in :heavy_check_mark: AC 179 ms 9 MB
g++ 04_maximum_02.in :heavy_check_mark: AC 284 ms 9 MB
g++ 04_maximum_03.in :heavy_check_mark: AC 182 ms 9 MB
g++ 05_critical_00.in :heavy_check_mark: AC 165 ms 9 MB
g++ 05_critical_01.in :heavy_check_mark: AC 280 ms 9 MB
g++ 05_critical_02.in :heavy_check_mark: AC 143 ms 9 MB
g++ 05_critical_03.in :heavy_check_mark: AC 143 ms 9 MB
clang++ 00_sample_00.in :heavy_check_mark: AC 12 ms 9 MB
clang++ 00_sample_01.in :heavy_check_mark: AC 11 ms 11 MB
clang++ 01_rand_00.in :heavy_check_mark: AC 11 ms 11 MB
clang++ 01_rand_01.in :heavy_check_mark: AC 11 ms 13 MB
clang++ 01_rand_02.in :heavy_check_mark: AC 11 ms 9 MB
clang++ 01_rand_03.in :heavy_check_mark: AC 13 ms 7 MB
clang++ 01_rand_04.in :heavy_check_mark: AC 15 ms 11 MB
clang++ 01_rand_05.in :heavy_check_mark: AC 25 ms 11 MB
clang++ 02_corner_00.in :heavy_check_mark: AC 11 ms 9 MB
clang++ 02_corner_01.in :heavy_check_mark: AC 11 ms 13 MB
clang++ 03_large_00.in :heavy_check_mark: AC 32 ms 13 MB
clang++ 03_large_01.in :heavy_check_mark: AC 34 ms 10 MB
clang++ 03_large_02.in :heavy_check_mark: AC 32 ms 13 MB
clang++ 03_large_03.in :heavy_check_mark: AC 34 ms 15 MB
clang++ 04_maximum_00.in :heavy_check_mark: AC 247 ms 14 MB
clang++ 04_maximum_01.in :heavy_check_mark: AC 236 ms 12 MB
clang++ 04_maximum_02.in :heavy_check_mark: AC 297 ms 15 MB
clang++ 04_maximum_03.in :heavy_check_mark: AC 218 ms 14 MB
clang++ 05_critical_00.in :heavy_check_mark: AC 305 ms 12 MB
clang++ 05_critical_01.in :heavy_check_mark: AC 196 ms 9 MB
clang++ 05_critical_02.in :heavy_check_mark: AC 210 ms 10 MB
clang++ 05_critical_03.in :heavy_check_mark: AC 213 ms 12 MB
Back to top page