結果

問題 No.2498 OX Operations
ユーザー ecotteaecottea
提出日時 2023-10-07 01:16:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,892 ms / 4,000 ms
コード長 10,186 bytes
コンパイル時間 5,100 ms
コンパイル使用メモリ 276,172 KB
実行使用メモリ 23,900 KB
最終ジャッジ日時 2023-10-07 01:16:43
合計ジャッジ時間 25,441 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 11 ms
4,380 KB
testcase_13 AC 7 ms
4,380 KB
testcase_14 AC 12 ms
4,380 KB
testcase_15 AC 1,441 ms
20,112 KB
testcase_16 AC 1,645 ms
21,716 KB
testcase_17 AC 1,306 ms
19,576 KB
testcase_18 AC 1,785 ms
23,888 KB
testcase_19 AC 1,731 ms
23,872 KB
testcase_20 AC 1,815 ms
23,872 KB
testcase_21 AC 1,878 ms
23,672 KB
testcase_22 AC 670 ms
9,500 KB
testcase_23 AC 570 ms
8,176 KB
testcase_24 AC 1,869 ms
23,896 KB
testcase_25 AC 1,885 ms
23,900 KB
testcase_26 AC 1,892 ms
23,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef HIDDEN_IN_VS // 折りたたみ用

// 警告の抑制
#define _CRT_SECURE_NO_WARNINGS

// ライブラリの読み込み
#include <bits/stdc++.h>
using namespace std;

// 型名の短縮
using ll = long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9)
using pii = pair<int, int>;	using pll = pair<ll, ll>;	using pil = pair<int, ll>;	using pli = pair<ll, int>;
using vi = vector<int>;		using vvi = vector<vi>;		using vvvi = vector<vvi>;	using vvvvi = vector<vvvi>;
using vl = vector<ll>;		using vvl = vector<vl>;		using vvvl = vector<vvl>;	using vvvvl = vector<vvvl>;
using vb = vector<bool>;	using vvb = vector<vb>;		using vvvb = vector<vvb>;
using vc = vector<char>;	using vvc = vector<vc>;		using vvvc = vector<vvc>;
using vd = vector<double>;	using vvd = vector<vd>;		using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;

// 定数の定義
const double PI = acos(-1);
const vi DX = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)
const vi DY = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004003104004004LL; // (int)INFL = 1010931620;
double EPS = 1e-15;

// 入出力高速化
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;

// 汎用マクロの定義
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x))
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順
#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)
#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)
#define repb(set, d) for(int set = 0; set < (1 << int(d)); ++set) // d ビット全探索(昇順)
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)
#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去
#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了
#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) // 半開矩形内判定

// 汎用関数の定義
template <class T> inline ll pow(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)
template <class T> inline T get(T set, int i) { return (set >> i) & T(1); }

// 演算子オーバーロード
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }

#endif // 折りたたみ用


#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;

#ifdef _MSC_VER
#include "localACL.hpp"
#endif

//using mint = modint1000000007;
using mint = modint998244353;
//using mint = modint; // mint::set_mod(m);

namespace atcoder {
	inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
	inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
}
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>;
#endif


#ifdef _MSC_VER // 手元環境(Visual Studio)
#include "local.hpp"
#else // 提出用(gcc)
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : -1; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
inline int msb(__int128 n) { return (n >> 64) != 0 ? (127 - __builtin_clzll((ll)(n >> 64))) : n != 0 ? (63 - __builtin_clzll((ll)(n))) : -1; }
#define gcd __gcd
#define dump(...)
#define dumpel(v)
#define dump_list(v)
#define dump_mat(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) while (1) cout << "OLE"; }
#endif


//【AND 作用付き XOR モノイド】
using S110 = int;
S110 op110(S110 x, S110 y) { return x ^ y; }
S110 e110() { return 0; }
using F110 = int;
S110 act110(F110 f, S110 x) { return f & x; }
F110 comp110(F110 f, F110 g) { return f & g; }
F110 id110() { return ~0; }
#define AND_XOR_mmonoid S110, op110, e110, F110, act110, comp110, id110


//【上から状態桁 DP,未満フラグ,数え上げ】O(n b m)(の改変)
/*
* b=10 進数で n 桁の数 num 以下の非負の整数で,数字和が m の倍数であるものの個数を返す.
*/
vm count_digit_sum(const string& num, const string& a0, const string& a1, int b = 2) {
	// 参考 : https://ferin-tech.hatenablog.com/entry/2019/11/10/%E6%A1%81DP%E3%81%AE%E5%AE%9F%E8%A3%85
	// verify : https://atcoder.jp/contests/dp/tasks/dp_s

	int n = sz(num);
//	dump(n);

	// dp[i][f][j] : 以下の条件を満たす数の個数:
	//	i : 上からの桁 d[0..i) まで決まっている.
	//	f : d[0..i) < num[0..i) なら 1,さもなくば 0(未満フラグ)
	//	j : d[0..i) の popcount
	int K = 30;
	vvvm dp(n + 1, vvm(1LL << 1, vm(K + 1)));
	dp[0][0][0] = 1;

	// 上の桁から順に配る DP
	rep(i, n) {
		// x : num の上から i 桁目の数
		int x = num[i] - '0';

		repb(f, 1) {
			// d_max : d[i] のとれる値の最大値
			int d_max = (f ? b - 1 : x);

			repi(j, 0, K) {
				// d : d[i]
				repi(d, 0, d_max) {
					int nf = (int)(f || (d < d_max));
					int nj = j;
					if (d == 0) nj += a0[i] - '0';
					else nj += a1[i] - '0';

					if (nj <= K) dp[i + 1][nf][nj] += dp[i][f][j];
				}
			}
		}

//		dump(i + 1);
//		dump("!smaller"); dump(dp[i + 1][0]);
//		dump("smaller"); dump(dp[i + 1][1]);
	}

	vm res(K + 1);
	repi(j, 0, K) res[j] += dp[n][0][j] + dp[n][1][j];

	return res;
}


//【ゼータ変換(下位要素)】O(n)
/*
* a[0..n) を
*       A[i] = Σj≦i a[j]
* なる A[0..n) に上書きする(下位要素の値全てを自身に加える)
*/
template <typename T>
void leq_zeta(vector<T>& a) {
	// 参考 : https://qiita.com/convexineq/items/afc84dfb9ee4ec4a67d5

	//【例(n = 8 のとき)】:
	//	A[0] = a[0]
	//	A[1] = a[0] + a[1]
	//	A[2] = a[0] + a[1] + a[2]
	//	A[3] = a[0] + a[1] + a[2] + a[3]
	//	A[4] = a[0] + a[1] + a[2] + a[3] + a[4]
	//	A[5] = a[0] + a[1] + a[2] + a[3] + a[4] + a[5]
	//	A[6] = a[0] + a[1] + a[2] + a[3] + a[4] + a[5] + a[6]
	//	A[7] = a[0] + a[1] + a[2] + a[3] + a[4] + a[5] + a[6] + a[7]

	int n = sz(a);
	repi(i, 1, n - 1) a[i] += a[i - 1];
}


//【メビウス変換(下位要素)】O(n)
/*
* A[0..n) を
*       A[i] = Σj≦i a[j]
* なる a[0..n) に上書きする(下位要素から自身の値への寄与を取り除く)
*/
template <typename T>
void leq_mobius(vector<T>& A) {
	// 参考 : https://qiita.com/convexineq/items/afc84dfb9ee4ec4a67d5

	//【例(n = 8 のとき)】:
	//	a[0] = + A[0]
	//	a[1] = - A[0] + A[1]
	//	a[2] =        - A[1] + A[2]
	//	a[3] =               - A[2] + A[3]
	//	a[4] =                      - A[3] + A[4]
	//	a[5] =                             - A[4] + A[5]
	//	a[6] =                                    - A[5] + A[6]
	//	a[7] =                                           - A[6] + A[7]

	int n = sz(A);
	repir(i, n - 1, 1) A[i] -= A[i - 1];
}


//【添字 max での畳込み】O(n)(の改変)
/*
* 与えられた a[0..n), b[0..n) に対して
*       c[k] = Σ_(max(i,j) = k) a[i] b[j]
* なる c[0..n) を返す.
*
* 利用:【ゼータ変換(下位要素)】,【メビウス変換(下位要素)】
*/
template <typename T>
vector<T> max_convolution(vector<vector<T>>& a) {
	// 参考 : https://qiita.com/convexineq/items/afc84dfb9ee4ec4a67d5

	int n = sz(a), K = 30;

	rep(i, n) leq_zeta(a[i]);

	vm b(K + 1, 1);
	rep(i, n) repi(j, 0, K) b[j] *= a[i][j];

	leq_mobius(b);

	return b;
}


int main() {
//	input_from_file("input.txt");
//	output_to_file("output.txt");

	int n, q;
	cin >> n >> q;

	vi m(n);
	cin >> m;

	vector<lazy_segtree<AND_XOR_mmonoid>> seg(2, lazy_segtree<AND_XOR_mmonoid>(n + 2));
	seg[1].set(0, ~0);
	dump(seg[0]); dump(seg[1]);

	rep(hoge, q) {
		char c; int l, r, x;
		cin >> c >> l >> r >> x;
		r++;

		rep(b, 2) {
			if (c == 'o') {				
				int al1 = seg[b].prod(0, l);
				int al2 = seg[b].prod(0, l + 1);

				int ar1 = seg[b].prod(0, r);
				int ar2 = seg[b].prod(0, r + 1);

				al2 |= x;
				ar1 |= x;

				seg[b].set(l, al1 ^ al2);
				seg[b].set(r, ar1 ^ ar2);

				seg[b].apply(l + 1, r, ~x);
			}
			else {
				seg[b].set(l, seg[b].get(l) ^ x);
				seg[b].set(r, seg[b].get(r) ^ x);
			}
		}

		dump(seg[0]); dump(seg[1]);
	}

	vi a0(n), a1(n);
	a0[0] = seg[0].prod(0, 2);
	a1[0] = seg[1].prod(0, 2);

	repi(i, 1, n - 1) {
		a0[i] = a0[i - 1] ^ seg[0].get(i + 1);
		a1[i] = a1[i - 1] ^ seg[1].get(i + 1);
	}
	dump(a0); dump(a1);

	int K = 30;
	vvm cnt(n);
	rep(i, n) cnt[i] = count_digit_sum(bitset<30>(m[i]).to_string(), 
		bitset<30>(a0[i]).to_string(), bitset<30>(a1[i]).to_string());
	dumpel(cnt);

	auto Cnt = max_convolution(cnt);
	dump(Cnt);

	mint res = 0;
	repi(j, 0, K) res += Cnt[j] * j;

	cout << res << endl;
}
0