mmrz's library

This documentation is automatically generated by online-judge-tools/verification-helper


Project maintained by mm-rz Hosted on GitHub Pages — Theme by mattgraham

:heavy_check_mark: verify/handmade/2d_segtree_stress.test.cpp

Depends on

Code

#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A" // DUMMY

#include "./../../template/template.hpp"
#include "./../../data_structure/segment_tree_2d.hpp"

class xor_shift_128 {
public:
	typedef uint32_t result_type;
	xor_shift_128(uint32_t seed = 42) {
		set_seed(seed);
	}
	void set_seed(uint32_t seed) {
		a = seed = 1812433253u * (seed ^ (seed >> 30));
		b = seed = 1812433253u * (seed ^ (seed >> 30)) + 1;
		c = seed = 1812433253u * (seed ^ (seed >> 30)) + 2;
		d = seed = 1812433253u * (seed ^ (seed >> 30)) + 3;
	}
	uint32_t operator() () {
		uint32_t t = (a ^ (a << 11));
		a = b; b = c; c = d;
		return d = (d ^ (d >> 19)) ^ (t ^ (t >> 8));
	}
	static constexpr uint32_t max() { return numeric_limits<result_type>::max(); }
	static constexpr uint32_t min() { return numeric_limits<result_type>::min(); }
private:
	uint32_t a, b, c, d;
};

xor_shift_128 gen;

//[_first, _second] の(たぶん)一様分布
#define RND(_first, _second)	uniform_int_distribution<int>(_first, _second)(gen)

void test_sum(int t){
	int h = RND(1, 1000), w = RND(1, 1000);
	int q = 100;
	vector a(h, vector(w, 0));
	rep(i, h)rep(j, w)a[i][j] = RND(1, 100);
	segment_tree_2d<int> seg(a, [](int l, int r){return l+r;}, 0);
	while(q--){
		// update
		{
			int y = RND(0, h-1), x = RND(0, w-1);
			a[y][x] = RND(1, 100);
			seg.set(y, x, a[y][x]);
		}
		// query
		{
			int li = RND(0, h-1), ri = RND(li+1, h);
			int lj = RND(0, w-1), rj = RND(lj+1, w);
			int ans_seg = seg.fold(li, lj, ri, rj);
			int ans_naive = 0;
			for(int i = li;i < ri;i++){
				for(int j = lj;j < rj;j++){
					ans_naive += a[i][j];
				}
			}

			assert(ans_seg == ans_naive);
		}
	}
	cerr << "test_sum : case " << t << " : passed" << endl;
}

void test_max(int t){
	int h = RND(1, 1000), w = RND(1, 1000);
	int q = 100;
	vector a(h, vector(w, 0));
	rep(i, h)rep(j, w)a[i][j] = RND(1, 100);
	segment_tree_2d<int> seg(a, [](int l, int r){return max(l, r);}, 0);
	while(q--){
		// update
		{
			int y = RND(0, h-1), x = RND(0, w-1);
			a[y][x] = RND(1, 100);
			seg.set(y, x, a[y][x]);
		}
		// query
		{
			int li = RND(0, h-1), ri = RND(li+1, h);
			int lj = RND(0, w-1), rj = RND(lj+1, w);
			int ans_seg = seg.fold(li, lj, ri, rj);
			int ans_naive = 0;
			for(int i = li;i < ri;i++){
				for(int j = lj;j < rj;j++){
					chmax(ans_naive, a[i][j]);
				}
			}
			
			assert(ans_seg == ans_naive);
		}
	}
	cerr << "test_max : case " << t << " : passed" << endl;
}

void test_min(int t){
	int h = RND(1, 1000), w = RND(1, 1000);
	int q = 100;
	vector a(h, vector(w, 0));
	rep(i, h)rep(j, w)a[i][j] = RND(1, 100);
	segment_tree_2d<int> seg(a, [](int l, int r){return min(l, r);}, hinf<int>());
	while(q--){
		// update
		{
			int y = RND(0, h-1), x = RND(0, w-1);
			a[y][x] = RND(1, 100);
			seg.set(y, x, a[y][x]);
		}
		// query
		{
			int li = RND(0, h-1), ri = RND(li+1, h);
			int lj = RND(0, w-1), rj = RND(lj+1, w);
			int ans_seg = seg.fold(li, lj, ri, rj);
			int ans_naive = hinf<int>();
			for(int i = li;i < ri;i++){
				for(int j = lj;j < rj;j++){
					chmin(ans_naive, a[i][j]);
				}
			}
			
			assert(ans_seg == ans_naive);
		}
	}
	cerr << "test_max : case " << t << " : passed" << endl;
}

void mmrz::solve(){
	// maybe verified with AOJ DSL 5-B
	rep(t, 100)test_sum(t);
	cerr << "test_sum : passed" << endl;
	rep(t, 100)test_max(t);
	cerr << "test_max : passed" << endl;
	rep(t, 100)test_min(t);
	cerr << "test_min : passed" << endl;

	// DUMMY PHASE
	cout << "Hello World\n";
}
#line 1 "verify/handmade/2d_segtree_stress.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A" // DUMMY

#line 1 "template/template.hpp"
# include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const double pi = acos(-1);
template<class T>constexpr T inf() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T hinf() { return inf<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); }
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); }
template<class T> bool chmin(T& a,T b) { if(a > b){a = b; return true;} return false; }
template<class T> bool chmax(T& a,T b) { if(a < b){a = b; return true;} return false; }
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(ll n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(ll n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
ll gcd(ll a, ll b) { if (b == 0)return a; return gcd(b, a%b); };
ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g*b; };
ll MOD(ll x, ll m){return (x%m+m)%m; }
ll FLOOR(ll x, ll m) {ll r = (x%m+m)%m; return (x-r)/m; }
template<class T> using dijk = priority_queue<T, vector<T>, greater<T>>;
# define all(qpqpq)           (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw)        (wpwpw).erase(unique(all((wpwpw))),(wpwpw).end())
# define LOWER(epepe)         transform(all((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr)         transform(all((rprpr)),(rprpr).begin(),TU<char>)
# define rep(i,upupu)         for(ll i = 0, i##_len = (upupu);(i) < (i##_len);(i)++)
# define reps(i,opopo)        for(ll i = 1, i##_len = (opopo);(i) <= (i##_len);(i)++)
# define len(x)                ((ll)(x).size())
# define bit(n)               (1LL << (n))
# define pb push_back
# define eb emplace_back
# define exists(c, e)         ((c).find(e) != (c).end())

struct INIT{
	INIT(){
		std::ios::sync_with_stdio(false);
		std::cin.tie(0);
		cout << fixed << setprecision(20);
	}
}INIT;

namespace mmrz {
	void solve();
}

int main(){
	mmrz::solve();
}
#line 2 "data_structure/segment_tree_2d.hpp"

#line 5 "data_structure/segment_tree_2d.hpp"

template<typename T>struct segment_tree_2d {
	using F = std::function<T(T, T)>;
	int id(int r, int c) const {return r*2*w+c; }

	int h, w;
	std::vector<T> node;
	F combine;
	T identify;

	segment_tree_2d(int _h, int _w, F _combine, T _identify) : combine(_combine), identify(_identify){
		h = w = 1;
		while(h < _h) h <<= 1;
		while(w < _w) w <<= 1;
		node.assign(4*h*w, identify);
	}

	segment_tree_2d(std::vector<std::vector<T>> &v, F _combine, T _identify) : combine(_combine), identify(_identify){
		h = w = 1;
		while(h < (int)v.size()) h <<= 1;
		while(w < (int)v[0].size()) w <<= 1;
		node.assign(4*h*w, identify);
		for(int i = 0;i < (int)v.size(); i++){
			for(int j = 0;j < (int)v[0].size();j++){
				node[id(i+h-1, j+w-1)] = v[i][j];
			}
		}
		for(int i = 2*h-2; i > h-2;i--){
			for(int j = w-2; j >= 0;j--){
				node[id(i, j)] = combine(node[id(i, 2*j+1)], node[id(i, 2*j+2)]);
			}
		}
		for(int i = h-2;i >= 0;i--){
			for(int j = 0;j < 2*w-1;j++){
				node[id(i, j)] = combine(node[id(2*i+1, j)], node[id(2*i+2, j)]);
			}
		}
	}

	void set(int y, int x, T val){
		y += h-1;
		x += w-1;
		node[id(y, x)] = val;

		for(int j = (x+1)/2-1;j >= 0;j = (j+1)/2-1){
			node[id(y, j)] = combine(node[id(y, 2*j+1)], node[id(y, 2*j+2)]);
		}

		for(int i = (y+1)/2-1;i >= 0;i = (i+1)/2-1){
			for(int j = x;j >= 0;j = (j+1)/2-1){
				node[id(i, j)] = combine(node[id(2*i+1, j)], node[id(2*i+2, j)]);
			}
		}
	}

	T get(int y, int x){
		return node[id(y+h-1, x+w-1)];
	}

	T fold(int li, int lj, int ri, int rj){
		return fold_h(li, lj, ri, rj);
	}

	T fold_h(int li, int lj, int ri, int rj, int k=0, int si=0, int ti=-1){
		if(ti<0)ti = h;

		if(ri <= si || ti <= li)return identify;
		if(li <= si && ti <= ri)return fold_w(lj, rj, k);
		T vs = fold_h(li, lj, ri, rj, 2*k+1, si, (si+ti)/2);
		T vt = fold_h(li, lj, ri, rj, 2*k+2, (si+ti)/2, ti);
		return combine(vs, vt);
	}

	T fold_w(int lj, int rj, int i, int k=0, int sj=0, int tj=-1){
		if(tj<0) tj = w;

		if(rj <= sj || tj <= lj)return identify;
		if(lj <= sj && tj <= rj)return node[id(i, k)];
		T vs = fold_w(lj, rj, i, 2*k+1, sj, (sj+tj)/2);
		T vt = fold_w(lj, rj, i, 2*k+2, (sj+tj)/2, tj);
		return combine(vs, vt);
	}
};

#line 5 "verify/handmade/2d_segtree_stress.test.cpp"

class xor_shift_128 {
public:
	typedef uint32_t result_type;
	xor_shift_128(uint32_t seed = 42) {
		set_seed(seed);
	}
	void set_seed(uint32_t seed) {
		a = seed = 1812433253u * (seed ^ (seed >> 30));
		b = seed = 1812433253u * (seed ^ (seed >> 30)) + 1;
		c = seed = 1812433253u * (seed ^ (seed >> 30)) + 2;
		d = seed = 1812433253u * (seed ^ (seed >> 30)) + 3;
	}
	uint32_t operator() () {
		uint32_t t = (a ^ (a << 11));
		a = b; b = c; c = d;
		return d = (d ^ (d >> 19)) ^ (t ^ (t >> 8));
	}
	static constexpr uint32_t max() { return numeric_limits<result_type>::max(); }
	static constexpr uint32_t min() { return numeric_limits<result_type>::min(); }
private:
	uint32_t a, b, c, d;
};

xor_shift_128 gen;

//[_first, _second] の(たぶん)一様分布
#define RND(_first, _second)	uniform_int_distribution<int>(_first, _second)(gen)

void test_sum(int t){
	int h = RND(1, 1000), w = RND(1, 1000);
	int q = 100;
	vector a(h, vector(w, 0));
	rep(i, h)rep(j, w)a[i][j] = RND(1, 100);
	segment_tree_2d<int> seg(a, [](int l, int r){return l+r;}, 0);
	while(q--){
		// update
		{
			int y = RND(0, h-1), x = RND(0, w-1);
			a[y][x] = RND(1, 100);
			seg.set(y, x, a[y][x]);
		}
		// query
		{
			int li = RND(0, h-1), ri = RND(li+1, h);
			int lj = RND(0, w-1), rj = RND(lj+1, w);
			int ans_seg = seg.fold(li, lj, ri, rj);
			int ans_naive = 0;
			for(int i = li;i < ri;i++){
				for(int j = lj;j < rj;j++){
					ans_naive += a[i][j];
				}
			}

			assert(ans_seg == ans_naive);
		}
	}
	cerr << "test_sum : case " << t << " : passed" << endl;
}

void test_max(int t){
	int h = RND(1, 1000), w = RND(1, 1000);
	int q = 100;
	vector a(h, vector(w, 0));
	rep(i, h)rep(j, w)a[i][j] = RND(1, 100);
	segment_tree_2d<int> seg(a, [](int l, int r){return max(l, r);}, 0);
	while(q--){
		// update
		{
			int y = RND(0, h-1), x = RND(0, w-1);
			a[y][x] = RND(1, 100);
			seg.set(y, x, a[y][x]);
		}
		// query
		{
			int li = RND(0, h-1), ri = RND(li+1, h);
			int lj = RND(0, w-1), rj = RND(lj+1, w);
			int ans_seg = seg.fold(li, lj, ri, rj);
			int ans_naive = 0;
			for(int i = li;i < ri;i++){
				for(int j = lj;j < rj;j++){
					chmax(ans_naive, a[i][j]);
				}
			}
			
			assert(ans_seg == ans_naive);
		}
	}
	cerr << "test_max : case " << t << " : passed" << endl;
}

void test_min(int t){
	int h = RND(1, 1000), w = RND(1, 1000);
	int q = 100;
	vector a(h, vector(w, 0));
	rep(i, h)rep(j, w)a[i][j] = RND(1, 100);
	segment_tree_2d<int> seg(a, [](int l, int r){return min(l, r);}, hinf<int>());
	while(q--){
		// update
		{
			int y = RND(0, h-1), x = RND(0, w-1);
			a[y][x] = RND(1, 100);
			seg.set(y, x, a[y][x]);
		}
		// query
		{
			int li = RND(0, h-1), ri = RND(li+1, h);
			int lj = RND(0, w-1), rj = RND(lj+1, w);
			int ans_seg = seg.fold(li, lj, ri, rj);
			int ans_naive = hinf<int>();
			for(int i = li;i < ri;i++){
				for(int j = lj;j < rj;j++){
					chmin(ans_naive, a[i][j]);
				}
			}
			
			assert(ans_seg == ans_naive);
		}
	}
	cerr << "test_max : case " << t << " : passed" << endl;
}

void mmrz::solve(){
	// maybe verified with AOJ DSL 5-B
	rep(t, 100)test_sum(t);
	cerr << "test_sum : passed" << endl;
	rep(t, 100)test_max(t);
	cerr << "test_max : passed" << endl;
	rep(t, 100)test_min(t);
	cerr << "test_min : passed" << endl;

	// DUMMY PHASE
	cout << "Hello World\n";
}
Back to top page