[ABC261E] Many Operations(dp,位运算,打表)

[ABC261E] Many Operations - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

Problem Statement

We have a variable X and N kinds of operations that change the value of X. Operation i is represented as a pair of integers (Ti​,Ai​), and is the following operation:

  • if Ti​=1, it replaces the value of X with  and X and Ai​;
  • if Ti​=2, it replaces the value of X with  or X or Ai​;
  • if Ti​=3, it replaces the value of X with  xor X xor Ai​.

Initialize X with the value of C and execute the following procedures in order:

  • Perform Operation 1, and then print the resulting value of X.
  • Next, perform Operation 1,2 in this order, and then print the value of X.
  • Next, perform Operation 1,2,3 in this order, and then print the value of X.
  • Next, perform Operation 1,2,…,N in this order, and then print the value of X.

What are and,or,xorand,or,xor?

Constraints

  • 1≤N≤2×105
  • 1≤Ti​≤3
  • 0≤Ai​<230
  • 0≤C<230
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N C
T1​ A1​
T2​ A2​
⋮
TN​ AN​

Output

Print N lines, as specified in the Problem Statement.

Sample 1

InputcopyOutputcopy
3 10
3 3
2 5
1 12
9
15
12

The initial value of X is 10.

  • Operation 1 changes X to 9.
  • Next, Operation 1 changes X to 10, and then Operation 2 changes it to 15.
  • Next, Operation 1 changes X to 12, and then Operation 2 changes it to 13, and then Operation 3 changes it to 12.

Sample 2

InputcopyOutputcopy
9 12
1 1
2 2
3 3
1 4
2 5
3 6
1 7
2 8
3 9
0
2
1
0
5
3
3
11
2

解析 :

用dp打表,将所有可能的状态都算出来,需要注意,这里如果不使用dp打表可能会超时(dp真的是最强的算法,可以解决的问题很多,效率还高)

集合划分:不重不漏,且需要将所有需要用到的状态体现出来

f[i][j][k] 表示:c的二进制的第 j 位是 i(0或1),且第 k 次操作所得的结果;

状态转移:

v = (a[k]>>j)&1;

当 t[k]==1 : f[i][j][k]=f[i][j][k-1] & v;

当 t[k]==2:f[i][j][k]=f[i][j][k-1] | v;

当 t[k]==3:f[i][j][k]=f[i][j][k-1] ^v;

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>

using namespace std;
typedef long long LL;
const int N = 2e5 + 5;
int n, c, t[N], a[N], f[2][35][N];

int main() {
	cin >> n >> c;
	for (int i = 1; i <= n; i++) {
		scanf("%d%d", &t[i], &a[i]);
	}
	for (int i = 0; i < 2; i++) {
		for (int j = 0; j <= 30; j++) {
			f[i][j][0] = i;
			for (int k = 1; k <= n; k++) {
				int v = (a[k] >> j) & 1;
				if (t[k] == 1) {
					f[i][j][k] = f[i][j][k - 1] & v;
				}
				else if (t[k] == 2) {
					f[i][j][k] = f[i][j][k - 1] | v;
				}
				else
					f[i][j][k] = f[i][j][k - 1] ^ v;
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		int ans = 0;
		for (int j = 0,k=c; j <= 30; j++,k>>=1) {
			ans += f[k & 1][j][i] * (1 << j);
		}
		cout << ans << endl;
		c = ans;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值