QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#336278#8279. Segment Treeucup-team3099#WA 0ms3920kbC++2332.1kb2024-02-24 14:26:262024-02-24 14:26:27

Judging History

你现在查看的是最新测评结果

  • [2024-02-24 14:26:27]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3920kb
  • [2024-02-24 14:26:26]
  • 提交

answer

// #pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
// #include <x86intrin.h>
using namespace std;
#if __cplusplus >= 202002L
using namespace numbers;
#endif

template<class data_t, data_t _mod>
struct modular_fixed_base{
#define IS_INTEGRAL(T) (is_integral_v<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
#define IS_UNSIGNED(T) (is_unsigned_v<T> || is_same_v<T, __uint128_t>)
	static_assert(IS_UNSIGNED(data_t));
	static_assert(_mod >= 1);
	static constexpr bool VARIATE_MOD_FLAG = false;
	static constexpr data_t mod(){
		return _mod;
	}
	template<class T>
	static vector<modular_fixed_base> precalc_power(T base, int SZ){
		vector<modular_fixed_base> res(SZ + 1, 1);
		for(auto i = 1; i <= SZ; ++ i) res[i] = res[i - 1] * base;
		return res;
	}	
	static vector<modular_fixed_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_fixed_base _primitive_root;
	static modular_fixed_base primitive_root(){
		if(_primitive_root) return _primitive_root;
		if(_mod == 2) return _primitive_root = 1;
		if(_mod == 998244353) return _primitive_root = 3;
		data_t divs[20] = {};
		divs[0] = 2;
		int cnt = 1;
		data_t 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_fixed_base(g).power((_mod - 1) / divs[i])) == 1){
					ok = false;
					break;
				}
			}
			if(ok) return _primitive_root = g;
		}
	}
	constexpr modular_fixed_base(){ }
	modular_fixed_base(const double &x){ data = _normalize(llround(x)); }
	modular_fixed_base(const long double &x){ data = _normalize(llround(x)); }
	template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> modular_fixed_base(const T &x){ data = _normalize(x); }
	template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> static data_t _normalize(const T &x){
		int sign = x >= 0 ? 1 : -1;
		data_t v =  _mod <= sign * x ? sign * x % _mod : sign * x;
		if(sign == -1 && v) v = _mod - v;
		return v;
	}
	template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> operator T() const{ return data; }
	modular_fixed_base &operator+=(const modular_fixed_base &otr){ if((data += otr.data) >= _mod) data -= _mod; return *this; }
	modular_fixed_base &operator-=(const modular_fixed_base &otr){ if((data += _mod - otr.data) >= _mod) data -= _mod; return *this; }
	template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> modular_fixed_base &operator+=(const T &otr){ return *this += modular_fixed_base(otr); }
	template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> modular_fixed_base &operator-=(const T &otr){ return *this -= modular_fixed_base(otr); }
	modular_fixed_base &operator++(){ return *this += 1; }
	modular_fixed_base &operator--(){ return *this += _mod - 1; }
	modular_fixed_base operator++(int){ modular_fixed_base result(*this); *this += 1; return result; }
	modular_fixed_base operator--(int){ modular_fixed_base result(*this); *this += _mod - 1; return result; }
	modular_fixed_base operator-() const{ return modular_fixed_base(_mod - data); }
	modular_fixed_base &operator*=(const modular_fixed_base &rhs){
		if constexpr(is_same_v<data_t, unsigned int>) data = (unsigned long long)data * rhs.data % _mod;
		else if constexpr(is_same_v<data_t, unsigned long long>){
			long long res = data * rhs.data - _mod * (unsigned long long)(1.L / _mod * data * rhs.data);
			data = res + _mod * (res < 0) - _mod * (res >= (long long)_mod);
		}
		else data = _normalize(data * rhs.data);
		return *this;
	}
	template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>
	modular_fixed_base &inplace_power(T e){
		if(e == 0) return *this = 1;
		if(data == 0) 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_fixed_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)>::type* = nullptr>
	modular_fixed_base power(T e) const{
		return modular_fixed_base(*this).inplace_power(e);
	}
	modular_fixed_base &operator/=(const modular_fixed_base &otr){
		make_signed_t<data_t> a = otr.data, m = _mod, u = 0, v = 1;
		if(a < _INV.size()) return *this *= _INV[a];
		while(a){
			make_signed_t<data_t> t = m / a;
			m -= t * a; swap(a, m);
			u -= t * v; swap(u, v);
		}
		assert(m == 1);
		return *this *= u;
	}
#define ARITHMETIC_OP(op, apply_op)\
modular_fixed_base operator op(const modular_fixed_base &x) const{ return modular_fixed_base(*this) apply_op x; }\
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>\
modular_fixed_base operator op(const T &x) const{ return modular_fixed_base(*this) apply_op modular_fixed_base(x); }\
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>\
friend modular_fixed_base operator op(const T &x, const modular_fixed_base &y){ return modular_fixed_base(x) apply_op y; }
	ARITHMETIC_OP(+, +=) ARITHMETIC_OP(-, -=) ARITHMETIC_OP(*, *=) ARITHMETIC_OP(/, /=)
#undef ARITHMETIC_OP
#define COMPARE_OP(op)\
bool operator op(const modular_fixed_base &x) const{ return data op x.data; }\
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>\
bool operator op(const T &x) const{ return data op modular_fixed_base(x).data; }\
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>\
friend bool operator op(const T &x, const modular_fixed_base &y){ return modular_fixed_base(x).data op y.data; }
	COMPARE_OP(==) COMPARE_OP(!=) COMPARE_OP(<) COMPARE_OP(<=) COMPARE_OP(>) COMPARE_OP(>=)
#undef COMPARE_OP
	friend istream &operator>>(istream &in, modular_fixed_base &number){
		long long x;
		in >> x;
		number.data = modular_fixed_base::_normalize(x);
		return in;
	}
//#define _SHOW_FRACTION
	friend ostream &operator<<(ostream &out, const modular_fixed_base &number){
		out << number.data;
	#if defined(LOCAL) && defined(_SHOW_FRACTION)
		cerr << "(";
		for(auto d = 1; ; ++ d){
			if((number * d).data <= 1000000){
				cerr << (number * d).data;
				if(d != 1) cerr << "/" << d;
				break;
			}
			else if((-number * d).data <= 1000000){
				cerr << "-" << (-number * d).data;
				if(d != 1) cerr << "/" << d;
				break;
			}
		}
		cerr << ")";
	#endif
		return out;
	}
	data_t data = 0;
#undef _SHOW_FRACTION
#undef IS_INTEGRAL
#undef IS_SIGNED
};
template<class data_t, data_t _mod> vector<modular_fixed_base<data_t, _mod>> modular_fixed_base<data_t, _mod>::_INV;
template<class data_t, data_t _mod> modular_fixed_base<data_t, _mod> modular_fixed_base<data_t, _mod>::_primitive_root;

const unsigned int mod = (119 << 23) + 1; // 998244353
// const unsigned int mod = 1e9 + 7; // 1000000007
// const unsigned int mod = 1e9 + 9; // 1000000009
// const unsigned long long mod = (unsigned long long)1e18 + 9;
using modular = modular_fixed_base<decay_t<decltype(mod)>, mod>;

template<class F>
struct y_combinator_result{
	F f;
	template<class T> explicit y_combinator_result(T &&f): f(forward<T>(f)){ }
	template<class ...Args> decltype(auto) operator()(Args &&...args){ return f(ref(*this), forward<Args>(args)...); }
};
template<class F>
decltype(auto) y_combinator(F &&f){
	return y_combinator_result<decay_t<F>>(forward<F>(f));
}

template<class T>
struct graph{
	using Weight_t = T;
	struct Edge_t{
		int from, to;
		T cost;
	};
	int n;
	vector<Edge_t> edge;
	vector<vector<int>> adj;
	function<bool(int)> ignore;
	graph(int n = 1): n(n), adj(n){
		assert(n >= 1);
	}
	graph(const vector<vector<int>> &adj, bool undirected = true): n((int)adj.size()), adj(n){
		assert(n >= 1);
		if(undirected){
			for(auto u = 0; u < n; ++ u) for(auto v: adj[u]) if(u < v) link(u, v);
		}
		else for(auto u = 0; u < n; ++ u) for(auto v: adj[u]) orient(u, v);
	}
	graph(const vector<vector<pair<int, T>>> &adj, bool undirected = true): n((int)adj.size()), adj(n){
		assert(n >= 1);
		if(undirected){
			for(auto u = 0; u < n; ++ u) for(auto [v, w]: adj[u]) if(u < v) link(u, v, w);
		}
		else for(auto u = 0; u < n; ++ u) for(auto [v, w]: adj[u]) orient(u, v, w);
	}
	graph(int n, vector<array<int, 2>> &edge, bool undirected = true): n(n), adj(n){
		assert(n >= 1);
		for(auto [u, v]: edge) undirected ? link(u, v) : orient(u, v);
	}
	graph(int n, vector<tuple<int, int, T>> &edge, bool undirected = true): n(n), adj(n){
		assert(n >= 1);
		for(auto [u, v, w]: edge) undirected ? link(u, v, w) : orient(u, v, w);
	}
	int operator()(int u, int id) const{
		#ifdef LOCAL
		assert(0 <= id && id < (int)edge.size());
		assert(edge[id].from == u || edge[id].to == u);
		#endif
		return u ^ edge[id].from ^ edge[id].to;
	}
	int link(int u, int v, T w = {}){ // insert an undirected edge
		int id = (int)edge.size();
		adj[u].push_back(id), adj[v].push_back(id), edge.push_back({u, v, w});
		return id;
	}
	int orient(int u, int v, T w = {}){ // insert a directed edge
		int id = (int)edge.size();
		adj[u].push_back(id), edge.push_back({u, v, w});
		return id;
	}
	void clear(){
		for(auto [u, v, w]: edge){
			adj[u].clear();
			adj[v].clear();
		}
		edge.clear();
		ignore = {};
	}
	graph transposed() const{ // the transpose of the directed graph
		graph res(n);
		for(auto &e: edge) res.orient(e.to, e.from, e.cost);
		res.ignore = ignore;
		return res;
	}
	int degree(int u) const{ // the degree (outdegree if directed) of u (without the ignoration rule)
		return (int)adj[u].size();
	}
	// The adjacency list is sorted for each vertex.
	vector<vector<int>> get_adjacency_list() const{
		vector<vector<int>> res(n);
		for(auto u = 0; u < n; ++ u) for(auto id: adj[u]){
			if(ignore && ignore(id)) continue;
			res[(*this)(u, id)].push_back(u);
		}
		return res;
	}
	void set_ignoration_rule(const function<bool(int)> &f){
		ignore = f;
	}
	void reset_ignoration_rule(){
		ignore = nullptr;
	}
	friend ostream &operator<<(ostream &out, const graph &g){
		for(auto id = 0; id < (int)g.edge.size(); ++ id){
			if(g.ignore && g.ignore(id)) continue;
			auto &e = g.edge[id];
			out << "{" << e.from << ", " << e.to << ", " << e.cost << "}\n";
		}
		return out;
	}
};

// Requires graph
template<bool ENABLE_LCA_SOLVER, bool ENABLE_LEVEL_ANCESTOR_SOLVER>
struct forest_query_solver_base{
	static_assert(ENABLE_LCA_SOLVER || ENABLE_LEVEL_ANCESTOR_SOLVER);
#ifdef LOCAL
	#define ASSERT(c) assert(c)
#else
	#define ASSERT(c) 42
#endif
#define ifLCA if constexpr(ENABLE_LCA_SOLVER)
#define ifLA if constexpr(ENABLE_LEVEL_ANCESTOR_SOLVER)
	int n;
	// For LCA Solver
	vector<int> label;
	vector<int> ascendant;
	vector<int> head;
	// For LA Solver
	static constexpr int kappa = 4;
	static constexpr int kappa_prime = (3 * kappa - 1) / (kappa - 2);
	vector<array<int, 3>> stack;
	vector<int> valley;
	vector<int> valley_cnt;
	vector<int> right;
	vector<int> jump;
	vector<vector<int>> ladder;
	// Common
	vector<int> order;
	vector<int> pos;
	vector<int> end;
	vector<int> root_of;
	vector<int> depth;
	vector<int> was;
	void init(int n){
		assert(n >= 1);
		this->n = n;
		ifLCA{
			label.assign(n, -1);
			ascendant.assign(n, -1);
			head.assign(n + 1, -1);
		}
		ifLA{
			stack.assign(2 * n, {});
			valley.assign(2 * n, -1);
			valley_cnt.assign(2 * n - 1, -1);
			right.assign(n + 1, -1);
			jump.assign(2 * n - 1, -1);
			ladder.assign(2 * n - 1, {});
		}
		order.clear();
		pos.assign(n, -1);
		end.assign(n, -1);
		root_of.assign(n, -1);
		depth.assign(n, -1);
		was.assign(n, -2);
		attempt = -1;
	}
	int attempt;
	// O(n)
	template<class T>
	void build(const graph<T> &g, const vector<int> &src){
		assert(g.n <= n);
		++ attempt;
		order.clear();
		int timer = 1;
		auto dfs = [&](auto self, int u, int pe)->void{
			assert(was[u] != attempt);
			was[u] = attempt;
			pos[u] = (int)order.size();
			order.push_back(u);
			ifLCA label[u] = timer ++;
			for(auto id: g.adj[u]){
				if(id == pe || g.ignore && g.ignore(id)) continue;
				int v = g(u, id);
				root_of[v] = root_of[u];
				depth[v] = depth[u] + 1;
				self(self, v, id);
				ifLCA if(__builtin_ctz(label[u]) < __builtin_ctz(label[v])) label[u] = label[v];
				order.push_back(u);
			}
			end[u] = (int)order.size();
		};
		for(auto r: src){
			if(was[r] == attempt) continue;
			depth[r] = 0;
			root_of[r] = r;
			dfs(dfs, r, -1);
		}
		ifLCA for(auto i = 0; i < (int)order.size(); ++ i){
			int u = order[i];
			if(pos[u] != i) continue;
			if(root_of[u] == u) ascendant[u] = label[u];
			for(auto id: g.adj[u]){
				if(g.ignore && g.ignore(id)) continue;
				int v = g(u, id);
				if(pos[v] < pos[u] || end[u] < end[v]) continue;
				ascendant[v] = ascendant[u];
				if(label[v] != label[u]){
					head[label[v]] = u;
					ascendant[v] += label[v] & -label[v];
				}
			}
		}
		ifLA{
			int si = 0, ymin = numeric_limits<int>::max(), ymax = numeric_limits<int>::min();
			stack[si ++] = {0, numeric_limits<int>::min(), numeric_limits<int>::max()};
			for(auto i = 0; i < (int)order.size(); ++ i){
				int u = order[i], y = n - 1 - depth[u];
				valley_cnt[i] = 0;
				ymin = min(ymin, y);
				ymax = max(ymax, y);
				while(stack[si - 1][1] >= y) -- si;
				if(stack[si - 1][2] >= y){
					valley[i] = i;
					if(stack[si - 1][2] > y) stack[si ++] = {i, y, y};
				}
				else{
					while(stack[si - 2][2] < y) -- si;
					valley[i] = stack[si - 1][0];
					if(stack[si - 2][2] > y) stack[si - 1][2] = y;
					else -- si;
				}
			}
			valley[(int)order.size()] = (int)order.size() - 1;
			for(auto i = 0; i < (int)order.size(); ++ i) ++ valley_cnt[valley[i]];
			for(auto y = ymin; y <= ymax + 1; ++ y) right[y] = (int)order.size();
			for(auto i = (int)order.size() - 1; i >= 0; -- i){
				int u = order[i], y = n - 1 - depth[u];
				right[y] = i;
				int h = ymax - y;
				if(0 < i && i < (int)order.size() - 2) h = min(h, max(kappa - 1, kappa_prime * (valley_cnt[i] - 1) - 2));
				ladder[i].resize(h);
				for(auto yi = 0; yi < h; ++ yi) ladder[i][yi] = right[y + yi + 1];
				jump[i] = i ? valley[right[min(ymax + 1, y + (kappa - 2 << __builtin_ctz(i)))]] : 0;
			}
		}
	}
	template<class T>
	void build_all(const graph<T> &g){
		vector<int> src(g.n);
		iota(src.begin(), src.end(), 0);
		build(g, src);
	}
	// Check if u is visited during the last build call
	bool visited(int u) const{
		ASSERT(0 <= u && u < n);
		return was[u] == attempt;
	}
	// O(1)
	bool ancestor_of(int u, int v) const{
		#ifdef LOCAL
		ASSERT(visited(u) && visited(v));
		#endif
		return pos[u] <= pos[v] && end[v] <= end[u];
	}
	// Assumes u and v are on the same component
	// O(1)
	int lca(int u, int v) const{
		static_assert(ENABLE_LCA_SOLVER);
		ASSERT(visited(u) && visited(v) && root_of[u] == root_of[v]);
		auto [x, y] = minmax(label[u], label[v]);
		int k = ascendant[u] & ascendant[v] & -(1 << __lg(x - 1 ^ y));
		if(ascendant[u] != k){
			int t = 1 << __lg(ascendant[u] ^ k);
			u = head[label[u] & -t | t];
		}
		if(ascendant[v] != k){
			int t = 1 << __lg(ascendant[v] ^ k);
			v = head[label[v] & -t | t];
		}
		return depth[u] < depth[v] ? u : v;
	}
	// Assumes u and v are on the same component
	// O(1)
	int steps(int u, int v, int w = -1) const{
		static_assert(ENABLE_LCA_SOLVER);
		ASSERT(visited(u) && visited(v) && root_of[u] == root_of[v]);
		return -2 * depth[~w ? w : lca(u, v)] + depth[u] + depth[v];
	}
	// Check if w lies in u-v path
	// O(1)
	bool on_path(int u, int v, int w) const{
		static_assert(ENABLE_LCA_SOLVER);
		ASSERT(visited(u) && visited(v) && visited(w) && root_of[u] == root_of[v] && root_of[v] == root_of[w]);
		return steps(u, v) == steps(u, w) + steps(w, v);
	}
	// Check if u-v path and w-x path intersect, and find their interseciton if they intersect
	// O(1)
	optional<pair<int, int>> intersect_path(int u, int v, int w, int x) const{
		static_assert(ENABLE_LCA_SOLVER);
		ASSERT(visited(u) && visited(v) && visited(w) && visited(x) && root_of[u] == root_of[v] && root_of[v] == root_of[w] && root_of[w] == root_of[x]);
		int optl = -1, optr = -1;
		for(auto y: {lca(u, w), lca(u, x), lca(v, w), lca(v, x)}){
			if(!on_path(u, v, y) || !on_path(w, x, y)) continue;
			if(!~optl) optl = optr = y;
			else if(depth[optl] < depth[y]) optr = optl, optl = y;
			else if(depth[optr] < depth[y]) optr = y;
		}
		if(!~optl) return {};
		return pair{optl, optr};
	}
	// Get the k-th ancestor of u
	// O(1)
	int find_ancestor_by_order(int u, int k) const{
		static_assert(ENABLE_LEVEL_ANCESTOR_SOLVER);
		ASSERT(visited(u) && 0 <= k && k <= depth[u]);
		if(k == 0) return u;
		if(k < kappa) return order[ladder[pos[u]][k - 1]];
		int p = __lg(k / kappa), x = pos[u] >> p << p;
		if(x > 0 && (x & (1 << p + 1) - 1) == 0) x -= 1 << p;
		return order[ladder[jump[x]][k + depth[order[jump[x]]] - depth[u] - 1]];
	}
	// Get the k-th vertex in the u-v path
	// Assumes u and v are on the same component
	// O(1)
	int find_vertex_by_order(int u, int v, int k) const{
		static_assert(ENABLE_LCA_SOLVER && ENABLE_LEVEL_ANCESTOR_SOLVER);
		ASSERT(visited(u) && visited(v) && root_of[u] == root_of[v] && 0 <= k);
		if(k == 0) return u;
		int w = lca(u, v);
		if(k <= depth[u] - depth[w]) return find_ancestor_by_order(u, k);
		else return find_ancestor_by_order(v, depth[u] + depth[v] - 2 * depth[w] - k);
	}
	// For an ancestor p of u, pred(p) is T, ..., T, F, ..., F in decreasing order of depth
	// Returns the furthest p with T
	// O(log(n))
	int find_furthest_ancestor(int u, auto pred) const{
		static_assert(ENABLE_LEVEL_ANCESTOR_SOLVER);
		ASSERT(visited(u) && pred(u));
		if(root_of[u] == u) return u;
		for(auto bit = __lg(depth[u]); bit >= 0; -- bit) if(1 << bit <= depth[u]){
			int v = find_ancestor_by_order(u, 1 << bit);
			if(pred(v)) u = v;
		}
		return u;
	}
	// For a vertex w in u-v path, pred(w) is T, ..., T, F, ..., F in order from u to v
	// Returns the furthest w with T
	// O(log(n))
	int find_furthest_vertex(int u, int v, auto pred) const{
		static_assert(ENABLE_LCA_SOLVER && ENABLE_LEVEL_ANCESTOR_SOLVER);
		ASSERT(visited(u) && visited(v) && root_of[u] == root_of[v] && pred(u));
		if(pred(v)) return v;
		int w = lca(u, v);
		if(!pred(w)){
			for(auto bit = __lg(depth[u] - depth[w]); bit >= 0; -- bit) if(1 << bit <= depth[u] - depth[w]){
				int v = find_ancestor_by_order(u, 1 << bit);
				if(pred(v)) u = v;
			}
			return u;
		}
		else{
			for(auto bit = __lg(depth[v] - depth[w]); bit >= 0; -- bit) if(1 << bit <= depth[v] - depth[w]){
				int u = find_ancestor_by_order(v, 1 << bit);
				if(!pred(u)) v = u;
			}
			return order[pos[v] - 1];
		}
	}
#undef ASSERT
#undef ifLCA
#undef ifLA
};

// Source: https://github.com/programming-team-code/programming_team_code/blob/main/graphs/linear_lca/linear_lca.hpp
auto make_lca_solver(){
	return forest_query_solver_base<true, false>();
}
// Source: Still Simpler Static Level Ancestors
auto make_la_solver(){
	return forest_query_solver_base<false, true>();
}
auto make_forest_query_solver(){
	return forest_query_solver_base<true, true>();
}

struct succinct_dictionary{
	static constexpr unsigned int wsize = 64;
	static unsigned int rank64(unsigned long long x, unsigned int i){
		return __builtin_popcountll(x & ((1ULL << i) - 1));
	}
#pragma pack(4)
	struct block_t{
		unsigned long long bit;
		unsigned int sum;
	};
#pragma pack()
	unsigned int n, zeros;
	vector<block_t> block;
	succinct_dictionary(unsigned int n = 0) : n(n), block(n / wsize + 1){}
	// O(1)
	int operator[](unsigned int i) const{
		return block[i / wsize].bit >> i % wsize & 1;
	}
	// O(1)
	void set(unsigned int i){
		block[i / wsize].bit |= 1ULL << i % wsize;
	}
	// O(n/w)
	void build(){
		for(auto i = 0; i < n / wsize; ++ i) block[i + 1].sum = block[i].sum + __builtin_popcountll(block[i].bit);
		zeros = rank0(n);
	}
	// O(1)
	unsigned int rank0(unsigned int i) const{
		return i - rank1(i);
	}
	// O(1)
	unsigned int rank1(unsigned int i) const{
		auto &&e = block[i / wsize];
		return e.sum + rank64(e.bit, i % wsize);
	}
	// O(log(n))
	unsigned int select0(unsigned int k) const{
		unsigned int low = 0, high = n;
		while(high - low >= 2){
			unsigned int mid = low + high >> 1;
			(rank0(mid) <= k ? low : high) = mid;
		}
		return low;
	}
	// O(log(n))
	unsigned int select1(unsigned int k) const{
		unsigned int low = 0, high = n;
		while(high - low >= 2){
			unsigned int mid = low + high >> 1;
			(rank1(mid) <= k ? low : high) = mid;
		}
		return low;
	}
};

// nor orz
// Requires succinct_dictionary
template<bool HAS_QUERY, class B, class T, class F, class I>
struct wavelet_matrix_base{
	int n, lg;
	B sigma;
	vector<succinct_dictionary> data;
	vector<vector<T>> aggregate;
	F TT; // commutative group operation
	T T_id; // commutative group identity
	I Tinv; // commutative group inverse
	wavelet_matrix_base(F TT, T T_id, I Tinv): TT(TT), T_id(T_id), Tinv(Tinv){ }
	wavelet_matrix_base &operator=(const wavelet_matrix_base &wm){
		n = wm.n;
		lg = wm.lg;
		sigma = wm.sigma;
		data = wm.data;
		return *this;
	}
	// O(n * log(sigma)) time and O(n * log(sigma) / w) memory
	void build(const vector<B> &key, B sigma){
		static_assert(!HAS_QUERY);
		assert(sigma > 0);
		for(auto x: key) assert(0 <= x && x < sigma);
		n = (int)key.size();
		this->sigma = sigma;
		lg = __lg(sigma) + (B(1) << lg != sigma) + 1;
		data.assign(lg, succinct_dictionary(n));
		vector<B> cur = key, next(n);
		for(auto h = lg; h --;){
			for(auto i = 0; i < n; ++ i) if(cur[i] >> h & 1) data[h].set(i);
			data[h].build();
			array it{next.begin(), next.begin() + data[h].zeros};
			for(auto i = 0; i < n; ++ i) *it[data[h][i]] ++ = cur[i];
			swap(cur, next);
		}
	}
	// O(n * log(sigma)) time and O(n * log(sigma)) memory
	template<class U>
	void build(const vector<B> &key, B sigma, const vector<U> &value){
		static_assert(HAS_QUERY);
		assert(sigma > 0);
		for(auto x: key) assert(0 <= x && x < sigma);
		n = (int)key.size();
		this->sigma = sigma;
		lg = __lg(sigma) + (B(1) << lg != sigma) + 1;
		data.assign(lg, succinct_dictionary(n));
		aggregate.assign(lg + 1, vector<T>(n + 1, T_id));
		vector<pair<B, T>> cur(n), next(n);
		for(auto i = 0; i < n; ++ i) cur[i] = {key[i], value[i]};
		for(auto h = lg; h --;){
			for(auto i = 0; i < n; ++ i) if(cur[i].first >> h & 1) data[h].set(i);
			data[h].build();
			array it{next.begin(), next.begin() + data[h].zeros};
			for(auto i = 0; i < n; ++ i){
				*it[data[h][i]] ++ = cur[i];
				aggregate[h + 1][i + 1] = data[h][i] ? aggregate[h + 1][i] : TT(aggregate[h + 1][i], cur[i].second);
			}
			swap(cur, next);
		}
		for(auto i = 0; i < n; ++ i) aggregate[0][i + 1] = TT(aggregate[0][i], cur[i].second);
	}
	// Returns the frequency of x in the interval [ql, qr)
	// O(log(sigma))
	int freq(int ql, int qr, int x) const{
		assert(0 <= ql && ql <= qr && qr <= n);
		assert(0 <= x);
		if(ql == qr || sigma <= x) return 0;
		for(auto h = lg; h --; ){
			auto lcnt = data[h].rank0(ql), rcnt = data[h].rank0(qr);
			if(~x >> h & 1) ql = lcnt, qr = rcnt;
			else ql += data[h].zeros - lcnt, qr += data[h].zeros - rcnt;
		}
		return qr - ql;
	}
	// Returns the frequency of x in the interval [ql, qr), along with the sum of their values
	// O(log(sigma))
	pair<int, T> freq_query(int ql, int qr, int x) const{
		static_assert(HAS_QUERY);
		assert(0 <= ql && ql <= qr && qr <= n);
		assert(0 <= x);
		if(ql == qr || sigma <= x) return {0, T_id};
		for(auto h = lg; h --; ){
			auto lcnt = data[h].rank0(ql), rcnt = data[h].rank0(qr);
			if(~x >> h & 1) ql = lcnt, qr = rcnt;
			else ql += data[h].zeros - lcnt, qr += data[h].zeros - rcnt;
		}
		return {qr - ql, TT(aggregate[0][qr], Tinv(aggregate[0][ql]))};
	}
	// Returns the number of occurrences of numbers in [0, xr) in the interval [ql, qr)
	// O(log(sigma))
	int count(int ql, int qr, B xr) const{
		assert(0 <= ql && ql <= qr && qr <= n);
		assert(0 <= xr);
		if(sigma <= xr) return qr - ql;
		if(xr == 0) return 0;
		int cnt = 0;
		for(auto h = lg; h --; ){
			auto lcnt = data[h].rank0(ql), rcnt = data[h].rank0(qr);
			if(~xr >> h & 1) ql = lcnt, qr = rcnt;
			else{
				cnt += rcnt - lcnt;
				ql += data[h].zeros - lcnt, qr += data[h].zeros - rcnt;
			}
		}
		return cnt;
	}
	// Returns the number of occurrences of numbers in [0, xr) in the interval [ql, qr), along with the sum of their values
	// O(log(sigma))
	pair<int, T> count_query(int ql, int qr, B xr) const{
		static_assert(HAS_QUERY);
		assert(0 <= ql && ql <= qr && qr <= n);
		assert(0 <= xr);
		if(xr == 0) return {0, T_id};
		xr = min(sigma, xr);
		int cnt = 0;
		T sum = T_id;
		for(auto h = lg; h --; ){
			auto lcnt = data[h].rank0(ql), rcnt = data[h].rank0(qr);
			if(~xr >> h & 1) ql = lcnt, qr = rcnt;
			else{
				cnt += rcnt - lcnt;
				sum = TT(sum, TT(aggregate[h + 1][qr], Tinv(aggregate[h + 1][ql])));
				ql += data[h].zeros - lcnt, qr += data[h].zeros - rcnt;
			}
		}
		return {cnt, sum};
	}
	// Returns the number of occurrences of numbers in [xl, xr) in the interval [ql, qr)
	// O(log(sigma))
	int count(int ql, int qr, B xl, B xr) const{
		assert(xl <= xr);
		if(xl == xr) return 0;
		return count(ql, qr, xr) - count(ql, qr, xl);
	}
	// Returns the number of occurrences of numbers in [xl, xr) in the interval [ql, qr), along with the sum of their values
	// O(log(sigma))
	pair<int, T> count_query(int ql, int qr, B xl, B xr) const{
		static_assert(HAS_QUERY);
		assert(xl <= xr);
		if(xl == xr) return {0, T_id};
		auto [lcnt, lsum] = count_query(ql, qr, xl);
		auto [rcnt, rsum] = count_query(ql, qr, xr);
		return {rcnt - lcnt, TT(rsum, Tinv(lsum))};
	}
	// Find the k-th smallest element in the interval [ql, qr), sigma if no such element
	// O(log(sigma))
	B find_by_order(int ql, int qr, int k) const{
		assert(0 <= k);
		if(k >= qr - ql) return sigma;
		B x = 0;
		for(auto h = lg; h --; ){
			auto lcnt = data[h].rank0(ql), rcnt = data[h].rank0(qr);
			if(k < rcnt - lcnt) ql = lcnt, qr = rcnt;
			else {
				k -= rcnt - lcnt;
				x |= (B)1 << h;
				ql += data[h].zeros - lcnt;
				qr += data[h].zeros - rcnt;
			}
		}
		return x;
	}
	// Find the k-th smallest element in the interval [ql, qr), sigma if no such element, along with the sum of values of the k smallest elements (prioritizing smaller index)
	// O(log(sigma))
	pair<B, T> find_by_order_query(int ql, int qr, int k) const{
		assert(0 <= k);
		k = min(k, qr - ql);
		B x = 0;
		T sum = T_id;
		for(auto h = lg; h --; ){
			auto lcnt = data[h].rank0(ql), rcnt = data[h].rank0(qr);
			if(k < rcnt - lcnt) ql = lcnt, qr = rcnt;
			else {
				k -= rcnt - lcnt;
				x |= (B)1 << h;
				sum = TT(sum, TT(aggregate[h + 1][qr], Tinv(aggregate[h + 1][ql])));
				ql += data[h].zeros - lcnt;
				qr += data[h].zeros - rcnt;
			}
		}
		return {x, TT(sum, TT(aggregate[0][ql + k], Tinv(aggregate[0][ql])))};
	}
	// Find the k-th smallest element in the interval [ql, qr) among elements >= xl, sigma if no such element
	// O(log(sigma))
	B find_by_order(int ql, int qr, B xl, int k) const{
		assert(0 <= ql && ql <= qr && qr <= n);
		assert(0 <= xl && 0 <= k);
		if(xl >= sigma) return sigma;
		k += count(ql, qr, 0, xl);
		if(k >= qr - ql) return sigma;
		return find_by_order(ql, qr, k);
	}
	// Find the k-th smallest element in the interval [ql, qr) among elements >= xl, sigma if no such element, along with the sum of values of the k smallest elements (prioritizing smaller index)
	// O(log(sigma))
	pair<B, T> find_by_order_query(int ql, int qr, B xl, int k) const{
		assert(0 <= ql && ql <= qr && qr <= n);
		assert(0 <= xl && 0 <= k);
		if(xl >= sigma) return {sigma, T_id};
		auto [cnt, sum] = count_query(ql, qr, 0, xl);
		k += cnt;
		auto [x, sum2] = find_by_order_query(ql, qr, k);
		return {x, TT(sum2, Tinv(sum))};
	}
	// Find the smallest element >= x, sigma if no such element
	// O(log(sigma))
	B lower_bound(int ql, int qr, B x) const{
		assert(0 <= x);
		return find_by_order(ql, qr, x, 0);
	}
	// Find the smallest element > x, sigma if no such element
	// O(log(sigma))
	B upper_bound(int ql, int qr, B x) const{
		assert(0 <= x);
		return find_by_order(ql, qr, x + 1, 0);
	}
	// Find the largest element <= x, -1 if no such element
	// O(log(sigma))
	B reverse_lower_bound(int ql, int qr, B x) const{
		assert(0 <= x);
		int cnt = count(ql, qr, x);
		return cnt ? find_by_order(ql, qr, cnt - 1) : -1;
	}
	// Find the largest element < x, -1 if no such element
	// O(log(sigma))
	B reverse_upper_bound(int ql, int qr, B x) const{
		assert(0 <= x);
		int cnt = count(ql, qr, x + 1);
		return cnt ? find_by_order(ql, qr, cnt - 1) : -1;
	}
};

template<class B>
auto make_wavelet_matrix(){
	return wavelet_matrix_base<false, B, int, plus<>, negate<>>(plus<>(), 0, negate<>());
}
// Supports query
template<class B, class T = long long, class F = plus<>, class I = negate<>>
auto make_Q_wavelet_matrix(F TT = plus<>(), T T_id = 0, I Tinv = negate<>()){
	return wavelet_matrix_base<true, B, T, F, I>(TT, T_id, Tinv);
}

int main(){
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(ios::badbit | ios::failbit);
	int n, qn;
	cin >> n >> qn;
	vector<array<int, 2>> inter{{0, n}};
	vector<array<int, 2>> child{{-1, -1}};
	vector<int> leaf;
	y_combinator([&](auto self, int u, int l, int r)->void{
		if(r - l == 1){
			assert((int)leaf.size() == l);
			leaf.push_back(u);
			return;
		}
		int m;
		cin >> m;
		int v = (int)inter.size();
		inter.push_back({l, m});
		child.push_back({-1, -1});
		child[u][0] = v;
		self(v, l, m);
		int w = (int)inter.size();
		inter.push_back({m, r});
		child.push_back({-1, -1});
		child[u][1] = w;
		self(w, m, r);
	})(0, 0, n);
	assert((int)leaf.size() == n);
	int m = (int)inter.size();
	graph<int> g(m);
	for(auto u = 0; u < m; ++ u){
		for(auto i = 0; i < 2; ++ i){
			if(~child[u][i]){
				g.orient(u, child[u][i]);
			}
		}
	}
	auto las = make_la_solver();
	las.init(m);
	las.build(g, {0});
	vector<array<int, 2>> q(qn);
	for(auto &[ql, qr]: q){
		cin >> ql >> qr;
	}
	ranges::sort(q);
	auto wm = make_wavelet_matrix<int>();
	{
		vector<int> r(qn);
		for(auto qi = 0; qi < qn; ++ qi){
			r[qi] = q[qi][1];
		}
		wm.build(r, n + 1);
	}
	vector<int> marked(m);
	for(auto x = 0; x < n; ++ x){
		int cnt;
		{
			int p = ranges::lower_bound(q, array{x + 1, 0}) - q.begin();
			cnt = p - wm.count(0, p, x + 1);
		}
		if(!cnt){
			continue;
		}
		int u = las.find_furthest_ancestor(leaf[x], [&](int u){
			int p = ranges::lower_bound(q, array{inter[u][0] + 1, 0}) - q.begin();
			return cnt == p - wm.count(0, p, inter[u][1]);
		});
		marked[u] = true;
	}
	vector<array<modular, 3>> dp(m);
	/*
	type 0: L->R is separated, and is not required to be connected later
	type 1: L->R is separated, but is required to be connected later
	type 2: L->R is connected, possibly indirectly
	*/
	for(auto u = m - 1; u >= 0; -- u){
		if(inter[u][1] - inter[u][0] == 1){
			if(marked[u]){
				dp[u][1] = dp[u][2] = 1;
			}
			else{
				dp[u][0] = dp[u][2] = 1;
			}
			continue;
		}
		auto [v, w] = child[u];
		if(marked[u]){
			dp[u][1] += dp[v][0] * dp[w][0];
			dp[u][2] += dp[v][0] * dp[w][0];

			dp[u][1] += dp[v][0] * dp[w][2];
			dp[u][2] += dp[v][0] * dp[w][2];

			dp[u][1] += dp[v][1] * dp[w][2];
			dp[u][2] += dp[v][1] * dp[w][2];

			dp[u][1] += dp[v][2] * dp[w][0];
			dp[u][2] += dp[v][2] * dp[w][0];

			dp[u][1] += dp[v][2] * dp[w][1];
			dp[u][2] += dp[v][2] * dp[w][1];

			dp[u][2] += 2 * dp[v][2] * dp[w][2];
		}
		else{
			dp[u][0] += dp[v][0] * dp[w][0];
			dp[u][2] += dp[v][0] * dp[w][0];

			dp[u][0] += dp[v][0] * dp[w][2];
			dp[u][2] += dp[v][0] * dp[w][2];

			dp[u][1] += dp[v][1] * dp[w][2];
			dp[u][2] += dp[v][1] * dp[w][2];

			dp[u][0] += dp[v][2] * dp[w][0];
			dp[u][2] += dp[v][2] * dp[w][0];

			dp[u][1] += dp[v][2] * dp[w][1];
			dp[u][2] += dp[v][2] * dp[w][1];
			
			dp[u][2] += 2 * dp[v][2] * dp[w][2];
		}
	}
	cout << dp[0][0] + dp[0][2] << "\n";
	return 0;
}

/*

*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3668kb

input:

2 1
1
0 2

output:

5

result:

ok 1 number(s): "5"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3648kb

input:

2 1
1
1 2

output:

5

result:

ok 1 number(s): "5"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3616kb

input:

5 2
2 1 4 3
1 3
2 5

output:

193

result:

ok 1 number(s): "193"

Test #4:

score: 0
Accepted
time: 0ms
memory: 3612kb

input:

10 10
5 2 1 3 4 7 6 8 9
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9
0 10

output:

70848

result:

ok 1 number(s): "70848"

Test #5:

score: 0
Accepted
time: 0ms
memory: 3604kb

input:

2 2
1
0 1
0 2

output:

4

result:

ok 1 number(s): "4"

Test #6:

score: 0
Accepted
time: 0ms
memory: 3660kb

input:

3 3
1 2
0 1
0 2
0 3

output:

14

result:

ok 1 number(s): "14"

Test #7:

score: 0
Accepted
time: 0ms
memory: 3608kb

input:

4 4
1 2 3
0 1
0 2
0 3
0 4

output:

48

result:

ok 1 number(s): "48"

Test #8:

score: 0
Accepted
time: 0ms
memory: 3828kb

input:

5 5
3 1 2 4
0 1
0 2
0 3
0 4
0 5

output:

164

result:

ok 1 number(s): "164"

Test #9:

score: 0
Accepted
time: 0ms
memory: 3616kb

input:

6 6
4 2 1 3 5
0 1
0 2
0 3
0 4
0 5
0 6

output:

544

result:

ok 1 number(s): "544"

Test #10:

score: 0
Accepted
time: 0ms
memory: 3572kb

input:

7 7
3 2 1 5 4 6
0 1
0 2
0 3
0 4
0 5
0 6
0 7

output:

1856

result:

ok 1 number(s): "1856"

Test #11:

score: 0
Accepted
time: 0ms
memory: 3648kb

input:

8 8
3 1 2 4 7 5 6
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8

output:

6528

result:

ok 1 number(s): "6528"

Test #12:

score: 0
Accepted
time: 0ms
memory: 3616kb

input:

9 9
3 1 2 4 7 6 5 8
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9

output:

21520

result:

ok 1 number(s): "21520"

Test #13:

score: 0
Accepted
time: 0ms
memory: 3620kb

input:

10 10
8 2 1 3 4 6 5 7 9
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9
0 10

output:

71296

result:

ok 1 number(s): "71296"

Test #14:

score: 0
Accepted
time: 0ms
memory: 3896kb

input:

2 3
1
0 1
0 2
1 2

output:

4

result:

ok 1 number(s): "4"

Test #15:

score: 0
Accepted
time: 0ms
memory: 3604kb

input:

3 6
1 2
0 1
0 2
0 3
1 2
1 3
2 3

output:

14

result:

ok 1 number(s): "14"

Test #16:

score: 0
Accepted
time: 0ms
memory: 3644kb

input:

4 10
1 2 3
0 1
0 2
0 3
0 4
1 2
1 3
1 4
2 3
2 4
3 4

output:

48

result:

ok 1 number(s): "48"

Test #17:

score: 0
Accepted
time: 0ms
memory: 3612kb

input:

5 15
1 4 3 2
0 1
0 2
0 3
0 4
0 5
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5

output:

164

result:

ok 1 number(s): "164"

Test #18:

score: 0
Accepted
time: 0ms
memory: 3676kb

input:

6 21
5 3 1 2 4
0 1
0 2
0 3
0 4
0 5
0 6
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6

output:

544

result:

ok 1 number(s): "544"

Test #19:

score: 0
Accepted
time: 0ms
memory: 3612kb

input:

7 28
4 1 2 3 6 5
0 1
0 2
0 3
0 4
0 5
0 6
0 7
1 2
1 3
1 4
1 5
1 6
1 7
2 3
2 4
2 5
2 6
2 7
3 4
3 5
3 6
3 7
4 5
4 6
4 7
5 6
5 7
6 7

output:

1912

result:

ok 1 number(s): "1912"

Test #20:

score: 0
Accepted
time: 0ms
memory: 3832kb

input:

8 36
5 2 1 3 4 7 6
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
1 2
1 3
1 4
1 5
1 6
1 7
1 8
2 3
2 4
2 5
2 6
2 7
2 8
3 4
3 5
3 6
3 7
3 8
4 5
4 6
4 7
4 8
5 6
5 7
5 8
6 7
6 8
7 8

output:

6304

result:

ok 1 number(s): "6304"

Test #21:

score: 0
Accepted
time: 0ms
memory: 3616kb

input:

9 45
6 2 1 4 3 5 7 8
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
2 3
2 4
2 5
2 6
2 7
2 8
2 9
3 4
3 5
3 6
3 7
3 8
3 9
4 5
4 6
4 7
4 8
4 9
5 6
5 7
5 8
5 9
6 7
6 8
6 9
7 8
7 9
8 9

output:

20736

result:

ok 1 number(s): "20736"

Test #22:

score: 0
Accepted
time: 0ms
memory: 3576kb

input:

10 55
6 3 2 1 4 5 8 7 9
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9
0 10
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
2 3
2 4
2 5
2 6
2 7
2 8
2 9
2 10
3 4
3 5
3 6
3 7
3 8
3 9
3 10
4 5
4 6
4 7
4 8
4 9
4 10
5 6
5 7
5 8
5 9
5 10
6 7
6 8
6 9
6 10
7 8
7 9
7 10
8 9
8 10
9 10

output:

70784

result:

ok 1 number(s): "70784"

Test #23:

score: 0
Accepted
time: 0ms
memory: 3620kb

input:

2 1
1
0 2

output:

5

result:

ok 1 number(s): "5"

Test #24:

score: 0
Accepted
time: 0ms
memory: 3920kb

input:

3 1
2 1
2 3

output:

21

result:

ok 1 number(s): "21"

Test #25:

score: 0
Accepted
time: 0ms
memory: 3664kb

input:

4 1
2 1 3
0 1

output:

85

result:

ok 1 number(s): "85"

Test #26:

score: 0
Accepted
time: 0ms
memory: 3864kb

input:

5 1
4 1 3 2
0 5

output:

341

result:

ok 1 number(s): "341"

Test #27:

score: -100
Wrong Answer
time: 0ms
memory: 3616kb

input:

6 1
5 1 2 3 4
0 2

output:

1155

result:

wrong answer 1st numbers differ - expected: '1260', found: '1155'