結果

問題 No.2395 区間二次変換一点取得
ユーザー AerenAeren
提出日時 2023-09-03 16:44:18
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 84 ms / 2,000 ms
コード長 18,234 bytes
コンパイル時間 4,439 ms
コンパイル使用メモリ 363,656 KB
実行使用メモリ 5,812 KB
最終ジャッジ日時 2023-09-03 16:44:28
合計ジャッジ時間 8,504 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,360 KB
testcase_01 AC 2 ms
4,356 KB
testcase_02 AC 2 ms
4,364 KB
testcase_03 AC 2 ms
4,356 KB
testcase_04 AC 1 ms
4,360 KB
testcase_05 AC 1 ms
4,360 KB
testcase_06 AC 2 ms
4,360 KB
testcase_07 AC 1 ms
4,356 KB
testcase_08 AC 2 ms
4,356 KB
testcase_09 AC 2 ms
4,360 KB
testcase_10 AC 2 ms
4,360 KB
testcase_11 AC 2 ms
4,360 KB
testcase_12 AC 9 ms
4,376 KB
testcase_13 AC 83 ms
5,692 KB
testcase_14 AC 84 ms
5,712 KB
testcase_15 AC 84 ms
5,664 KB
testcase_16 AC 83 ms
5,712 KB
testcase_17 AC 84 ms
5,628 KB
testcase_18 AC 63 ms
5,812 KB
testcase_19 AC 64 ms
5,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <x86intrin.h>
using namespace std;
using namespace numbers;

template<int id>
struct modular_unfixed_base{
	static unsigned int _mod;
	static unsigned long long _inverse_mod;
	static unsigned int &mod(){
		return _mod;
	}
	static void precalc_barrett(){
		_inverse_mod = (unsigned long long)-1 / _mod + 1;
	}
	static void setup(unsigned int mod = 0){
		if(!mod) cin >> mod;
		_mod = mod;
		assert(_mod >= 1);
		precalc_barrett();
	}
	template<class T>
	static vector<modular_unfixed_base> precalc_power(T base, int SZ){
		vector<modular_unfixed_base> res(SZ + 1, 1);
		for(auto i = 1; i <= SZ; ++ i) res[i] = res[i - 1] * base;
		return res;
	}
	static vector<modular_unfixed_base> _INV;
	static void precalc_inverse(int SZ){
		if(_INV.empty()) _INV.assign(2, 1);
		for(auto x = _INV.size(); x <= SZ; ++ x) _INV.push_back(_mod / x * -_INV[_mod % x]);
	}
	// _mod must be a prime
	static modular_unfixed_base _primitive_root;
	static modular_unfixed_base primitive_root(){
		if(_primitive_root) return _primitive_root;
		if(_mod == 2) return _primitive_root = 1;
		if(_mod == 998244353) return _primitive_root = 3;
		unsigned int divs[20] = {};
		divs[0] = 2;
		int cnt = 1;
		unsigned int x = (_mod - 1) / 2;
		while(x % 2 == 0) x /= 2;
		for(auto i = 3; 1LL * i * i <= x; i += 2){
			if(x % i == 0){
				divs[cnt ++] = i;
				while(x % i == 0) x /= i;
			}
		}
		if(x > 1) divs[cnt ++] = x;
		for(auto g = 2; ; ++ g){
			bool ok = true;
			for(auto i = 0; i < cnt; ++ i){
				if((modular_unfixed_base(g).power((_mod - 1) / divs[i])) == 1){
					ok = false;
					break;
				}
			}
			if(ok) return _primitive_root = g;
		}
	}
	constexpr modular_unfixed_base(): data(){ }
	modular_unfixed_base(const double &x){ data = normalize(llround(x)); }
	modular_unfixed_base(const long double &x){ data = normalize(llround(x)); }
	template<class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_unfixed_base(const T &x){ data = normalize(x); }
	template<class T, typename enable_if<is_integral<T>::value>::type* = nullptr> static unsigned int normalize(const T &x){
		if(_mod == 1) return 0;
		assert(_inverse_mod);
		int sign = x >= 0 ? 1 : -1;
		unsigned int v = _mod <= sign * x ? sign * x - ((__uint128_t)(sign * x) * _inverse_mod >> 64) * _mod : sign * x;
		if(v >= _mod) v += _mod;
		if(sign == -1 && v) v = _mod - v;
		return v;
	}
	const unsigned int &operator()() const{ return data; }
	template<class T> operator T() const{ return data; }
	modular_unfixed_base &operator+=(const modular_unfixed_base &otr){ if((data += otr.data) >= _mod) data -= _mod; return *this; }
	modular_unfixed_base &operator-=(const modular_unfixed_base &otr){ if((data += _mod - otr.data) >= _mod) data -= _mod; return *this; }
	template<class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_unfixed_base &operator+=(const T &otr){ return *this += modular_unfixed_base(otr); }
	template<class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_unfixed_base &operator-=(const T &otr){ return *this -= modular_unfixed_base(otr); }
	modular_unfixed_base &operator++(){ return *this += 1; }
	modular_unfixed_base &operator--(){ return *this += _mod - 1; }
	modular_unfixed_base operator++(int){ modular_unfixed_base result(*this); *this += 1; return result; }
	modular_unfixed_base operator--(int){ modular_unfixed_base result(*this); *this += _mod - 1; return result; }
	modular_unfixed_base operator-() const{ return modular_unfixed_base(_mod - data); }
	modular_unfixed_base &operator*=(const modular_unfixed_base &rhs){
		data = normalize((unsigned long long)data * rhs.data);
		return *this;
	}
	template<class T, typename enable_if<is_integral<T>::value>::type* = nullptr>
	modular_unfixed_base &inplace_power(T e){
		if(!data) return *this = {};
		if(data == 1) return *this;
		if(data == mod() - 1) return e % 2 ? *this : *this = -*this;
		if(e < 0) *this = 1 / *this, e = -e;
		modular_unfixed_base res = 1;
		for(; e; *this *= *this, e >>= 1) if(e & 1) res *= *this;
		return *this = res;
	}
	template<class T, typename enable_if<is_integral<T>::value>::type* = nullptr>
	modular_unfixed_base power(T e) const{
		return modular_unfixed_base(*this).inplace_power(e);
	}
	modular_unfixed_base &operator/=(const modular_unfixed_base &otr){
		int a = otr.data, m = _mod, u = 0, v = 1;
		if(a < _INV.size()) return *this *= _INV[a];
		while(a){
			int t = m / a;
			m -= t * a; swap(a, m);
			u -= t * v; swap(u, v);
		}
		assert(m == 1);
		return *this *= u;
	}
	unsigned int data;
};
template<int id> unsigned int modular_unfixed_base<id>::_mod;
template<int id> unsigned long long modular_unfixed_base<id>::_inverse_mod;
template<int id> vector<modular_unfixed_base<id>> modular_unfixed_base<id>::_INV;
template<int id> modular_unfixed_base<id> modular_unfixed_base<id>::_primitive_root;
template<int id> bool operator==(const modular_unfixed_base<id> &lhs, const modular_unfixed_base<id> &rhs){ return lhs.data == rhs.data; }
template<int id, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> bool operator==(const modular_unfixed_base<id> &lhs, T rhs){ return lhs == modular_unfixed_base<id>(rhs); }
template<int id, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> bool operator==(T lhs, const modular_unfixed_base<id> &rhs){ return modular_unfixed_base<id>(lhs) == rhs; }
template<int id> bool operator!=(const modular_unfixed_base<id> &lhs, const modular_unfixed_base<id> &rhs){ return !(lhs == rhs); }
template<int id, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> bool operator!=(const modular_unfixed_base<id> &lhs, T rhs){ return !(lhs == rhs); }
template<int id, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> bool operator!=(T lhs, const modular_unfixed_base<id> &rhs){ return !(lhs == rhs); }
template<int id> bool operator<(const modular_unfixed_base<id> &lhs, const modular_unfixed_base<id> &rhs){ return lhs.data < rhs.data; }
template<int id> bool operator>(const modular_unfixed_base<id> &lhs, const modular_unfixed_base<id> &rhs){ return lhs.data > rhs.data; }
template<int id> bool operator<=(const modular_unfixed_base<id> &lhs, const modular_unfixed_base<id> &rhs){ return lhs.data <= rhs.data; }
template<int id> bool operator>=(const modular_unfixed_base<id> &lhs, const modular_unfixed_base<id> &rhs){ return lhs.data >= rhs.data; }
template<int id> modular_unfixed_base<id> operator+(const modular_unfixed_base<id> &lhs, const modular_unfixed_base<id> &rhs){ return modular_unfixed_base<id>(lhs) += rhs; }
template<int id, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_unfixed_base<id> operator+(const modular_unfixed_base<id> &lhs, T rhs){ return modular_unfixed_base<id>(lhs) += rhs; }
template<int id, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_unfixed_base<id> operator+(T lhs, const modular_unfixed_base<id> &rhs){ return modular_unfixed_base<id>(lhs) += rhs; }
template<int id> modular_unfixed_base<id> operator-(const modular_unfixed_base<id> &lhs, const modular_unfixed_base<id> &rhs){ return modular_unfixed_base<id>(lhs) -= rhs; }
template<int id, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_unfixed_base<id> operator-(const modular_unfixed_base<id> &lhs, T rhs){ return modular_unfixed_base<id>(lhs) -= rhs; }
template<int id, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_unfixed_base<id> operator-(T lhs, const modular_unfixed_base<id> &rhs){ return modular_unfixed_base<id>(lhs) -= rhs; }
template<int id> modular_unfixed_base<id> operator*(const modular_unfixed_base<id> &lhs, const modular_unfixed_base<id> &rhs){ return modular_unfixed_base<id>(lhs) *= rhs; }
template<int id, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_unfixed_base<id> operator*(const modular_unfixed_base<id> &lhs, T rhs){ return modular_unfixed_base<id>(lhs) *= rhs; }
template<int id, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_unfixed_base<id> operator*(T lhs, const modular_unfixed_base<id> &rhs){ return modular_unfixed_base<id>(lhs) *= rhs; }
template<int id> modular_unfixed_base<id> operator/(const modular_unfixed_base<id> &lhs, const modular_unfixed_base<id> &rhs) { return modular_unfixed_base<id>(lhs) /= rhs; }
template<int id, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_unfixed_base<id> operator/(const modular_unfixed_base<id> &lhs, T rhs) { return modular_unfixed_base<id>(lhs) /= rhs; }
template<int id, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_unfixed_base<id> operator/(T lhs, const modular_unfixed_base<id> &rhs) { return modular_unfixed_base<id>(lhs) /= rhs; }
template<int id> istream &operator>>(istream &in, modular_unfixed_base<id> &number){
	long long x;
	in >> x;
	number.data = modular_unfixed_base<id>::normalize(x);
	return in;
}
// #define _SHOW_FRACTION
template<int id> ostream &operator<<(ostream &out, const modular_unfixed_base<id> &number){
#if defined(LOCAL) && defined(_SHOW_FRACTION)
	out << number();
	cerr << "(";
	for(auto d = 1; ; ++ d){
		if((number * d).data <= 1000000){
			cerr << (number * d).data << "/" << d;
			break;
		}
		else if((-number * d).data <= 1000000){
			cerr << "-" << (-number * d).data << "/" << d;
			break;
		}
	}
	cerr << ")";
	return out;
#else
	return out << number();
#endif
}
#undef _SHOW_FRACTION

using modular = modular_unfixed_base<0>;

template<bool HAS_QUERY, bool HAS_UPDATE, class T, class U, class F1, class F2, class F3>
struct segment_tree_base{
#define ifQ if constexpr(HAS_QUERY)
#define ifU if constexpr(HAS_UPDATE)
	int n, size, log;
	vector<T> data;
	vector<U> data_action;
	F1 TT; // monoid operation (always adjacent)
	T T_id; // monoid identity
	F2 UU; // monoid operation (superset, subset)
	U U_id; // monoid identity
	F3 UT; // action of U on T (superset, subset)
	// O(n)
	segment_tree_base(F1 TT, T T_id, F2 UU, U U_id, F3 UT): TT(TT), T_id(T_id), UU(UU), U_id(U_id), UT(UT){ }
	segment_tree_base &operator=(const segment_tree_base &seg){
		n = seg.n;
		size = seg.size;
		log = seg.log;
		data = seg.data;
		data_action = seg.data_action;
	}
	// O(n)
	void build(int n){
		this->n = n;
		size = 1;
		while(size < n) size <<= 1;
		log = __lg(size);
		ifQ data.assign(size << 1, T_id);
		ifU data_action.assign(HAS_QUERY ? size : size << 1, U_id);
	}
	// O(n)
	void build(int n, T x){
		static_assert(HAS_QUERY);
		this->n = n;
		size = 1;
		while(size < n) size <<= 1;
		log = __lg(size);
		data.assign(size << 1, T_id);
		fill(data.begin() + size, data.begin() + size + n, x);
		for(auto i = size - 1; i >= 1; -- i) refresh(i);
		ifU data_action.assign(size, U_id);
	}
	// O(n)
	void build(const vector<T> &a){
		static_assert(HAS_QUERY);
		n = (int)a.size();
		size = 1;
		while(size < n) size <<= 1;
		log = __lg(size);
		data.assign(size << 1, T_id);
		copy(a.begin(), a.end(), data.begin() + size);
		for(auto i = size - 1; i >= 1; -- i) refresh(i);
		ifU data_action.assign(size, U_id);
	}
	// O(n)
	void build_action(int n){
		static_assert(!HAS_QUERY && HAS_UPDATE);
		build(n);
	}
	// O(n)
	void build_action(int n, U f){
		static_assert(!HAS_QUERY && HAS_UPDATE);
		this->n = n;
		size = 1;
		while(size < n) size <<= 1;
		log = __lg(size);
		data_action.assign(size << 1, U_id);
		fill(data_action.begin() + size, data_action.begin() + size + n, f);
	}
	// O(n)
	void build_action(const vector<U> &a){
		static_assert(!HAS_QUERY && HAS_UPDATE);
		n = (int)a.size();
		size = 1;
		while(size < n) size <<= 1;
		log = __lg(size);
		data_action.assign(size << 1, U_id);
		copy(a.begin(), a.end(), data_action.begin() + size);
	}
	// O(1)
	void refresh(int i){
		static_assert(HAS_QUERY);
		data[i] = TT(data[i << 1], data[i << 1 | 1]);
	}
	// O(1)
	void apply(int i, U f){
		static_assert(HAS_UPDATE);
		ifQ data[i] = UT(f, data[i]);
		if(!HAS_QUERY || i < size) data_action[i] = UU(f, data_action[i]);
	}
	// O(1)
	void push(int i){
		static_assert(HAS_UPDATE);
		apply(i << 1, data_action[i]), apply(i << 1 | 1, data_action[i]);
		data_action[i] = U_id;
	}
	// O(log(n)) if HAS_UPDATE, O(1) otherwise.
	T query(int p){
		static_assert(HAS_QUERY);
		assert(0 <= p && p < n);
		p += size;
		ifU for(auto i = log; i >= 1; -- i) push(p >> i);
		return data[p];
	}
	// O(log(n))
	U query_action(int p){
		static_assert(!HAS_QUERY && HAS_UPDATE);
		p += size;
		ifU for(auto i = log; i >= 1; -- i) push(p >> i);
		return data_action[p];
	}
	// O(log(n))
	T query(int l, int r){
		static_assert(HAS_QUERY);
		assert(0 <= l && l <= r && r <= n);
		if(l == r) return T_id;
		l += size, r += size;
		ifU for(auto i = log; i >= 1; -- i){
			if(l >> i << i != l) push(l >> i);
			if(r >> i << i != r) push(r - 1 >> i);
		}
		T res_left = T_id, res_right = T_id;
		for(; l < r; l >>= 1, r >>= 1){
			if(l & 1) res_left = TT(res_left, data[l ++]);
			if(r & 1) res_right = TT(data[-- r], res_right);
		}
		return TT(res_left, res_right);
	}
	// O(1)
	T query_all() const{
		static_assert(HAS_QUERY);
		return data[1];
	}
	// O(n)
	vector<T> to_array(){
		static_assert(HAS_QUERY);
		ifU for(auto i = 1; i < size; ++ i) push(i);
		return vector<T>(data.begin() + size, data.begin() + size + n);
	}
	// O(n)
	vector<U> to_array_of_updates(){
		static_assert(!HAS_QUERY && HAS_UPDATE);
		for(auto i = 1; i < size; ++ i) push(i);
		return vector<U>(data_action.begin() + size, data_action.begin() + size + n);
	}
	// O(log(n))
	void set(int p, T x){
		static_assert(HAS_QUERY);
		assert(0 <= p && p < n);
		p += size;
		ifU for(auto i = log; i >= 1; -- i) push(p >> i);
		data[p] = x;
		for(auto i = 1; i <= log; ++ i) refresh(p >> i);
	}
	// O(log(n))
	void set_action(int p, U f){
		static_assert(!HAS_QUERY && HAS_UPDATE);
		assert(0 <= p && p < n);
		p += size;
		for(auto i = log; i >= 1; -- i) push(p >> i);
		data_action[p] = f;
	}
	// O(log(n))
	void update(int p, U f){
		static_assert(HAS_UPDATE);
		assert(0 <= p && p < n);
		p += size;
		for(auto i = log; i >= 1; -- i) push(p >> i);
		ifQ{
			data[p] = UT(f, data[p]);
			for(auto i = 1; i <= log; ++ i) refresh(p >> i);
		}
		else data_action[p] = UU(f, data_action[p]);
	}
	// O(log(n))
	void update(int l, int r, U f){
		static_assert(HAS_UPDATE);
		assert(0 <= l && l <= r && r <= n);
		if(l == r) return;
		l += size, r += size;
		for(auto i = log; i >= 1; -- i){
			if(l >> i << i != l) push(l >> i);
			if(r >> i << i != r) push(r - 1 >> i);
		}
		int l2 = l, r2 = r;
		for(; l < r; l >>= 1, r >>= 1){
			if(l & 1) apply(l ++, f);
			if(r & 1) apply(-- r, f);
		}
		l = l2, r = r2;
		ifQ for(auto i = 1; i <= log; ++ i){
			if(l >> i << i != l) refresh(l >> i);
			if(r >> i << i != r) refresh(r - 1 >> i);
		}
	}
	// pred(sum[l, r)) is T, T, ..., T, F, F, ..., F
	// Returns max r with T
	// O(log(n))
	int max_pref(int l, auto pred){
		static_assert(HAS_QUERY);
		assert(0 <= l && l <= n && pred(T_id));
		if(l == n) return n;
		l += size;
		ifU for(auto i = log; i >= 1; -- i) push(l >> i);
		T sum = T_id;
		do{
			while(~l & 1) l >>= 1;
			if(!pred(TT(sum, data[l]))){
				while(l < size){
					ifU push(l);
					l = l << 1;
					if(pred(TT(sum, data[l]))) sum = TT(sum, data[l ++]);
				}
				return l - size;
			}
			sum = TT(sum, data[l]);
			++ l;
		}while((l & -l) != l);
		return n;
	}
	// pred(sum[l, r)) is F, F, ..., F, T, T, ..., T
	// Returns min l with T
	// O(log(n))
	int min_suff(int r, auto pred){
		static_assert(HAS_QUERY);
		assert(0 <= r && r <= n && pred(T_id));
		if(r == 0) return 0;
		r += size;
		ifU for(auto i = log; i >= 1; -- i) push(r - 1 >> i);
		T sum = T_id;
		do{
			-- r;
			while(r > 1 && r & 1) r >>= 1;
			if(!pred(TT(data[r], sum))){
				while(r < size){
					ifU push(r);
					r = r << 1 | 1;
					if(pred(TT(data[r], sum))) sum = TT(data[r --], sum);
				}
				return r + 1 - size;
			}
			sum = TT(data[r], sum);
		}while((r & -r) != r);
		return 0;
	}
	template<class output_stream>
	friend output_stream &operator<<(output_stream &out, segment_tree_base<HAS_QUERY, HAS_UPDATE, T, U, F1, F2, F3> seg){
		out << "{";
		for(auto i = 0; i < seg.n; ++ i){
			HAS_QUERY ? out << seg.query(i) : out << seg.query_action(i);
			if(i != seg.n - 1) out << ", ";
		}
		return out << '}';
	}
};

// Supports query
template<class T, class F>
auto make_Q_segment_tree(F TT, T T_id){
	using U = int;
	auto _UU = [&](U, U)->U{ return U{}; };
	auto _UT = [&](U, T)->T{ return T{}; };
	return segment_tree_base<true, false, T, U, F, decltype(_UU), decltype(_UT)>(TT, T_id, _UU, U{}, _UT);
}
// Supports update
template<class U, class F>
auto make_U_segment_tree(F UU, U U_id){
	using T = int;
	auto _TT = [&](T, T)->T{ return T{}; };
	auto _UT = [&](U, T)->T{ return T{}; };
	return segment_tree_base<false, true, T, U, decltype(_TT), F, decltype(_UT)>(_TT, T{}, UU, U_id, _UT);
}
// Supports query and update
template<class T, class U, class F1, class F2, class F3>
auto make_Q_segment_tree(F1 TT, T T_id, F2 UU, U U_id, F3 UT){
	return segment_tree_base<true, true, T, U, F1, F2, F3>(TT, T_id, UU, U_id, UT);
}

int main(){
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(ios::badbit | ios::failbit);
	int n, qn;
	cin >> n;
	modular::setup();
	cin >> qn;
	auto seg = make_U_segment_tree(plus<>(), 0);
	seg.build_action(n);
	vector<modular> x(qn + 1), y(qn + 1), z(qn + 1);
	iota(x.begin(), x.end(), 1);
	z = modular::precalc_power(3, qn);
	y[0] = 1;
	for(auto i = 1; i <= qn; ++ i){
		y[i] = 3 * y[i - 1] + 2 * x[i] * z[i - 1];
	}
	for(auto qi = 0; qi < qn; ++ qi){
		int l, p, r;
		cin >> l >> p >> r, -- l, -- p;
		seg.update(l, r, 1);
		int cnt = seg.query_action(p);
		cout << x[cnt] << " " << y[cnt] << " " << z[cnt] << "\n";
	}
	return 0;
}

/*

*/

////////////////////////////////////////////////////////////////////////////////////////
//                                                                                    //
//                                   Coded by Aeren                                   //
//                                                                                    //
////////////////////////////////////////////////////////////////////////////////////////
0