当前位置: 首页 > news >正文

[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;}elsef[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;
}

http://www.lryc.cn/news/247981.html

相关文章:

  • 一、爬虫-爬取豆瓣电影案例
  • 4G5G防爆执法记录仪、防爆智能安全帽赋能智慧燃气,可视化巡检巡线,安全生产管控
  • 武汉数字孪生赋能工业制造,加速推进制造业数字化转型
  • 安卓密码框、EditText
  • ROS命令行工具
  • 深入浅出 Golang 中的直接依赖和间接依赖管理
  • 深入Python元编程:了解声明与初始化定制元类
  • [传智杯初赛] 期末考试成绩
  • Linux 常用基本命令
  • 阿里云语雀频繁崩溃,有什么文档管理工具是比较稳定的?
  • 二分查找(折半查找)探究学习
  • Android : 异常记录
  • 西南科技大学电路分析基础实验A1(元件伏安特性测试 )
  • 【Java】泛型的简单使用
  • 注册Zoho Mail邮箱:优势与使用体验
  • 第十四届蓝桥杯大赛国赛模拟题C++卷1
  • 基于UDP的TFTP文件传输
  • 抵御代码重用攻击:指针认证(PAC)和分支目标识别(BTI)
  • 业务逻辑漏洞
  • Vue框架学习笔记——计算属性
  • 初识PO模式并在Selenium中简单实践
  • 读书笔记:彼得·德鲁克《认识管理》第35章 以任务和工作为中心的设计
  • 算法基础课 (一) 基础算法
  • 【Python】jieba分词基础
  • 使用jmeter对接口进行简单测试
  • 成长在于积累——https 认证失败的学习与思考
  • C语言——数字金字塔
  • 关于 typedef 的用法
  • Webshell流量分析
  • 高级IO—poll,epoll,reactor