ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Many Operations

2022-09-04 19:02:10  阅读:158  来源: 互联网

标签:Operations 12 Many 取反 value Operation changes 赋值


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 \((T_i,A_i)\), and is the following operation:

  • if \(T_i=1\), it replaces the value of \(X\) with \(X\) and \(A_i\);
  • if \(T_i=2\), it replaces the value of \(X\) with \(X\) or \(A_i\);
  • if \(T_i=3\),, it replaces the value of X with X xor \(A_i\).

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.

Constraints

  • \(1≤N≤2×10^5\)
  • \(1≤T_i≤3\)
  • \(0≤A_i<2^{30}\)
  • \(0≤C<2^{30}\)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:
\(N\) \(C\)
\(T_1\) \(A_1\)
\(T_2\) \(A_2\)

\(T_N\) \(A_N\)

Output

Print \(N\) lines, as specified in the Problem Statement.

Sample Input 1

3 10
3 3
2 5
1 12

Sample Output 1

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 Input 2

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

Sample Output 2

0
2
1
0
5
3
3
11
2

为了方便,称执行操作1,2\(\cdots\)为第i轮操作

位运算问题,考虑按位计算。把C拆成每一位,求出他的运算后的答案。这是就只用考虑and,xor,or 0/1

  • xor 0,and 1,or 0可以看作不变,不操作

  • xor 1其实就是取反,可以打上取反标记。

  • and 0=直接赋值为0,取反标记清空,答案直接为0,不管前面有哪些多少操作。

  • or 1=直接赋值为1,同上

但是有很多轮操作,所以我们可以推出第i轮结束时赋值和取反操作情况。如果是没有赋值,那么第i轮结束后这一位答案就是上一轮结束答案^取反操作,否则就直接赋值。注意取反后赋值操作跟着取反。

每一位都这样操作,相加,就求出了答案。

#include<cstdio>
const int N=2e5+5;
int n,c,ans[N],rt,tx,tt,t[N],a[N];
int main()
{
	scanf("%d%d",&n,&c);
	for(int i=1;i<=n;i++)
		scanf("%d%d",t+i,a+i);
	for(int i=30;~i;i--)
	{
		tx=0,rt=-1,tt=c>>i&1;//rt:赋值,tx:取反
		for(int j=1;j<=n;j++)
		{
			if(t[j]==1)
				if(!(a[j]&1<<i))
					rt=tx=0;
			if(t[j]==2)
				if(a[j]&1<<i)
					rt=1,tx=0;
			if(t[j]==3)
				if(a[j]&1<<i)
					tx^=1;
			if(rt==-1)
				tt=tt^tx;
			else
				tt=rt^tx;
			ans[j]+=tt<<i;
		}
	}
	for(int i=1;i<=n;i++)
		printf("%d\n",ans[i]);
}

标签:Operations,12,Many,取反,value,Operation,changes,赋值
来源: https://www.cnblogs.com/mekoszc/p/16655713.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有