公式

E - Many Operations 解説 by en_translator


Since the operations are independent bitwise, we can consider each bit independently.

Consider Operation \(i\) as a function \(f_i\) from \(\{0,1\}\) to \(\{0,1\}\). By expressing \(f\) by a pair \((f(0),f(1))\) while computing the composition function \(f_i\circ f_{i-1}\circ\ldots \circ f_1\) incrementally, the problem can be solved in a total of \(O(N\log A)\) time.

Sample code (C++)

#include<bits/stdc++.h>
using namespace std;
#define bit(x,i)(((x)>>(i))&1)

int main(){
	int n,c;
	cin >> n >> c;
	vector<pair<int,int>>op(n);
	for(int i=0;i<n;i++)cin >> op[i].first >> op[i].second;

	vector<int>ans(n);
	for(int k=0;k<30;k++){
		array<int,2>func={0,1};
		int crr=bit(c,k);
		for(int i=0;i<n;i++){
			array<int,2>f;
			int x=bit(op[i].second,k);
			if(op[i].first==1)f={0&x,1&x};
			if(op[i].first==2)f={0|x,1|x};
			if(op[i].first==3)f={0^x,1^x};
			func={f[func[0]],f[func[1]]};
			crr=func[crr];
			ans[i]|=crr<<k;
		}
	}

	for(int i=0;i<n;i++)cout << ans[i] << endl;
}

Similar Problem: 『Odd Operator』(MojaCoder) (only in Japanese)

投稿日時:
最終更新: