output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include<bits/stdc++.h>
using namespace std;
int h,m;//一天h小时,1小时m分钟
int rref[10]={0,1,5,-1,-1,2,-1,-1,8,-1};
//下标是原数字,值是镜面反射后的结果
//-1表示反射后不是一个数字
bool check(int hh,int mm)//判断反射是否合法
{
int cnt=0;
int tmp_h=0;//反射后的小时
while(mm)//反射之后,原分钟作为小时
{
if(rref[mm%10]==-1)//反射后不是一个数字
return 0;
tmp_h=tmp_h*10+rref[mm%10];//反射后的值
mm/=10;
cnt++;
}
if(cnt==1)//只转换了一次
{
tmp_h*=10;
}
if(tmp_h>h-1)//小时数不合法
return 0;
cnt=0;
int tmp_m=0;//反射后的分钟
while(hh)//反射之后,原小时作为分钟
{
if(rref[hh%10]==-1)//反射后不是一个数字
return 0;
tmp_m=tmp_m*10+rref[hh%10];//反射后的值
hh/=10;
cnt++;
}
if(cnt==1)//只转换了一次
{
tmp_m*=10;
}
if(tmp_m>m-1)//小时数不合法
return 0;
return 1;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&h,&m);//一天h小时,1小时m分钟
int hh,mm;//原始时间
scanf("%d:%d",&hh,&mm);
int t_h=hh,t_m=mm;//临时时间
while(1)
{
//printf("%02d:%02d\n",t_h,t_m);
if(check(t_h,t_m))
{
printf("%02d:%02d\n",t_h,t_m);
break;
}
else
{
t_m++;
if(t_m>=m)
{
t_m=0;
t_h++;
}
if(t_h>=h)
{
t_h=0;
}
}
}
}
return 0;
} | ### Prompt
Construct a cpp code solution to the problem outlined:
The time on the planet Lapituletti goes the same way it goes on Earth but a day lasts h hours and each hour lasts m minutes. The inhabitants of that planet use digital clocks similar to earth ones. Clocks display time in a format HH:MM (the number of hours in decimal is displayed first, then (after the colon) follows the number of minutes in decimal; the number of minutes and hours is written with leading zeros if needed to form a two-digit number). Hours are numbered from 0 to h-1 and minutes are numbered from 0 to m-1.
<image>
That's how the digits are displayed on the clock. Please note that digit 1 is placed in the middle of its position.
A standard mirror is in use on the planet Lapituletti. Inhabitants often look at the reflection of the digital clocks in the mirror and feel happy when what you see on the reflected clocks is a valid time (that means that you see valid digits in the reflection and this time can be seen on the normal clocks at some moment of a day).
The image of the clocks in the mirror is reflected against a vertical axis.
<image>
The reflection is not a valid time.
<image>
The reflection is a valid time with h=24, m = 60. However, for example, if h=10, m=60, then the reflection is not a valid time.
An inhabitant of the planet Lapituletti begins to look at a mirrored image of the clocks at some time moment s and wants to know the nearest future time moment (which can possibly happen on the next day), when the reflected clock time is valid.
It can be shown that with any h, m, s such a moment exists. If the reflected time is correct at the moment the inhabitant began to look at the clock, that moment is considered the nearest.
You are asked to solve the problem for several test cases.
Input
The first line contains a single integer T (1 ≤ T ≤ 100) — the number of test cases.
The next 2 ⋅ T lines contain the description of test cases. The description of each test case consists of two lines.
The first line of a test case contains two integers h, m (1 ≤ h, m ≤ 100).
The second line contains the start time s in the described format HH:MM.
Output
For each test case output in a separate line the nearest moment in format HH:MM when the reflected time is correct.
Example
Input
5
24 60
12:21
24 60
23:59
90 80
52:26
1 100
00:01
10 10
04:04
Output
12:21
00:00
52:28
00:00
00:00
Note
In the second test case it is not hard to show that the reflection of 23:59 is incorrect, while the reflection of the moment 00:00 on the next day is correct.
<image>
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
int h,m;//一天h小时,1小时m分钟
int rref[10]={0,1,5,-1,-1,2,-1,-1,8,-1};
//下标是原数字,值是镜面反射后的结果
//-1表示反射后不是一个数字
bool check(int hh,int mm)//判断反射是否合法
{
int cnt=0;
int tmp_h=0;//反射后的小时
while(mm)//反射之后,原分钟作为小时
{
if(rref[mm%10]==-1)//反射后不是一个数字
return 0;
tmp_h=tmp_h*10+rref[mm%10];//反射后的值
mm/=10;
cnt++;
}
if(cnt==1)//只转换了一次
{
tmp_h*=10;
}
if(tmp_h>h-1)//小时数不合法
return 0;
cnt=0;
int tmp_m=0;//反射后的分钟
while(hh)//反射之后,原小时作为分钟
{
if(rref[hh%10]==-1)//反射后不是一个数字
return 0;
tmp_m=tmp_m*10+rref[hh%10];//反射后的值
hh/=10;
cnt++;
}
if(cnt==1)//只转换了一次
{
tmp_m*=10;
}
if(tmp_m>m-1)//小时数不合法
return 0;
return 1;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&h,&m);//一天h小时,1小时m分钟
int hh,mm;//原始时间
scanf("%d:%d",&hh,&mm);
int t_h=hh,t_m=mm;//临时时间
while(1)
{
//printf("%02d:%02d\n",t_h,t_m);
if(check(t_h,t_m))
{
printf("%02d:%02d\n",t_h,t_m);
break;
}
else
{
t_m++;
if(t_m>=m)
{
t_m=0;
t_h++;
}
if(t_h>=h)
{
t_h=0;
}
}
}
}
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
typedef long long int ll;
int A[]={0,1,5,-1,-1,2,-1,-1,8,-1};
bool judge(int H, int M, int h, int m)
{
if(A[h%10]==-1 || A[h/10]==-1 || A[m%10]==-1 || A[m/10]==-1)
return false;
if((A[m%10]*10 + A[m/10])<H && (A[h%10]*10 + A[h/10]<M))
return true;
else
return false;
}
int main()
{
boost
int t,H,M;
cin>>t;
while(t--){
char colon;
int h,m;
cin>>H>>M>>h>>colon>>m;
while(!judge(H,M,h,m)){
m=(m+1)%M;
if(!m)
h=(h+1)%H;
}
cout<<h/10<<h%10<<":"<<m/10<<m%10<<endl;
}
return 0;
} | ### Prompt
Develop a solution in Cpp to the problem described below:
The time on the planet Lapituletti goes the same way it goes on Earth but a day lasts h hours and each hour lasts m minutes. The inhabitants of that planet use digital clocks similar to earth ones. Clocks display time in a format HH:MM (the number of hours in decimal is displayed first, then (after the colon) follows the number of minutes in decimal; the number of minutes and hours is written with leading zeros if needed to form a two-digit number). Hours are numbered from 0 to h-1 and minutes are numbered from 0 to m-1.
<image>
That's how the digits are displayed on the clock. Please note that digit 1 is placed in the middle of its position.
A standard mirror is in use on the planet Lapituletti. Inhabitants often look at the reflection of the digital clocks in the mirror and feel happy when what you see on the reflected clocks is a valid time (that means that you see valid digits in the reflection and this time can be seen on the normal clocks at some moment of a day).
The image of the clocks in the mirror is reflected against a vertical axis.
<image>
The reflection is not a valid time.
<image>
The reflection is a valid time with h=24, m = 60. However, for example, if h=10, m=60, then the reflection is not a valid time.
An inhabitant of the planet Lapituletti begins to look at a mirrored image of the clocks at some time moment s and wants to know the nearest future time moment (which can possibly happen on the next day), when the reflected clock time is valid.
It can be shown that with any h, m, s such a moment exists. If the reflected time is correct at the moment the inhabitant began to look at the clock, that moment is considered the nearest.
You are asked to solve the problem for several test cases.
Input
The first line contains a single integer T (1 ≤ T ≤ 100) — the number of test cases.
The next 2 ⋅ T lines contain the description of test cases. The description of each test case consists of two lines.
The first line of a test case contains two integers h, m (1 ≤ h, m ≤ 100).
The second line contains the start time s in the described format HH:MM.
Output
For each test case output in a separate line the nearest moment in format HH:MM when the reflected time is correct.
Example
Input
5
24 60
12:21
24 60
23:59
90 80
52:26
1 100
00:01
10 10
04:04
Output
12:21
00:00
52:28
00:00
00:00
Note
In the second test case it is not hard to show that the reflection of 23:59 is incorrect, while the reflection of the moment 00:00 on the next day is correct.
<image>
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
typedef long long int ll;
int A[]={0,1,5,-1,-1,2,-1,-1,8,-1};
bool judge(int H, int M, int h, int m)
{
if(A[h%10]==-1 || A[h/10]==-1 || A[m%10]==-1 || A[m/10]==-1)
return false;
if((A[m%10]*10 + A[m/10])<H && (A[h%10]*10 + A[h/10]<M))
return true;
else
return false;
}
int main()
{
boost
int t,H,M;
cin>>t;
while(t--){
char colon;
int h,m;
cin>>H>>M>>h>>colon>>m;
while(!judge(H,M,h,m)){
m=(m+1)%M;
if(!m)
h=(h+1)%H;
}
cout<<h/10<<h%10<<":"<<m/10<<m%10<<endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7 + 5, K = 20, W = 1 << K;
int n, q, tot;
int ls[N], rs[N], sum[N], or0[N], or1[N], tag[N], dep[N];
void pushup(int p) {
sum[p] = sum[ls[p]] + sum[rs[p]];
or0[p] = or0[ls[p]] | or0[rs[p]];
or1[p] = or1[ls[p]] | or1[rs[p]];
}
void Tag(int p, int k) {
// printf("Tag(%d, %d)\n", p, k);
if (!p) return ;
tag[p] ^= k;
if (dep[p] != -1 && k >> dep[p] & 1) swap(ls[p], rs[p]);
int tmp0 = (or0[p] & (~k)) | (or1[p] & k);
int tmp1 = (or1[p] & (~k)) | (or0[p] & k);
or0[p] = tmp0, or1[p] = tmp1;
// printf("or0[%d] = %d, or1[%d] = %d\n", p, or0[p], p, or1[p]);
}
void pushdown(int p) {
if (!tag[p]) return ;
Tag(ls[p], tag[p]), Tag(rs[p], tag[p]);
tag[p] = 0;
}
void insert(int &p, int k, int d) {
if (!p) p = ++tot;
dep[p] = d;
if (d == -1) {
or1[p] = k, or0[p] = k ^ (W - 1), sum[p] = 1;
return ;
}
insert((k >> d & 1) ? rs[p] : ls[p], k, d - 1);
pushup(p);
}
void split(int &p, int &q, int l, int r, int x, int y) {
if (!p || y < l || x > r) return q = 0, void();
if (x <= l && r <= y) {
q = p, p = 0;
return ;
}
int mid = (l + r) >> 1;
pushdown(p), q = ++tot;
dep[q] = dep[p];
split(ls[p], ls[q], l, mid, x, y);
split(rs[p], rs[q], mid + 1, r, x, y);
pushup(p), pushup(q);
}
void merge(int &p, int q) {
if (!p || !q) return p = p + q, void();
pushdown(p), pushdown(q);
merge(ls[p], ls[q]);
merge(rs[p], rs[q]);
if (dep[p] != -1) pushup(p);
}
void modify(int p, int k) {
// printf("modify(%d, %d)\n", p, k);
if (!p) return ;
if ((k & or0[p] & or1[p]) == 0) {
return Tag(p, k & or0[p]), void();
}
pushdown(p);
if (dep[p] != -1 && k >> dep[p] & 1) {
Tag(ls[p], 1 << dep[p]);
merge(rs[p], ls[p]), ls[p] = 0;
}
modify(ls[p], k), modify(rs[p], k);
pushup(p);
}
int main() {
scanf("%d%d", &n, &q);
int x = 0;
for (int i = 1; i <= n; i++) {
int v; scanf("%d", &v);
insert(x, v, K - 1);
}
// for (int i = 1; i <= tot; i++) {
// printf("%d : ls = %d, rs = %d, or0 = %d, or1 = %d, tag = %d, sum = %d\n", i, ls[i], rs[i], or0[i], or1[i], tag[i], sum[i]);
// }
while (q--) {
int t, l, r, y, k;
scanf("%d%d%d", &t, &l, &r);
split(x, y, 0, W - 1, l, r);
if (t == 1) {
scanf("%d", &k);
Tag(y, W - 1);
modify(y, k ^ (W - 1));
Tag(y, W - 1);
} else if (t == 2) {
scanf("%d", &k);
modify(y, k);
} else if (t == 3) {
scanf("%d", &k);
Tag(y, k);
} else {
printf("%d\n", sum[y]);
}
merge(x, y);
// for (int i = 1; i <= tot; i++) {
// printf("%d : ls = %d, rs = %d, or0 = %d, or1 = %d, tag = %d, sum = %d\n", i, ls[i], rs[i], or0[i], or1[i], tag[i], sum[i]);
// }
}
return 0;
} | ### Prompt
Your task is to create a CPP solution to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7 + 5, K = 20, W = 1 << K;
int n, q, tot;
int ls[N], rs[N], sum[N], or0[N], or1[N], tag[N], dep[N];
void pushup(int p) {
sum[p] = sum[ls[p]] + sum[rs[p]];
or0[p] = or0[ls[p]] | or0[rs[p]];
or1[p] = or1[ls[p]] | or1[rs[p]];
}
void Tag(int p, int k) {
// printf("Tag(%d, %d)\n", p, k);
if (!p) return ;
tag[p] ^= k;
if (dep[p] != -1 && k >> dep[p] & 1) swap(ls[p], rs[p]);
int tmp0 = (or0[p] & (~k)) | (or1[p] & k);
int tmp1 = (or1[p] & (~k)) | (or0[p] & k);
or0[p] = tmp0, or1[p] = tmp1;
// printf("or0[%d] = %d, or1[%d] = %d\n", p, or0[p], p, or1[p]);
}
void pushdown(int p) {
if (!tag[p]) return ;
Tag(ls[p], tag[p]), Tag(rs[p], tag[p]);
tag[p] = 0;
}
void insert(int &p, int k, int d) {
if (!p) p = ++tot;
dep[p] = d;
if (d == -1) {
or1[p] = k, or0[p] = k ^ (W - 1), sum[p] = 1;
return ;
}
insert((k >> d & 1) ? rs[p] : ls[p], k, d - 1);
pushup(p);
}
void split(int &p, int &q, int l, int r, int x, int y) {
if (!p || y < l || x > r) return q = 0, void();
if (x <= l && r <= y) {
q = p, p = 0;
return ;
}
int mid = (l + r) >> 1;
pushdown(p), q = ++tot;
dep[q] = dep[p];
split(ls[p], ls[q], l, mid, x, y);
split(rs[p], rs[q], mid + 1, r, x, y);
pushup(p), pushup(q);
}
void merge(int &p, int q) {
if (!p || !q) return p = p + q, void();
pushdown(p), pushdown(q);
merge(ls[p], ls[q]);
merge(rs[p], rs[q]);
if (dep[p] != -1) pushup(p);
}
void modify(int p, int k) {
// printf("modify(%d, %d)\n", p, k);
if (!p) return ;
if ((k & or0[p] & or1[p]) == 0) {
return Tag(p, k & or0[p]), void();
}
pushdown(p);
if (dep[p] != -1 && k >> dep[p] & 1) {
Tag(ls[p], 1 << dep[p]);
merge(rs[p], ls[p]), ls[p] = 0;
}
modify(ls[p], k), modify(rs[p], k);
pushup(p);
}
int main() {
scanf("%d%d", &n, &q);
int x = 0;
for (int i = 1; i <= n; i++) {
int v; scanf("%d", &v);
insert(x, v, K - 1);
}
// for (int i = 1; i <= tot; i++) {
// printf("%d : ls = %d, rs = %d, or0 = %d, or1 = %d, tag = %d, sum = %d\n", i, ls[i], rs[i], or0[i], or1[i], tag[i], sum[i]);
// }
while (q--) {
int t, l, r, y, k;
scanf("%d%d%d", &t, &l, &r);
split(x, y, 0, W - 1, l, r);
if (t == 1) {
scanf("%d", &k);
Tag(y, W - 1);
modify(y, k ^ (W - 1));
Tag(y, W - 1);
} else if (t == 2) {
scanf("%d", &k);
modify(y, k);
} else if (t == 3) {
scanf("%d", &k);
Tag(y, k);
} else {
printf("%d\n", sum[y]);
}
merge(x, y);
// for (int i = 1; i <= tot; i++) {
// printf("%d : ls = %d, rs = %d, or0 = %d, or1 = %d, tag = %d, sum = %d\n", i, ls[i], rs[i], or0[i], or1[i], tag[i], sum[i]);
// }
}
return 0;
}
``` |
#include <bits/stdc++.h>
const int N = 5e6 + 5, K = 20, M = 1 << K;
int n, q, tot, ls[N], rs[N], tk[N], t0[N], t1[N], ts[N], tt[N];
inline void pushup(int p) {
ts[p] = ts[ls[p]] + ts[rs[p]];
t0[p] = t0[ls[p]] | t0[rs[p]];
t1[p] = t1[ls[p]] | t1[rs[p]];
}
inline void tag(int p, int t) {
if (!p) return; tt[p] ^= t;
if (~tk[p] && t >> tk[p] & 1) std::swap(ls[p], rs[p]);
int x = (t0[p] & (~t)) | (t1[p] & t), y = (t1[p] & (~t)) | (t0[p] & t);
t0[p] = x, t1[p] = y;
}
inline void pushdown(int p) {
if (tt[p]) {
tag(ls[p], tt[p]), tag(rs[p], tt[p]);
tt[p] = 0;
}
}
void insert(int &p, int s, int k) {
if (!p) p = ++tot;
tk[p] = k;
if (k == -1) {
t1[p] = s & (M - 1), t0[p] = t1[p] ^ (M - 1);
ts[p] = 1;
return;
}
insert((s >> k & 1) ? rs[p] : ls[p], s, k - 1);
pushup(p);
}
void split(int &p, int &q, int l, int r, int x, int y) {
if (!p || y < l || x > r) {q = 0; return;}
if (x <= l && r <= y) {q = p, p = 0; return;}
int mid = l + r >> 1; pushdown(p);
tk[q = ++tot] = tk[p];
split(ls[p], ls[q], l, mid, x, y);
split(rs[p], rs[q], mid + 1, r, x, y);
pushup(p), pushup(q);
}
void merge(int &p, int q) {
if (!p || !q) {p = p | q; return;}
pushdown(p), pushdown(q);
merge(ls[p], ls[q]), merge(rs[p], rs[q]);
if (~tk[p]) pushup(p);
}
void modify(int p, int s) {
if (!p) return;
if (!(s & t0[p] & t1[p])) {tag(p, s & t0[p]); return;}
pushdown(p);
if (s >> tk[p] & 1) tag(ls[p], 1 << tk[p]), merge(rs[p], ls[p]), ls[p] = 0;
modify(ls[p], s), modify(rs[p], s);
pushup(p);
}
int main() {
scanf("%d%d", &n, &q); int p = 0;
for (int i = 1; i <= n; ++i) {
int x; scanf("%d", &x);
insert(p, x, K - 1);
}
for (; q; --q) {
int t, l, r, q, x; scanf("%d%d%d", &t, &l, &r);
split(p, q, 0, M - 1, l, r);
if (t == 1) scanf("%d", &x), tag(q, M - 1), modify(q, x ^ (M - 1)), tag(q, M - 1);
else if (t == 2) scanf("%d", &x), modify(q, x);
else if (t == 3) scanf("%d", &x), tag(q, x);
else printf("%d\n", ts[q]);
merge(p, q);
}
return 0;
} | ### Prompt
Generate a Cpp solution to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <bits/stdc++.h>
const int N = 5e6 + 5, K = 20, M = 1 << K;
int n, q, tot, ls[N], rs[N], tk[N], t0[N], t1[N], ts[N], tt[N];
inline void pushup(int p) {
ts[p] = ts[ls[p]] + ts[rs[p]];
t0[p] = t0[ls[p]] | t0[rs[p]];
t1[p] = t1[ls[p]] | t1[rs[p]];
}
inline void tag(int p, int t) {
if (!p) return; tt[p] ^= t;
if (~tk[p] && t >> tk[p] & 1) std::swap(ls[p], rs[p]);
int x = (t0[p] & (~t)) | (t1[p] & t), y = (t1[p] & (~t)) | (t0[p] & t);
t0[p] = x, t1[p] = y;
}
inline void pushdown(int p) {
if (tt[p]) {
tag(ls[p], tt[p]), tag(rs[p], tt[p]);
tt[p] = 0;
}
}
void insert(int &p, int s, int k) {
if (!p) p = ++tot;
tk[p] = k;
if (k == -1) {
t1[p] = s & (M - 1), t0[p] = t1[p] ^ (M - 1);
ts[p] = 1;
return;
}
insert((s >> k & 1) ? rs[p] : ls[p], s, k - 1);
pushup(p);
}
void split(int &p, int &q, int l, int r, int x, int y) {
if (!p || y < l || x > r) {q = 0; return;}
if (x <= l && r <= y) {q = p, p = 0; return;}
int mid = l + r >> 1; pushdown(p);
tk[q = ++tot] = tk[p];
split(ls[p], ls[q], l, mid, x, y);
split(rs[p], rs[q], mid + 1, r, x, y);
pushup(p), pushup(q);
}
void merge(int &p, int q) {
if (!p || !q) {p = p | q; return;}
pushdown(p), pushdown(q);
merge(ls[p], ls[q]), merge(rs[p], rs[q]);
if (~tk[p]) pushup(p);
}
void modify(int p, int s) {
if (!p) return;
if (!(s & t0[p] & t1[p])) {tag(p, s & t0[p]); return;}
pushdown(p);
if (s >> tk[p] & 1) tag(ls[p], 1 << tk[p]), merge(rs[p], ls[p]), ls[p] = 0;
modify(ls[p], s), modify(rs[p], s);
pushup(p);
}
int main() {
scanf("%d%d", &n, &q); int p = 0;
for (int i = 1; i <= n; ++i) {
int x; scanf("%d", &x);
insert(p, x, K - 1);
}
for (; q; --q) {
int t, l, r, q, x; scanf("%d%d%d", &t, &l, &r);
split(p, q, 0, M - 1, l, r);
if (t == 1) scanf("%d", &x), tag(q, M - 1), modify(q, x ^ (M - 1)), tag(q, M - 1);
else if (t == 2) scanf("%d", &x), modify(q, x);
else if (t == 3) scanf("%d", &x), tag(q, x);
else printf("%d\n", ts[q]);
merge(p, q);
}
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
const int N=1e7+7,B=(1<<20)-1; bool fl[N]; int op,l,r,x;
int n,m,ql,qr,rt,ans,A,q[N],fa[N],s1[N],s2[N],ch[N][2],dep[N],f1[N],f2[N],f3[N],qt[N],qc[N];
inline bool get(int u){
return (ch[fa[u]][1]==u);
}
inline void upd(int u){
if(dep[u]==-1) return;
if(!ch[u][1]) f1[u]=f1[ch[u][0]],f2[u]=f2[ch[u][0]],s1[u]=s1[ch[u][0]],s2[u]=s2[ch[u][0]];
else if(!ch[u][0]) f1[u]=f1[ch[u][1]],f2[u]=f2[ch[u][1]],s1[u]=s1[ch[u][1]],s2[u]=s2[ch[u][1]];
else{
f1[u]=(f1[ch[u][0]]&f1[ch[u][1]]);
f2[u]=(f2[ch[u][0]]&f2[ch[u][1]]);
s1[u]=(s1[ch[u][0]]+s1[ch[u][1]]);
s2[u]=(s2[ch[u][0]]+s2[ch[u][1]]);
}
}
inline void psh(int u){
if(f3[u]&(1<<dep[u])) swap(ch[u][0],ch[u][1]);
f3[ch[u][0]]^=f3[u],f3[ch[u][1]]^=f3[u];
int a=f1[u],b=f2[u];
f1[u]=(b&f3[u])|(a&(B^f3[u]));
f2[u]=(a&f3[u])|(b&(B^f3[u]));
f3[u]=0;
}
inline void pushdown(int u){
psh(u),psh(ch[u][0]),psh(ch[u][1]);
}
inline int newnode(){
ql++; if(ql==10000000) ql=0; fl[q[ql]]=1; return q[ql];
}
inline void delnode(int u){
if(u==1) return;
qr++; if(qr==10000000) qr=0;
ch[fa[u]][get(u)]=0;
if(fa[u]){
upd(fa[u]);
}
f1[u]=f2[u]=f3[u]=ch[u][0]=ch[u][1]=fa[u]=s1[u]=s2[u]=dep[u]=0,q[qr]=u,fl[u]=0;
}
inline void ins(int u,int c){
if(dep[u]==-1){
s1[u]=1,s2[u]++,f1[u]=(c^B),f2[u]=c; return;
}
if(c&(1<<dep[u])){
if(!ch[u][1]) ch[u][1]=newnode(),dep[ch[u][1]]=dep[u]-1,fa[ch[u][1]]=u;
ins(ch[u][1],c);
}
else{
if(!ch[u][0]) ch[u][0]=newnode(),dep[ch[u][0]]=dep[u]-1,fa[ch[u][0]]=u;
ins(ch[u][0],c);
}
upd(u);
}
inline int chk(int A,int op,int x,int y,int d){
if(op==1) x=(x&y);
if(op==2) x=(x|y);
if(op==3) x=(x^y);
if(x) A=A|(1<<d); else A=(((A^B)|(1<<d))^B);
return A;
}
inline void bfs(int t,int l,int r,int ql,int qr){
pushdown(t);
if(l==ql&&r==qr){
if(op<=3){
qt[++qt[0]]=t,qc[qt[0]]=A;
ch[fa[t]][get(t)]=0,upd(fa[t]);
fa[t]=0;
}
else ans+=s1[t];
return;
}
int d=(l+r)>>1;
if(ql<=d){
if(ch[t][0]){
A=chk(A,op,(x&(1<<dep[t]))>0,0,dep[t]);
bfs(ch[t][0],l,d,ql,min(d,qr));
}
}
if(d+1<=qr){
if(ch[t][1]){
A=chk(A,op,(x&(1<<dep[t]))>0,1,dep[t]);
bfs(ch[t][1],d+1,r,max(d+1,ql),qr);
}
}
upd(t);
}
inline int merge(int u,int t){
pushdown(u);
pushdown(t);
if(!s1[u]) return t;
if(!s1[t]) return u;
if(!u||!t) return (u+t);
if(dep[u]==-1){
s1[u]=1,s2[u]=s1[u]+s2[t]; delnode(t);
return u;
}
ch[u][0]=merge(ch[u][0],ch[t][0]);
ch[u][1]=merge(ch[u][1],ch[t][1]);
delnode(t),upd(u);
fa[ch[u][0]]=u,fa[ch[u][1]]=u;
return u;
}
inline void AND(int u,int c){
pushdown(u);
if(((f1[u]|f2[u])&(B^c))==(B^c)){
int C=0;
for(int i=0;i<=19;i++) if(!((1<<i)&c)&&((1<<i)&f2[u])) f3[u]^=(1<<i);
pushdown(u); return;
}
if(ch[u][0]) AND(ch[u][0],c);
if(ch[u][1]) AND(ch[u][1],c);
if(!(c&(1<<dep[u]))) ch[u][0]=merge(ch[u][0],ch[u][1]),ch[u][1]=0;
upd(u);
}
inline void OR(int u,int c){
pushdown(u);
if(((f1[u]|f2[u])&c)==c){
int C=0;
for(int i=0;i<=19;i++) if(((1<<i)&c)&&((1<<i)&f1[u])) f3[u]^=(1<<i);
pushdown(u); return;
}
if(ch[u][0]) OR(ch[u][0],c);
if(ch[u][1]) OR(ch[u][1],c);
if(c&(1<<dep[u])) ch[u][1]=merge(ch[u][0],ch[u][1]),ch[u][0]=0;
upd(u);
}
inline int dfs(int u,int A,int B){
pushdown(u);
if(dep[u]==dep[A]) return merge(A,u);
if(B&(1<<dep[u])){
if(!ch[u][1]) ch[u][1]=newnode(),dep[ch[u][1]]=dep[u]-1,fa[ch[u][1]]=u;
ch[u][1]=dfs(ch[u][1],A,B);
fa[ch[u][1]]=u;
}
else{
if(!ch[u][0]) ch[u][0]=newnode(),dep[ch[u][0]]=dep[u]-1,fa[ch[u][0]]=u;
ch[u][0]=dfs(ch[u][0],A,B);
fa[ch[u][0]]=u;
}
upd(u);
return u;
}
int main(){
for(int i=2;i<=10000000;i++) qr++,q[qr]=i;
cin>>n>>m,rt=1,dep[rt]=19;
for(int i=1;i<=n;i++) scanf("%d",&x),ins(rt,x);
while(m--){
scanf("%d%d%d",&op,&l,&r);
if(op==1){
scanf("%d",&x),qt[0]=0,A=0,bfs(rt,0,B,l,r);
for(int i=1;i<=qt[0];i++){
AND(qt[i],x);
if(qt[i]==1) continue;
// cout<<s1[qt[i]]<<","<<f1[qt[i]]<<","<<f2[qt[i]]<<","<<dep[qt[i]]<<endl;
rt=dfs(rt,qt[i],qc[i]);
}
}
if(op==2){
scanf("%d",&x),qt[0]=0,A=0,bfs(rt,0,B,l,r);
for(int i=1;i<=qt[0];i++){
OR(qt[i],x);
// cout<<qt[i]<<","<<qc[i]<<","<<dep[qt[i]]<<","<<f2[qt[i]]<<","<<ch[24][0]<<","<<f3[24]<<","<<dep[24]<<endl;
if(qt[i]==1) continue;
rt=dfs(rt,qt[i],qc[i]);
}
}
if(op==3){
scanf("%d",&x),qt[0]=0,A=0,bfs(rt,0,B,l,r);
for(int i=1;i<=qt[0];i++){
f3[qt[i]]^=x,pushdown(qt[i]);
if(qt[i]==1) continue;
rt=dfs(rt,qt[i],qc[i]);
}
}
if(op==4){
ans=0,bfs(rt,0,B,l,r);
printf("%d\n",ans);
}
}
}
//2 5
//29 10
//1 0 17 29
//4 10 23
//2 4 30 31
//4 9 31
| ### Prompt
Please formulate a Cpp solution to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
const int N=1e7+7,B=(1<<20)-1; bool fl[N]; int op,l,r,x;
int n,m,ql,qr,rt,ans,A,q[N],fa[N],s1[N],s2[N],ch[N][2],dep[N],f1[N],f2[N],f3[N],qt[N],qc[N];
inline bool get(int u){
return (ch[fa[u]][1]==u);
}
inline void upd(int u){
if(dep[u]==-1) return;
if(!ch[u][1]) f1[u]=f1[ch[u][0]],f2[u]=f2[ch[u][0]],s1[u]=s1[ch[u][0]],s2[u]=s2[ch[u][0]];
else if(!ch[u][0]) f1[u]=f1[ch[u][1]],f2[u]=f2[ch[u][1]],s1[u]=s1[ch[u][1]],s2[u]=s2[ch[u][1]];
else{
f1[u]=(f1[ch[u][0]]&f1[ch[u][1]]);
f2[u]=(f2[ch[u][0]]&f2[ch[u][1]]);
s1[u]=(s1[ch[u][0]]+s1[ch[u][1]]);
s2[u]=(s2[ch[u][0]]+s2[ch[u][1]]);
}
}
inline void psh(int u){
if(f3[u]&(1<<dep[u])) swap(ch[u][0],ch[u][1]);
f3[ch[u][0]]^=f3[u],f3[ch[u][1]]^=f3[u];
int a=f1[u],b=f2[u];
f1[u]=(b&f3[u])|(a&(B^f3[u]));
f2[u]=(a&f3[u])|(b&(B^f3[u]));
f3[u]=0;
}
inline void pushdown(int u){
psh(u),psh(ch[u][0]),psh(ch[u][1]);
}
inline int newnode(){
ql++; if(ql==10000000) ql=0; fl[q[ql]]=1; return q[ql];
}
inline void delnode(int u){
if(u==1) return;
qr++; if(qr==10000000) qr=0;
ch[fa[u]][get(u)]=0;
if(fa[u]){
upd(fa[u]);
}
f1[u]=f2[u]=f3[u]=ch[u][0]=ch[u][1]=fa[u]=s1[u]=s2[u]=dep[u]=0,q[qr]=u,fl[u]=0;
}
inline void ins(int u,int c){
if(dep[u]==-1){
s1[u]=1,s2[u]++,f1[u]=(c^B),f2[u]=c; return;
}
if(c&(1<<dep[u])){
if(!ch[u][1]) ch[u][1]=newnode(),dep[ch[u][1]]=dep[u]-1,fa[ch[u][1]]=u;
ins(ch[u][1],c);
}
else{
if(!ch[u][0]) ch[u][0]=newnode(),dep[ch[u][0]]=dep[u]-1,fa[ch[u][0]]=u;
ins(ch[u][0],c);
}
upd(u);
}
inline int chk(int A,int op,int x,int y,int d){
if(op==1) x=(x&y);
if(op==2) x=(x|y);
if(op==3) x=(x^y);
if(x) A=A|(1<<d); else A=(((A^B)|(1<<d))^B);
return A;
}
inline void bfs(int t,int l,int r,int ql,int qr){
pushdown(t);
if(l==ql&&r==qr){
if(op<=3){
qt[++qt[0]]=t,qc[qt[0]]=A;
ch[fa[t]][get(t)]=0,upd(fa[t]);
fa[t]=0;
}
else ans+=s1[t];
return;
}
int d=(l+r)>>1;
if(ql<=d){
if(ch[t][0]){
A=chk(A,op,(x&(1<<dep[t]))>0,0,dep[t]);
bfs(ch[t][0],l,d,ql,min(d,qr));
}
}
if(d+1<=qr){
if(ch[t][1]){
A=chk(A,op,(x&(1<<dep[t]))>0,1,dep[t]);
bfs(ch[t][1],d+1,r,max(d+1,ql),qr);
}
}
upd(t);
}
inline int merge(int u,int t){
pushdown(u);
pushdown(t);
if(!s1[u]) return t;
if(!s1[t]) return u;
if(!u||!t) return (u+t);
if(dep[u]==-1){
s1[u]=1,s2[u]=s1[u]+s2[t]; delnode(t);
return u;
}
ch[u][0]=merge(ch[u][0],ch[t][0]);
ch[u][1]=merge(ch[u][1],ch[t][1]);
delnode(t),upd(u);
fa[ch[u][0]]=u,fa[ch[u][1]]=u;
return u;
}
inline void AND(int u,int c){
pushdown(u);
if(((f1[u]|f2[u])&(B^c))==(B^c)){
int C=0;
for(int i=0;i<=19;i++) if(!((1<<i)&c)&&((1<<i)&f2[u])) f3[u]^=(1<<i);
pushdown(u); return;
}
if(ch[u][0]) AND(ch[u][0],c);
if(ch[u][1]) AND(ch[u][1],c);
if(!(c&(1<<dep[u]))) ch[u][0]=merge(ch[u][0],ch[u][1]),ch[u][1]=0;
upd(u);
}
inline void OR(int u,int c){
pushdown(u);
if(((f1[u]|f2[u])&c)==c){
int C=0;
for(int i=0;i<=19;i++) if(((1<<i)&c)&&((1<<i)&f1[u])) f3[u]^=(1<<i);
pushdown(u); return;
}
if(ch[u][0]) OR(ch[u][0],c);
if(ch[u][1]) OR(ch[u][1],c);
if(c&(1<<dep[u])) ch[u][1]=merge(ch[u][0],ch[u][1]),ch[u][0]=0;
upd(u);
}
inline int dfs(int u,int A,int B){
pushdown(u);
if(dep[u]==dep[A]) return merge(A,u);
if(B&(1<<dep[u])){
if(!ch[u][1]) ch[u][1]=newnode(),dep[ch[u][1]]=dep[u]-1,fa[ch[u][1]]=u;
ch[u][1]=dfs(ch[u][1],A,B);
fa[ch[u][1]]=u;
}
else{
if(!ch[u][0]) ch[u][0]=newnode(),dep[ch[u][0]]=dep[u]-1,fa[ch[u][0]]=u;
ch[u][0]=dfs(ch[u][0],A,B);
fa[ch[u][0]]=u;
}
upd(u);
return u;
}
int main(){
for(int i=2;i<=10000000;i++) qr++,q[qr]=i;
cin>>n>>m,rt=1,dep[rt]=19;
for(int i=1;i<=n;i++) scanf("%d",&x),ins(rt,x);
while(m--){
scanf("%d%d%d",&op,&l,&r);
if(op==1){
scanf("%d",&x),qt[0]=0,A=0,bfs(rt,0,B,l,r);
for(int i=1;i<=qt[0];i++){
AND(qt[i],x);
if(qt[i]==1) continue;
// cout<<s1[qt[i]]<<","<<f1[qt[i]]<<","<<f2[qt[i]]<<","<<dep[qt[i]]<<endl;
rt=dfs(rt,qt[i],qc[i]);
}
}
if(op==2){
scanf("%d",&x),qt[0]=0,A=0,bfs(rt,0,B,l,r);
for(int i=1;i<=qt[0];i++){
OR(qt[i],x);
// cout<<qt[i]<<","<<qc[i]<<","<<dep[qt[i]]<<","<<f2[qt[i]]<<","<<ch[24][0]<<","<<f3[24]<<","<<dep[24]<<endl;
if(qt[i]==1) continue;
rt=dfs(rt,qt[i],qc[i]);
}
}
if(op==3){
scanf("%d",&x),qt[0]=0,A=0,bfs(rt,0,B,l,r);
for(int i=1;i<=qt[0];i++){
f3[qt[i]]^=x,pushdown(qt[i]);
if(qt[i]==1) continue;
rt=dfs(rt,qt[i],qc[i]);
}
}
if(op==4){
ans=0,bfs(rt,0,B,l,r);
printf("%d\n",ans);
}
}
}
//2 5
//29 10
//1 0 17 29
//4 10 23
//2 4 30 31
//4 9 31
``` |
#include <bits/stdc++.h>
const int N = 200005, M = 1 << 20;
int n, Q, rt = 0;
int lc[N * 50], rc[N * 50], tagx[N * 50], tagl[N * 50], tagr[N * 50], sum[N * 50], tot = 0;
void up(int o) {
tagl[o] = tagl[lc[o]] | tagl[rc[o]];
tagr[o] = tagr[lc[o]] | tagr[rc[o]];
sum[o] = sum[lc[o]] + sum[rc[o]];
}
void pushx(int o, int x, int dep) {
if (x >> dep & 1) std::swap(lc[o], rc[o]);
int L = tagl[o], R = tagr[o];
tagl[o] = (L & (x ^ (M - 1))) | (R & x);
tagr[o] = (L & x) | (R & (x ^ (M - 1)));
tagx[o] ^= x;
}
void down(int o, int dep) {
if (!tagx[o]) return;
pushx(lc[o], tagx[o], dep - 1), pushx(rc[o], tagx[o], dep - 1);
tagx[o] = 0;
}
void ins(int &o, int x, int dep) {
if (!o) o = ++tot;
if (dep == -1) {
tagl[o] = x ^ (M - 1), tagr[o] = x, sum[o] = 1;
return;
}
x >> dep & 1 ? ins(rc[o], x, dep - 1) : ins(lc[o], x, dep - 1);
up(o);
}
void split(int &x, int &y, int l, int r, int L, int R, int dep) {
if (!x) return;
if (L <= l && r <= R) {
y = x, x = 0;
return;
}
int mid = l + r >> 1; down(x, dep);
y = ++tot;
if (L <= mid) split(lc[x], lc[y], l, mid, L, R, dep - 1);
if (mid < R) split(rc[x], rc[y], mid + 1, r, L, R, dep - 1);
up(x), up(y);
}
void merge(int &x, int y, int dep) {
if (!x || !y) { x += y; return; }
if (dep == -1) return;
down(x, dep), down(y, dep);
merge(lc[x], lc[y], dep - 1), merge(rc[x], rc[y], dep - 1);
up(x);
}
void pushor(int &o, int x, int dep) {
if (dep == -1 || !o) return;
if (!(x & tagl[o] & tagr[o])) {
pushx(o, x & tagl[o], dep);
return;
}
down(o, dep);
if (x >> dep & 1) {
pushx(lc[o], 1 << dep, dep - 1);
merge(rc[o], lc[o], dep - 1);
lc[o] = 0;
}
pushor(lc[o], x, dep - 1), pushor(rc[o], x, dep - 1);
up(o);
}
int main() {
scanf("%d%d", &n, &Q);
for (int i = 1; i <= n; i++) {
int x; scanf("%d", &x);
ins(rt, x, 19);
}
while (Q--) {
int ty, l, r, now = 0; scanf("%d%d%d", &ty, &l, &r);
split(rt, now, 0, M - 1, l, r, 19);
if (ty == 4) printf("%d\n", sum[now]);
else {
int x; scanf("%d", &x);
if (ty == 1) pushx(now, M - 1, 19), pushor(now, x ^ (M - 1), 19), pushx(now, M - 1, 19);
if (ty == 2) pushor(now, x, 19);
if (ty == 3) pushx(now, x, 19);
}
merge(rt, now, 19);
}
return 0;
} | ### Prompt
Construct a Cpp code solution to the problem outlined:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <bits/stdc++.h>
const int N = 200005, M = 1 << 20;
int n, Q, rt = 0;
int lc[N * 50], rc[N * 50], tagx[N * 50], tagl[N * 50], tagr[N * 50], sum[N * 50], tot = 0;
void up(int o) {
tagl[o] = tagl[lc[o]] | tagl[rc[o]];
tagr[o] = tagr[lc[o]] | tagr[rc[o]];
sum[o] = sum[lc[o]] + sum[rc[o]];
}
void pushx(int o, int x, int dep) {
if (x >> dep & 1) std::swap(lc[o], rc[o]);
int L = tagl[o], R = tagr[o];
tagl[o] = (L & (x ^ (M - 1))) | (R & x);
tagr[o] = (L & x) | (R & (x ^ (M - 1)));
tagx[o] ^= x;
}
void down(int o, int dep) {
if (!tagx[o]) return;
pushx(lc[o], tagx[o], dep - 1), pushx(rc[o], tagx[o], dep - 1);
tagx[o] = 0;
}
void ins(int &o, int x, int dep) {
if (!o) o = ++tot;
if (dep == -1) {
tagl[o] = x ^ (M - 1), tagr[o] = x, sum[o] = 1;
return;
}
x >> dep & 1 ? ins(rc[o], x, dep - 1) : ins(lc[o], x, dep - 1);
up(o);
}
void split(int &x, int &y, int l, int r, int L, int R, int dep) {
if (!x) return;
if (L <= l && r <= R) {
y = x, x = 0;
return;
}
int mid = l + r >> 1; down(x, dep);
y = ++tot;
if (L <= mid) split(lc[x], lc[y], l, mid, L, R, dep - 1);
if (mid < R) split(rc[x], rc[y], mid + 1, r, L, R, dep - 1);
up(x), up(y);
}
void merge(int &x, int y, int dep) {
if (!x || !y) { x += y; return; }
if (dep == -1) return;
down(x, dep), down(y, dep);
merge(lc[x], lc[y], dep - 1), merge(rc[x], rc[y], dep - 1);
up(x);
}
void pushor(int &o, int x, int dep) {
if (dep == -1 || !o) return;
if (!(x & tagl[o] & tagr[o])) {
pushx(o, x & tagl[o], dep);
return;
}
down(o, dep);
if (x >> dep & 1) {
pushx(lc[o], 1 << dep, dep - 1);
merge(rc[o], lc[o], dep - 1);
lc[o] = 0;
}
pushor(lc[o], x, dep - 1), pushor(rc[o], x, dep - 1);
up(o);
}
int main() {
scanf("%d%d", &n, &Q);
for (int i = 1; i <= n; i++) {
int x; scanf("%d", &x);
ins(rt, x, 19);
}
while (Q--) {
int ty, l, r, now = 0; scanf("%d%d%d", &ty, &l, &r);
split(rt, now, 0, M - 1, l, r, 19);
if (ty == 4) printf("%d\n", sum[now]);
else {
int x; scanf("%d", &x);
if (ty == 1) pushx(now, M - 1, 19), pushor(now, x ^ (M - 1), 19), pushx(now, M - 1, 19);
if (ty == 2) pushor(now, x, 19);
if (ty == 3) pushx(now, x, 19);
}
merge(rt, now, 19);
}
return 0;
}
``` |
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<set>
#include<vector>
#include<ctime>
#include<cstring>
#include<map>
#include<queue>
#define mp make_pair
#define PII pair<int,int>
#define fi first
#define se second
#define pb push_back
using namespace std;
inline int read(){
int x=0,f=1;
char c=getchar();
while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}
while(isdigit(c)){x=(x<<3)+(x<<1)+(c^48);c=getchar();}
return f==1?x:~x+1;
}
int n,q;
int a[200010];
int cnt=1,rt=1;
int siz[8000010];
int trie[8000010][2];
int tag[8000010],tb[8000010],t0[8000010],t1[8000010];
void pushup(int p){
siz[p]=siz[trie[p][0]]+siz[trie[p][1]];
t0[p]=t0[trie[p][0]]|t0[trie[p][1]];
t1[p]=t1[trie[p][0]]|t1[trie[p][1]];
}
void pushxor(int p,int x){
if(!p) return ;
tag[p]^=x;
if(tb[p]!=-1&&x>>tb[p]&1) swap(trie[p][0],trie[p][1]);
int a=(t0[p]&(~x))|(t1[p]&x),y=(t0[p]&x)|(t1[p]&(~x));
t0[p]=a,t1[p]=y;
}
void pushdown(int p,int bit){
if(tag[p]){
pushxor(trie[p][0],tag[p]);
pushxor(trie[p][1],tag[p]);
tag[p]=0;
}
}
map<int,int>Map;
void insert(int x){
if(Map.count(x)) return ;
Map[x]=1;
int pos=rt;tb[rt]=20;++siz[rt];
for(int i=19;i>=0;--i){
tb[pos]=i;
t1[pos]|=x;
t0[pos]|=((1<<20)-1)^x;
if(x&(1<<i)){
if(trie[pos][1]) pos=trie[pos][1];
else pos=trie[pos][1]=++cnt;
}
else {
if(trie[pos][0]) pos=trie[pos][0];
else pos=trie[pos][0]=++cnt;
}
++siz[pos];
}
t1[pos]|=x;
t0[pos]|=((1<<20)-1)^x;
tb[pos]=-1;
}
void split(int &p,int &q,int l,int r,int L,int R){
if(!p||R<l||L>r) return ;
if(l>=L&&r<=R){
// printf("p:%d,l:%d,r:%d,%d %d\n",p,l,r,t0[p],t1[p]);
q=p;p=0;return ;
}
int mid=l+r>>1;
pushdown(p,tb[p]);
q=++cnt;tb[q]=tb[p];
split(trie[p][0],trie[q][0],l,mid,L,R);
split(trie[p][1],trie[q][1],mid+1,r,L,R);
if(tb[p]!=-1)pushup(p),pushup(q);
}
void merge(int &p,int q){
if(!p||!q){
p=p|q;return ;
}
pushdown(p,tb[p]);pushdown(q,tb[q]);
merge(trie[p][0],trie[q][0]);
merge(trie[p][1],trie[q][1]);
if(tb[p]!=-1) pushup(p);
}
void pushor(int p,int x){
// printf("p:%d,x:%d\n",p,x);
if(!p) return ;
if(!(x&t0[p]&t1[p])){
// printf("%d\n",t0[p]);
// printf("%d\n",x&t0[p]);
pushxor(p,x&t0[p]);
return ;
}
pushdown(p,tb[p]);
if(x>>tb[p]&1) pushxor(trie[p][0],1<<tb[p]),merge(trie[p][1],trie[p][0]),trie[p][0]=0;
// x&=((1<<tb[p])-1);
pushor(trie[p][0],x);pushor(trie[p][1],x);
pushup(p);
}
int query(int p,int l,int r,int L,int R){
if(!p) return 0;
if(l>=L&&r<=R) return siz[p];
int mid=l+r>>1,res=0;
pushdown(p,tb[p]);
if(L<=mid) res+=query(trie[p][0],l,mid,L,R);
if(R>mid) res+=query(trie[p][1],mid+1,r,L,R);
return res;
}
//void dfs(int x,int l,int r){
// if(!x) return ;
//// if(l==r) printf("x:%d,l:%d,r:%d\n",x,l,r);
// int mid=l+r>>1;
// pushdown(x,tb[x]);
// dfs(trie[x][0],l,mid);
// dfs(trie[x][1],mid+1,r);
//}
int main(){
n=read(),q=read();
for(int i=1;i<=n;++i) a[i]=read(),insert(a[i]);
while(q--){
int t=read();
if(t==4){
int l=read(),r=read();
printf("%d\n",query(rt,0,(1<<20)-1,l,r));
}
else{
int l=read(),r=read(),x=read();
int q;split(rt,q,0,(1<<20)-1,l,r);
// printf("q:%d,%d\n",q,siz[q]);
// printf("%d %d\n",rt,siz[rt]);
// printf("%d %d\n",t0[q],t1[q]);
if(t==1) pushxor(q,(1<<20)-1),pushor(q,x^((1<<20)-1)),pushxor(q,(1<<20)-1);
else if(t==2) pushor(q,x);
else pushxor(q,x);
merge(rt,q);
}
// dfs(rt,0,(1<<20)-1);
}
} | ### Prompt
Your task is to create a Cpp solution to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<set>
#include<vector>
#include<ctime>
#include<cstring>
#include<map>
#include<queue>
#define mp make_pair
#define PII pair<int,int>
#define fi first
#define se second
#define pb push_back
using namespace std;
inline int read(){
int x=0,f=1;
char c=getchar();
while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}
while(isdigit(c)){x=(x<<3)+(x<<1)+(c^48);c=getchar();}
return f==1?x:~x+1;
}
int n,q;
int a[200010];
int cnt=1,rt=1;
int siz[8000010];
int trie[8000010][2];
int tag[8000010],tb[8000010],t0[8000010],t1[8000010];
void pushup(int p){
siz[p]=siz[trie[p][0]]+siz[trie[p][1]];
t0[p]=t0[trie[p][0]]|t0[trie[p][1]];
t1[p]=t1[trie[p][0]]|t1[trie[p][1]];
}
void pushxor(int p,int x){
if(!p) return ;
tag[p]^=x;
if(tb[p]!=-1&&x>>tb[p]&1) swap(trie[p][0],trie[p][1]);
int a=(t0[p]&(~x))|(t1[p]&x),y=(t0[p]&x)|(t1[p]&(~x));
t0[p]=a,t1[p]=y;
}
void pushdown(int p,int bit){
if(tag[p]){
pushxor(trie[p][0],tag[p]);
pushxor(trie[p][1],tag[p]);
tag[p]=0;
}
}
map<int,int>Map;
void insert(int x){
if(Map.count(x)) return ;
Map[x]=1;
int pos=rt;tb[rt]=20;++siz[rt];
for(int i=19;i>=0;--i){
tb[pos]=i;
t1[pos]|=x;
t0[pos]|=((1<<20)-1)^x;
if(x&(1<<i)){
if(trie[pos][1]) pos=trie[pos][1];
else pos=trie[pos][1]=++cnt;
}
else {
if(trie[pos][0]) pos=trie[pos][0];
else pos=trie[pos][0]=++cnt;
}
++siz[pos];
}
t1[pos]|=x;
t0[pos]|=((1<<20)-1)^x;
tb[pos]=-1;
}
void split(int &p,int &q,int l,int r,int L,int R){
if(!p||R<l||L>r) return ;
if(l>=L&&r<=R){
// printf("p:%d,l:%d,r:%d,%d %d\n",p,l,r,t0[p],t1[p]);
q=p;p=0;return ;
}
int mid=l+r>>1;
pushdown(p,tb[p]);
q=++cnt;tb[q]=tb[p];
split(trie[p][0],trie[q][0],l,mid,L,R);
split(trie[p][1],trie[q][1],mid+1,r,L,R);
if(tb[p]!=-1)pushup(p),pushup(q);
}
void merge(int &p,int q){
if(!p||!q){
p=p|q;return ;
}
pushdown(p,tb[p]);pushdown(q,tb[q]);
merge(trie[p][0],trie[q][0]);
merge(trie[p][1],trie[q][1]);
if(tb[p]!=-1) pushup(p);
}
void pushor(int p,int x){
// printf("p:%d,x:%d\n",p,x);
if(!p) return ;
if(!(x&t0[p]&t1[p])){
// printf("%d\n",t0[p]);
// printf("%d\n",x&t0[p]);
pushxor(p,x&t0[p]);
return ;
}
pushdown(p,tb[p]);
if(x>>tb[p]&1) pushxor(trie[p][0],1<<tb[p]),merge(trie[p][1],trie[p][0]),trie[p][0]=0;
// x&=((1<<tb[p])-1);
pushor(trie[p][0],x);pushor(trie[p][1],x);
pushup(p);
}
int query(int p,int l,int r,int L,int R){
if(!p) return 0;
if(l>=L&&r<=R) return siz[p];
int mid=l+r>>1,res=0;
pushdown(p,tb[p]);
if(L<=mid) res+=query(trie[p][0],l,mid,L,R);
if(R>mid) res+=query(trie[p][1],mid+1,r,L,R);
return res;
}
//void dfs(int x,int l,int r){
// if(!x) return ;
//// if(l==r) printf("x:%d,l:%d,r:%d\n",x,l,r);
// int mid=l+r>>1;
// pushdown(x,tb[x]);
// dfs(trie[x][0],l,mid);
// dfs(trie[x][1],mid+1,r);
//}
int main(){
n=read(),q=read();
for(int i=1;i<=n;++i) a[i]=read(),insert(a[i]);
while(q--){
int t=read();
if(t==4){
int l=read(),r=read();
printf("%d\n",query(rt,0,(1<<20)-1,l,r));
}
else{
int l=read(),r=read(),x=read();
int q;split(rt,q,0,(1<<20)-1,l,r);
// printf("q:%d,%d\n",q,siz[q]);
// printf("%d %d\n",rt,siz[rt]);
// printf("%d %d\n",t0[q],t1[q]);
if(t==1) pushxor(q,(1<<20)-1),pushor(q,x^((1<<20)-1)),pushxor(q,(1<<20)-1);
else if(t==2) pushor(q,x);
else pushxor(q,x);
merge(rt,q);
}
// dfs(rt,0,(1<<20)-1);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define debug(...) //ignore
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long double ld;
const int LEV = 22;
int BIT(int x, int bit) { return (1<<bit) & x; }
struct node;
using pn = node*;
pn merge(pn a, pn b);
struct node {
pn l = nullptr, r = nullptr;
int cnt_ = 0, here = 0;
int level = -1;
int lazy_xor = 0;
int has_on = 0, has_off = 0;
friend int cnt(pn x) { return x ? x->cnt_ : 0; }
friend int ON(pn x) { return x ? x->has_on : 0; }
friend int OFF(pn x) { return x ? x->has_off : 0; }
friend pn create(pn l, pn r, int lev) {
pn t = new node();
t->level = lev;
t->l = l;
t->r = r;
t->pull();
return t;
}
void pull() {
cnt_ = here + cnt(l) + cnt(r);
has_on = ON(l) | ON(r);
has_off = OFF(l) | OFF(r);
if(r) has_on ^= (1<<level);
if(l) has_off ^= (1<<level);
}
void apply_xor(int x) {
rep(bit,0,LEV) if(x>>bit&1){
lazy_xor ^= (1<<bit);
if((has_on>>bit&1) != (has_off>>bit&1)){
has_on ^= (1<<bit);
has_off ^= (1<<bit);
}
}
}
void push() {
if(level == -1) return;
if(lazy_xor>>level&1) swap(l,r);
for(auto c : {l,r}) if(c) c->apply_xor(lazy_xor);
lazy_xor = 0;
pull();
}
};
pn merge(pn a, pn b) {
if(!a) return b;
if(!b) return a;
a->push();
b->push();
pn t = create(merge(a->l, b->l), merge(a->r, b->r), a->level);
t->here = (a->here || b->here);
t->pull();
delete a;
delete b;
return t;
}
pair<pn,pn> split(pn t, int x, int level = LEV) {
if(!t) return {t,t};
assert(level == t->level);
t->push();
if(level == -1) return {nullptr, t}; // x on RHS
if(x>>level&1) {
auto [a,b] = split(t->r,x,level-1);
auto p = make_pair(create(t->l,a,level), create(nullptr,b,level));
delete t;
return p;
}
else {
auto [a,b] = split(t->l,x,level-1);
auto p = make_pair(create(a,nullptr,level), create(b,t->r,level));
delete t;
return p;
}
}
void insert(pn& t, int x, int level = LEV) {
if(!t) t = create(t, t, level);
assert(level == t->level);
t->push();
if(level == -1) {
t->here = 1;
t->pull();
return;
}
if(x>>level&1) insert(t->r, x, level-1);
else insert(t->l, x, level-1);
t->pull();
}
tuple<pn,pn,pn> split3(pn t, int l, int r) {
auto [L,T] = split(t,l);
auto [M,R] = split(T,r+1);
return {L,M,R};
}
void XOR(pn& t, int x) {
if(!t) return;
t->apply_xor(x);
t->push();
}
void OR(pn& t, int x) {
if(!x) return;
if(!t) return;
t->push();
if(t->level == -1) return;
if(x>>t->level&1) {
t->r = merge(t->l,t->r);
t->l = nullptr;
t->pull();
}
rep(bit,0,t->level) if(x>>bit&1) {
if((t->has_off>>bit&1) == 0) { x ^= (1<<bit); continue; }
if((t->has_on>>bit&1) == 0) { x ^= (1<<bit); XOR(t, 1<<bit); continue; }
}
OR(t->l, x);
OR(t->r, x);
t->pull();
}
void AND(pn& t, int x) {
const int m1 = (1<<LEV)-1;
XOR(t, m1);
OR(t, x^m1);
XOR(t, m1);
}
void deb(pn t) {
debug("=====",cnt(t));
function<void(pn,int)> dfs = [&](pn x, int y) {
if(!x) return;
x->push();
if(x->level == -1) {
if(x->here) { debug(y);}
else { debug(y, "not?");}
return;
}
if(x->level <= 5) debug(string(10-x->level, ' '), cnt(x));
dfs(x->l,y+(0<<x->level));
dfs(x->r,y+(1<<x->level));
};
dfs(t,0);
}
set<int> collect(pn t) {
ll c = cnt(t);
set<int> ans;
function<void(pn,int)> dfs = [&](pn x, int y) {
if(!x) return;
x->push();
if(x->level == -1) {
assert(x->here);
ans.emplace(y);
return;
}
dfs(x->l,y+(0<<x->level));
dfs(x->r,y+(1<<x->level));
};
dfs(t,0);
return ans;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int n, q;
cin>>n>>q;
set<int> brute;
pn root = nullptr;
rep(i,0,n) {
int a;
cin>>a;
//a = rand()%(1<<20);
insert(root, a);
brute.emplace(a);
}
//deb(root);
rep(i,0,q) {
int typ,l,r,x;
cin>>typ>>l>>r;
if(typ <= 3) cin>>x;
//typ = rand()%4+1; l = rand()%(1<<20); r = rand()%(1<<20); x = rand()%(1<<20);
debug(typ, l, r, x);
auto [a,b,c] = split3(root,l,r);
//int cnt_brute = 0;
//set<int> brute2;
//for(int y : brute) {
// if(l <= y && y <= r) {
// if(typ == 1) brute2.emplace(y&x);
// if(typ == 2) brute2.emplace(y|x);
// if(typ == 3) brute2.emplace(y^x);
// if(typ == 4) brute2.emplace(y);
// ++cnt_brute;
// }
// else brute2.emplace(y);
//}
//brute = move(brute2);
if(typ == 1) AND(b,x);
if(typ == 2) OR(b,x);
if(typ == 3) XOR(b,x);
if(typ == 4) {
//debug(cnt_brute, cnt(b));
//assert(cnt_brute == cnt(b));
cout << cnt(b) << "\n";
}
root = merge(a,merge(b,c));
//set<int> bla = collect(root);
//debug(bla);
//debug(brute);
//assert(bla == brute);
//debug()
//debug();
//deb(root);
}
cout << flush;
_Exit(0);
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define debug(...) //ignore
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long double ld;
const int LEV = 22;
int BIT(int x, int bit) { return (1<<bit) & x; }
struct node;
using pn = node*;
pn merge(pn a, pn b);
struct node {
pn l = nullptr, r = nullptr;
int cnt_ = 0, here = 0;
int level = -1;
int lazy_xor = 0;
int has_on = 0, has_off = 0;
friend int cnt(pn x) { return x ? x->cnt_ : 0; }
friend int ON(pn x) { return x ? x->has_on : 0; }
friend int OFF(pn x) { return x ? x->has_off : 0; }
friend pn create(pn l, pn r, int lev) {
pn t = new node();
t->level = lev;
t->l = l;
t->r = r;
t->pull();
return t;
}
void pull() {
cnt_ = here + cnt(l) + cnt(r);
has_on = ON(l) | ON(r);
has_off = OFF(l) | OFF(r);
if(r) has_on ^= (1<<level);
if(l) has_off ^= (1<<level);
}
void apply_xor(int x) {
rep(bit,0,LEV) if(x>>bit&1){
lazy_xor ^= (1<<bit);
if((has_on>>bit&1) != (has_off>>bit&1)){
has_on ^= (1<<bit);
has_off ^= (1<<bit);
}
}
}
void push() {
if(level == -1) return;
if(lazy_xor>>level&1) swap(l,r);
for(auto c : {l,r}) if(c) c->apply_xor(lazy_xor);
lazy_xor = 0;
pull();
}
};
pn merge(pn a, pn b) {
if(!a) return b;
if(!b) return a;
a->push();
b->push();
pn t = create(merge(a->l, b->l), merge(a->r, b->r), a->level);
t->here = (a->here || b->here);
t->pull();
delete a;
delete b;
return t;
}
pair<pn,pn> split(pn t, int x, int level = LEV) {
if(!t) return {t,t};
assert(level == t->level);
t->push();
if(level == -1) return {nullptr, t}; // x on RHS
if(x>>level&1) {
auto [a,b] = split(t->r,x,level-1);
auto p = make_pair(create(t->l,a,level), create(nullptr,b,level));
delete t;
return p;
}
else {
auto [a,b] = split(t->l,x,level-1);
auto p = make_pair(create(a,nullptr,level), create(b,t->r,level));
delete t;
return p;
}
}
void insert(pn& t, int x, int level = LEV) {
if(!t) t = create(t, t, level);
assert(level == t->level);
t->push();
if(level == -1) {
t->here = 1;
t->pull();
return;
}
if(x>>level&1) insert(t->r, x, level-1);
else insert(t->l, x, level-1);
t->pull();
}
tuple<pn,pn,pn> split3(pn t, int l, int r) {
auto [L,T] = split(t,l);
auto [M,R] = split(T,r+1);
return {L,M,R};
}
void XOR(pn& t, int x) {
if(!t) return;
t->apply_xor(x);
t->push();
}
void OR(pn& t, int x) {
if(!x) return;
if(!t) return;
t->push();
if(t->level == -1) return;
if(x>>t->level&1) {
t->r = merge(t->l,t->r);
t->l = nullptr;
t->pull();
}
rep(bit,0,t->level) if(x>>bit&1) {
if((t->has_off>>bit&1) == 0) { x ^= (1<<bit); continue; }
if((t->has_on>>bit&1) == 0) { x ^= (1<<bit); XOR(t, 1<<bit); continue; }
}
OR(t->l, x);
OR(t->r, x);
t->pull();
}
void AND(pn& t, int x) {
const int m1 = (1<<LEV)-1;
XOR(t, m1);
OR(t, x^m1);
XOR(t, m1);
}
void deb(pn t) {
debug("=====",cnt(t));
function<void(pn,int)> dfs = [&](pn x, int y) {
if(!x) return;
x->push();
if(x->level == -1) {
if(x->here) { debug(y);}
else { debug(y, "not?");}
return;
}
if(x->level <= 5) debug(string(10-x->level, ' '), cnt(x));
dfs(x->l,y+(0<<x->level));
dfs(x->r,y+(1<<x->level));
};
dfs(t,0);
}
set<int> collect(pn t) {
ll c = cnt(t);
set<int> ans;
function<void(pn,int)> dfs = [&](pn x, int y) {
if(!x) return;
x->push();
if(x->level == -1) {
assert(x->here);
ans.emplace(y);
return;
}
dfs(x->l,y+(0<<x->level));
dfs(x->r,y+(1<<x->level));
};
dfs(t,0);
return ans;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int n, q;
cin>>n>>q;
set<int> brute;
pn root = nullptr;
rep(i,0,n) {
int a;
cin>>a;
//a = rand()%(1<<20);
insert(root, a);
brute.emplace(a);
}
//deb(root);
rep(i,0,q) {
int typ,l,r,x;
cin>>typ>>l>>r;
if(typ <= 3) cin>>x;
//typ = rand()%4+1; l = rand()%(1<<20); r = rand()%(1<<20); x = rand()%(1<<20);
debug(typ, l, r, x);
auto [a,b,c] = split3(root,l,r);
//int cnt_brute = 0;
//set<int> brute2;
//for(int y : brute) {
// if(l <= y && y <= r) {
// if(typ == 1) brute2.emplace(y&x);
// if(typ == 2) brute2.emplace(y|x);
// if(typ == 3) brute2.emplace(y^x);
// if(typ == 4) brute2.emplace(y);
// ++cnt_brute;
// }
// else brute2.emplace(y);
//}
//brute = move(brute2);
if(typ == 1) AND(b,x);
if(typ == 2) OR(b,x);
if(typ == 3) XOR(b,x);
if(typ == 4) {
//debug(cnt_brute, cnt(b));
//assert(cnt_brute == cnt(b));
cout << cnt(b) << "\n";
}
root = merge(a,merge(b,c));
//set<int> bla = collect(root);
//debug(bla);
//debug(brute);
//assert(bla == brute);
//debug()
//debug();
//deb(root);
}
cout << flush;
_Exit(0);
}
``` |
#include<bits/stdc++.h>
template <typename _Tp>void read(_Tp &x){
char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar();
x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar();
if(f)x=-x;
}
template <typename _Tp,typename... Args>void read(_Tp &t,Args &...args){read(t);read(args...);}
const int N=10000005,M=(1<<20)-1;
int ch[N][2],s0[N],s1[N],siz[N],tag[N],dep[N],nc,rt;
inline void pushup(int u){
siz[u]=siz[ch[u][0]]+siz[ch[u][1]];
s0[u]=s0[ch[u][0]]|s0[ch[u][1]];
s1[u]=s1[ch[u][0]]&s1[ch[u][1]];
}
inline void upd(int u,int C){
if(!u)return;
tag[u]^=C;
if(~dep[u]&&(C>>dep[u]&1))std::swap(ch[u][0],ch[u][1]);
int x=(s0[u]&(~C))|((~s1[u])&C),y=((~s0[u])&C)|(s1[u]&(~C));
s0[u]=x,s1[u]=y;
}
inline void pushdown(int u){if(tag[u])upd(ch[u][0],tag[u]),upd(ch[u][1],tag[u]),tag[u]=0;}
void ins(int &u,int x,int d){
if(!u)dep[u=++nc]=d;
if(d==-1)return s0[u]=x,s1[u]=x,siz[u]=1,void();
ins(ch[u][x>>d&1],x,d-1),pushup(u);
}
void split(int &x,int &y,int L,int R,int l,int r){
if(!x)return y=0,void();
if(L<=l&&r<=R)return y=x,x=0,void();
int mid=(l+r)>>1;pushdown(x),dep[y=++nc]=dep[x];
if(L<=mid)split(ch[x][0],ch[y][0],L,R,l,mid);
if(R>mid)split(ch[x][1],ch[y][1],L,R,mid+1,r);
pushup(x),pushup(y);
}
int merge(int a,int b){
if(!a||!b)return a|b;
if(dep[a]==-1)return siz[a]|=siz[b],a;
pushdown(a),pushdown(b);
ch[a][0]=merge(ch[a][0],ch[b][0]);
ch[a][1]=merge(ch[a][1],ch[b][1]);
return pushup(a),a;
}
void OR(int u,int C){
if(!u)return;
if((s0[u]&C)==(s1[u]&C))return upd(u,(s0[u]|C)^s0[u]);
pushdown(u);
if(C>>dep[u]&1)upd(ch[u][0],1<<dep[u]),ch[u][1]=merge(ch[u][1],ch[u][0]),C^=1<<dep[u],ch[u][0]=0;
OR(ch[u][0],C),OR(ch[u][1],C),pushup(u);
}
int main(){
std::fill(s1,s1+N,M);
int n,q;read(n,q);
for(int i=0,x;i<n;++i)read(x),ins(rt,x,19);
int opt,l,r,x,p;
while(q--){
read(opt,l,r);
split(rt,p,l,r,0,M);
if(opt==4)printf("%d\n",siz[p]);
else{
read(x);
if(opt==1)upd(p,M),OR(p,x^M),upd(p,M);
else if(opt==2)OR(p,x);
else upd(p,x);
}
rt=merge(rt,p);
}
return 0;
} | ### Prompt
Please create a solution in Cpp to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include<bits/stdc++.h>
template <typename _Tp>void read(_Tp &x){
char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar();
x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar();
if(f)x=-x;
}
template <typename _Tp,typename... Args>void read(_Tp &t,Args &...args){read(t);read(args...);}
const int N=10000005,M=(1<<20)-1;
int ch[N][2],s0[N],s1[N],siz[N],tag[N],dep[N],nc,rt;
inline void pushup(int u){
siz[u]=siz[ch[u][0]]+siz[ch[u][1]];
s0[u]=s0[ch[u][0]]|s0[ch[u][1]];
s1[u]=s1[ch[u][0]]&s1[ch[u][1]];
}
inline void upd(int u,int C){
if(!u)return;
tag[u]^=C;
if(~dep[u]&&(C>>dep[u]&1))std::swap(ch[u][0],ch[u][1]);
int x=(s0[u]&(~C))|((~s1[u])&C),y=((~s0[u])&C)|(s1[u]&(~C));
s0[u]=x,s1[u]=y;
}
inline void pushdown(int u){if(tag[u])upd(ch[u][0],tag[u]),upd(ch[u][1],tag[u]),tag[u]=0;}
void ins(int &u,int x,int d){
if(!u)dep[u=++nc]=d;
if(d==-1)return s0[u]=x,s1[u]=x,siz[u]=1,void();
ins(ch[u][x>>d&1],x,d-1),pushup(u);
}
void split(int &x,int &y,int L,int R,int l,int r){
if(!x)return y=0,void();
if(L<=l&&r<=R)return y=x,x=0,void();
int mid=(l+r)>>1;pushdown(x),dep[y=++nc]=dep[x];
if(L<=mid)split(ch[x][0],ch[y][0],L,R,l,mid);
if(R>mid)split(ch[x][1],ch[y][1],L,R,mid+1,r);
pushup(x),pushup(y);
}
int merge(int a,int b){
if(!a||!b)return a|b;
if(dep[a]==-1)return siz[a]|=siz[b],a;
pushdown(a),pushdown(b);
ch[a][0]=merge(ch[a][0],ch[b][0]);
ch[a][1]=merge(ch[a][1],ch[b][1]);
return pushup(a),a;
}
void OR(int u,int C){
if(!u)return;
if((s0[u]&C)==(s1[u]&C))return upd(u,(s0[u]|C)^s0[u]);
pushdown(u);
if(C>>dep[u]&1)upd(ch[u][0],1<<dep[u]),ch[u][1]=merge(ch[u][1],ch[u][0]),C^=1<<dep[u],ch[u][0]=0;
OR(ch[u][0],C),OR(ch[u][1],C),pushup(u);
}
int main(){
std::fill(s1,s1+N,M);
int n,q;read(n,q);
for(int i=0,x;i<n;++i)read(x),ins(rt,x,19);
int opt,l,r,x,p;
while(q--){
read(opt,l,r);
split(rt,p,l,r,0,M);
if(opt==4)printf("%d\n",siz[p]);
else{
read(x);
if(opt==1)upd(p,M),OR(p,x^M),upd(p,M);
else if(opt==2)OR(p,x);
else upd(p,x);
}
rt=merge(rt,p);
}
return 0;
}
``` |
//Original Code:
//#include <self/utility>
//#include <self/debug>
//using namespace std;
//const int U=(1<<20)-1;
//int n,q;
//int inv[(1<<20)];
//
//struct Node
//{
// Node *l=0,*r=0;
// int sz;
// int ed;
// int swp=0;
// int hasl=0,hasr=0;
// Node(int _v):sz(_v),ed(_v){}
// void pushup()
// {
// sz=(l?l->sz:0)+(r?r->sz:0)+ed;
// hasl=((l?l->hasl:0)|(r?r->hasl:0))<<1|bool(l);
// hasr=((l?l->hasr:0)|(r?r->hasr:0))<<1|bool(r);
// }
// void pushswp(int x)
// {
// if(x&1)
// {
// swap(l,r);
// }
// swp^=x;
// int wl=(hasl&x),wr=(hasr&x);
// hasl^=wl;hasr^=wr;
// swap(wl,wr);
// hasl|=wl;hasr|=wr;
// }
// void pushdown()
// {
// if(l) l->pushswp(swp>>1);
// if(r) r->pushswp(swp>>1);
// swp=0;
// }
//}*rt;
//
//Node* make(int x,int l=20)
//{
// if(l==0) return new Node(1);
// Node *rt=new Node(0);
// if(x&1) rt->r=make(x>>1,l-1);
// else rt->l=make(x>>1,l-1);
// rt->pushup();
// return rt;
//}
//
//pair<Node*,Node*> split(Node *rt,int key,int l=20)
//{
// if(key<0) return mp(nullptr,rt);
// if(!rt) return mp(nullptr,nullptr);
// if(l==0) return mp(rt,nullptr);
// rt->pushdown();
// if(key&1)
// {
// auto o=split(rt->r,key>>1,l-1);
// rt->r=o.first;
// rt->pushup();
// auto tr=new Node(0);
// tr->r=o.second;
// tr->pushup();
// return mp(rt,tr);
// }
// else
// {
// auto o=split(rt->l,key>>1,l-1);
// rt->l=o.second;
// rt->pushup();
// auto tl=new Node(0);
// tl->l=o.first;
// tl->pushup();
// return mp(tl,rt);
// }
//}
//
//Node* merge(Node *a,Node *b)
//{
// if(!a) return b;
// if(!b) return a;
// a->pushdown();
// b->pushdown();
// a->l=merge(a->l,b->l);
// a->r=merge(a->r,b->r);
// a->pushup();
// return a;
//}
//
//tuple<Node*,Node*,Node*> splitlr(Node *&a,int l,int r)
//{
// auto o=split(a,r);
// auto o2=split(o.first,l);
// return mt(o2.first,o2.second,o.second);
//}
//
//Node* mergelr(tuple<Node*,Node*,Node*> o)
//{
// Node *a,*b,*c;
// tie(a,b,c)=o;
// return merge(merge(a,b),c);
//}
//
//void flip(Node *&rt,int w)
//{
// if(!rt) return;
// rt->pushswp(w);
//}
//
//void print(Node *&rt,int mask=0)
//{
// rt->pushdown();
// if(rt->ed)
// {
// cout<<mask<<endl;
// return;
// }
// if(rt->l) print(rt->l,mask<<1);
// if(rt->r) print(rt->r,(mask<<1)|1);
//}
//
//void push(Node *&rt,int w,int l=20)
//{
// if(!rt) return;
// int m=(rt->hasr^U)&w;
// flip(rt,m);
// w^=m;
// if(!(rt->hasl&w)) return;
// rt->pushdown();
// if(rt->l) push(rt->l,w>>1,l-1);
// if(rt->r) push(rt->r,w>>1,l-1);
// if(w&1)
// {
// rt->r=merge(rt->l,rt->r);
// rt->l=0;
// }
// rt->pushup();
//}
//
//int main()
//{
// // freopen("input.txt","r",stdin);
// for(int i=1;i<(1<<20);i++)
// {
// inv[i]=(inv[i>>1]>>1)|(((i&1)<<19));
// }
// cin>>n>>q;
// rt=new Node(0);
// for(int i=0;i<n;i++)
// {
// int x;
// scanf("%d",&x);
// x=inv[x];
// rt=merge(rt,make(x));
// }
// for(int i=0;i<q;i++)
// {
// int type;
// scanf("%d",&type);
// int l,r;
// scanf("%d%d",&l,&r);
// auto o=splitlr(rt,(l==0?-1:inv[l-1]),inv[r]);
// auto &tr=get<1>(o);
// if(type==4)
// {
// printf("%d\n",(tr?tr->sz:0));
// }
// else if(type==3)
// {
// int x;
// scanf("%d",&x);
// x=inv[x];
// flip(tr,x);
// }
// else if(type==2)
// {
// int x;
// scanf("%d",&x);
// x=inv[x];
// push(tr,x);
// }
// else
// {
// int x;
// scanf("%d",&x);
// x=inv[x];
// flip(tr,((1<<20)-1));
// push(tr,((1<<20)-1)^x);
// flip(tr,((1<<20)-1));
// }
// rt=mergelr(o);
// }
// // print(rt);
// return 0;
//}
//substituted with C++ Inliner
#ifndef _SELF_UTILITY
#define _SELF_UTILITY 1
#include <numeric>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <string.h>
#include <stack>
#include <assert.h>
#include <bitset>
#include <time.h>
#define Endl endl
#define mp make_pair
#define mt make_tuple
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define over(A) {cout<<A<<endl;exit(0);}
#define all(A) A.begin(),A.end()
#define quickcin ios_base::sync_with_stdio(false);
#ifdef __TAKE_MOD
int mod;
#else
#ifdef __TAKE_CONST_MOD
const int mod=__TAKE_CONST_MOD;
#else
const int mod=1000000007;
#endif
#endif
const int gmod=3;
const int inf=1039074182;
#ifdef __TAKE_CONST_EPS
const double eps=__TAKE_CONST_EPS;
#else
const double eps=1e-9;
#endif
const double pi=3.141592653589793238462643383279;
const ll llinf=2LL*inf*inf;
template <typename T1,typename T2> inline void chmin(T1 &x,T2 b) {if(b<x) x=b;}
template <typename T1,typename T2> inline void chmax(T1 &x,T2 b) {if(b>x) x=b;}
inline void chadd(int &x,int b) {x+=b-mod;x+=(x>>31 & mod);}
template <typename T1,typename T2> inline void chadd(T1 &x,T2 b) {x+=b;if(x>=mod) x-=mod;}
template <typename T1,typename T2> inline void chmul(T1 &x,T2 b) {x=1LL*x*b%mod;}
template <typename T1,typename T2> inline void chmod(T1 &x,T2 b) {x%=b,x+=b;if(x>=b) x-=b;}
template <typename T> inline T mabs(T x) {return (x<0?-x:x);}
using namespace std;
#endif
#ifndef _SELF_DEBUG
#define _SELF_DEBUG 1
#ifndef _SELF_OPERATOR
#define _SELF_OPERATOR 1
using namespace std;
template <typename T>
ostream & operator<<(ostream &cout, const vector<T> &vec)
{
cout << "{";
for (int i = 0; i < (int)vec.size(); i++)
{
cout << vec[i];
if (i != (int)vec.size() - 1)
cout << ',';
}
cout << "}";
return cout;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &cout, pair<T1, T2> p)
{
cout << "(" << p.first << ',' << p.second << ")";
return cout;
}
template <typename T, typename T2>
ostream &operator<<(ostream &cout, set<T, T2> s)
{
vector<T> t;
for (auto x : s)
t.push_back(x);
cout << t;
return cout;
}
template <typename T, typename T2>
ostream &operator<<(ostream &cout, multiset<T, T2> s)
{
vector<T> t;
for (auto x : s)
t.push_back(x);
cout << t;
return cout;
}
template <typename T>
ostream &operator<<(ostream &cout, queue<T> q)
{
vector<T> t;
while (q.size())
{
t.push_back(q.front());
q.pop();
}
cout << t;
return cout;
}
template <typename T1, typename T2, typename T3>
ostream &operator<<(ostream &cout, map<T1, T2, T3> m)
{
for (auto &x : m)
{
cout << "Key: " << x.first << ' ' << "Value: " << x.second << endl;
}
return cout;
}
template <typename T1, typename T2>
void operator+=(pair<T1, T2> &x,const pair<T1, T2> y)
{
x.first += y.first;
x.second += y.second;
}
template <typename T1,typename T2>
pair<T1,T2> operator + (const pair<T1,T2> &x,const pair<T1,T2> &y)
{
return make_pair(x.first+y.first,x.second+y.second);
}
template <typename T1,typename T2>
pair<T1,T2> operator - (const pair<T1,T2> &x,const pair<T1,T2> &y)
{
return mp(x.first-y.first,x.second-y.second);
}
template <typename T1, typename T2>
pair<T1, T2> operator-(pair<T1, T2> x)
{
return make_pair(-x.first, -x.second);
}
template <typename T>
vector<vector<T>> operator~(vector<vector<T>> vec)
{
vector<vector<T>> v;
int n = vec.size(), m = vec[0].size();
v.resize(m);
for (int i = 0; i < m; i++)
{
v[i].resize(n);
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
v[i][j] = vec[j][i];
}
}
return v;
}
#endif
#include <sstream>
void print0x(int x)
{
std::vector <int> vec;
while(x)
{
vec.push_back(x&1);
x>>=1;
}
std::reverse(vec.begin(),vec.end());
for(int i=0;i<(int)vec.size();i++)
{
std::cout<<vec[i];
}
std::cout<<' ';
}
template <typename T>
void print0x(T x,int len)
{
std::vector <int> vec;
while(x)
{
vec.push_back(x&1);
x>>=1;
}
reverse(vec.begin(),vec.end());
for(int i=(int)vec.size();i<len;i++)
{
putchar('0');
}
for(size_t i=0;i<vec.size();i++)
{
std::cout<<vec[i];
}
std::cout<<' ';
}
vector<string> vec_splitter(string s) {
s += ',';
vector<string> res;
while(!s.empty()) {
res.push_back(s.substr(0, s.find(',')));
s = s.substr(s.find(',') + 1);
}
return res;
}
void debug_out(
vector<string> __attribute__ ((unused)) args,
__attribute__ ((unused)) int idx,
__attribute__ ((unused)) int LINE_NUM) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
if(idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") ";
stringstream ss; ss << H;
cerr << args[idx] << " = " << ss.str();
debug_out(args, idx + 1, LINE_NUM, T...);
}
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#endif
using namespace std;
const int U=(1<<20)-1;
int n,q;
int inv[(1<<20)];
struct Node
{
Node *l=0,*r=0;
int sz;
int ed;
int swp=0;
int hasl=0,hasr=0;
Node(int _v):sz(_v),ed(_v){}
void pushup()
{
sz=(l?l->sz:0)+(r?r->sz:0)+ed;
hasl=((l?l->hasl:0)|(r?r->hasl:0))<<1|bool(l);
hasr=((l?l->hasr:0)|(r?r->hasr:0))<<1|bool(r);
}
void pushswp(int x)
{
if(x&1)
{
swap(l,r);
}
swp^=x;
int wl=(hasl&x),wr=(hasr&x);
hasl^=wl;hasr^=wr;
swap(wl,wr);
hasl|=wl;hasr|=wr;
}
void pushdown()
{
if(l) l->pushswp(swp>>1);
if(r) r->pushswp(swp>>1);
swp=0;
}
}*rt;
Node* make(int x,int l=20)
{
if(l==0) return new Node(1);
Node *rt=new Node(0);
if(x&1) rt->r=make(x>>1,l-1);
else rt->l=make(x>>1,l-1);
rt->pushup();
return rt;
}
pair<Node*,Node*> split(Node *rt,int key,int l=20)
{
if(key<0) return mp(nullptr,rt);
if(!rt) return mp(nullptr,nullptr);
if(l==0) return mp(rt,nullptr);
rt->pushdown();
if(key&1)
{
auto o=split(rt->r,key>>1,l-1);
rt->r=o.first;
rt->pushup();
auto tr=new Node(0);
tr->r=o.second;
tr->pushup();
return mp(rt,tr);
}
else
{
auto o=split(rt->l,key>>1,l-1);
rt->l=o.second;
rt->pushup();
auto tl=new Node(0);
tl->l=o.first;
tl->pushup();
return mp(tl,rt);
}
}
Node* merge(Node *a,Node *b)
{
if(!a) return b;
if(!b) return a;
a->pushdown();
b->pushdown();
a->l=merge(a->l,b->l);
a->r=merge(a->r,b->r);
a->pushup();
return a;
}
tuple<Node*,Node*,Node*> splitlr(Node *&a,int l,int r)
{
auto o=split(a,r);
auto o2=split(o.first,l);
return mt(o2.first,o2.second,o.second);
}
Node* mergelr(tuple<Node*,Node*,Node*> o)
{
Node *a,*b,*c;
tie(a,b,c)=o;
return merge(merge(a,b),c);
}
void flip(Node *&rt,int w)
{
if(!rt) return;
rt->pushswp(w);
}
void print(Node *&rt,int mask=0)
{
rt->pushdown();
if(rt->ed)
{
cout<<mask<<endl;
return;
}
if(rt->l) print(rt->l,mask<<1);
if(rt->r) print(rt->r,(mask<<1)|1);
}
void push(Node *&rt,int w,int l=20)
{
if(!rt) return;
int m=(rt->hasr^U)&w;
flip(rt,m);
w^=m;
if(!(rt->hasl&w)) return;
rt->pushdown();
if(rt->l) push(rt->l,w>>1,l-1);
if(rt->r) push(rt->r,w>>1,l-1);
if(w&1)
{
rt->r=merge(rt->l,rt->r);
rt->l=0;
}
rt->pushup();
}
int main()
{
// // freopen("input.txt","r",stdin);
for(int i=1;i<(1<<20);i++)
{
inv[i]=(inv[i>>1]>>1)|(((i&1)<<19));
}
cin>>n>>q;
rt=new Node(0);
for(int i=0;i<n;i++)
{
int x;
scanf("%d",&x);
x=inv[x];
rt=merge(rt,make(x));
}
for(int i=0;i<q;i++)
{
int type;
scanf("%d",&type);
int l,r;
scanf("%d%d",&l,&r);
auto o=splitlr(rt,(l==0?-1:inv[l-1]),inv[r]);
auto &tr=get<1>(o);
if(type==4)
{
printf("%d\n",(tr?tr->sz:0));
}
else if(type==3)
{
int x;
scanf("%d",&x);
x=inv[x];
flip(tr,x);
}
else if(type==2)
{
int x;
scanf("%d",&x);
x=inv[x];
push(tr,x);
}
else
{
int x;
scanf("%d",&x);
x=inv[x];
flip(tr,((1<<20)-1));
push(tr,((1<<20)-1)^x);
flip(tr,((1<<20)-1));
}
rt=mergelr(o);
}
// print(rt);
return 0;
}
| ### Prompt
Please create a solution in cpp to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
//Original Code:
//#include <self/utility>
//#include <self/debug>
//using namespace std;
//const int U=(1<<20)-1;
//int n,q;
//int inv[(1<<20)];
//
//struct Node
//{
// Node *l=0,*r=0;
// int sz;
// int ed;
// int swp=0;
// int hasl=0,hasr=0;
// Node(int _v):sz(_v),ed(_v){}
// void pushup()
// {
// sz=(l?l->sz:0)+(r?r->sz:0)+ed;
// hasl=((l?l->hasl:0)|(r?r->hasl:0))<<1|bool(l);
// hasr=((l?l->hasr:0)|(r?r->hasr:0))<<1|bool(r);
// }
// void pushswp(int x)
// {
// if(x&1)
// {
// swap(l,r);
// }
// swp^=x;
// int wl=(hasl&x),wr=(hasr&x);
// hasl^=wl;hasr^=wr;
// swap(wl,wr);
// hasl|=wl;hasr|=wr;
// }
// void pushdown()
// {
// if(l) l->pushswp(swp>>1);
// if(r) r->pushswp(swp>>1);
// swp=0;
// }
//}*rt;
//
//Node* make(int x,int l=20)
//{
// if(l==0) return new Node(1);
// Node *rt=new Node(0);
// if(x&1) rt->r=make(x>>1,l-1);
// else rt->l=make(x>>1,l-1);
// rt->pushup();
// return rt;
//}
//
//pair<Node*,Node*> split(Node *rt,int key,int l=20)
//{
// if(key<0) return mp(nullptr,rt);
// if(!rt) return mp(nullptr,nullptr);
// if(l==0) return mp(rt,nullptr);
// rt->pushdown();
// if(key&1)
// {
// auto o=split(rt->r,key>>1,l-1);
// rt->r=o.first;
// rt->pushup();
// auto tr=new Node(0);
// tr->r=o.second;
// tr->pushup();
// return mp(rt,tr);
// }
// else
// {
// auto o=split(rt->l,key>>1,l-1);
// rt->l=o.second;
// rt->pushup();
// auto tl=new Node(0);
// tl->l=o.first;
// tl->pushup();
// return mp(tl,rt);
// }
//}
//
//Node* merge(Node *a,Node *b)
//{
// if(!a) return b;
// if(!b) return a;
// a->pushdown();
// b->pushdown();
// a->l=merge(a->l,b->l);
// a->r=merge(a->r,b->r);
// a->pushup();
// return a;
//}
//
//tuple<Node*,Node*,Node*> splitlr(Node *&a,int l,int r)
//{
// auto o=split(a,r);
// auto o2=split(o.first,l);
// return mt(o2.first,o2.second,o.second);
//}
//
//Node* mergelr(tuple<Node*,Node*,Node*> o)
//{
// Node *a,*b,*c;
// tie(a,b,c)=o;
// return merge(merge(a,b),c);
//}
//
//void flip(Node *&rt,int w)
//{
// if(!rt) return;
// rt->pushswp(w);
//}
//
//void print(Node *&rt,int mask=0)
//{
// rt->pushdown();
// if(rt->ed)
// {
// cout<<mask<<endl;
// return;
// }
// if(rt->l) print(rt->l,mask<<1);
// if(rt->r) print(rt->r,(mask<<1)|1);
//}
//
//void push(Node *&rt,int w,int l=20)
//{
// if(!rt) return;
// int m=(rt->hasr^U)&w;
// flip(rt,m);
// w^=m;
// if(!(rt->hasl&w)) return;
// rt->pushdown();
// if(rt->l) push(rt->l,w>>1,l-1);
// if(rt->r) push(rt->r,w>>1,l-1);
// if(w&1)
// {
// rt->r=merge(rt->l,rt->r);
// rt->l=0;
// }
// rt->pushup();
//}
//
//int main()
//{
// // freopen("input.txt","r",stdin);
// for(int i=1;i<(1<<20);i++)
// {
// inv[i]=(inv[i>>1]>>1)|(((i&1)<<19));
// }
// cin>>n>>q;
// rt=new Node(0);
// for(int i=0;i<n;i++)
// {
// int x;
// scanf("%d",&x);
// x=inv[x];
// rt=merge(rt,make(x));
// }
// for(int i=0;i<q;i++)
// {
// int type;
// scanf("%d",&type);
// int l,r;
// scanf("%d%d",&l,&r);
// auto o=splitlr(rt,(l==0?-1:inv[l-1]),inv[r]);
// auto &tr=get<1>(o);
// if(type==4)
// {
// printf("%d\n",(tr?tr->sz:0));
// }
// else if(type==3)
// {
// int x;
// scanf("%d",&x);
// x=inv[x];
// flip(tr,x);
// }
// else if(type==2)
// {
// int x;
// scanf("%d",&x);
// x=inv[x];
// push(tr,x);
// }
// else
// {
// int x;
// scanf("%d",&x);
// x=inv[x];
// flip(tr,((1<<20)-1));
// push(tr,((1<<20)-1)^x);
// flip(tr,((1<<20)-1));
// }
// rt=mergelr(o);
// }
// // print(rt);
// return 0;
//}
//substituted with C++ Inliner
#ifndef _SELF_UTILITY
#define _SELF_UTILITY 1
#include <numeric>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <string.h>
#include <stack>
#include <assert.h>
#include <bitset>
#include <time.h>
#define Endl endl
#define mp make_pair
#define mt make_tuple
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define over(A) {cout<<A<<endl;exit(0);}
#define all(A) A.begin(),A.end()
#define quickcin ios_base::sync_with_stdio(false);
#ifdef __TAKE_MOD
int mod;
#else
#ifdef __TAKE_CONST_MOD
const int mod=__TAKE_CONST_MOD;
#else
const int mod=1000000007;
#endif
#endif
const int gmod=3;
const int inf=1039074182;
#ifdef __TAKE_CONST_EPS
const double eps=__TAKE_CONST_EPS;
#else
const double eps=1e-9;
#endif
const double pi=3.141592653589793238462643383279;
const ll llinf=2LL*inf*inf;
template <typename T1,typename T2> inline void chmin(T1 &x,T2 b) {if(b<x) x=b;}
template <typename T1,typename T2> inline void chmax(T1 &x,T2 b) {if(b>x) x=b;}
inline void chadd(int &x,int b) {x+=b-mod;x+=(x>>31 & mod);}
template <typename T1,typename T2> inline void chadd(T1 &x,T2 b) {x+=b;if(x>=mod) x-=mod;}
template <typename T1,typename T2> inline void chmul(T1 &x,T2 b) {x=1LL*x*b%mod;}
template <typename T1,typename T2> inline void chmod(T1 &x,T2 b) {x%=b,x+=b;if(x>=b) x-=b;}
template <typename T> inline T mabs(T x) {return (x<0?-x:x);}
using namespace std;
#endif
#ifndef _SELF_DEBUG
#define _SELF_DEBUG 1
#ifndef _SELF_OPERATOR
#define _SELF_OPERATOR 1
using namespace std;
template <typename T>
ostream & operator<<(ostream &cout, const vector<T> &vec)
{
cout << "{";
for (int i = 0; i < (int)vec.size(); i++)
{
cout << vec[i];
if (i != (int)vec.size() - 1)
cout << ',';
}
cout << "}";
return cout;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &cout, pair<T1, T2> p)
{
cout << "(" << p.first << ',' << p.second << ")";
return cout;
}
template <typename T, typename T2>
ostream &operator<<(ostream &cout, set<T, T2> s)
{
vector<T> t;
for (auto x : s)
t.push_back(x);
cout << t;
return cout;
}
template <typename T, typename T2>
ostream &operator<<(ostream &cout, multiset<T, T2> s)
{
vector<T> t;
for (auto x : s)
t.push_back(x);
cout << t;
return cout;
}
template <typename T>
ostream &operator<<(ostream &cout, queue<T> q)
{
vector<T> t;
while (q.size())
{
t.push_back(q.front());
q.pop();
}
cout << t;
return cout;
}
template <typename T1, typename T2, typename T3>
ostream &operator<<(ostream &cout, map<T1, T2, T3> m)
{
for (auto &x : m)
{
cout << "Key: " << x.first << ' ' << "Value: " << x.second << endl;
}
return cout;
}
template <typename T1, typename T2>
void operator+=(pair<T1, T2> &x,const pair<T1, T2> y)
{
x.first += y.first;
x.second += y.second;
}
template <typename T1,typename T2>
pair<T1,T2> operator + (const pair<T1,T2> &x,const pair<T1,T2> &y)
{
return make_pair(x.first+y.first,x.second+y.second);
}
template <typename T1,typename T2>
pair<T1,T2> operator - (const pair<T1,T2> &x,const pair<T1,T2> &y)
{
return mp(x.first-y.first,x.second-y.second);
}
template <typename T1, typename T2>
pair<T1, T2> operator-(pair<T1, T2> x)
{
return make_pair(-x.first, -x.second);
}
template <typename T>
vector<vector<T>> operator~(vector<vector<T>> vec)
{
vector<vector<T>> v;
int n = vec.size(), m = vec[0].size();
v.resize(m);
for (int i = 0; i < m; i++)
{
v[i].resize(n);
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
v[i][j] = vec[j][i];
}
}
return v;
}
#endif
#include <sstream>
void print0x(int x)
{
std::vector <int> vec;
while(x)
{
vec.push_back(x&1);
x>>=1;
}
std::reverse(vec.begin(),vec.end());
for(int i=0;i<(int)vec.size();i++)
{
std::cout<<vec[i];
}
std::cout<<' ';
}
template <typename T>
void print0x(T x,int len)
{
std::vector <int> vec;
while(x)
{
vec.push_back(x&1);
x>>=1;
}
reverse(vec.begin(),vec.end());
for(int i=(int)vec.size();i<len;i++)
{
putchar('0');
}
for(size_t i=0;i<vec.size();i++)
{
std::cout<<vec[i];
}
std::cout<<' ';
}
vector<string> vec_splitter(string s) {
s += ',';
vector<string> res;
while(!s.empty()) {
res.push_back(s.substr(0, s.find(',')));
s = s.substr(s.find(',') + 1);
}
return res;
}
void debug_out(
vector<string> __attribute__ ((unused)) args,
__attribute__ ((unused)) int idx,
__attribute__ ((unused)) int LINE_NUM) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
if(idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") ";
stringstream ss; ss << H;
cerr << args[idx] << " = " << ss.str();
debug_out(args, idx + 1, LINE_NUM, T...);
}
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#endif
using namespace std;
const int U=(1<<20)-1;
int n,q;
int inv[(1<<20)];
struct Node
{
Node *l=0,*r=0;
int sz;
int ed;
int swp=0;
int hasl=0,hasr=0;
Node(int _v):sz(_v),ed(_v){}
void pushup()
{
sz=(l?l->sz:0)+(r?r->sz:0)+ed;
hasl=((l?l->hasl:0)|(r?r->hasl:0))<<1|bool(l);
hasr=((l?l->hasr:0)|(r?r->hasr:0))<<1|bool(r);
}
void pushswp(int x)
{
if(x&1)
{
swap(l,r);
}
swp^=x;
int wl=(hasl&x),wr=(hasr&x);
hasl^=wl;hasr^=wr;
swap(wl,wr);
hasl|=wl;hasr|=wr;
}
void pushdown()
{
if(l) l->pushswp(swp>>1);
if(r) r->pushswp(swp>>1);
swp=0;
}
}*rt;
Node* make(int x,int l=20)
{
if(l==0) return new Node(1);
Node *rt=new Node(0);
if(x&1) rt->r=make(x>>1,l-1);
else rt->l=make(x>>1,l-1);
rt->pushup();
return rt;
}
pair<Node*,Node*> split(Node *rt,int key,int l=20)
{
if(key<0) return mp(nullptr,rt);
if(!rt) return mp(nullptr,nullptr);
if(l==0) return mp(rt,nullptr);
rt->pushdown();
if(key&1)
{
auto o=split(rt->r,key>>1,l-1);
rt->r=o.first;
rt->pushup();
auto tr=new Node(0);
tr->r=o.second;
tr->pushup();
return mp(rt,tr);
}
else
{
auto o=split(rt->l,key>>1,l-1);
rt->l=o.second;
rt->pushup();
auto tl=new Node(0);
tl->l=o.first;
tl->pushup();
return mp(tl,rt);
}
}
Node* merge(Node *a,Node *b)
{
if(!a) return b;
if(!b) return a;
a->pushdown();
b->pushdown();
a->l=merge(a->l,b->l);
a->r=merge(a->r,b->r);
a->pushup();
return a;
}
tuple<Node*,Node*,Node*> splitlr(Node *&a,int l,int r)
{
auto o=split(a,r);
auto o2=split(o.first,l);
return mt(o2.first,o2.second,o.second);
}
Node* mergelr(tuple<Node*,Node*,Node*> o)
{
Node *a,*b,*c;
tie(a,b,c)=o;
return merge(merge(a,b),c);
}
void flip(Node *&rt,int w)
{
if(!rt) return;
rt->pushswp(w);
}
void print(Node *&rt,int mask=0)
{
rt->pushdown();
if(rt->ed)
{
cout<<mask<<endl;
return;
}
if(rt->l) print(rt->l,mask<<1);
if(rt->r) print(rt->r,(mask<<1)|1);
}
void push(Node *&rt,int w,int l=20)
{
if(!rt) return;
int m=(rt->hasr^U)&w;
flip(rt,m);
w^=m;
if(!(rt->hasl&w)) return;
rt->pushdown();
if(rt->l) push(rt->l,w>>1,l-1);
if(rt->r) push(rt->r,w>>1,l-1);
if(w&1)
{
rt->r=merge(rt->l,rt->r);
rt->l=0;
}
rt->pushup();
}
int main()
{
// // freopen("input.txt","r",stdin);
for(int i=1;i<(1<<20);i++)
{
inv[i]=(inv[i>>1]>>1)|(((i&1)<<19));
}
cin>>n>>q;
rt=new Node(0);
for(int i=0;i<n;i++)
{
int x;
scanf("%d",&x);
x=inv[x];
rt=merge(rt,make(x));
}
for(int i=0;i<q;i++)
{
int type;
scanf("%d",&type);
int l,r;
scanf("%d%d",&l,&r);
auto o=splitlr(rt,(l==0?-1:inv[l-1]),inv[r]);
auto &tr=get<1>(o);
if(type==4)
{
printf("%d\n",(tr?tr->sz:0));
}
else if(type==3)
{
int x;
scanf("%d",&x);
x=inv[x];
flip(tr,x);
}
else if(type==2)
{
int x;
scanf("%d",&x);
x=inv[x];
push(tr,x);
}
else
{
int x;
scanf("%d",&x);
x=inv[x];
flip(tr,((1<<20)-1));
push(tr,((1<<20)-1)^x);
flip(tr,((1<<20)-1));
}
rt=mergelr(o);
}
// print(rt);
return 0;
}
``` |
/*
author: Maksim1744
created: 06.05.2021 16:01:27
*/
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define sum(a) ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
template<typename T> vector<T>& operator-- (vector<T> &v){for (auto& i : v) --i; return v;}
template<typename T> vector<T>& operator++ (vector<T> &v){for (auto& i : v) ++i; return v;}
template<typename T> istream& operator>>(istream& is, vector<T> &v){for (auto& i : v) is >> i; return is;}
template<typename T> ostream& operator<<(ostream& os, vector<T> v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> pair<T,U>& operator-- (pair<T, U> &p){--p.first; --p.second; return p;}
template<typename T, typename U> pair<T,U>& operator++ (pair<T, U> &p){++p.first; ++p.second; return p;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U> &p){is >> p.first >> p.second; return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U> p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}
#ifdef HOME
#define SHOW_COLORS
#include "C:/C++ libs/print.cpp"
#else
#define show(...) void(0)
#define mclock void(0)
#define shows void(0)
#define debug if (false)
#endif
const int B = 20;
struct Node {
Node* to[2];
int sz = 0;
int mod = 0;
int def0 = (1 << B) - 1;
int def1 = (1 << B) - 1;
Node() {
to[0] = nullptr;
to[1] = nullptr;
}
};
ostream &operator << (ostream &o, const Node &n) {
return o << "[sz=" << n.sz << ", mod=" << n.mod
<< ", def0=" << n.def0 << ", def1=" << n.def1 << "]";
}
void add_num(Node *node, int x) {
for (int i = 0; i < B; ++i) {
node->sz++;
node->def1 &= x;
node->def0 &= ((1 << B) - 1) ^ x;
int b = ((x >> (B - i - 1)) & 1);
if (!node->to[b]) {
node->to[b] = new Node();
}
node = node->to[b];
}
node->sz++;
node->def1 &= x;
node->def0 &= ((1 << B) - 1) ^ x;
}
void update(Node *node, int level) {
if (!node) return;
node->sz = 0;
if (node->to[0]) node->sz += node->to[0]->sz;
if (node->to[1]) node->sz += node->to[1]->sz;
node->def0 = (1 << B) - 1;
node->def1 = (1 << B) - 1;
if (node->to[0]) {
node->def0 &= node->to[0]->def0;
node->def1 &= node->to[0]->def1;
}
if (node->to[1]) {
node->def0 &= node->to[1]->def0;
node->def1 &= node->to[1]->def1;
}
if (!node->to[0])
node->def1 |= (1 << (B - level - 1));
if (!node->to[1])
node->def0 |= (1 << (B - level - 1));
}
void modify(Node *node, int x) {
if (!node) return;
node->mod ^= x;
int mask = (x & (node->def0 | node->def1));
node->def0 ^= mask;
node->def1 ^= mask;
}
void push(Node *node, int level) {
if (!node) {
return;
}
if ((node->mod >> (B - level - 1)) & 1) {
swap(node->to[0], node->to[1]);
}
modify(node->to[0], node->mod);
modify(node->to[1], node->mod);
node->mod = 0;
}
pair<Node*, Node*> split(Node *node, int m, int l = 0, int r = (1 << B) - 1, int level = 0) {
push(node, level);
if (!node) return {nullptr, nullptr};
if (r <= m) return {node, nullptr};
if (l > m) return {nullptr, node};
int mid = (l + r) / 2;
auto L = new Node();
auto R = new Node();
if (mid < m) {
auto [a, b] = split(node->to[1], m, mid + 1, r, level + 1);
if (b) {
R->to[1] = b;
} else {
R = nullptr;
}
if (node->to[0] || a) {
L->to[0] = node->to[0];
L->to[1] = a;
} else {
L = nullptr;
}
update(L, level);
update(R, level);
return {L, R};
} else {
auto [a, b] = split(node->to[0], m, l, mid, level + 1);
if (node->to[1] || b) {
R->to[0] = b;
R->to[1] = node->to[1];
} else {
R = nullptr;
}
if (a) {
L->to[0] = a;
} else {
L = nullptr;
}
update(L, level);
update(R, level);
return {L, R};
}
}
Node* mrg(Node *a, Node *b, int level) {
if (!a) return b;
if (!b) return a;
if (level == B) {
assert(a->sz == 1 && b->sz == 1);
return a;
}
Node *root = a;
push(a, level);
push(b, level);
a->to[0] = mrg(a->to[0], b->to[0], level + 1);
a->to[1] = mrg(a->to[1], b->to[1], level + 1);
update(root, level);
return root;
}
void print(Node *node, int x = 0) {
debug {
if (x == 0 && node)
show(*node);
cerr << string(x, ' ') << "- ";
if (!node) {
cerr << endl;
return;
}
cerr << *node << endl;
print(node->to[0], x + 2);
print(node->to[1], x + 2);
}
}
tuple<Node*, Node*, Node*> cut(Node *root, int l, int r) {
auto [a, bc] = split(root, l - 1);
auto [b, c] = split(bc, r);
return {a, b, c};
}
Node* merge3(Node *a, Node *b, Node *c) {
return mrg(mrg(a, b, 0), c, 0);
}
void push_or_bit(Node *node, int b, int level) {
if (!node) return;
show(*node);
push(node, level);
if (level == B - b - 1) {
modify(node->to[0], 1 << b);
node->to[1] = mrg(node->to[0], node->to[1], level + 1);
node->to[0] = nullptr;
update(node, level);
return;
}
if ((node->def1 >> b) & 1) {
show("already good");
return;
}
if ((node->def0 >> b) & 1) {
modify(node, 1 << b);
return;
}
push_or_bit(node->to[0], b, level + 1);
push_or_bit(node->to[1], b, level + 1);
update(node, level);
}
void push_or(Node *root, int x) {
for (int b = 0; b < B; ++b) {
if ((x >> b) & 1) {
show(b);
push_or_bit(root, b, 0);
print(root);
}
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
Node *root = new Node();
auto call_xor = [&](int l, int r, int x) {
shows;
auto [a, b, c] = cut(root, l, r);
modify(b, x);
print(a);
print(b);
print(c);
root = merge3(a, b, c);
print(root);
};
auto call_or = [&](int l, int r, int x) {
shows;
auto [a, b, c] = cut(root, l, r);
show(l, r);
print(a);
print(b);
print(c);
push_or(b, x);
root = merge3(a, b, c);
print(root);
};
auto call_ask = [&](int l, int r) {
shows;
print(root);
auto [a, b, c] = cut(root, l, r);
print(a);
print(b);
print(c);
int res = (b ? b->sz : 0);
return res;
};
auto call_and = [&](int l, int r, int x) {
shows;
call_xor(0, (1 << B) - 1, (1 << B) - 1);
call_or(r ^ ((1 << B) - 1), l ^ ((1 << B) - 1), ((1 << B) - 1) ^ x);
call_xor(0, (1 << B) - 1, (1 << B) - 1);
};
int n, q;
cin >> n >> q;
vector<int> a(n);
cin >> a;
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
n = a.size();
for (int k : a) {
show(k);
add_num(root, k);
}
print(root);
while (q--) {
shows;
shows;
shows;
shows;
shows;
shows;
show(*root);
print(root);
shows;
int tp, l, r, x;
cin >> tp >> l >> r;
if (tp != 4) cin >> x;
if (tp == 1) {
call_and(l, r, x);
} else if (tp == 2) {
call_or(l, r, x);
} else if (tp == 3) {
call_xor(l, r, x);
} else if (tp == 4) {
cout << call_ask(l, r) << '\n';
} else {
assert(false);
}
}
return 0;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
/*
author: Maksim1744
created: 06.05.2021 16:01:27
*/
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define sum(a) ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
template<typename T> vector<T>& operator-- (vector<T> &v){for (auto& i : v) --i; return v;}
template<typename T> vector<T>& operator++ (vector<T> &v){for (auto& i : v) ++i; return v;}
template<typename T> istream& operator>>(istream& is, vector<T> &v){for (auto& i : v) is >> i; return is;}
template<typename T> ostream& operator<<(ostream& os, vector<T> v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> pair<T,U>& operator-- (pair<T, U> &p){--p.first; --p.second; return p;}
template<typename T, typename U> pair<T,U>& operator++ (pair<T, U> &p){++p.first; ++p.second; return p;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U> &p){is >> p.first >> p.second; return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U> p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}
#ifdef HOME
#define SHOW_COLORS
#include "C:/C++ libs/print.cpp"
#else
#define show(...) void(0)
#define mclock void(0)
#define shows void(0)
#define debug if (false)
#endif
const int B = 20;
struct Node {
Node* to[2];
int sz = 0;
int mod = 0;
int def0 = (1 << B) - 1;
int def1 = (1 << B) - 1;
Node() {
to[0] = nullptr;
to[1] = nullptr;
}
};
ostream &operator << (ostream &o, const Node &n) {
return o << "[sz=" << n.sz << ", mod=" << n.mod
<< ", def0=" << n.def0 << ", def1=" << n.def1 << "]";
}
void add_num(Node *node, int x) {
for (int i = 0; i < B; ++i) {
node->sz++;
node->def1 &= x;
node->def0 &= ((1 << B) - 1) ^ x;
int b = ((x >> (B - i - 1)) & 1);
if (!node->to[b]) {
node->to[b] = new Node();
}
node = node->to[b];
}
node->sz++;
node->def1 &= x;
node->def0 &= ((1 << B) - 1) ^ x;
}
void update(Node *node, int level) {
if (!node) return;
node->sz = 0;
if (node->to[0]) node->sz += node->to[0]->sz;
if (node->to[1]) node->sz += node->to[1]->sz;
node->def0 = (1 << B) - 1;
node->def1 = (1 << B) - 1;
if (node->to[0]) {
node->def0 &= node->to[0]->def0;
node->def1 &= node->to[0]->def1;
}
if (node->to[1]) {
node->def0 &= node->to[1]->def0;
node->def1 &= node->to[1]->def1;
}
if (!node->to[0])
node->def1 |= (1 << (B - level - 1));
if (!node->to[1])
node->def0 |= (1 << (B - level - 1));
}
void modify(Node *node, int x) {
if (!node) return;
node->mod ^= x;
int mask = (x & (node->def0 | node->def1));
node->def0 ^= mask;
node->def1 ^= mask;
}
void push(Node *node, int level) {
if (!node) {
return;
}
if ((node->mod >> (B - level - 1)) & 1) {
swap(node->to[0], node->to[1]);
}
modify(node->to[0], node->mod);
modify(node->to[1], node->mod);
node->mod = 0;
}
pair<Node*, Node*> split(Node *node, int m, int l = 0, int r = (1 << B) - 1, int level = 0) {
push(node, level);
if (!node) return {nullptr, nullptr};
if (r <= m) return {node, nullptr};
if (l > m) return {nullptr, node};
int mid = (l + r) / 2;
auto L = new Node();
auto R = new Node();
if (mid < m) {
auto [a, b] = split(node->to[1], m, mid + 1, r, level + 1);
if (b) {
R->to[1] = b;
} else {
R = nullptr;
}
if (node->to[0] || a) {
L->to[0] = node->to[0];
L->to[1] = a;
} else {
L = nullptr;
}
update(L, level);
update(R, level);
return {L, R};
} else {
auto [a, b] = split(node->to[0], m, l, mid, level + 1);
if (node->to[1] || b) {
R->to[0] = b;
R->to[1] = node->to[1];
} else {
R = nullptr;
}
if (a) {
L->to[0] = a;
} else {
L = nullptr;
}
update(L, level);
update(R, level);
return {L, R};
}
}
Node* mrg(Node *a, Node *b, int level) {
if (!a) return b;
if (!b) return a;
if (level == B) {
assert(a->sz == 1 && b->sz == 1);
return a;
}
Node *root = a;
push(a, level);
push(b, level);
a->to[0] = mrg(a->to[0], b->to[0], level + 1);
a->to[1] = mrg(a->to[1], b->to[1], level + 1);
update(root, level);
return root;
}
void print(Node *node, int x = 0) {
debug {
if (x == 0 && node)
show(*node);
cerr << string(x, ' ') << "- ";
if (!node) {
cerr << endl;
return;
}
cerr << *node << endl;
print(node->to[0], x + 2);
print(node->to[1], x + 2);
}
}
tuple<Node*, Node*, Node*> cut(Node *root, int l, int r) {
auto [a, bc] = split(root, l - 1);
auto [b, c] = split(bc, r);
return {a, b, c};
}
Node* merge3(Node *a, Node *b, Node *c) {
return mrg(mrg(a, b, 0), c, 0);
}
void push_or_bit(Node *node, int b, int level) {
if (!node) return;
show(*node);
push(node, level);
if (level == B - b - 1) {
modify(node->to[0], 1 << b);
node->to[1] = mrg(node->to[0], node->to[1], level + 1);
node->to[0] = nullptr;
update(node, level);
return;
}
if ((node->def1 >> b) & 1) {
show("already good");
return;
}
if ((node->def0 >> b) & 1) {
modify(node, 1 << b);
return;
}
push_or_bit(node->to[0], b, level + 1);
push_or_bit(node->to[1], b, level + 1);
update(node, level);
}
void push_or(Node *root, int x) {
for (int b = 0; b < B; ++b) {
if ((x >> b) & 1) {
show(b);
push_or_bit(root, b, 0);
print(root);
}
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
Node *root = new Node();
auto call_xor = [&](int l, int r, int x) {
shows;
auto [a, b, c] = cut(root, l, r);
modify(b, x);
print(a);
print(b);
print(c);
root = merge3(a, b, c);
print(root);
};
auto call_or = [&](int l, int r, int x) {
shows;
auto [a, b, c] = cut(root, l, r);
show(l, r);
print(a);
print(b);
print(c);
push_or(b, x);
root = merge3(a, b, c);
print(root);
};
auto call_ask = [&](int l, int r) {
shows;
print(root);
auto [a, b, c] = cut(root, l, r);
print(a);
print(b);
print(c);
int res = (b ? b->sz : 0);
return res;
};
auto call_and = [&](int l, int r, int x) {
shows;
call_xor(0, (1 << B) - 1, (1 << B) - 1);
call_or(r ^ ((1 << B) - 1), l ^ ((1 << B) - 1), ((1 << B) - 1) ^ x);
call_xor(0, (1 << B) - 1, (1 << B) - 1);
};
int n, q;
cin >> n >> q;
vector<int> a(n);
cin >> a;
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
n = a.size();
for (int k : a) {
show(k);
add_num(root, k);
}
print(root);
while (q--) {
shows;
shows;
shows;
shows;
shows;
shows;
show(*root);
print(root);
shows;
int tp, l, r, x;
cin >> tp >> l >> r;
if (tp != 4) cin >> x;
if (tp == 1) {
call_and(l, r, x);
} else if (tp == 2) {
call_or(l, r, x);
} else if (tp == 3) {
call_xor(l, r, x);
} else if (tp == 4) {
cout << call_ask(l, r) << '\n';
} else {
assert(false);
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
template<int H>
struct MergeTree {
array<MergeTree<H-1>*, 2> ch;
array<int, H+1> branch_cou;
// (01 -> no change), (00 -> set 0), (11 -> set 1), (10 -> flip)
uint s0 = 0, s1 = -1;
uint size = 0;
MergeTree() : branch_cou(), ch{nullptr, nullptr} {}
MergeTree(int v) : branch_cou(), size(1) {
int b = (bool)(v & (1 << H));
ch[b] = new MergeTree<H-1>(v);
ch[b ^ 1] = nullptr;
}
static int getSize(MergeTree<H>* t) { return (t == nullptr ? 0 : t->size); }
void apply(uint t0, uint t1) {
uint b0 = (bool)(t0 & (1 << H));
uint b1 = (bool)(t1 & (1 << H));
if (b0 && ! b1) {
swap(ch[0], ch[1]);
} else if (b0 == b1) {
// We GUARANTEE that when we merge children here, one is nullptr
if (ch[b0] == nullptr) ch[b0] = ch[b0 ^ 1];
ch[b0 ^ 1] = nullptr;
}
s0 = (s0 | t0) & ((~s0) | t1);
s1 = (s1 & t1) | ((~s1) & t0);
}
void push() {
if (ch[0]) ch[0]->apply(s0, s1);
if (ch[1]) ch[1]->apply(s0, s1);
s0 = 0; s1 = -1;
}
void update() {
size = MergeTree<H-1>::getSize(ch[0]) + MergeTree<H-1>::getSize(ch[1]);
if (ch[0] && ch[1]) {
branch_cou[H] = 1;
if constexpr(H > 0) {
for (int j = 0; j < H; ++j) {
branch_cou[j] = ch[0]->branch_cou[j] + ch[1]->branch_cou[j];
}
}
} else {
branch_cou[H] = 0;
if constexpr(H > 0) {
if (ch[0]) {
for (int j = 0; j < H; ++j) branch_cou[j] = ch[0]->branch_cou[j];
} else if (ch[1]) {
for (int j = 0; j < H; ++j) branch_cou[j] = ch[1]->branch_cou[j];
} else {
for (int j = 0; j < H; ++j) branch_cou[j] = 0;
}
}
}
}
static MergeTree<H>* merge(MergeTree<H>* a, MergeTree<H>* b) {
if (!a) return b;
if (!b) return a;
a->push(), b->push();
a->ch[0] = MergeTree<H-1>::merge(a->ch[0], b->ch[0]);
a->ch[1] = MergeTree<H-1>::merge(a->ch[1], b->ch[1]);
a->update();
delete b;
return a;
}
static pair<MergeTree<H>*, MergeTree<H>*> split(MergeTree<H>* a, uint v) {
if (! a) return {nullptr, nullptr};
MergeTree<H>* b = nullptr;
a->push();
if (v & (1 << H)) {
auto sub = MergeTree<H-1>::split(a->ch[1], v);
a->ch[1] = sub.first;
a->update();
if (sub.second != nullptr) {
b = new MergeTree<H>();
b->ch[1] = sub.second;
b->update();
}
return {a, b};
} else {
auto sub = MergeTree<H-1>::split(a->ch[0], v);
a->ch[0] = sub.second;
a->update();
if (sub.first != nullptr) {
b = new MergeTree<H>();
b->ch[0] = sub.first;
b->update();
}
return {b, a};
}
}
static void recDelete(MergeTree<H>* t) {
if (! t) return;
MergeTree<H-1>::recDelete(t->ch[0]);
MergeTree<H-1>::recDelete(t->ch[1]);
}
};
template<>
struct MergeTree<-1> {
// No data :)
MergeTree() {}
MergeTree(int v) {}
static int getSize(MergeTree<-1>* t) { return t != nullptr; };
void apply(uint t0, uint t1) {}
void push() {}
void update() {}
static MergeTree<-1>* merge(MergeTree<-1>* a, MergeTree<-1>* b) {
if (! a) return b;
else if (b) delete b;
return a;
}
static pair<MergeTree<-1>*, MergeTree<-1>*> split(MergeTree<-1>* a, uint v) {
return {nullptr, a};
}
static void recDelete(MergeTree<-1>* t) {
if (t) delete t;
}
};
template<int H>
void andTree(MergeTree<H>* t, uint x) {
if (t == nullptr) return;
bool cont = 0;
for (int j = 0; j <= H; ++j) cont |= ((! (x & (1 << j))) && (t->branch_cou[j] > 0));
if (! cont) t->apply(0, x);
else {
t->push();
if (! (x & (1 << H))) {
t->ch[0] = MergeTree<H-1>::merge(t->ch[0], t->ch[1]);
t->ch[1] = nullptr;
}
andTree<H-1>(t->ch[0], x);
andTree<H-1>(t->ch[1], x);
t->update();
}
}
template<>
void andTree<-1>(MergeTree<-1>* t, uint x) { return; }
template<int H>
void orTree(MergeTree<H>* t, uint x) {
if (t == nullptr) return;
bool cont = 0;
for (int j = 0; j <= H; ++j) cont |= ((x & (1 << j)) && (t->branch_cou[j] > 0));
if (! cont) {
t->apply(x, -1);
} else {
t->push();
if (x & (1 << H)) {
t->ch[1] = MergeTree<H-1>::merge(t->ch[0], t->ch[1]);
t->ch[0] = nullptr;
}
orTree<H-1>(t->ch[0], x);
orTree<H-1>(t->ch[1], x);
t->update();
}
}
template<>
void orTree<-1>(MergeTree<-1>* t, uint x) { return; }
template<int H>
void printTree(MergeTree<H>* a, uint ind = 0) {
if (a == nullptr) return;
a->push();
printTree<H-1>(a->ch[0], ind);
printTree<H-1>(a->ch[1], ind | (1 << H));
}
template<>
void printTree(MergeTree<-1>* a, uint ind) {
if (a != nullptr) cerr << ind << ' ';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
// Numbers up to 2^20
// 1. AND all a_i with L <= a_i <= R with x
// 2. OR all a_i with L <= a_i <= R with x
// 3. XOR all a_i with L <= a_i <= R with x
// 4. Count DISTINCT a_i with L <= a_i <= R
const int H = 21;
MergeTree<H>* root = nullptr;
for (int i = 0; i < n; ++i) {
uint v;
cin >> v;
root = MergeTree<H>::merge(root, new MergeTree<H>(v));
}
/*
printTree<H>(root);
cerr << '\n';
*/
for (int qi = 0; qi < q; ++qi) {
uint t, a, b;
cin >> t >> a >> b;
++b;
MergeTree<H> *le = nullptr, *ri = nullptr;
tie(le, root) = MergeTree<H>::split(root, a);
if (b < (1 << H)) tie(root, ri) = MergeTree<H>::split(root, b);
if (t <= 3) {
uint x;
cin >> x;
if (root != nullptr) {
if (t == 1) andTree<H>(root, x);
if (t == 2) orTree<H>(root, x);
if (t == 3) root->apply(x, ~x);
}
} else {
cout << MergeTree<H>::getSize(root) << '\n';
}
root = MergeTree<H>::merge(le, root);
root = MergeTree<H>::merge(root, ri);
/*
printTree<H>(root);
cerr << '\n';
*/
}
MergeTree<H>::recDelete(root);
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
template<int H>
struct MergeTree {
array<MergeTree<H-1>*, 2> ch;
array<int, H+1> branch_cou;
// (01 -> no change), (00 -> set 0), (11 -> set 1), (10 -> flip)
uint s0 = 0, s1 = -1;
uint size = 0;
MergeTree() : branch_cou(), ch{nullptr, nullptr} {}
MergeTree(int v) : branch_cou(), size(1) {
int b = (bool)(v & (1 << H));
ch[b] = new MergeTree<H-1>(v);
ch[b ^ 1] = nullptr;
}
static int getSize(MergeTree<H>* t) { return (t == nullptr ? 0 : t->size); }
void apply(uint t0, uint t1) {
uint b0 = (bool)(t0 & (1 << H));
uint b1 = (bool)(t1 & (1 << H));
if (b0 && ! b1) {
swap(ch[0], ch[1]);
} else if (b0 == b1) {
// We GUARANTEE that when we merge children here, one is nullptr
if (ch[b0] == nullptr) ch[b0] = ch[b0 ^ 1];
ch[b0 ^ 1] = nullptr;
}
s0 = (s0 | t0) & ((~s0) | t1);
s1 = (s1 & t1) | ((~s1) & t0);
}
void push() {
if (ch[0]) ch[0]->apply(s0, s1);
if (ch[1]) ch[1]->apply(s0, s1);
s0 = 0; s1 = -1;
}
void update() {
size = MergeTree<H-1>::getSize(ch[0]) + MergeTree<H-1>::getSize(ch[1]);
if (ch[0] && ch[1]) {
branch_cou[H] = 1;
if constexpr(H > 0) {
for (int j = 0; j < H; ++j) {
branch_cou[j] = ch[0]->branch_cou[j] + ch[1]->branch_cou[j];
}
}
} else {
branch_cou[H] = 0;
if constexpr(H > 0) {
if (ch[0]) {
for (int j = 0; j < H; ++j) branch_cou[j] = ch[0]->branch_cou[j];
} else if (ch[1]) {
for (int j = 0; j < H; ++j) branch_cou[j] = ch[1]->branch_cou[j];
} else {
for (int j = 0; j < H; ++j) branch_cou[j] = 0;
}
}
}
}
static MergeTree<H>* merge(MergeTree<H>* a, MergeTree<H>* b) {
if (!a) return b;
if (!b) return a;
a->push(), b->push();
a->ch[0] = MergeTree<H-1>::merge(a->ch[0], b->ch[0]);
a->ch[1] = MergeTree<H-1>::merge(a->ch[1], b->ch[1]);
a->update();
delete b;
return a;
}
static pair<MergeTree<H>*, MergeTree<H>*> split(MergeTree<H>* a, uint v) {
if (! a) return {nullptr, nullptr};
MergeTree<H>* b = nullptr;
a->push();
if (v & (1 << H)) {
auto sub = MergeTree<H-1>::split(a->ch[1], v);
a->ch[1] = sub.first;
a->update();
if (sub.second != nullptr) {
b = new MergeTree<H>();
b->ch[1] = sub.second;
b->update();
}
return {a, b};
} else {
auto sub = MergeTree<H-1>::split(a->ch[0], v);
a->ch[0] = sub.second;
a->update();
if (sub.first != nullptr) {
b = new MergeTree<H>();
b->ch[0] = sub.first;
b->update();
}
return {b, a};
}
}
static void recDelete(MergeTree<H>* t) {
if (! t) return;
MergeTree<H-1>::recDelete(t->ch[0]);
MergeTree<H-1>::recDelete(t->ch[1]);
}
};
template<>
struct MergeTree<-1> {
// No data :)
MergeTree() {}
MergeTree(int v) {}
static int getSize(MergeTree<-1>* t) { return t != nullptr; };
void apply(uint t0, uint t1) {}
void push() {}
void update() {}
static MergeTree<-1>* merge(MergeTree<-1>* a, MergeTree<-1>* b) {
if (! a) return b;
else if (b) delete b;
return a;
}
static pair<MergeTree<-1>*, MergeTree<-1>*> split(MergeTree<-1>* a, uint v) {
return {nullptr, a};
}
static void recDelete(MergeTree<-1>* t) {
if (t) delete t;
}
};
template<int H>
void andTree(MergeTree<H>* t, uint x) {
if (t == nullptr) return;
bool cont = 0;
for (int j = 0; j <= H; ++j) cont |= ((! (x & (1 << j))) && (t->branch_cou[j] > 0));
if (! cont) t->apply(0, x);
else {
t->push();
if (! (x & (1 << H))) {
t->ch[0] = MergeTree<H-1>::merge(t->ch[0], t->ch[1]);
t->ch[1] = nullptr;
}
andTree<H-1>(t->ch[0], x);
andTree<H-1>(t->ch[1], x);
t->update();
}
}
template<>
void andTree<-1>(MergeTree<-1>* t, uint x) { return; }
template<int H>
void orTree(MergeTree<H>* t, uint x) {
if (t == nullptr) return;
bool cont = 0;
for (int j = 0; j <= H; ++j) cont |= ((x & (1 << j)) && (t->branch_cou[j] > 0));
if (! cont) {
t->apply(x, -1);
} else {
t->push();
if (x & (1 << H)) {
t->ch[1] = MergeTree<H-1>::merge(t->ch[0], t->ch[1]);
t->ch[0] = nullptr;
}
orTree<H-1>(t->ch[0], x);
orTree<H-1>(t->ch[1], x);
t->update();
}
}
template<>
void orTree<-1>(MergeTree<-1>* t, uint x) { return; }
template<int H>
void printTree(MergeTree<H>* a, uint ind = 0) {
if (a == nullptr) return;
a->push();
printTree<H-1>(a->ch[0], ind);
printTree<H-1>(a->ch[1], ind | (1 << H));
}
template<>
void printTree(MergeTree<-1>* a, uint ind) {
if (a != nullptr) cerr << ind << ' ';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
// Numbers up to 2^20
// 1. AND all a_i with L <= a_i <= R with x
// 2. OR all a_i with L <= a_i <= R with x
// 3. XOR all a_i with L <= a_i <= R with x
// 4. Count DISTINCT a_i with L <= a_i <= R
const int H = 21;
MergeTree<H>* root = nullptr;
for (int i = 0; i < n; ++i) {
uint v;
cin >> v;
root = MergeTree<H>::merge(root, new MergeTree<H>(v));
}
/*
printTree<H>(root);
cerr << '\n';
*/
for (int qi = 0; qi < q; ++qi) {
uint t, a, b;
cin >> t >> a >> b;
++b;
MergeTree<H> *le = nullptr, *ri = nullptr;
tie(le, root) = MergeTree<H>::split(root, a);
if (b < (1 << H)) tie(root, ri) = MergeTree<H>::split(root, b);
if (t <= 3) {
uint x;
cin >> x;
if (root != nullptr) {
if (t == 1) andTree<H>(root, x);
if (t == 2) orTree<H>(root, x);
if (t == 3) root->apply(x, ~x);
}
} else {
cout << MergeTree<H>::getSize(root) << '\n';
}
root = MergeTree<H>::merge(le, root);
root = MergeTree<H>::merge(root, ri);
/*
printTree<H>(root);
cerr << '\n';
*/
}
MergeTree<H>::recDelete(root);
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxlog=19;
const int maxnode=(1<<21)+((maxlog+5)<<1);
int rt,tag[maxnode][2],frk[maxnode],ch[maxnode][2],sz[maxnode],sta[maxnode],top,tot; // 0000->tag[0] 1111->tag[1] frk:can merge
// time complexity: potetion:#nodes, ~delta*log
inline int newnode() { return ((top>=0)?assert(sta[top]),sta[top--]:++tot); }
// { return ++tot; }
inline void pushup(int rt,int l)
{
if(!rt) return;
assert(l>=0);
sz[rt]=sz[ch[rt][0]]+sz[ch[rt][1]];
frk[rt]=frk[ch[rt][0]]|frk[ch[rt][1]]|((ch[rt][0] && ch[rt][1])<<l);
}
void push(int rt,int l,int c0,int c1)
{
if(!rt) return;
// if(((c0>>l)&1) || !((c1>>l)&1)) swap(ch[rt][0],ch[rt][1]);
if(!(((c0^c1)>>l)&1))
{
assert(!ch[rt][0] || !ch[rt][1]);
ch[rt][(c0>>l)&1]|=ch[rt][!((c0>>l)&1)];
ch[rt][!((c0>>l)&1)]=0;
}
else if((c0>>l)&1) swap(ch[rt][0],ch[rt][1]);
int _=((~tag[rt][0])&c0)|(tag[rt][0]&c1);
tag[rt][1]=((~tag[rt][1])&c0)|(tag[rt][1]&c1);
tag[rt][0]=_;
}
inline void pushdown(int rt,int l)
{
if(!rt || !l) return;
if(tag[rt][0]==0 && tag[rt][1]==-1) return;
push(ch[rt][0],l-1,tag[rt][0],tag[rt][1]),push(ch[rt][1],l-1,tag[rt][0],tag[rt][1]);
tag[rt][0]=0,tag[rt][1]=-1;
}
void insert(int &rt,int l,int x)
{
if(!rt) rt=++tot,ch[rt][0]=ch[rt][1]=0,frk[rt]=0,tag[rt][0]=0,tag[rt][1]=-1;
if(l<0) { sz[rt]=1; return; }
insert(ch[rt][(x>>l)&1],l-1,x),pushup(rt,l);
}
void split(int &rt1,int &rt2,int L,int l,int r,int x,int y)
{
if(x<=l && r<=y) { rt2=rt1,rt1=0; return; }
if(!rt1) { rt2=0; return; }
int mid=(l+r)>>1; pushdown(rt1,L);
rt2=newnode(),ch[rt2][0]=ch[rt2][1]=0,frk[rt2]=0,tag[rt2][0]=0,tag[rt2][1]=-1;
assert(rt2);
if(x<=mid) split(ch[rt1][0],ch[rt2][0],L-1,l,mid,x,y);
if(mid<y) split(ch[rt1][1],ch[rt2][1],L-1,mid+1,r,x,y);
pushup(rt1,L),pushup(rt2,L);
if(!sz[rt1]) assert(rt1),sta[++top]=rt1,rt1=0; // dprintf("- %d\n",sta[top]);
if(!sz[rt2]) assert(rt2),sta[++top]=rt2,rt2=0; // dprintf("- %d\n",sta[top]);
}
void merge(int &rt1,int rt2,int l)
{
if(!rt1 || !rt2) { rt1|=rt2; return; }
if(l<0) { sta[++top]=rt2/*,dprintf("- %d\n",sta[top])*/; return; }
pushdown(rt1,l),pushdown(rt2,l);
merge(ch[rt1][0],ch[rt2][0],l-1),merge(ch[rt1][1],ch[rt2][1],l-1);
pushup(rt1,l),sta[++top]=rt2/*,dprintf("- %d\n",sta[top])*/;
// printf(".!. %d\n",sz[rt1]);
}
void merge(int rt,int l,int x,int y)
{
if(!rt) return;
if(!((frk[rt]>>x)&1)) { push(rt,l,y<<x,~((y^1)<<x)); /*printf("... %d %d %d %d\n",l,x,y,frk[rt]);*/ return; }
pushdown(rt,l);
if(l==x)
{
assert(ch[rt][0] && ch[rt][1]);
merge(ch[rt][y],ch[rt][y^1],l-1);
/*sta[++top]=ch[rt][y^1],*/ch[rt][y^1]=0;//printf("- %d\n",sta[top]);
}
else merge(ch[rt][0],l-1,x,y),merge(ch[rt][1],l-1,x,y);
pushup(rt,l);
}
int query(int rt,int L,int l,int r,int x,int y)
{
if(!rt) return 0;
if(x<=l && r<=y) return sz[rt];
int mid=(l+r)>>1,res=0; pushdown(rt,L);
if(x<=mid) res+=query(ch[rt][0],L-1,l,mid,x,y);
if(mid<y) res+=query(ch[rt][1],L-1,mid+1,r,x,y);
return res;
}
void print(int rt,int L,int l,int r)
{
if(!rt) return;
if(l==r) { assert(sz[rt]==1); printf("%d ",l); return; }
int mid=(l+r)>>1; pushdown(rt,L);
// printf("%d %d\n%d %d\n",rt,ch[rt][0],rt,ch[rt][1]);
print(ch[rt][0],L-1,l,mid),print(ch[rt][1],L-1,mid+1,r);
}
int main()
{
int n,q,l,r,x,t;
scanf("%d%d",&n,&q);
rt=0,ch[0][0]=ch[0][1]=0,sz[0]=0,frk[rt]=0,tot=0,top=-1;
while(n--) scanf("%d",&x),insert(rt,maxlog,x);
while(q--)
{
scanf("%d%d%d",&t,&l,&r);
if(t!=4)
{
scanf("%d",&x);
split(rt,n,maxlog,0,(2<<maxlog)-1,l,r);
// printf("??? %d: ",sz[n]); print(n,maxlog,0,(2<<maxlog)-1); printf("\n");
if(t==1)
{
for(l=0;l<=maxlog;l++)
if(!((x>>l)&1))
merge(n,maxlog,l,0);//printf("%d: ",l),print(n,maxlog,0,(2<<maxlog)-1),printf("\n");
}
else if(t==2)
{
for(l=0;l<=maxlog;l++)
if((x>>l)&1)
merge(n,maxlog,l,1);
}
else push(n,maxlog,x,~x);
// printf("??? %d: ",sz[rt]); print(rt,maxlog,0,(2<<maxlog)-1); printf("\n");
// printf("??? %d: ",sz[n]); print(n,maxlog,0,(2<<maxlog)-1); printf("\n");
merge(rt,n,maxlog);
// printf("!!! %d: ",sz[rt]); print(rt,maxlog,0,(2<<maxlog)-1); printf("\n");
}
else printf("%d\n",query(rt,maxlog,0,(2<<maxlog)-1,l,r));
}
return 0;
} | ### Prompt
Please provide a Cpp coded solution to the problem described below:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxlog=19;
const int maxnode=(1<<21)+((maxlog+5)<<1);
int rt,tag[maxnode][2],frk[maxnode],ch[maxnode][2],sz[maxnode],sta[maxnode],top,tot; // 0000->tag[0] 1111->tag[1] frk:can merge
// time complexity: potetion:#nodes, ~delta*log
inline int newnode() { return ((top>=0)?assert(sta[top]),sta[top--]:++tot); }
// { return ++tot; }
inline void pushup(int rt,int l)
{
if(!rt) return;
assert(l>=0);
sz[rt]=sz[ch[rt][0]]+sz[ch[rt][1]];
frk[rt]=frk[ch[rt][0]]|frk[ch[rt][1]]|((ch[rt][0] && ch[rt][1])<<l);
}
void push(int rt,int l,int c0,int c1)
{
if(!rt) return;
// if(((c0>>l)&1) || !((c1>>l)&1)) swap(ch[rt][0],ch[rt][1]);
if(!(((c0^c1)>>l)&1))
{
assert(!ch[rt][0] || !ch[rt][1]);
ch[rt][(c0>>l)&1]|=ch[rt][!((c0>>l)&1)];
ch[rt][!((c0>>l)&1)]=0;
}
else if((c0>>l)&1) swap(ch[rt][0],ch[rt][1]);
int _=((~tag[rt][0])&c0)|(tag[rt][0]&c1);
tag[rt][1]=((~tag[rt][1])&c0)|(tag[rt][1]&c1);
tag[rt][0]=_;
}
inline void pushdown(int rt,int l)
{
if(!rt || !l) return;
if(tag[rt][0]==0 && tag[rt][1]==-1) return;
push(ch[rt][0],l-1,tag[rt][0],tag[rt][1]),push(ch[rt][1],l-1,tag[rt][0],tag[rt][1]);
tag[rt][0]=0,tag[rt][1]=-1;
}
void insert(int &rt,int l,int x)
{
if(!rt) rt=++tot,ch[rt][0]=ch[rt][1]=0,frk[rt]=0,tag[rt][0]=0,tag[rt][1]=-1;
if(l<0) { sz[rt]=1; return; }
insert(ch[rt][(x>>l)&1],l-1,x),pushup(rt,l);
}
void split(int &rt1,int &rt2,int L,int l,int r,int x,int y)
{
if(x<=l && r<=y) { rt2=rt1,rt1=0; return; }
if(!rt1) { rt2=0; return; }
int mid=(l+r)>>1; pushdown(rt1,L);
rt2=newnode(),ch[rt2][0]=ch[rt2][1]=0,frk[rt2]=0,tag[rt2][0]=0,tag[rt2][1]=-1;
assert(rt2);
if(x<=mid) split(ch[rt1][0],ch[rt2][0],L-1,l,mid,x,y);
if(mid<y) split(ch[rt1][1],ch[rt2][1],L-1,mid+1,r,x,y);
pushup(rt1,L),pushup(rt2,L);
if(!sz[rt1]) assert(rt1),sta[++top]=rt1,rt1=0; // dprintf("- %d\n",sta[top]);
if(!sz[rt2]) assert(rt2),sta[++top]=rt2,rt2=0; // dprintf("- %d\n",sta[top]);
}
void merge(int &rt1,int rt2,int l)
{
if(!rt1 || !rt2) { rt1|=rt2; return; }
if(l<0) { sta[++top]=rt2/*,dprintf("- %d\n",sta[top])*/; return; }
pushdown(rt1,l),pushdown(rt2,l);
merge(ch[rt1][0],ch[rt2][0],l-1),merge(ch[rt1][1],ch[rt2][1],l-1);
pushup(rt1,l),sta[++top]=rt2/*,dprintf("- %d\n",sta[top])*/;
// printf(".!. %d\n",sz[rt1]);
}
void merge(int rt,int l,int x,int y)
{
if(!rt) return;
if(!((frk[rt]>>x)&1)) { push(rt,l,y<<x,~((y^1)<<x)); /*printf("... %d %d %d %d\n",l,x,y,frk[rt]);*/ return; }
pushdown(rt,l);
if(l==x)
{
assert(ch[rt][0] && ch[rt][1]);
merge(ch[rt][y],ch[rt][y^1],l-1);
/*sta[++top]=ch[rt][y^1],*/ch[rt][y^1]=0;//printf("- %d\n",sta[top]);
}
else merge(ch[rt][0],l-1,x,y),merge(ch[rt][1],l-1,x,y);
pushup(rt,l);
}
int query(int rt,int L,int l,int r,int x,int y)
{
if(!rt) return 0;
if(x<=l && r<=y) return sz[rt];
int mid=(l+r)>>1,res=0; pushdown(rt,L);
if(x<=mid) res+=query(ch[rt][0],L-1,l,mid,x,y);
if(mid<y) res+=query(ch[rt][1],L-1,mid+1,r,x,y);
return res;
}
void print(int rt,int L,int l,int r)
{
if(!rt) return;
if(l==r) { assert(sz[rt]==1); printf("%d ",l); return; }
int mid=(l+r)>>1; pushdown(rt,L);
// printf("%d %d\n%d %d\n",rt,ch[rt][0],rt,ch[rt][1]);
print(ch[rt][0],L-1,l,mid),print(ch[rt][1],L-1,mid+1,r);
}
int main()
{
int n,q,l,r,x,t;
scanf("%d%d",&n,&q);
rt=0,ch[0][0]=ch[0][1]=0,sz[0]=0,frk[rt]=0,tot=0,top=-1;
while(n--) scanf("%d",&x),insert(rt,maxlog,x);
while(q--)
{
scanf("%d%d%d",&t,&l,&r);
if(t!=4)
{
scanf("%d",&x);
split(rt,n,maxlog,0,(2<<maxlog)-1,l,r);
// printf("??? %d: ",sz[n]); print(n,maxlog,0,(2<<maxlog)-1); printf("\n");
if(t==1)
{
for(l=0;l<=maxlog;l++)
if(!((x>>l)&1))
merge(n,maxlog,l,0);//printf("%d: ",l),print(n,maxlog,0,(2<<maxlog)-1),printf("\n");
}
else if(t==2)
{
for(l=0;l<=maxlog;l++)
if((x>>l)&1)
merge(n,maxlog,l,1);
}
else push(n,maxlog,x,~x);
// printf("??? %d: ",sz[rt]); print(rt,maxlog,0,(2<<maxlog)-1); printf("\n");
// printf("??? %d: ",sz[n]); print(n,maxlog,0,(2<<maxlog)-1); printf("\n");
merge(rt,n,maxlog);
// printf("!!! %d: ",sz[rt]); print(rt,maxlog,0,(2<<maxlog)-1); printf("\n");
}
else printf("%d\n",query(rt,maxlog,0,(2<<maxlog)-1,l,r));
}
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
const int N=1234568,B=(1<<20)-1; bool fl[N]; int op,l,r,x;
int n,m,ql,qr,rt,ans,A,q[N],fa[N],s1[N],s2[N],ch[N][2],dep[N],f1[N],f2[N],f3[N],qt[N],qc[N];
inline bool get(int u){
return (ch[fa[u]][1]==u);
}
inline void upd(int u){
if(dep[u]==-1) return;
if(!ch[u][1]) f1[u]=f1[ch[u][0]],f2[u]=f2[ch[u][0]],s1[u]=s1[ch[u][0]],s2[u]=s2[ch[u][0]];
else if(!ch[u][0]) f1[u]=f1[ch[u][1]],f2[u]=f2[ch[u][1]],s1[u]=s1[ch[u][1]],s2[u]=s2[ch[u][1]];
else{
f1[u]=(f1[ch[u][0]]&f1[ch[u][1]]);
f2[u]=(f2[ch[u][0]]&f2[ch[u][1]]);
s1[u]=(s1[ch[u][0]]+s1[ch[u][1]]);
s2[u]=(s2[ch[u][0]]+s2[ch[u][1]]);
}
}
inline void psh(int u){
if(f3[u]&(1<<dep[u])) swap(ch[u][0],ch[u][1]);
f3[ch[u][0]]^=f3[u],f3[ch[u][1]]^=f3[u];
int a=f1[u],b=f2[u];
f1[u]=(b&f3[u])|(a&(B^f3[u]));
f2[u]=(a&f3[u])|(b&(B^f3[u]));
f3[u]=0;
}
inline void pushdown(int u){
psh(u),psh(ch[u][0]),psh(ch[u][1]);
}
inline int newnode(){
ql++; if(ql==1234567) ql=0; fl[q[ql]]=1; return q[ql];
}
inline void delnode(int u){
if(u<=1) return;
qr++; if(qr==1234567) qr=0;
ch[fa[u]][get(u)]=0;
if(fa[u]){
upd(fa[u]);
}
f1[u]=f2[u]=f3[u]=ch[u][0]=ch[u][1]=fa[u]=s1[u]=s2[u]=dep[u]=0,q[qr]=u,fl[u]=0;
}
inline void ins(int u,int c){
if(dep[u]==-1){
s1[u]=1,s2[u]++,f1[u]=(c^B),f2[u]=c; return;
}
if(c&(1<<dep[u])){
if(!ch[u][1]) ch[u][1]=newnode(),dep[ch[u][1]]=dep[u]-1,fa[ch[u][1]]=u;
ins(ch[u][1],c);
}
else{
if(!ch[u][0]) ch[u][0]=newnode(),dep[ch[u][0]]=dep[u]-1,fa[ch[u][0]]=u;
ins(ch[u][0],c);
}
upd(u);
}
inline int chk(int A,int op,int x,int y,int d){
if(op==1) x=(x&y);
if(op==2) x=(x|y);
if(op==3) x=(x^y);
if(x) A=A|(1<<d); else A=(((A^B)|(1<<d))^B);
return A;
}
inline void bfs(int t,int l,int r,int ql,int qr){
pushdown(t);
if(l==ql&&r==qr){
if(op<=3){
qt[++qt[0]]=t,qc[qt[0]]=A;
ch[fa[t]][get(t)]=0,upd(fa[t]);
fa[t]=0;
}
else ans+=s1[t];
return;
}
int d=(l+r)>>1;
if(ql<=d){
if(ch[t][0]){
A=chk(A,op,(x&(1<<dep[t]))>0,0,dep[t]);
bfs(ch[t][0],l,d,ql,min(d,qr));
}
}
if(d+1<=qr){
if(ch[t][1]){
A=chk(A,op,(x&(1<<dep[t]))>0,1,dep[t]);
bfs(ch[t][1],d+1,r,max(d+1,ql),qr);
}
}
upd(t);
}
inline int merge(int u,int t){
pushdown(u);
pushdown(t);
if(!s1[u]){
delnode(u); return t;
}
if(!s1[t]){
delnode(t); return u;
}
if(!u||!t) return (u+t);
if(dep[u]==-1){
s1[u]=1,s2[u]=s1[u]+s2[t]; delnode(t);
return u;
}
ch[u][0]=merge(ch[u][0],ch[t][0]);
ch[u][1]=merge(ch[u][1],ch[t][1]);
delnode(t),upd(u);
fa[ch[u][0]]=u,fa[ch[u][1]]=u;
return u;
}
inline void AND(int u,int c){
pushdown(u);
if(((f1[u]|f2[u])&(B^c))==(B^c)){
int C=0;
for(int i=0;i<=19;i++) if(!((1<<i)&c)&&((1<<i)&f2[u])) f3[u]^=(1<<i);
pushdown(u); return;
}
if(ch[u][0]) AND(ch[u][0],c);
if(ch[u][1]) AND(ch[u][1],c);
if(!(c&(1<<dep[u]))) ch[u][0]=merge(ch[u][0],ch[u][1]),ch[u][1]=0;
upd(u);
}
inline void OR(int u,int c){
pushdown(u);
if(((f1[u]|f2[u])&c)==c){
int C=0;
for(int i=0;i<=19;i++) if(((1<<i)&c)&&((1<<i)&f1[u])) f3[u]^=(1<<i);
pushdown(u); return;
}
if(ch[u][0]) OR(ch[u][0],c);
if(ch[u][1]) OR(ch[u][1],c);
if(c&(1<<dep[u])) ch[u][1]=merge(ch[u][0],ch[u][1]),ch[u][0]=0;
upd(u);
}
inline int dfs(int u,int A,int B){
pushdown(u);
if(dep[u]==dep[A]) return merge(A,u);
if(B&(1<<dep[u])){
if(!ch[u][1]) ch[u][1]=newnode(),dep[ch[u][1]]=dep[u]-1,fa[ch[u][1]]=u;
ch[u][1]=dfs(ch[u][1],A,B);
fa[ch[u][1]]=u;
}
else{
if(!ch[u][0]) ch[u][0]=newnode(),dep[ch[u][0]]=dep[u]-1,fa[ch[u][0]]=u;
ch[u][0]=dfs(ch[u][0],A,B);
fa[ch[u][0]]=u;
}
upd(u);
return u;
}
int main(){
for(int i=2;i<=1234567;i++) qr++,q[qr]=i;
cin>>n>>m,rt=1,dep[rt]=19;
for(int i=1;i<=n;i++) scanf("%d",&x),ins(rt,x);
while(m--){
scanf("%d%d%d",&op,&l,&r);
if(op==1){
scanf("%d",&x),qt[0]=0,A=0,bfs(rt,0,B,l,r);
for(int i=1;i<=qt[0];i++){
AND(qt[i],x);
if(qt[i]==1) continue;
rt=dfs(rt,qt[i],qc[i]);
}
}
if(op==2){
scanf("%d",&x),qt[0]=0,A=0,bfs(rt,0,B,l,r);
for(int i=1;i<=qt[0];i++){
OR(qt[i],x);
if(qt[i]==1) continue;
rt=dfs(rt,qt[i],qc[i]);
}
}
if(op==3){
scanf("%d",&x),qt[0]=0,A=0,bfs(rt,0,B,l,r);
for(int i=1;i<=qt[0];i++){
f3[qt[i]]^=x,pushdown(qt[i]);
if(qt[i]==1) continue;
rt=dfs(rt,qt[i],qc[i]);
}
}
if(op==4){
ans=0,bfs(rt,0,B,l,r);
printf("%d\n",ans);
}
}
}
| ### Prompt
Please create a solution in cpp to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
const int N=1234568,B=(1<<20)-1; bool fl[N]; int op,l,r,x;
int n,m,ql,qr,rt,ans,A,q[N],fa[N],s1[N],s2[N],ch[N][2],dep[N],f1[N],f2[N],f3[N],qt[N],qc[N];
inline bool get(int u){
return (ch[fa[u]][1]==u);
}
inline void upd(int u){
if(dep[u]==-1) return;
if(!ch[u][1]) f1[u]=f1[ch[u][0]],f2[u]=f2[ch[u][0]],s1[u]=s1[ch[u][0]],s2[u]=s2[ch[u][0]];
else if(!ch[u][0]) f1[u]=f1[ch[u][1]],f2[u]=f2[ch[u][1]],s1[u]=s1[ch[u][1]],s2[u]=s2[ch[u][1]];
else{
f1[u]=(f1[ch[u][0]]&f1[ch[u][1]]);
f2[u]=(f2[ch[u][0]]&f2[ch[u][1]]);
s1[u]=(s1[ch[u][0]]+s1[ch[u][1]]);
s2[u]=(s2[ch[u][0]]+s2[ch[u][1]]);
}
}
inline void psh(int u){
if(f3[u]&(1<<dep[u])) swap(ch[u][0],ch[u][1]);
f3[ch[u][0]]^=f3[u],f3[ch[u][1]]^=f3[u];
int a=f1[u],b=f2[u];
f1[u]=(b&f3[u])|(a&(B^f3[u]));
f2[u]=(a&f3[u])|(b&(B^f3[u]));
f3[u]=0;
}
inline void pushdown(int u){
psh(u),psh(ch[u][0]),psh(ch[u][1]);
}
inline int newnode(){
ql++; if(ql==1234567) ql=0; fl[q[ql]]=1; return q[ql];
}
inline void delnode(int u){
if(u<=1) return;
qr++; if(qr==1234567) qr=0;
ch[fa[u]][get(u)]=0;
if(fa[u]){
upd(fa[u]);
}
f1[u]=f2[u]=f3[u]=ch[u][0]=ch[u][1]=fa[u]=s1[u]=s2[u]=dep[u]=0,q[qr]=u,fl[u]=0;
}
inline void ins(int u,int c){
if(dep[u]==-1){
s1[u]=1,s2[u]++,f1[u]=(c^B),f2[u]=c; return;
}
if(c&(1<<dep[u])){
if(!ch[u][1]) ch[u][1]=newnode(),dep[ch[u][1]]=dep[u]-1,fa[ch[u][1]]=u;
ins(ch[u][1],c);
}
else{
if(!ch[u][0]) ch[u][0]=newnode(),dep[ch[u][0]]=dep[u]-1,fa[ch[u][0]]=u;
ins(ch[u][0],c);
}
upd(u);
}
inline int chk(int A,int op,int x,int y,int d){
if(op==1) x=(x&y);
if(op==2) x=(x|y);
if(op==3) x=(x^y);
if(x) A=A|(1<<d); else A=(((A^B)|(1<<d))^B);
return A;
}
inline void bfs(int t,int l,int r,int ql,int qr){
pushdown(t);
if(l==ql&&r==qr){
if(op<=3){
qt[++qt[0]]=t,qc[qt[0]]=A;
ch[fa[t]][get(t)]=0,upd(fa[t]);
fa[t]=0;
}
else ans+=s1[t];
return;
}
int d=(l+r)>>1;
if(ql<=d){
if(ch[t][0]){
A=chk(A,op,(x&(1<<dep[t]))>0,0,dep[t]);
bfs(ch[t][0],l,d,ql,min(d,qr));
}
}
if(d+1<=qr){
if(ch[t][1]){
A=chk(A,op,(x&(1<<dep[t]))>0,1,dep[t]);
bfs(ch[t][1],d+1,r,max(d+1,ql),qr);
}
}
upd(t);
}
inline int merge(int u,int t){
pushdown(u);
pushdown(t);
if(!s1[u]){
delnode(u); return t;
}
if(!s1[t]){
delnode(t); return u;
}
if(!u||!t) return (u+t);
if(dep[u]==-1){
s1[u]=1,s2[u]=s1[u]+s2[t]; delnode(t);
return u;
}
ch[u][0]=merge(ch[u][0],ch[t][0]);
ch[u][1]=merge(ch[u][1],ch[t][1]);
delnode(t),upd(u);
fa[ch[u][0]]=u,fa[ch[u][1]]=u;
return u;
}
inline void AND(int u,int c){
pushdown(u);
if(((f1[u]|f2[u])&(B^c))==(B^c)){
int C=0;
for(int i=0;i<=19;i++) if(!((1<<i)&c)&&((1<<i)&f2[u])) f3[u]^=(1<<i);
pushdown(u); return;
}
if(ch[u][0]) AND(ch[u][0],c);
if(ch[u][1]) AND(ch[u][1],c);
if(!(c&(1<<dep[u]))) ch[u][0]=merge(ch[u][0],ch[u][1]),ch[u][1]=0;
upd(u);
}
inline void OR(int u,int c){
pushdown(u);
if(((f1[u]|f2[u])&c)==c){
int C=0;
for(int i=0;i<=19;i++) if(((1<<i)&c)&&((1<<i)&f1[u])) f3[u]^=(1<<i);
pushdown(u); return;
}
if(ch[u][0]) OR(ch[u][0],c);
if(ch[u][1]) OR(ch[u][1],c);
if(c&(1<<dep[u])) ch[u][1]=merge(ch[u][0],ch[u][1]),ch[u][0]=0;
upd(u);
}
inline int dfs(int u,int A,int B){
pushdown(u);
if(dep[u]==dep[A]) return merge(A,u);
if(B&(1<<dep[u])){
if(!ch[u][1]) ch[u][1]=newnode(),dep[ch[u][1]]=dep[u]-1,fa[ch[u][1]]=u;
ch[u][1]=dfs(ch[u][1],A,B);
fa[ch[u][1]]=u;
}
else{
if(!ch[u][0]) ch[u][0]=newnode(),dep[ch[u][0]]=dep[u]-1,fa[ch[u][0]]=u;
ch[u][0]=dfs(ch[u][0],A,B);
fa[ch[u][0]]=u;
}
upd(u);
return u;
}
int main(){
for(int i=2;i<=1234567;i++) qr++,q[qr]=i;
cin>>n>>m,rt=1,dep[rt]=19;
for(int i=1;i<=n;i++) scanf("%d",&x),ins(rt,x);
while(m--){
scanf("%d%d%d",&op,&l,&r);
if(op==1){
scanf("%d",&x),qt[0]=0,A=0,bfs(rt,0,B,l,r);
for(int i=1;i<=qt[0];i++){
AND(qt[i],x);
if(qt[i]==1) continue;
rt=dfs(rt,qt[i],qc[i]);
}
}
if(op==2){
scanf("%d",&x),qt[0]=0,A=0,bfs(rt,0,B,l,r);
for(int i=1;i<=qt[0];i++){
OR(qt[i],x);
if(qt[i]==1) continue;
rt=dfs(rt,qt[i],qc[i]);
}
}
if(op==3){
scanf("%d",&x),qt[0]=0,A=0,bfs(rt,0,B,l,r);
for(int i=1;i<=qt[0];i++){
f3[qt[i]]^=x,pushdown(qt[i]);
if(qt[i]==1) continue;
rt=dfs(rt,qt[i],qc[i]);
}
}
if(op==4){
ans=0,bfs(rt,0,B,l,r);
printf("%d\n",ans);
}
}
}
``` |
/*
author: Maksim1744
created: 06.05.2021 16:01:27
*/
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define sum(a) ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
template<typename T> vector<T>& operator-- (vector<T> &v){for (auto& i : v) --i; return v;}
template<typename T> vector<T>& operator++ (vector<T> &v){for (auto& i : v) ++i; return v;}
template<typename T> istream& operator>>(istream& is, vector<T> &v){for (auto& i : v) is >> i; return is;}
template<typename T> ostream& operator<<(ostream& os, vector<T> v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> pair<T,U>& operator-- (pair<T, U> &p){--p.first; --p.second; return p;}
template<typename T, typename U> pair<T,U>& operator++ (pair<T, U> &p){++p.first; ++p.second; return p;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U> &p){is >> p.first >> p.second; return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U> p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}
#ifdef HOME
#define SHOW_COLORS
#include "C:/C++ libs/print.cpp"
#else
#define show(...) void(0)
#define mclock void(0)
#define shows void(0)
#define debug if (false)
#endif
const int MEMSIZE = 1e9/2;
char memory[MEMSIZE];
int memorypos;
inline void * operator new(size_t n){
if (memorypos + n >= MEMSIZE)
memorypos = MEMSIZE / 3;
char * ret = memory + memorypos;
memorypos += n;
return (void *)ret;
}
inline void operator delete(void *){}
const int B = 20;
struct Node {
vector<Node*> to;
int sz = 0;
int mod = 0;
int def0 = (1 << B) - 1;
int def1 = (1 << B) - 1;
Node() {
to.assign(2, nullptr);
}
};
ostream &operator << (ostream &o, const Node &n) {
return o << "[sz=" << n.sz << ", mod=" << n.mod
<< ", def0=" << n.def0 << ", def1=" << n.def1 << "]";
}
void add_num(Node *node, int x) {
for (int i = 0; i < B; ++i) {
node->sz++;
node->def1 &= x;
node->def0 &= ((1 << B) - 1) ^ x;
int b = ((x >> (B - i - 1)) & 1);
if (!node->to[b]) {
node->to[b] = new Node();
}
node = node->to[b];
}
node->sz++;
node->def1 &= x;
node->def0 &= ((1 << B) - 1) ^ x;
}
void update(Node *node, int level) {
if (!node) return;
node->sz = 0;
if (node->to[0]) node->sz += node->to[0]->sz;
if (node->to[1]) node->sz += node->to[1]->sz;
node->def0 = (1 << B) - 1;
node->def1 = (1 << B) - 1;
if (node->to[0]) {
node->def0 &= node->to[0]->def0;
node->def1 &= node->to[0]->def1;
}
if (node->to[1]) {
node->def0 &= node->to[1]->def0;
node->def1 &= node->to[1]->def1;
}
if (!node->to[0])
node->def1 |= (1 << (B - level - 1));
if (!node->to[1])
node->def0 |= (1 << (B - level - 1));
}
void modify(Node *node, int x) {
if (!node) return;
node->mod ^= x;
int mask = (x & (node->def0 | node->def1));
node->def0 ^= mask;
node->def1 ^= mask;
}
void push(Node *node, int level) {
if (!node) {
return;
}
if ((node->mod >> (B - level - 1)) & 1) {
show(node->level);
swap(node->to[0], node->to[1]);
}
modify(node->to[0], node->mod);
modify(node->to[1], node->mod);
node->mod = 0;
}
pair<Node*, Node*> split(Node *node, int m, int l = 0, int r = (1 << B) - 1, int level = 0) {
push(node, level);
if (!node) return {nullptr, nullptr};
if (r <= m) return {node, nullptr};
if (l > m) return {nullptr, node};
int mid = (l + r) / 2;
auto L = new Node();
auto R = new Node();
if (mid < m) {
auto [a, b] = split(node->to[1], m, mid + 1, r, level + 1);
if (b) {
R->to[1] = b;
} else {
R = nullptr;
}
if (node->to[0] || a) {
L->to = {node->to[0], a};
} else {
L = nullptr;
}
update(L, level);
update(R, level);
return {L, R};
} else {
auto [a, b] = split(node->to[0], m, l, mid, level + 1);
if (node->to[1] || b) {
R->to = {b, node->to[1]};
} else {
R = nullptr;
}
if (a) {
L->to[0] = a;
} else {
L = nullptr;
}
update(L, level);
update(R, level);
return {L, R};
}
}
Node* mrg(Node *a, Node *b, int level) {
if (!a) return b;
if (!b) return a;
if (level == B) {
assert(a->sz == 1 && b->sz == 1);
return a;
}
Node *root = a;
push(a, level);
push(b, level);
a->to[0] = mrg(a->to[0], b->to[0], level + 1);
a->to[1] = mrg(a->to[1], b->to[1], level + 1);
update(root, level);
return root;
}
void print(Node *node, int x = 0) {
debug {
if (x == 0 && node)
show(*node);
cerr << string(x, ' ') << "- ";
if (!node) {
cerr << endl;
return;
}
cerr << *node << endl;
print(node->to[0], x + 2);
print(node->to[1], x + 2);
}
}
tuple<Node*, Node*, Node*> cut(Node *root, int l, int r) {
auto [a, bc] = split(root, l - 1);
auto [b, c] = split(bc, r);
return {a, b, c};
}
Node* merge3(Node *a, Node *b, Node *c) {
return mrg(mrg(a, b, 0), c, 0);
}
void push_or_bit(Node *node, int b, int level) {
if (!node) return;
show(*node);
push(node, level);
if (level == B - b - 1) {
modify(node->to[0], 1 << b);
node->to[1] = mrg(node->to[0], node->to[1], level + 1);
node->to[0] = nullptr;
update(node, level);
return;
}
if ((node->def1 >> b) & 1) {
show("already good");
return;
}
if ((node->def0 >> b) & 1) {
modify(node, 1 << b);
return;
}
push_or_bit(node->to[0], b, level + 1);
push_or_bit(node->to[1], b, level + 1);
update(node, level);
}
void push_or(Node *root, int x) {
for (int b = 0; b < B; ++b) {
if ((x >> b) & 1) {
show(b);
push_or_bit(root, b, 0);
print(root);
}
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
Node *root = new Node();
auto call_xor = [&](int l, int r, int x) {
shows;
auto [a, b, c] = cut(root, l, r);
modify(b, x);
print(a);
print(b);
print(c);
root = merge3(a, b, c);
print(root);
};
auto call_or = [&](int l, int r, int x) {
shows;
auto [a, b, c] = cut(root, l, r);
show(l, r);
print(a);
print(b);
print(c);
push_or(b, x);
root = merge3(a, b, c);
print(root);
};
auto call_ask = [&](int l, int r) {
shows;
print(root);
auto [a, b, c] = cut(root, l, r);
print(a);
print(b);
print(c);
int res = (b ? b->sz : 0);
return res;
};
auto call_and = [&](int l, int r, int x) {
shows;
call_xor(0, (1 << B) - 1, (1 << B) - 1);
call_or(r ^ ((1 << B) - 1), l ^ ((1 << B) - 1), ((1 << B) - 1) ^ x);
call_xor(0, (1 << B) - 1, (1 << B) - 1);
};
int n, q;
cin >> n >> q;
vector<int> a(n);
cin >> a;
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
n = a.size();
for (int k : a) {
show(k);
add_num(root, k);
}
print(root);
while (q--) {
shows;
shows;
shows;
shows;
shows;
shows;
show(*root);
print(root);
shows;
int tp, l, r, x;
cin >> tp >> l >> r;
if (tp != 4) cin >> x;
if (tp == 1) {
call_and(l, r, x);
} else if (tp == 2) {
call_or(l, r, x);
} else if (tp == 3) {
call_xor(l, r, x);
} else if (tp == 4) {
cout << call_ask(l, r) << '\n';
} else {
assert(false);
}
}
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
/*
author: Maksim1744
created: 06.05.2021 16:01:27
*/
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define sum(a) ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
template<typename T> vector<T>& operator-- (vector<T> &v){for (auto& i : v) --i; return v;}
template<typename T> vector<T>& operator++ (vector<T> &v){for (auto& i : v) ++i; return v;}
template<typename T> istream& operator>>(istream& is, vector<T> &v){for (auto& i : v) is >> i; return is;}
template<typename T> ostream& operator<<(ostream& os, vector<T> v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> pair<T,U>& operator-- (pair<T, U> &p){--p.first; --p.second; return p;}
template<typename T, typename U> pair<T,U>& operator++ (pair<T, U> &p){++p.first; ++p.second; return p;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U> &p){is >> p.first >> p.second; return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U> p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}
#ifdef HOME
#define SHOW_COLORS
#include "C:/C++ libs/print.cpp"
#else
#define show(...) void(0)
#define mclock void(0)
#define shows void(0)
#define debug if (false)
#endif
const int MEMSIZE = 1e9/2;
char memory[MEMSIZE];
int memorypos;
inline void * operator new(size_t n){
if (memorypos + n >= MEMSIZE)
memorypos = MEMSIZE / 3;
char * ret = memory + memorypos;
memorypos += n;
return (void *)ret;
}
inline void operator delete(void *){}
const int B = 20;
struct Node {
vector<Node*> to;
int sz = 0;
int mod = 0;
int def0 = (1 << B) - 1;
int def1 = (1 << B) - 1;
Node() {
to.assign(2, nullptr);
}
};
ostream &operator << (ostream &o, const Node &n) {
return o << "[sz=" << n.sz << ", mod=" << n.mod
<< ", def0=" << n.def0 << ", def1=" << n.def1 << "]";
}
void add_num(Node *node, int x) {
for (int i = 0; i < B; ++i) {
node->sz++;
node->def1 &= x;
node->def0 &= ((1 << B) - 1) ^ x;
int b = ((x >> (B - i - 1)) & 1);
if (!node->to[b]) {
node->to[b] = new Node();
}
node = node->to[b];
}
node->sz++;
node->def1 &= x;
node->def0 &= ((1 << B) - 1) ^ x;
}
void update(Node *node, int level) {
if (!node) return;
node->sz = 0;
if (node->to[0]) node->sz += node->to[0]->sz;
if (node->to[1]) node->sz += node->to[1]->sz;
node->def0 = (1 << B) - 1;
node->def1 = (1 << B) - 1;
if (node->to[0]) {
node->def0 &= node->to[0]->def0;
node->def1 &= node->to[0]->def1;
}
if (node->to[1]) {
node->def0 &= node->to[1]->def0;
node->def1 &= node->to[1]->def1;
}
if (!node->to[0])
node->def1 |= (1 << (B - level - 1));
if (!node->to[1])
node->def0 |= (1 << (B - level - 1));
}
void modify(Node *node, int x) {
if (!node) return;
node->mod ^= x;
int mask = (x & (node->def0 | node->def1));
node->def0 ^= mask;
node->def1 ^= mask;
}
void push(Node *node, int level) {
if (!node) {
return;
}
if ((node->mod >> (B - level - 1)) & 1) {
show(node->level);
swap(node->to[0], node->to[1]);
}
modify(node->to[0], node->mod);
modify(node->to[1], node->mod);
node->mod = 0;
}
pair<Node*, Node*> split(Node *node, int m, int l = 0, int r = (1 << B) - 1, int level = 0) {
push(node, level);
if (!node) return {nullptr, nullptr};
if (r <= m) return {node, nullptr};
if (l > m) return {nullptr, node};
int mid = (l + r) / 2;
auto L = new Node();
auto R = new Node();
if (mid < m) {
auto [a, b] = split(node->to[1], m, mid + 1, r, level + 1);
if (b) {
R->to[1] = b;
} else {
R = nullptr;
}
if (node->to[0] || a) {
L->to = {node->to[0], a};
} else {
L = nullptr;
}
update(L, level);
update(R, level);
return {L, R};
} else {
auto [a, b] = split(node->to[0], m, l, mid, level + 1);
if (node->to[1] || b) {
R->to = {b, node->to[1]};
} else {
R = nullptr;
}
if (a) {
L->to[0] = a;
} else {
L = nullptr;
}
update(L, level);
update(R, level);
return {L, R};
}
}
Node* mrg(Node *a, Node *b, int level) {
if (!a) return b;
if (!b) return a;
if (level == B) {
assert(a->sz == 1 && b->sz == 1);
return a;
}
Node *root = a;
push(a, level);
push(b, level);
a->to[0] = mrg(a->to[0], b->to[0], level + 1);
a->to[1] = mrg(a->to[1], b->to[1], level + 1);
update(root, level);
return root;
}
void print(Node *node, int x = 0) {
debug {
if (x == 0 && node)
show(*node);
cerr << string(x, ' ') << "- ";
if (!node) {
cerr << endl;
return;
}
cerr << *node << endl;
print(node->to[0], x + 2);
print(node->to[1], x + 2);
}
}
tuple<Node*, Node*, Node*> cut(Node *root, int l, int r) {
auto [a, bc] = split(root, l - 1);
auto [b, c] = split(bc, r);
return {a, b, c};
}
Node* merge3(Node *a, Node *b, Node *c) {
return mrg(mrg(a, b, 0), c, 0);
}
void push_or_bit(Node *node, int b, int level) {
if (!node) return;
show(*node);
push(node, level);
if (level == B - b - 1) {
modify(node->to[0], 1 << b);
node->to[1] = mrg(node->to[0], node->to[1], level + 1);
node->to[0] = nullptr;
update(node, level);
return;
}
if ((node->def1 >> b) & 1) {
show("already good");
return;
}
if ((node->def0 >> b) & 1) {
modify(node, 1 << b);
return;
}
push_or_bit(node->to[0], b, level + 1);
push_or_bit(node->to[1], b, level + 1);
update(node, level);
}
void push_or(Node *root, int x) {
for (int b = 0; b < B; ++b) {
if ((x >> b) & 1) {
show(b);
push_or_bit(root, b, 0);
print(root);
}
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
Node *root = new Node();
auto call_xor = [&](int l, int r, int x) {
shows;
auto [a, b, c] = cut(root, l, r);
modify(b, x);
print(a);
print(b);
print(c);
root = merge3(a, b, c);
print(root);
};
auto call_or = [&](int l, int r, int x) {
shows;
auto [a, b, c] = cut(root, l, r);
show(l, r);
print(a);
print(b);
print(c);
push_or(b, x);
root = merge3(a, b, c);
print(root);
};
auto call_ask = [&](int l, int r) {
shows;
print(root);
auto [a, b, c] = cut(root, l, r);
print(a);
print(b);
print(c);
int res = (b ? b->sz : 0);
return res;
};
auto call_and = [&](int l, int r, int x) {
shows;
call_xor(0, (1 << B) - 1, (1 << B) - 1);
call_or(r ^ ((1 << B) - 1), l ^ ((1 << B) - 1), ((1 << B) - 1) ^ x);
call_xor(0, (1 << B) - 1, (1 << B) - 1);
};
int n, q;
cin >> n >> q;
vector<int> a(n);
cin >> a;
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
n = a.size();
for (int k : a) {
show(k);
add_num(root, k);
}
print(root);
while (q--) {
shows;
shows;
shows;
shows;
shows;
shows;
show(*root);
print(root);
shows;
int tp, l, r, x;
cin >> tp >> l >> r;
if (tp != 4) cin >> x;
if (tp == 1) {
call_and(l, r, x);
} else if (tp == 2) {
call_or(l, r, x);
} else if (tp == 3) {
call_xor(l, r, x);
} else if (tp == 4) {
cout << call_ask(l, r) << '\n';
} else {
assert(false);
}
}
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
const int M = 4000003, all = (1<<20)-1;
template<typename T>
void read(T &x){
int ch = getchar(); x = 0; bool f = false;
for(;ch < '0' || ch > '9';ch = getchar()) f |= ch == '-';
for(;ch >= '0' && ch <= '9';ch = getchar()) x = x * 10 + ch - '0';
if(f) x = -x;
}
int n, q, rt, cnt, stk[M], tp, ch[M][2], seg[M][2], siz[M], tag[M];
int crep(){
int x = tp ? stk[tp--] : ++cnt;
ch[x][0] = ch[x][1] = seg[x][0] = seg[x][1] = tag[x] = siz[x] = 0;
return x;
}
void pup(int x){
siz[x] = siz[ch[x][0]] + siz[ch[x][1]];
for(int _ = 0;_ < 2;++ _)
seg[x][_] = seg[ch[x][0]][_] | seg[ch[x][1]][_];
}
void ptag(int x, int val, int d = 19){
if(val >> d & 1) swap(ch[x][0], ch[x][1]);
int _0 = (seg[x][0] & ~val) | (seg[x][1] & val), _1 = (seg[x][0] & val) | (seg[x][1] & ~val);
seg[x][0] = _0; seg[x][1] = _1; tag[x] ^= val;
}
void pdown(int x, int d){
if(tag[x]){ptag(ch[x][0], tag[x], d-1); ptag(ch[x][1], tag[x], d-1); tag[x] = 0;}
}
void ins(int val, int &x = rt, int d = 19){
if(!x) x = crep();
if(!~d){siz[x] = 1; seg[x][0] = all ^ val; seg[x][1] = val; return;}
ins(val, ch[x][val>>d&1], d-1); pup(x);
}
int qry(int ql, int qr, int x = rt, int L = 0, int R = all, int d = 19){
if(!x) return 0;
if(ql <= L && R <= qr) return siz[x];
int mid = L+R>>1, ans = 0; pdown(x, d);
if(ql <= mid) ans = qry(ql, qr, ch[x][0], L, mid, d-1);
if(mid < qr) ans += qry(ql, qr, ch[x][1], mid+1, R, d-1);
return ans;
}
void split(int &x, int &y, int ql, int qr, int L = 0, int R = all, int d = 19){
if(!x) return;
if(ql <= L && R <= qr){y = x; x = 0; return;}
int mid = L+R>>1; y = crep(); pdown(x, d);
if(ql <= mid) split(ch[x][0], ch[y][0], ql, qr, L, mid, d-1);
if(mid < qr) split(ch[x][1], ch[y][1], ql, qr, mid+1, R, d-1);
pup(x); pup(y);
}
void merge(int &x, int y, int d = 19){
if(!x || !y){x |= y; return;}
if(d >= 0){
pdown(x, d); pdown(y, d);
merge(ch[x][0], ch[y][0], d-1);
merge(ch[x][1], ch[y][1], d-1);
pup(x);
} stk[++tp] = y;
}
void chan(int x, int val, int d = 19){
if(!x) return;
if(!(val & seg[x][0] & seg[x][1])){
ptag(x, val & seg[x][0], d); return;
} pdown(x, d);
if(val >> d & 1){
ptag(ch[x][0], 1<<d, d-1);
merge(ch[x][1], ch[x][0], d-1);
ch[x][0] = 0;
} else chan(ch[x][0], val, d-1);
chan(ch[x][1], val, d-1); pup(x);
}
int main(){
read(n); read(q);
while(n --){int x; read(x); ins(x);}
while(q --){
int op, l, r, x, u; read(op); read(l); read(r);
if(op == 4) printf("%d\n", qry(l, r));
else {
read(x); split(rt, u, l, r);
if(op == 1){
ptag(u, all); chan(u, x ^ all); ptag(u, all);
} else if(op == 2) chan(u, x); else ptag(u, x);
merge(rt, u);
}
}
} | ### Prompt
Please formulate a cpp solution to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
const int M = 4000003, all = (1<<20)-1;
template<typename T>
void read(T &x){
int ch = getchar(); x = 0; bool f = false;
for(;ch < '0' || ch > '9';ch = getchar()) f |= ch == '-';
for(;ch >= '0' && ch <= '9';ch = getchar()) x = x * 10 + ch - '0';
if(f) x = -x;
}
int n, q, rt, cnt, stk[M], tp, ch[M][2], seg[M][2], siz[M], tag[M];
int crep(){
int x = tp ? stk[tp--] : ++cnt;
ch[x][0] = ch[x][1] = seg[x][0] = seg[x][1] = tag[x] = siz[x] = 0;
return x;
}
void pup(int x){
siz[x] = siz[ch[x][0]] + siz[ch[x][1]];
for(int _ = 0;_ < 2;++ _)
seg[x][_] = seg[ch[x][0]][_] | seg[ch[x][1]][_];
}
void ptag(int x, int val, int d = 19){
if(val >> d & 1) swap(ch[x][0], ch[x][1]);
int _0 = (seg[x][0] & ~val) | (seg[x][1] & val), _1 = (seg[x][0] & val) | (seg[x][1] & ~val);
seg[x][0] = _0; seg[x][1] = _1; tag[x] ^= val;
}
void pdown(int x, int d){
if(tag[x]){ptag(ch[x][0], tag[x], d-1); ptag(ch[x][1], tag[x], d-1); tag[x] = 0;}
}
void ins(int val, int &x = rt, int d = 19){
if(!x) x = crep();
if(!~d){siz[x] = 1; seg[x][0] = all ^ val; seg[x][1] = val; return;}
ins(val, ch[x][val>>d&1], d-1); pup(x);
}
int qry(int ql, int qr, int x = rt, int L = 0, int R = all, int d = 19){
if(!x) return 0;
if(ql <= L && R <= qr) return siz[x];
int mid = L+R>>1, ans = 0; pdown(x, d);
if(ql <= mid) ans = qry(ql, qr, ch[x][0], L, mid, d-1);
if(mid < qr) ans += qry(ql, qr, ch[x][1], mid+1, R, d-1);
return ans;
}
void split(int &x, int &y, int ql, int qr, int L = 0, int R = all, int d = 19){
if(!x) return;
if(ql <= L && R <= qr){y = x; x = 0; return;}
int mid = L+R>>1; y = crep(); pdown(x, d);
if(ql <= mid) split(ch[x][0], ch[y][0], ql, qr, L, mid, d-1);
if(mid < qr) split(ch[x][1], ch[y][1], ql, qr, mid+1, R, d-1);
pup(x); pup(y);
}
void merge(int &x, int y, int d = 19){
if(!x || !y){x |= y; return;}
if(d >= 0){
pdown(x, d); pdown(y, d);
merge(ch[x][0], ch[y][0], d-1);
merge(ch[x][1], ch[y][1], d-1);
pup(x);
} stk[++tp] = y;
}
void chan(int x, int val, int d = 19){
if(!x) return;
if(!(val & seg[x][0] & seg[x][1])){
ptag(x, val & seg[x][0], d); return;
} pdown(x, d);
if(val >> d & 1){
ptag(ch[x][0], 1<<d, d-1);
merge(ch[x][1], ch[x][0], d-1);
ch[x][0] = 0;
} else chan(ch[x][0], val, d-1);
chan(ch[x][1], val, d-1); pup(x);
}
int main(){
read(n); read(q);
while(n --){int x; read(x); ins(x);}
while(q --){
int op, l, r, x, u; read(op); read(l); read(r);
if(op == 4) printf("%d\n", qry(l, r));
else {
read(x); split(rt, u, l, r);
if(op == 1){
ptag(u, all); chan(u, x ^ all); ptag(u, all);
} else if(op == 2) chan(u, x); else ptag(u, x);
merge(rt, u);
}
}
}
``` |
#include <cstdio>
#include <iostream>
using namespace std;
const int M = 10000005;
int read()
{
int x=0,f=1;char c;
while((c=getchar())<'0' || c>'9') {if(c=='-') f=-1;}
while(c>='0' && c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
return x*f;
}
int n,k,q,rt,cnt,now,ls[M],rs[M],res[M],la[M],v[M],vk[M];
void up(int x)
{
v[x]=v[ls[x]]|v[rs[x]];
vk[x]=vk[ls[x]]|vk[rs[x]];
res[x]=res[ls[x]]+res[rs[x]];
}
void ins(int d,int &x,int val)
{
if(!x) x=++cnt;
if(d==0)
{
v[x]=val;vk[x]=val^k;res[x]=1;
return ;
}
if(val&(1<<(d-1))) ins(d-1,rs[x],val);
else ins(d-1,ls[x],val);
up(x);
}
void getxor(int d,int x,int val)
{
if(x==0) return ;
if(val&(1<<(d-1))) swap(ls[x],rs[x]);
int a=v[x],b=vk[x];
v[x]=(a&(k^val))|(b&val);
vk[x]=(b&(k^val))|(a&val);
la[x]^=val;
}
void down(int d,int x)
{
if(la[x]==0) return ;
getxor(d-1,ls[x],la[x]);getxor(d-1,rs[x],la[x]);
la[x]=0;
}
void split(int d,int &x,int &y,int l,int r,int L,int R)
{
if(x==0 || R<l || r<L) {y=0;return ;}
if(L<=l && r<=R) {y=x;x=0;return ;}
int mid=(l+r)>>1;y=++cnt;
down(d,x);
split(d-1,ls[x],ls[y],l,mid,L,R);
split(d-1,rs[x],rs[y],mid+1,r,L,R);
up(x);up(y);
}
void merge(int d,int &x,int &y)
{
if(x==0) {x=y;return ;}
if(y==0 || d==0) return ;
down(d,x);down(d,y);
merge(d-1,ls[x],ls[y]);
merge(d-1,rs[x],rs[y]);
up(x);
}
void getor(int d,int x,int val)
{
if(!x) return ;
int ad=val&vk[x];
if((ad&v[x])==0) {getxor(d,x,ad);return ;}
down(d,x);
if(val&(1<<(d-1)))
{
getxor(d-1,ls[x],1<<(d-1));
merge(d-1,rs[x],ls[x]);
ls[x]=0;
}
getor(d-1,ls[x],val);
getor(d-1,rs[x],val);
up(x);
}
int ask(int d,int &x,int l,int r,int L,int R)
{
if(!x || L>r || l>R) return 0;
if(L<=l && r<=R) return res[x];
down(d,x);
int mid=(l+r)>>1;
return ask(d-1,ls[x],l,mid,L,R)+ask(d-1,rs[x],mid+1,r,L,R);
}
signed main()
{
n=read();q=read();k=(1<<20)-1;
for(int i=1;i<=n;i++)
{
int x=read();
ins(20,rt,x);
}
while(q--)
{
int now=0,t=read(),x=read(),y=read();
if(t==4)
{
printf("%d\n",ask(20,rt,0,k,x,y));
continue;
}
split(20,rt,now,0,k,x,y);
int z=read();
if(t==1)
getxor(20,now,k),getor(20,now,k^z),getxor(20,now,k);
if(t==2)
getor(20,now,z);
if(t==3)
getxor(20,now,z);
merge(20,rt,now);
}
} | ### Prompt
Please formulate a cpp solution to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <cstdio>
#include <iostream>
using namespace std;
const int M = 10000005;
int read()
{
int x=0,f=1;char c;
while((c=getchar())<'0' || c>'9') {if(c=='-') f=-1;}
while(c>='0' && c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
return x*f;
}
int n,k,q,rt,cnt,now,ls[M],rs[M],res[M],la[M],v[M],vk[M];
void up(int x)
{
v[x]=v[ls[x]]|v[rs[x]];
vk[x]=vk[ls[x]]|vk[rs[x]];
res[x]=res[ls[x]]+res[rs[x]];
}
void ins(int d,int &x,int val)
{
if(!x) x=++cnt;
if(d==0)
{
v[x]=val;vk[x]=val^k;res[x]=1;
return ;
}
if(val&(1<<(d-1))) ins(d-1,rs[x],val);
else ins(d-1,ls[x],val);
up(x);
}
void getxor(int d,int x,int val)
{
if(x==0) return ;
if(val&(1<<(d-1))) swap(ls[x],rs[x]);
int a=v[x],b=vk[x];
v[x]=(a&(k^val))|(b&val);
vk[x]=(b&(k^val))|(a&val);
la[x]^=val;
}
void down(int d,int x)
{
if(la[x]==0) return ;
getxor(d-1,ls[x],la[x]);getxor(d-1,rs[x],la[x]);
la[x]=0;
}
void split(int d,int &x,int &y,int l,int r,int L,int R)
{
if(x==0 || R<l || r<L) {y=0;return ;}
if(L<=l && r<=R) {y=x;x=0;return ;}
int mid=(l+r)>>1;y=++cnt;
down(d,x);
split(d-1,ls[x],ls[y],l,mid,L,R);
split(d-1,rs[x],rs[y],mid+1,r,L,R);
up(x);up(y);
}
void merge(int d,int &x,int &y)
{
if(x==0) {x=y;return ;}
if(y==0 || d==0) return ;
down(d,x);down(d,y);
merge(d-1,ls[x],ls[y]);
merge(d-1,rs[x],rs[y]);
up(x);
}
void getor(int d,int x,int val)
{
if(!x) return ;
int ad=val&vk[x];
if((ad&v[x])==0) {getxor(d,x,ad);return ;}
down(d,x);
if(val&(1<<(d-1)))
{
getxor(d-1,ls[x],1<<(d-1));
merge(d-1,rs[x],ls[x]);
ls[x]=0;
}
getor(d-1,ls[x],val);
getor(d-1,rs[x],val);
up(x);
}
int ask(int d,int &x,int l,int r,int L,int R)
{
if(!x || L>r || l>R) return 0;
if(L<=l && r<=R) return res[x];
down(d,x);
int mid=(l+r)>>1;
return ask(d-1,ls[x],l,mid,L,R)+ask(d-1,rs[x],mid+1,r,L,R);
}
signed main()
{
n=read();q=read();k=(1<<20)-1;
for(int i=1;i<=n;i++)
{
int x=read();
ins(20,rt,x);
}
while(q--)
{
int now=0,t=read(),x=read(),y=read();
if(t==4)
{
printf("%d\n",ask(20,rt,0,k,x,y));
continue;
}
split(20,rt,now,0,k,x,y);
int z=read();
if(t==1)
getxor(20,now,k),getor(20,now,k^z),getxor(20,now,k);
if(t==2)
getor(20,now,z);
if(t==3)
getxor(20,now,z);
merge(20,rt,now);
}
}
``` |
/*
author: Maksim1744
created: 06.05.2021 16:01:27
*/
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define sum(a) ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
template<typename T> vector<T>& operator-- (vector<T> &v){for (auto& i : v) --i; return v;}
template<typename T> vector<T>& operator++ (vector<T> &v){for (auto& i : v) ++i; return v;}
template<typename T> istream& operator>>(istream& is, vector<T> &v){for (auto& i : v) is >> i; return is;}
template<typename T> ostream& operator<<(ostream& os, vector<T> v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> pair<T,U>& operator-- (pair<T, U> &p){--p.first; --p.second; return p;}
template<typename T, typename U> pair<T,U>& operator++ (pair<T, U> &p){++p.first; ++p.second; return p;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U> &p){is >> p.first >> p.second; return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U> p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}
#ifdef HOME
#define SHOW_COLORS
#include "C:/C++ libs/print.cpp"
#else
#define show(...) void(0)
#define mclock void(0)
#define shows void(0)
#define debug if (false)
#endif
const int MEMSIZE = 1e9/2;
char memory[MEMSIZE];
int memorypos;
inline void * operator new(size_t n){
if (memorypos + n >= MEMSIZE) {
memorypos = MEMSIZE / 3;
}
char * ret = memory + memorypos;
memorypos += n;
return (void *)ret;
}
inline void operator delete(void *){}
inline void operator delete(void *, size_t){}
const int B = 20;
struct Node {
Node* to[2];
int sz = 0;
int mod = 0;
int def0 = (1 << B) - 1;
int def1 = (1 << B) - 1;
Node() {
to[0] = nullptr;
to[1] = nullptr;
}
};
ostream &operator << (ostream &o, const Node &n) {
return o << "[sz=" << n.sz << ", mod=" << n.mod
<< ", def0=" << n.def0 << ", def1=" << n.def1 << "]";
}
void add_num(Node *node, int x) {
for (int i = 0; i < B; ++i) {
node->sz++;
node->def1 &= x;
node->def0 &= ((1 << B) - 1) ^ x;
int b = ((x >> (B - i - 1)) & 1);
if (!node->to[b]) {
node->to[b] = new Node();
}
node = node->to[b];
}
node->sz++;
node->def1 &= x;
node->def0 &= ((1 << B) - 1) ^ x;
}
void update(Node *node, int level) {
if (!node) return;
node->sz = 0;
if (node->to[0]) node->sz += node->to[0]->sz;
if (node->to[1]) node->sz += node->to[1]->sz;
node->def0 = (1 << B) - 1;
node->def1 = (1 << B) - 1;
if (node->to[0]) {
node->def0 &= node->to[0]->def0;
node->def1 &= node->to[0]->def1;
}
if (node->to[1]) {
node->def0 &= node->to[1]->def0;
node->def1 &= node->to[1]->def1;
}
if (!node->to[0])
node->def1 |= (1 << (B - level - 1));
if (!node->to[1])
node->def0 |= (1 << (B - level - 1));
}
void modify(Node *node, int x) {
if (!node) return;
node->mod ^= x;
int mask = (x & (node->def0 | node->def1));
node->def0 ^= mask;
node->def1 ^= mask;
}
void push(Node *node, int level) {
if (!node) {
return;
}
if ((node->mod >> (B - level - 1)) & 1) {
swap(node->to[0], node->to[1]);
}
modify(node->to[0], node->mod);
modify(node->to[1], node->mod);
node->mod = 0;
}
pair<Node*, Node*> split(Node *node, int m, int l = 0, int r = (1 << B) - 1, int level = 0) {
push(node, level);
if (!node) return {nullptr, nullptr};
if (r <= m) return {node, nullptr};
if (l > m) return {nullptr, node};
int mid = (l + r) / 2;
auto L = new Node();
auto R = new Node();
if (mid < m) {
auto [a, b] = split(node->to[1], m, mid + 1, r, level + 1);
if (b) {
R->to[1] = b;
} else {
R = nullptr;
}
if (node->to[0] || a) {
L->to[0] = node->to[0];
L->to[1] = a;
} else {
L = nullptr;
}
update(L, level);
update(R, level);
return {L, R};
} else {
auto [a, b] = split(node->to[0], m, l, mid, level + 1);
if (node->to[1] || b) {
R->to[0] = b;
R->to[1] = node->to[1];
} else {
R = nullptr;
}
if (a) {
L->to[0] = a;
} else {
L = nullptr;
}
update(L, level);
update(R, level);
return {L, R};
}
}
Node* mrg(Node *a, Node *b, int level) {
if (!a) return b;
if (!b) return a;
if (level == B) {
assert(a->sz == 1 && b->sz == 1);
return a;
}
Node *root = a;
push(a, level);
push(b, level);
a->to[0] = mrg(a->to[0], b->to[0], level + 1);
a->to[1] = mrg(a->to[1], b->to[1], level + 1);
update(root, level);
return root;
}
void print(Node *node, int x = 0) {
debug {
if (x == 0 && node)
show(*node);
cerr << string(x, ' ') << "- ";
if (!node) {
cerr << endl;
return;
}
cerr << *node << endl;
print(node->to[0], x + 2);
print(node->to[1], x + 2);
}
}
tuple<Node*, Node*, Node*> cut(Node *root, int l, int r) {
auto [a, bc] = split(root, l - 1);
auto [b, c] = split(bc, r);
return {a, b, c};
}
Node* merge3(Node *a, Node *b, Node *c) {
return mrg(mrg(a, b, 0), c, 0);
}
void push_or_bit(Node *node, int b, int level) {
if (!node) return;
show(*node);
push(node, level);
if (level == B - b - 1) {
modify(node->to[0], 1 << b);
node->to[1] = mrg(node->to[0], node->to[1], level + 1);
node->to[0] = nullptr;
update(node, level);
return;
}
if ((node->def1 >> b) & 1) {
show("already good");
return;
}
if ((node->def0 >> b) & 1) {
modify(node, 1 << b);
return;
}
push_or_bit(node->to[0], b, level + 1);
push_or_bit(node->to[1], b, level + 1);
update(node, level);
}
void push_or(Node *root, int x) {
for (int b = 0; b < B; ++b) {
if ((x >> b) & 1) {
show(b);
push_or_bit(root, b, 0);
print(root);
}
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
Node *root = new Node();
auto call_xor = [&](int l, int r, int x) {
shows;
auto [a, b, c] = cut(root, l, r);
modify(b, x);
print(a);
print(b);
print(c);
root = merge3(a, b, c);
print(root);
};
auto call_or = [&](int l, int r, int x) {
shows;
auto [a, b, c] = cut(root, l, r);
show(l, r);
print(a);
print(b);
print(c);
push_or(b, x);
root = merge3(a, b, c);
print(root);
};
auto call_ask = [&](int l, int r) {
shows;
print(root);
auto [a, b, c] = cut(root, l, r);
print(a);
print(b);
print(c);
int res = (b ? b->sz : 0);
return res;
};
auto call_and = [&](int l, int r, int x) {
shows;
call_xor(0, (1 << B) - 1, (1 << B) - 1);
call_or(r ^ ((1 << B) - 1), l ^ ((1 << B) - 1), ((1 << B) - 1) ^ x);
call_xor(0, (1 << B) - 1, (1 << B) - 1);
};
int n, q;
cin >> n >> q;
vector<int> a(n);
cin >> a;
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
n = a.size();
for (int k : a) {
show(k);
add_num(root, k);
}
print(root);
while (q--) {
shows;
shows;
shows;
shows;
shows;
shows;
show(*root);
print(root);
shows;
int tp, l, r, x;
cin >> tp >> l >> r;
if (tp != 4) cin >> x;
if (tp == 1) {
call_and(l, r, x);
} else if (tp == 2) {
call_or(l, r, x);
} else if (tp == 3) {
call_xor(l, r, x);
} else if (tp == 4) {
cout << call_ask(l, r) << '\n';
} else {
assert(false);
}
}
return 0;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
/*
author: Maksim1744
created: 06.05.2021 16:01:27
*/
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define sum(a) ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
template<typename T> vector<T>& operator-- (vector<T> &v){for (auto& i : v) --i; return v;}
template<typename T> vector<T>& operator++ (vector<T> &v){for (auto& i : v) ++i; return v;}
template<typename T> istream& operator>>(istream& is, vector<T> &v){for (auto& i : v) is >> i; return is;}
template<typename T> ostream& operator<<(ostream& os, vector<T> v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> pair<T,U>& operator-- (pair<T, U> &p){--p.first; --p.second; return p;}
template<typename T, typename U> pair<T,U>& operator++ (pair<T, U> &p){++p.first; ++p.second; return p;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U> &p){is >> p.first >> p.second; return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U> p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}
#ifdef HOME
#define SHOW_COLORS
#include "C:/C++ libs/print.cpp"
#else
#define show(...) void(0)
#define mclock void(0)
#define shows void(0)
#define debug if (false)
#endif
const int MEMSIZE = 1e9/2;
char memory[MEMSIZE];
int memorypos;
inline void * operator new(size_t n){
if (memorypos + n >= MEMSIZE) {
memorypos = MEMSIZE / 3;
}
char * ret = memory + memorypos;
memorypos += n;
return (void *)ret;
}
inline void operator delete(void *){}
inline void operator delete(void *, size_t){}
const int B = 20;
struct Node {
Node* to[2];
int sz = 0;
int mod = 0;
int def0 = (1 << B) - 1;
int def1 = (1 << B) - 1;
Node() {
to[0] = nullptr;
to[1] = nullptr;
}
};
ostream &operator << (ostream &o, const Node &n) {
return o << "[sz=" << n.sz << ", mod=" << n.mod
<< ", def0=" << n.def0 << ", def1=" << n.def1 << "]";
}
void add_num(Node *node, int x) {
for (int i = 0; i < B; ++i) {
node->sz++;
node->def1 &= x;
node->def0 &= ((1 << B) - 1) ^ x;
int b = ((x >> (B - i - 1)) & 1);
if (!node->to[b]) {
node->to[b] = new Node();
}
node = node->to[b];
}
node->sz++;
node->def1 &= x;
node->def0 &= ((1 << B) - 1) ^ x;
}
void update(Node *node, int level) {
if (!node) return;
node->sz = 0;
if (node->to[0]) node->sz += node->to[0]->sz;
if (node->to[1]) node->sz += node->to[1]->sz;
node->def0 = (1 << B) - 1;
node->def1 = (1 << B) - 1;
if (node->to[0]) {
node->def0 &= node->to[0]->def0;
node->def1 &= node->to[0]->def1;
}
if (node->to[1]) {
node->def0 &= node->to[1]->def0;
node->def1 &= node->to[1]->def1;
}
if (!node->to[0])
node->def1 |= (1 << (B - level - 1));
if (!node->to[1])
node->def0 |= (1 << (B - level - 1));
}
void modify(Node *node, int x) {
if (!node) return;
node->mod ^= x;
int mask = (x & (node->def0 | node->def1));
node->def0 ^= mask;
node->def1 ^= mask;
}
void push(Node *node, int level) {
if (!node) {
return;
}
if ((node->mod >> (B - level - 1)) & 1) {
swap(node->to[0], node->to[1]);
}
modify(node->to[0], node->mod);
modify(node->to[1], node->mod);
node->mod = 0;
}
pair<Node*, Node*> split(Node *node, int m, int l = 0, int r = (1 << B) - 1, int level = 0) {
push(node, level);
if (!node) return {nullptr, nullptr};
if (r <= m) return {node, nullptr};
if (l > m) return {nullptr, node};
int mid = (l + r) / 2;
auto L = new Node();
auto R = new Node();
if (mid < m) {
auto [a, b] = split(node->to[1], m, mid + 1, r, level + 1);
if (b) {
R->to[1] = b;
} else {
R = nullptr;
}
if (node->to[0] || a) {
L->to[0] = node->to[0];
L->to[1] = a;
} else {
L = nullptr;
}
update(L, level);
update(R, level);
return {L, R};
} else {
auto [a, b] = split(node->to[0], m, l, mid, level + 1);
if (node->to[1] || b) {
R->to[0] = b;
R->to[1] = node->to[1];
} else {
R = nullptr;
}
if (a) {
L->to[0] = a;
} else {
L = nullptr;
}
update(L, level);
update(R, level);
return {L, R};
}
}
Node* mrg(Node *a, Node *b, int level) {
if (!a) return b;
if (!b) return a;
if (level == B) {
assert(a->sz == 1 && b->sz == 1);
return a;
}
Node *root = a;
push(a, level);
push(b, level);
a->to[0] = mrg(a->to[0], b->to[0], level + 1);
a->to[1] = mrg(a->to[1], b->to[1], level + 1);
update(root, level);
return root;
}
void print(Node *node, int x = 0) {
debug {
if (x == 0 && node)
show(*node);
cerr << string(x, ' ') << "- ";
if (!node) {
cerr << endl;
return;
}
cerr << *node << endl;
print(node->to[0], x + 2);
print(node->to[1], x + 2);
}
}
tuple<Node*, Node*, Node*> cut(Node *root, int l, int r) {
auto [a, bc] = split(root, l - 1);
auto [b, c] = split(bc, r);
return {a, b, c};
}
Node* merge3(Node *a, Node *b, Node *c) {
return mrg(mrg(a, b, 0), c, 0);
}
void push_or_bit(Node *node, int b, int level) {
if (!node) return;
show(*node);
push(node, level);
if (level == B - b - 1) {
modify(node->to[0], 1 << b);
node->to[1] = mrg(node->to[0], node->to[1], level + 1);
node->to[0] = nullptr;
update(node, level);
return;
}
if ((node->def1 >> b) & 1) {
show("already good");
return;
}
if ((node->def0 >> b) & 1) {
modify(node, 1 << b);
return;
}
push_or_bit(node->to[0], b, level + 1);
push_or_bit(node->to[1], b, level + 1);
update(node, level);
}
void push_or(Node *root, int x) {
for (int b = 0; b < B; ++b) {
if ((x >> b) & 1) {
show(b);
push_or_bit(root, b, 0);
print(root);
}
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
Node *root = new Node();
auto call_xor = [&](int l, int r, int x) {
shows;
auto [a, b, c] = cut(root, l, r);
modify(b, x);
print(a);
print(b);
print(c);
root = merge3(a, b, c);
print(root);
};
auto call_or = [&](int l, int r, int x) {
shows;
auto [a, b, c] = cut(root, l, r);
show(l, r);
print(a);
print(b);
print(c);
push_or(b, x);
root = merge3(a, b, c);
print(root);
};
auto call_ask = [&](int l, int r) {
shows;
print(root);
auto [a, b, c] = cut(root, l, r);
print(a);
print(b);
print(c);
int res = (b ? b->sz : 0);
return res;
};
auto call_and = [&](int l, int r, int x) {
shows;
call_xor(0, (1 << B) - 1, (1 << B) - 1);
call_or(r ^ ((1 << B) - 1), l ^ ((1 << B) - 1), ((1 << B) - 1) ^ x);
call_xor(0, (1 << B) - 1, (1 << B) - 1);
};
int n, q;
cin >> n >> q;
vector<int> a(n);
cin >> a;
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
n = a.size();
for (int k : a) {
show(k);
add_num(root, k);
}
print(root);
while (q--) {
shows;
shows;
shows;
shows;
shows;
shows;
show(*root);
print(root);
shows;
int tp, l, r, x;
cin >> tp >> l >> r;
if (tp != 4) cin >> x;
if (tp == 1) {
call_and(l, r, x);
} else if (tp == 2) {
call_or(l, r, x);
} else if (tp == 3) {
call_xor(l, r, x);
} else if (tp == 4) {
cout << call_ask(l, r) << '\n';
} else {
assert(false);
}
}
return 0;
}
``` |
#include<stdio.h>
const int maxn=200005,maxk=maxn*50;
int n,q,k,rt,cnt;
int lc[maxk],rc[maxk],lazy[maxk],Vand[maxk],Vor[maxk],res[maxk];
inline void swp(int &a,int &b){
a+=b,b=a-b,a-=b;
}
inline void pushup(int now){
Vand[now]=Vand[lc[now]]|Vand[rc[now]];
Vor[now]=Vor[lc[now]]|Vor[rc[now]];
res[now]=res[lc[now]]+res[rc[now]];
}
void insert(int dep,int &now,int val){
if(now==0)
now=++cnt;
if(dep==0){
Vand[now]=(k^val),Vor[now]=val,res[now]=1;
return ;
}
if(((val>>(dep-1))&1)==0)
insert(dep-1,lc[now],val);
else insert(dep-1,rc[now],val);
pushup(now);
}
void getxor(int dep,int now,int val){
if(now==0)
return ;
if((val>>(dep-1))&1)
swp(lc[now],rc[now]);
int Va=Vand[now],Vo=Vor[now];
Vand[now]=(Va&(k^val))|(Vo&val);//
Vor[now]=(Vo&(k^val))|(Va&val);
lazy[now]^=val;
}
void pushdown(int dep,int now){
if(lazy[now]==0)
return ;
getxor(dep-1,lc[now],lazy[now]),getxor(dep-1,rc[now],lazy[now]);
lazy[now]=0;
}
void split(int dep,int &x,int &y,int l,int r,int L,int R){
if(x==0||R<=l||r<=L){
y=0;
return ;
}
if(L<=l&&r<=R){
y=x,x=0;
return ;
}
int mid=(l+r)>>1;
pushdown(dep,x),y=++cnt;
split(dep-1,lc[x],lc[y],l,mid,L,R),split(dep-1,rc[x],rc[y],mid/*+1*/,r,L,R);
pushup(x),pushup(y);
}
void merge(int dep,int &x,int &y){
if(x==0){
x=y;
return ;
}
if(y==0||dep==0)
return ;
pushdown(dep,x),pushdown(dep,y);
merge(dep-1,lc[x],lc[y]),merge(dep-1,rc[x],rc[y]);
pushup(x);
}
void getor(int dep,int now,int val){
if(now==0)
return ;
if((val&Vand[now]&Vor[now])==0){
getxor(dep,now,val&Vand[now]);
return ;
}
pushdown(dep,now);
if((val>>(dep-1))&1){
getxor(dep-1,lc[now],1<<(dep-1));
merge(dep-1,rc[now],lc[now]);
lc[now]=0;
}
getor(dep-1,lc[now],val),getor(dep-1,rc[now],val);
pushup(now);
}
int query(int dep,int now,int l,int r,int L,int R){
if(now==0||R<=l||r<=L)
return 0;
if(L<=l&&r<=R)
return res[now];
int mid=(l+r)>>1;
pushdown(dep,now);
return query(dep-1,lc[now],l,mid,L,R)+query(dep-1,rc[now],mid/*+1*/,r,L,R);
}
int main(){
scanf("%d%d",&n,&q),k=(1<<20)-1;
for(int i=1;i<=n;i++){
int x;
scanf("%d",&x);
insert(20,rt,x);
}
for(int i=1;i<=q;i++){
int t,x,y,z;
scanf("%d",&t);
if(t==4){
scanf("%d%d",&x,&y);
printf("%d\n",query(20,rt,0,1<<20,x,y+1));
continue;
}
int now;
scanf("%d%d%d",&x,&y,&z);
split(20,rt,now,0,1<<20,x,y+1);
if(t==1)
getxor(20,now,k),getor(20,now,z^k),getxor(20,now,k);
if(t==2)
getor(20,now,z);
if(t==3)
getxor(20,now,z);
merge(20,rt,now);
}
return 0;
} | ### Prompt
Construct a Cpp code solution to the problem outlined:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include<stdio.h>
const int maxn=200005,maxk=maxn*50;
int n,q,k,rt,cnt;
int lc[maxk],rc[maxk],lazy[maxk],Vand[maxk],Vor[maxk],res[maxk];
inline void swp(int &a,int &b){
a+=b,b=a-b,a-=b;
}
inline void pushup(int now){
Vand[now]=Vand[lc[now]]|Vand[rc[now]];
Vor[now]=Vor[lc[now]]|Vor[rc[now]];
res[now]=res[lc[now]]+res[rc[now]];
}
void insert(int dep,int &now,int val){
if(now==0)
now=++cnt;
if(dep==0){
Vand[now]=(k^val),Vor[now]=val,res[now]=1;
return ;
}
if(((val>>(dep-1))&1)==0)
insert(dep-1,lc[now],val);
else insert(dep-1,rc[now],val);
pushup(now);
}
void getxor(int dep,int now,int val){
if(now==0)
return ;
if((val>>(dep-1))&1)
swp(lc[now],rc[now]);
int Va=Vand[now],Vo=Vor[now];
Vand[now]=(Va&(k^val))|(Vo&val);//
Vor[now]=(Vo&(k^val))|(Va&val);
lazy[now]^=val;
}
void pushdown(int dep,int now){
if(lazy[now]==0)
return ;
getxor(dep-1,lc[now],lazy[now]),getxor(dep-1,rc[now],lazy[now]);
lazy[now]=0;
}
void split(int dep,int &x,int &y,int l,int r,int L,int R){
if(x==0||R<=l||r<=L){
y=0;
return ;
}
if(L<=l&&r<=R){
y=x,x=0;
return ;
}
int mid=(l+r)>>1;
pushdown(dep,x),y=++cnt;
split(dep-1,lc[x],lc[y],l,mid,L,R),split(dep-1,rc[x],rc[y],mid/*+1*/,r,L,R);
pushup(x),pushup(y);
}
void merge(int dep,int &x,int &y){
if(x==0){
x=y;
return ;
}
if(y==0||dep==0)
return ;
pushdown(dep,x),pushdown(dep,y);
merge(dep-1,lc[x],lc[y]),merge(dep-1,rc[x],rc[y]);
pushup(x);
}
void getor(int dep,int now,int val){
if(now==0)
return ;
if((val&Vand[now]&Vor[now])==0){
getxor(dep,now,val&Vand[now]);
return ;
}
pushdown(dep,now);
if((val>>(dep-1))&1){
getxor(dep-1,lc[now],1<<(dep-1));
merge(dep-1,rc[now],lc[now]);
lc[now]=0;
}
getor(dep-1,lc[now],val),getor(dep-1,rc[now],val);
pushup(now);
}
int query(int dep,int now,int l,int r,int L,int R){
if(now==0||R<=l||r<=L)
return 0;
if(L<=l&&r<=R)
return res[now];
int mid=(l+r)>>1;
pushdown(dep,now);
return query(dep-1,lc[now],l,mid,L,R)+query(dep-1,rc[now],mid/*+1*/,r,L,R);
}
int main(){
scanf("%d%d",&n,&q),k=(1<<20)-1;
for(int i=1;i<=n;i++){
int x;
scanf("%d",&x);
insert(20,rt,x);
}
for(int i=1;i<=q;i++){
int t,x,y,z;
scanf("%d",&t);
if(t==4){
scanf("%d%d",&x,&y);
printf("%d\n",query(20,rt,0,1<<20,x,y+1));
continue;
}
int now;
scanf("%d%d%d",&x,&y,&z);
split(20,rt,now,0,1<<20,x,y+1);
if(t==1)
getxor(20,now,k),getor(20,now,z^k),getxor(20,now,k);
if(t==2)
getor(20,now,z);
if(t==3)
getxor(20,now,z);
merge(20,rt,now);
}
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn=4444444,mod=998244353,A1=(1<<20)-1;
#define MP make_pair
#define PB push_back
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define ROF(i,a,b) for(int i=(a);i>=(b);i--)
#define MEM(x,v) memset(x,v,sizeof(x))
inline ll read(){
char ch=getchar();ll x=0,f=0;
while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
return f?-x:x;
}
inline int qpow(int a,int b){
int ans=1;
for(;b;b>>=1,a=1ll*a*a%mod) if(b&1) ans=1ll*ans*a%mod;
return ans;
}
int n,q,rt,ch[maxn][2],s1[maxn],s2[maxn],sz[maxn],add[maxn],stk[maxn],tp;
inline void pushup(int x){
s1[x]=A1;s2[x]=sz[x]=0;
FOR(i,0,1) if(ch[x][i]) s1[x]&=s1[ch[x][i]],s2[x]|=s2[ch[x][i]],sz[x]+=sz[ch[x][i]];
}
inline void setadd(int x,int v,int dep){
if((v>>dep)&1) swap(ch[x][0],ch[x][1]);
add[x]^=v;
v&=s1[x]^s2[x]^A1;
s1[x]^=v;
s2[x]^=v;
}
inline void pushdown(int x,int dep){
if(add[x]){
FOR(i,0,1) if(ch[x][i]) setadd(ch[x][i],add[x],dep-1);
add[x]=0;
}
}
inline int newnode(){
int now=stk[tp--];
ch[now][0]=ch[now][1]=sz[now]=s1[now]=s2[now]=add[now]=0;
return now;
}
void init(){
ROF(i,maxn-100,1) stk[++tp]=i;
}
void merge(int &x,int &y,int dep){
if(!x || !y) return x|=y,y=0,void();
if(dep==-1) return sz[x]|=sz[y],s1[x]|=s1[y],s2[x]|=s2[y],stk[++tp]=y,y=0,void();
pushdown(x,dep);pushdown(y,dep);
FOR(i,0,1) merge(ch[x][i],ch[y][i],dep-1);
pushup(x);
stk[++tp]=y;y=0;
}
void insert(int &x,int v,int dep=19){
if(!x) x=newnode();
if(dep==-1) return s1[x]=s2[x]=v,sz[x]=1,void();
pushdown(x,dep);
insert(ch[x][(v>>dep)&1],v,dep-1);
pushup(x);
}
struct node{
int x,w,d;
}tmp[maxn];
int tl;
void split(int &x,int l,int r,int w=0,int dep=19){
if(!x) return;
if(r<w || w+((1<<(dep+1))-1)<l) return;
if(l<=w && w+((1<<(dep+1))-1)<=r){
tmp[++tl]=(node){x,w,dep};
x=0;
return;
}
pushdown(x,dep);
split(ch[x][0],l,r,w,dep-1);
split(ch[x][1],l,r,w|(1<<dep),dep-1);
pushup(x);
}
void insert2(int &x,node y,int v,int dep=19){
if(!x) x=newnode();
if(dep==y.d){
merge(x,y.x,dep);
return;
}
pushdown(x,dep);
insert2(ch[x][(v>>dep)&1],y,v,dep-1);
pushup(x);
}
void work1(int x,int v,int dep){
if(!x) return;
if(dep==-1 || !((s1[x]^s2[x])&(v^A1))) return setadd(x,s2[x]&(v^A1),dep);
pushdown(x,dep);
FOR(i,0,1) work1(ch[x][i],v,dep-1);
if(!((v>>dep)&1)) merge(ch[x][0],ch[x][1],dep-1);
pushup(x);
}
void work2(int x,int v,int dep){
if(!x) return;
if(dep==-1 || !((s1[x]^s2[x])&v)) return setadd(x,(s1[x]^A1)&v,dep);
pushdown(x,dep);
FOR(i,0,1) work2(ch[x][i],v,dep-1);
if((v>>dep)&1) merge(ch[x][1],ch[x][0],dep-1);
pushup(x);
}
void update1(int v){
FOR(i,1,tl){
work1(tmp[i].x,v,tmp[i].d);
insert2(rt,tmp[i],v&tmp[i].w);
}
}
void update2(int v){
FOR(i,1,tl){
work2(tmp[i].x,v,tmp[i].d);
insert2(rt,tmp[i],v|tmp[i].w);
}
}
void update3(int v){
FOR(i,1,tl){
setadd(tmp[i].x,v,tmp[i].d);
insert2(rt,tmp[i],v^tmp[i].w);
}
}
int query(int x,int l,int r,int w=0,int dep=19){
if(!x) return 0;
if(r<w || w+((1<<(dep+1))-1)<l) return 0;
if(l<=w && w+((1<<(dep+1))-1)<=r) return sz[x];
pushdown(x,dep);
return query(ch[x][0],l,r,w,dep-1)+query(ch[x][1],l,r,w|(1<<dep),dep-1);
}
int main(){
init();
n=read();q=read();
FOR(i,1,n) insert(rt,read());
while(q--){
int op=read(),l=read(),r=read(),x=op==4?0:read();
if(op==4) printf("%d\n",query(rt,l,r));
else{
tl=0;
split(rt,l,r);
if(op==1) update1(x);
if(op==2) update2(x);
if(op==3) update3(x);
}
}
} | ### Prompt
Develop a solution in CPP to the problem described below:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn=4444444,mod=998244353,A1=(1<<20)-1;
#define MP make_pair
#define PB push_back
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define ROF(i,a,b) for(int i=(a);i>=(b);i--)
#define MEM(x,v) memset(x,v,sizeof(x))
inline ll read(){
char ch=getchar();ll x=0,f=0;
while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
return f?-x:x;
}
inline int qpow(int a,int b){
int ans=1;
for(;b;b>>=1,a=1ll*a*a%mod) if(b&1) ans=1ll*ans*a%mod;
return ans;
}
int n,q,rt,ch[maxn][2],s1[maxn],s2[maxn],sz[maxn],add[maxn],stk[maxn],tp;
inline void pushup(int x){
s1[x]=A1;s2[x]=sz[x]=0;
FOR(i,0,1) if(ch[x][i]) s1[x]&=s1[ch[x][i]],s2[x]|=s2[ch[x][i]],sz[x]+=sz[ch[x][i]];
}
inline void setadd(int x,int v,int dep){
if((v>>dep)&1) swap(ch[x][0],ch[x][1]);
add[x]^=v;
v&=s1[x]^s2[x]^A1;
s1[x]^=v;
s2[x]^=v;
}
inline void pushdown(int x,int dep){
if(add[x]){
FOR(i,0,1) if(ch[x][i]) setadd(ch[x][i],add[x],dep-1);
add[x]=0;
}
}
inline int newnode(){
int now=stk[tp--];
ch[now][0]=ch[now][1]=sz[now]=s1[now]=s2[now]=add[now]=0;
return now;
}
void init(){
ROF(i,maxn-100,1) stk[++tp]=i;
}
void merge(int &x,int &y,int dep){
if(!x || !y) return x|=y,y=0,void();
if(dep==-1) return sz[x]|=sz[y],s1[x]|=s1[y],s2[x]|=s2[y],stk[++tp]=y,y=0,void();
pushdown(x,dep);pushdown(y,dep);
FOR(i,0,1) merge(ch[x][i],ch[y][i],dep-1);
pushup(x);
stk[++tp]=y;y=0;
}
void insert(int &x,int v,int dep=19){
if(!x) x=newnode();
if(dep==-1) return s1[x]=s2[x]=v,sz[x]=1,void();
pushdown(x,dep);
insert(ch[x][(v>>dep)&1],v,dep-1);
pushup(x);
}
struct node{
int x,w,d;
}tmp[maxn];
int tl;
void split(int &x,int l,int r,int w=0,int dep=19){
if(!x) return;
if(r<w || w+((1<<(dep+1))-1)<l) return;
if(l<=w && w+((1<<(dep+1))-1)<=r){
tmp[++tl]=(node){x,w,dep};
x=0;
return;
}
pushdown(x,dep);
split(ch[x][0],l,r,w,dep-1);
split(ch[x][1],l,r,w|(1<<dep),dep-1);
pushup(x);
}
void insert2(int &x,node y,int v,int dep=19){
if(!x) x=newnode();
if(dep==y.d){
merge(x,y.x,dep);
return;
}
pushdown(x,dep);
insert2(ch[x][(v>>dep)&1],y,v,dep-1);
pushup(x);
}
void work1(int x,int v,int dep){
if(!x) return;
if(dep==-1 || !((s1[x]^s2[x])&(v^A1))) return setadd(x,s2[x]&(v^A1),dep);
pushdown(x,dep);
FOR(i,0,1) work1(ch[x][i],v,dep-1);
if(!((v>>dep)&1)) merge(ch[x][0],ch[x][1],dep-1);
pushup(x);
}
void work2(int x,int v,int dep){
if(!x) return;
if(dep==-1 || !((s1[x]^s2[x])&v)) return setadd(x,(s1[x]^A1)&v,dep);
pushdown(x,dep);
FOR(i,0,1) work2(ch[x][i],v,dep-1);
if((v>>dep)&1) merge(ch[x][1],ch[x][0],dep-1);
pushup(x);
}
void update1(int v){
FOR(i,1,tl){
work1(tmp[i].x,v,tmp[i].d);
insert2(rt,tmp[i],v&tmp[i].w);
}
}
void update2(int v){
FOR(i,1,tl){
work2(tmp[i].x,v,tmp[i].d);
insert2(rt,tmp[i],v|tmp[i].w);
}
}
void update3(int v){
FOR(i,1,tl){
setadd(tmp[i].x,v,tmp[i].d);
insert2(rt,tmp[i],v^tmp[i].w);
}
}
int query(int x,int l,int r,int w=0,int dep=19){
if(!x) return 0;
if(r<w || w+((1<<(dep+1))-1)<l) return 0;
if(l<=w && w+((1<<(dep+1))-1)<=r) return sz[x];
pushdown(x,dep);
return query(ch[x][0],l,r,w,dep-1)+query(ch[x][1],l,r,w|(1<<dep),dep-1);
}
int main(){
init();
n=read();q=read();
FOR(i,1,n) insert(rt,read());
while(q--){
int op=read(),l=read(),r=read(),x=op==4?0:read();
if(op==4) printf("%d\n",query(rt,l,r));
else{
tl=0;
split(rt,l,r);
if(op==1) update1(x);
if(op==2) update2(x);
if(op==3) update3(x);
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
constexpr int LV = 20, M = (1 << LV) - 1;
int n, q;
struct D {
D* c[2] = {nullptr, nullptr};
// bx: bits that differ, by: not in bx, the same bit
int bx = 0, by = 0, num = 0, lz = 0;
void down(int lv) {
if (lv < 0) return;
if (lz >> lv & 1) {
swap(c[0], c[1]);
lz ^= 1 << lv;
}
for (int i = 0; i < 2; ++i)
if (c[i]) {
c[i]->lz ^= lz;
c[i]->by = ~c[i]->bx & (c[i]->by ^ lz);
}
lz = 0;
}
void up(int lv) {
if (!c[0] && !c[1]) {
bx = by = 0;
num = lv < 0;
} else if (c[0] && c[1]) {
bx = c[0]->bx | c[1]->bx | (c[0]->by ^ c[1]->by) | 1 << lv;
by = ~bx & (c[0]->by | c[1]->by);
num = c[0]->num + c[1]->num;
} else {
int t = !c[0];
bx = c[t]->bx;
by = c[t]->by | t << lv;
num = c[t]->num;
}
by = ~bx & (by ^ lz);
}
} * root;
void dbg(D* a, int lv, int l = 0, int r = M) {
return;
vector<int> s;
function<void(D*, int, int, int)> dfs = [&](D* a, int lv, int l, int r) {
if (!a) return;
a->down(lv);
if (a->num <= 0) {
cout << "ERROR A "
<< " " << lv << " " << l << " " << r << " " << a->num << endl;
exit(0);
};
if (l == r) {
s.push_back(l);
return;
}
dfs(a->c[0], lv - 1, l, (l + r + 1) / 2 - 1);
dfs(a->c[1], lv - 1, (l + r + 1) / 2, r);
};
dfs(a, lv, l, r);
// DEBUG(s);
/*
if (lv == LV - 1) {
cerr << l << "~" << r << endl;
}
if (!a) return;
string pad((LV - lv) * 2, ' ');
cerr << pad << "num,bx,by,lz = " << a->num << "," << a->bx << "," << a->by << "," << a->lz
<< "\n";
int mid = (l + r + 1) / 2;
if (lv < 0) return;
cerr << pad << "0: " << l << "~" << mid - 1 << "\n";
dbg(a->c[0], lv - 1, l, mid - 1);
cerr << pad << "1: " << mid << "~" << r << "\n";
dbg(a->c[1], lv - 1, mid, r);*/
}
// [0,x) [x,M]
pair<D*, D*> splt(D* a, int lv, int x, int l, int r) {
if (!a) return {nullptr, nullptr};
a->down(lv);
if (r < x) return {a, nullptr};
if (l >= x) return {nullptr, a};
D *p[2] = {nullptr, nullptr}, *pt;
int mid = (l + r + 1) / 2; // l,mid-1 | mid,r
if (x < mid) {
tie(pt, a->c[0]) = splt(a->c[0], lv - 1, x, l, mid - 1);
p[1] = a;
if (pt) p[0] = new D, p[0]->c[0] = pt;
} else {
tie(a->c[1], pt) = splt(a->c[1], lv - 1, x, mid, r);
p[0] = a;
if (pt) p[1] = new D, p[1]->c[1] = pt;
}
for (int t : {0, 1})
if (p[t]) {
p[t]->up(lv);
if (!p[t]->num) {
delete p[t];
p[t] = nullptr;
}
}
return {p[0], p[1]};
}
D* mrg(D* a, D* b, int lv) {
if (!a || !b) return a ? a : b;
a->down(lv);
b->down(lv);
a->c[0] = mrg(a->c[0], b->c[0], lv - 1);
a->c[1] = mrg(a->c[1], b->c[1], lv - 1);
a->up(lv);
delete b;
return a;
}
D* ins(D* a, int lv, int z) {
if (!a) a = new D();
if (lv < 0) {
a->lz = a->bx = a->by = 0;
a->num = 1;
return a;
}
a->down(lv);
int b = z >> lv & 1;
a->c[b] = ins(a->c[b], lv - 1, z);
a->up(lv);
return a;
}
void dxor(D* a, int lv, int x) {
if (!a) return;
a->lz ^= x;
a->by ^= ~a->bx & x;
}
void dor(D* a, int lv, int x) {
if (!a) return;
a->down(lv);
if (!(a->bx & x)) {
dxor(a, lv, x & ~a->by);
} else {
if (x >> lv & 1) {
a->c[1] = mrg(a->c[0], a->c[1], lv - 1);
a->c[0] = nullptr;
a->up(lv);
x ^= 1 << lv;
}
dor(a->c[0], lv - 1, x);
dor(a->c[1], lv - 1, x);
}
a->up(lv);
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> q;
for (int i = 0, x; i < n; ++i) {
cin >> x;
root = ins(root, LV - 1, x);
}
for (int tc = 0; tc < q; ++tc) {
dbg(root, LV - 1);
int t, l, r, x;
cin >> t >> l >> r;
auto [r1, rt] = splt(root, LV - 1, l, 0, M);
auto [r2, r3] = splt(rt, LV - 1, r + 1, 0, M);
dbg(r2, LV - 1);
if (t < 4) cin >> x;
if (t == 1) {
dxor(r2, LV - 1, M);
dor(r2, LV - 1, M ^ x);
dxor(r2, LV - 1, M);
} else if (t == 2) {
dor(r2, LV - 1, x);
} else if (t == 3) {
dxor(r2, LV - 1, x);
} else {
cout << (r2 ? r2->num : 0) << endl;
}
r2 = mrg(r2, r3, LV - 1);
root = mrg(r1, r2, LV - 1);
}
} | ### Prompt
Please provide a CPP coded solution to the problem described below:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
constexpr int LV = 20, M = (1 << LV) - 1;
int n, q;
struct D {
D* c[2] = {nullptr, nullptr};
// bx: bits that differ, by: not in bx, the same bit
int bx = 0, by = 0, num = 0, lz = 0;
void down(int lv) {
if (lv < 0) return;
if (lz >> lv & 1) {
swap(c[0], c[1]);
lz ^= 1 << lv;
}
for (int i = 0; i < 2; ++i)
if (c[i]) {
c[i]->lz ^= lz;
c[i]->by = ~c[i]->bx & (c[i]->by ^ lz);
}
lz = 0;
}
void up(int lv) {
if (!c[0] && !c[1]) {
bx = by = 0;
num = lv < 0;
} else if (c[0] && c[1]) {
bx = c[0]->bx | c[1]->bx | (c[0]->by ^ c[1]->by) | 1 << lv;
by = ~bx & (c[0]->by | c[1]->by);
num = c[0]->num + c[1]->num;
} else {
int t = !c[0];
bx = c[t]->bx;
by = c[t]->by | t << lv;
num = c[t]->num;
}
by = ~bx & (by ^ lz);
}
} * root;
void dbg(D* a, int lv, int l = 0, int r = M) {
return;
vector<int> s;
function<void(D*, int, int, int)> dfs = [&](D* a, int lv, int l, int r) {
if (!a) return;
a->down(lv);
if (a->num <= 0) {
cout << "ERROR A "
<< " " << lv << " " << l << " " << r << " " << a->num << endl;
exit(0);
};
if (l == r) {
s.push_back(l);
return;
}
dfs(a->c[0], lv - 1, l, (l + r + 1) / 2 - 1);
dfs(a->c[1], lv - 1, (l + r + 1) / 2, r);
};
dfs(a, lv, l, r);
// DEBUG(s);
/*
if (lv == LV - 1) {
cerr << l << "~" << r << endl;
}
if (!a) return;
string pad((LV - lv) * 2, ' ');
cerr << pad << "num,bx,by,lz = " << a->num << "," << a->bx << "," << a->by << "," << a->lz
<< "\n";
int mid = (l + r + 1) / 2;
if (lv < 0) return;
cerr << pad << "0: " << l << "~" << mid - 1 << "\n";
dbg(a->c[0], lv - 1, l, mid - 1);
cerr << pad << "1: " << mid << "~" << r << "\n";
dbg(a->c[1], lv - 1, mid, r);*/
}
// [0,x) [x,M]
pair<D*, D*> splt(D* a, int lv, int x, int l, int r) {
if (!a) return {nullptr, nullptr};
a->down(lv);
if (r < x) return {a, nullptr};
if (l >= x) return {nullptr, a};
D *p[2] = {nullptr, nullptr}, *pt;
int mid = (l + r + 1) / 2; // l,mid-1 | mid,r
if (x < mid) {
tie(pt, a->c[0]) = splt(a->c[0], lv - 1, x, l, mid - 1);
p[1] = a;
if (pt) p[0] = new D, p[0]->c[0] = pt;
} else {
tie(a->c[1], pt) = splt(a->c[1], lv - 1, x, mid, r);
p[0] = a;
if (pt) p[1] = new D, p[1]->c[1] = pt;
}
for (int t : {0, 1})
if (p[t]) {
p[t]->up(lv);
if (!p[t]->num) {
delete p[t];
p[t] = nullptr;
}
}
return {p[0], p[1]};
}
D* mrg(D* a, D* b, int lv) {
if (!a || !b) return a ? a : b;
a->down(lv);
b->down(lv);
a->c[0] = mrg(a->c[0], b->c[0], lv - 1);
a->c[1] = mrg(a->c[1], b->c[1], lv - 1);
a->up(lv);
delete b;
return a;
}
D* ins(D* a, int lv, int z) {
if (!a) a = new D();
if (lv < 0) {
a->lz = a->bx = a->by = 0;
a->num = 1;
return a;
}
a->down(lv);
int b = z >> lv & 1;
a->c[b] = ins(a->c[b], lv - 1, z);
a->up(lv);
return a;
}
void dxor(D* a, int lv, int x) {
if (!a) return;
a->lz ^= x;
a->by ^= ~a->bx & x;
}
void dor(D* a, int lv, int x) {
if (!a) return;
a->down(lv);
if (!(a->bx & x)) {
dxor(a, lv, x & ~a->by);
} else {
if (x >> lv & 1) {
a->c[1] = mrg(a->c[0], a->c[1], lv - 1);
a->c[0] = nullptr;
a->up(lv);
x ^= 1 << lv;
}
dor(a->c[0], lv - 1, x);
dor(a->c[1], lv - 1, x);
}
a->up(lv);
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> q;
for (int i = 0, x; i < n; ++i) {
cin >> x;
root = ins(root, LV - 1, x);
}
for (int tc = 0; tc < q; ++tc) {
dbg(root, LV - 1);
int t, l, r, x;
cin >> t >> l >> r;
auto [r1, rt] = splt(root, LV - 1, l, 0, M);
auto [r2, r3] = splt(rt, LV - 1, r + 1, 0, M);
dbg(r2, LV - 1);
if (t < 4) cin >> x;
if (t == 1) {
dxor(r2, LV - 1, M);
dor(r2, LV - 1, M ^ x);
dxor(r2, LV - 1, M);
} else if (t == 2) {
dor(r2, LV - 1, x);
} else if (t == 3) {
dxor(r2, LV - 1, x);
} else {
cout << (r2 ? r2->num : 0) << endl;
}
r2 = mrg(r2, r3, LV - 1);
root = mrg(r1, r2, LV - 1);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
template<class t> inline t read(t &x){
char c=getchar();bool f=0;x=0;
while(!isdigit(c)) f|=c=='-',c=getchar();
while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
if(f) x=-x;return x;
}
template<class t> inline void write(t x){
if(x<0) putchar('-'),write(-x);
else{if(x>9) write(x/10);putchar('0'+x%10);}
}
const int N=2e5+5,B=20,S=(1<<B)-1,M=N*23;
int tr[M],t0[M],t1[M],n,lc[M],rc[M],nd,tg[M],rt;
void pushup(int x){
tr[x]=tr[lc[x]]+tr[rc[x]];
t0[x]=t0[lc[x]]|t0[rc[x]];
t1[x]=t1[lc[x]]|t1[rc[x]];
}
void Xor(int x,int d,int v){
if(!x) return ;
if(v>>d&1) swap(lc[x],rc[x]);
int v0=t1[x]&v|t0[x]&~v,v1=t0[x]&v|t1[x]&~v;
t0[x]=v0;t1[x]=v1;tg[x]^=v;
}
void pushdown(int x,int d){
if(tg[x]){
Xor(lc[x],d-1,tg[x]);
Xor(rc[x],d-1,tg[x]);
tg[x]=0;
}
}
void split(int &x,int &y,int d,int l,int r,int p,int q){
if(p<=l&&r<=q) return y=x,x=0,void();
pushdown(x,d);
y=++nd;
int mid=l+r>>1;
if(p<=mid) split(lc[x],lc[y],d-1,l,mid,p,q);
if(q>mid) split(rc[x],rc[y],d-1,mid+1,r,p,q);
pushup(x);pushup(y);
}
void merge(int &x,int y,int d){
if(!x) return x=y,void();
if(d<0||!y) return ;
pushdown(x,d);pushdown(y,d);
merge(lc[x],lc[y],d-1);
merge(rc[x],rc[y],d-1);
pushup(x);
}
void Or(int x,int d,int v){
if(!x||d<0) return ;
if(!(v&t0[x]&t1[x])) return Xor(x,d,v&t0[x]);
pushdown(x,d);
if(v>>d&1){
Xor(lc[x],d-1,1<<d);
merge(rc[x],lc[x],d-1);
lc[x]=0;
}
Or(lc[x],d-1,v);
Or(rc[x],d-1,v);
pushup(x);
}
int que(int x,int d,int l,int r,int p,int q){
if(p<=l&&r<=q) return tr[x];
int mid=l+r>>1;
pushdown(x,d);
int res=0;
if(p<=mid) res=que(lc[x],d-1,l,mid,p,q);
if(q>mid) res+=que(rc[x],d-1,mid+1,r,p,q);
return res;
}
void insert(int &x,int d,int v){
if(!x) x=++nd;
if(d<0) return tr[x]=1,t0[x]=v^S,t1[x]=v,void();
if(v>>d&1) insert(rc[x],d-1,v);
else insert(lc[x],d-1,v);
pushup(x);
}
void doit(){
int o,l,r,x=0,v;
read(o);read(l);read(r);
if(o<=3){
read(v);
split(rt,x,19,0,S,l,r);
if(o==1) Xor(x,19,S),Or(x,19,S^v),Xor(x,19,S);
if(o==2) Or(x,19,v);
if(o==3) Xor(x,19,v);
merge(rt,x,19);
}
else write(que(rt,19,0,S,l,r)),puts("");
}
signed main(){
read(n);int t;read(t);
for(int i=1,x;i<=n;i++) insert(rt,19,read(x));
while(t--) doit();
} | ### Prompt
Please create a solution in Cpp to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template<class t> inline t read(t &x){
char c=getchar();bool f=0;x=0;
while(!isdigit(c)) f|=c=='-',c=getchar();
while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
if(f) x=-x;return x;
}
template<class t> inline void write(t x){
if(x<0) putchar('-'),write(-x);
else{if(x>9) write(x/10);putchar('0'+x%10);}
}
const int N=2e5+5,B=20,S=(1<<B)-1,M=N*23;
int tr[M],t0[M],t1[M],n,lc[M],rc[M],nd,tg[M],rt;
void pushup(int x){
tr[x]=tr[lc[x]]+tr[rc[x]];
t0[x]=t0[lc[x]]|t0[rc[x]];
t1[x]=t1[lc[x]]|t1[rc[x]];
}
void Xor(int x,int d,int v){
if(!x) return ;
if(v>>d&1) swap(lc[x],rc[x]);
int v0=t1[x]&v|t0[x]&~v,v1=t0[x]&v|t1[x]&~v;
t0[x]=v0;t1[x]=v1;tg[x]^=v;
}
void pushdown(int x,int d){
if(tg[x]){
Xor(lc[x],d-1,tg[x]);
Xor(rc[x],d-1,tg[x]);
tg[x]=0;
}
}
void split(int &x,int &y,int d,int l,int r,int p,int q){
if(p<=l&&r<=q) return y=x,x=0,void();
pushdown(x,d);
y=++nd;
int mid=l+r>>1;
if(p<=mid) split(lc[x],lc[y],d-1,l,mid,p,q);
if(q>mid) split(rc[x],rc[y],d-1,mid+1,r,p,q);
pushup(x);pushup(y);
}
void merge(int &x,int y,int d){
if(!x) return x=y,void();
if(d<0||!y) return ;
pushdown(x,d);pushdown(y,d);
merge(lc[x],lc[y],d-1);
merge(rc[x],rc[y],d-1);
pushup(x);
}
void Or(int x,int d,int v){
if(!x||d<0) return ;
if(!(v&t0[x]&t1[x])) return Xor(x,d,v&t0[x]);
pushdown(x,d);
if(v>>d&1){
Xor(lc[x],d-1,1<<d);
merge(rc[x],lc[x],d-1);
lc[x]=0;
}
Or(lc[x],d-1,v);
Or(rc[x],d-1,v);
pushup(x);
}
int que(int x,int d,int l,int r,int p,int q){
if(p<=l&&r<=q) return tr[x];
int mid=l+r>>1;
pushdown(x,d);
int res=0;
if(p<=mid) res=que(lc[x],d-1,l,mid,p,q);
if(q>mid) res+=que(rc[x],d-1,mid+1,r,p,q);
return res;
}
void insert(int &x,int d,int v){
if(!x) x=++nd;
if(d<0) return tr[x]=1,t0[x]=v^S,t1[x]=v,void();
if(v>>d&1) insert(rc[x],d-1,v);
else insert(lc[x],d-1,v);
pushup(x);
}
void doit(){
int o,l,r,x=0,v;
read(o);read(l);read(r);
if(o<=3){
read(v);
split(rt,x,19,0,S,l,r);
if(o==1) Xor(x,19,S),Or(x,19,S^v),Xor(x,19,S);
if(o==2) Or(x,19,v);
if(o==3) Xor(x,19,v);
merge(rt,x,19);
}
else write(que(rt,19,0,S,l,r)),puts("");
}
signed main(){
read(n);int t;read(t);
for(int i=1,x;i<=n;i++) insert(rt,19,read(x));
while(t--) doit();
}
``` |
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef long long ll;
template <typename T>
inline void read(T &f) {
f = 0; T fu = 1; char c = getchar();
while (c < '0' || c > '9') { if (c == '-') { fu = -1; } c = getchar(); }
while (c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); c = getchar(); }
f *= fu;
}
template <typename T>
void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x < 10) putchar(x + 48);
else print(x / 10), putchar(x % 10 + 48);
}
template <typename T>
void print(T x, char t) {
print(x); putchar(t);
}
const int N = 2e5 + 5, M = N * 60, ALL = (1 << 20) - 1;
int ch[M][2], tag0[M], tag1[M], tagx[M], siz[M], can[M], tot;
int a[N];
void update(int u, int dep) {
siz[u] = siz[ch[u][0]] + siz[ch[u][1]];
can[u] = can[ch[u][0]] | can[ch[u][1]];
if (ch[u][0] && ch[u][1]) can[u] |= (1 << dep);
}
void add_tag(int u, int tg0, int tg1, int tgx) {
int tem = ALL ^ tg0 ^ tg1 ^ tgx, temu = ALL ^ tag0[u] ^ tag1[u] ^ tagx[u];
int ans0 = tg0 | (tag1[u] & tgx) | (tag0[u] & tem);
int ans1 = tg1 | (tag0[u] & tgx) | (tag1[u] & tem);
tagx[u] = (tagx[u] & tem) | (tgx & temu);
tag0[u] = ans0; tag1[u] = ans1;
}
int merge(int u, int v, int dep);
void pushdown(int u, int dep) {
if ((tag0[u] >> dep) & 1) {
ch[u][0] = merge(ch[u][0], ch[u][1], dep - 1); ch[u][1] = 0;
}
if ((tag1[u] >> dep) & 1) {
ch[u][1] = merge(ch[u][0], ch[u][1], dep - 1); ch[u][0] = 0;
}
if ((tagx[u] >> dep) & 1) {
swap(ch[u][0], ch[u][1]);
}
update(u, dep);
for (int i = 0; i <= 1; i++) {
if (ch[u][i]) {
add_tag(ch[u][i], tag0[u], tag1[u], tagx[u]);
}
}
tag0[u] = tag1[u] = tagx[u] = 0;
}
void check(int u, int dep) {
if (!u || dep == -1) return;
if (((tag0[u] | tag1[u]) & can[u]) == 0) return;
pushdown(u, dep);
check(ch[u][0], dep - 1); check(ch[u][1], dep - 1);
update(u, dep);
}
int merge(int u, int v, int dep) {
if (!u || !v) return u | v;
if (dep == -1) return u;
pushdown(u, dep); pushdown(v, dep);
ch[u][0] = merge(ch[u][0], ch[v][0], dep - 1);
ch[u][1] = merge(ch[u][1], ch[v][1], dep - 1);
update(u, dep); return u;
}
// split <= x
void split(int u, int x, int &l, int &r, int dep) {
if (!u) { l = r = 0; return; }
if (dep == -1) {
l = u; r = 0;
return;
}
pushdown(u, dep);
if ((x >> dep) & 1) {
l = u; r = ++tot;
split(ch[u][1], x, ch[l][1], ch[r][1], dep - 1);
update(l, dep); update(r, dep);
if (!siz[r]) r = 0;
} else {
l = ++tot; r = u;
split(ch[u][0], x, ch[l][0], ch[r][0], dep - 1);
update(l, dep); update(r, dep);
if (!siz[l]) l = 0;
}
}
void insert(int &u, int x, int dep) {
if (dep == -1) {
u = ++tot;
siz[tot] = 1;
return;
}
if (!u) u = ++tot;
insert(ch[u][(x >> dep) & 1], x, dep - 1);
update(u, dep);
}
void dfs(int u, int dep) {
if (!u) return;
// if (dep != -1) pushdown(u, dep);
fprintf(stderr, "u = %d, tag0[u] = %d, tag1[u] = %d, tagx[u] = %d, ch[u][0] = %d, ch[u][1] = %d, siz[u] = %d, dep = %d\n", u, tag0[u], tag1[u], tagx[u], ch[u][0], ch[u][1], siz[u], dep);
dfs(ch[u][0], dep - 1); dfs(ch[u][1], dep - 1);
}
int n, q, root;
int main() {
read(n); read(q);
for (int i = 1; i <= n; i++) {
int a; read(a);
insert(root, a, 19);
}
while (q--) {
int opt, l, r; read(opt); read(l); read(r);
int a = 0, b = 0, c = 0;
// fprintf(stderr, "root = %d\n", root);
if (l != 0) split(root, l - 1, a, b, 19);
else b = root;
split(b, r, b, c, 19);
if (opt <= 3) {
int x; read(x);
if (opt == 1) add_tag(b, ALL ^ x, 0, 0);
if (opt == 2) add_tag(b, 0, x, 0);
if (opt == 3) add_tag(b, 0, 0, x);
// fprintf(stderr, ">>> %d\n", can[b] & (tag0[b] | tag1[b]));
check(b, 19);
}
// dfs(b, 19);
if (opt == 4) print(siz[b], '\n');
// fprintf(stderr, "b = %d, tag0[b] = %d, tag1[b] = %d, tag2[b] = %d\n", b, tag0[b], tag1[b], tagx[b]);
root = merge(merge(a, b, 19), c, 19);
// dfs(root, 19);
}
return 0;
} | ### Prompt
Create a solution in cpp for the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef long long ll;
template <typename T>
inline void read(T &f) {
f = 0; T fu = 1; char c = getchar();
while (c < '0' || c > '9') { if (c == '-') { fu = -1; } c = getchar(); }
while (c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); c = getchar(); }
f *= fu;
}
template <typename T>
void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x < 10) putchar(x + 48);
else print(x / 10), putchar(x % 10 + 48);
}
template <typename T>
void print(T x, char t) {
print(x); putchar(t);
}
const int N = 2e5 + 5, M = N * 60, ALL = (1 << 20) - 1;
int ch[M][2], tag0[M], tag1[M], tagx[M], siz[M], can[M], tot;
int a[N];
void update(int u, int dep) {
siz[u] = siz[ch[u][0]] + siz[ch[u][1]];
can[u] = can[ch[u][0]] | can[ch[u][1]];
if (ch[u][0] && ch[u][1]) can[u] |= (1 << dep);
}
void add_tag(int u, int tg0, int tg1, int tgx) {
int tem = ALL ^ tg0 ^ tg1 ^ tgx, temu = ALL ^ tag0[u] ^ tag1[u] ^ tagx[u];
int ans0 = tg0 | (tag1[u] & tgx) | (tag0[u] & tem);
int ans1 = tg1 | (tag0[u] & tgx) | (tag1[u] & tem);
tagx[u] = (tagx[u] & tem) | (tgx & temu);
tag0[u] = ans0; tag1[u] = ans1;
}
int merge(int u, int v, int dep);
void pushdown(int u, int dep) {
if ((tag0[u] >> dep) & 1) {
ch[u][0] = merge(ch[u][0], ch[u][1], dep - 1); ch[u][1] = 0;
}
if ((tag1[u] >> dep) & 1) {
ch[u][1] = merge(ch[u][0], ch[u][1], dep - 1); ch[u][0] = 0;
}
if ((tagx[u] >> dep) & 1) {
swap(ch[u][0], ch[u][1]);
}
update(u, dep);
for (int i = 0; i <= 1; i++) {
if (ch[u][i]) {
add_tag(ch[u][i], tag0[u], tag1[u], tagx[u]);
}
}
tag0[u] = tag1[u] = tagx[u] = 0;
}
void check(int u, int dep) {
if (!u || dep == -1) return;
if (((tag0[u] | tag1[u]) & can[u]) == 0) return;
pushdown(u, dep);
check(ch[u][0], dep - 1); check(ch[u][1], dep - 1);
update(u, dep);
}
int merge(int u, int v, int dep) {
if (!u || !v) return u | v;
if (dep == -1) return u;
pushdown(u, dep); pushdown(v, dep);
ch[u][0] = merge(ch[u][0], ch[v][0], dep - 1);
ch[u][1] = merge(ch[u][1], ch[v][1], dep - 1);
update(u, dep); return u;
}
// split <= x
void split(int u, int x, int &l, int &r, int dep) {
if (!u) { l = r = 0; return; }
if (dep == -1) {
l = u; r = 0;
return;
}
pushdown(u, dep);
if ((x >> dep) & 1) {
l = u; r = ++tot;
split(ch[u][1], x, ch[l][1], ch[r][1], dep - 1);
update(l, dep); update(r, dep);
if (!siz[r]) r = 0;
} else {
l = ++tot; r = u;
split(ch[u][0], x, ch[l][0], ch[r][0], dep - 1);
update(l, dep); update(r, dep);
if (!siz[l]) l = 0;
}
}
void insert(int &u, int x, int dep) {
if (dep == -1) {
u = ++tot;
siz[tot] = 1;
return;
}
if (!u) u = ++tot;
insert(ch[u][(x >> dep) & 1], x, dep - 1);
update(u, dep);
}
void dfs(int u, int dep) {
if (!u) return;
// if (dep != -1) pushdown(u, dep);
fprintf(stderr, "u = %d, tag0[u] = %d, tag1[u] = %d, tagx[u] = %d, ch[u][0] = %d, ch[u][1] = %d, siz[u] = %d, dep = %d\n", u, tag0[u], tag1[u], tagx[u], ch[u][0], ch[u][1], siz[u], dep);
dfs(ch[u][0], dep - 1); dfs(ch[u][1], dep - 1);
}
int n, q, root;
int main() {
read(n); read(q);
for (int i = 1; i <= n; i++) {
int a; read(a);
insert(root, a, 19);
}
while (q--) {
int opt, l, r; read(opt); read(l); read(r);
int a = 0, b = 0, c = 0;
// fprintf(stderr, "root = %d\n", root);
if (l != 0) split(root, l - 1, a, b, 19);
else b = root;
split(b, r, b, c, 19);
if (opt <= 3) {
int x; read(x);
if (opt == 1) add_tag(b, ALL ^ x, 0, 0);
if (opt == 2) add_tag(b, 0, x, 0);
if (opt == 3) add_tag(b, 0, 0, x);
// fprintf(stderr, ">>> %d\n", can[b] & (tag0[b] | tag1[b]));
check(b, 19);
}
// dfs(b, 19);
if (opt == 4) print(siz[b], '\n');
// fprintf(stderr, "b = %d, tag0[b] = %d, tag1[b] = %d, tag2[b] = %d\n", b, tag0[b], tag1[b], tagx[b]);
root = merge(merge(a, b, 19), c, 19);
// dfs(root, 19);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int MOD;
struct modint {
private:
int v;
static int minv(int a, int m) {
a %= m;
assert(a);
return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a);
}
public:
modint() : v(0) {}
modint(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }
explicit operator int() const { return v; }
friend std::ostream& operator << (std::ostream& out, const modint& n) { return out << int(n); }
friend std::istream& operator >> (std::istream& in, modint& n) { ll v_; in >> v_; n = modint(v_); return in; }
friend bool operator == (const modint& a, const modint& b) { return a.v == b.v; }
friend bool operator != (const modint& a, const modint& b) { return a.v != b.v; }
modint inv() const {
modint res;
res.v = minv(v, MOD);
return res;
}
friend modint inv(const modint& m) { return m.inv(); }
modint neg() const {
modint res;
res.v = v ? MOD-v : 0;
return res;
}
friend modint neg(const modint& m) { return m.neg(); }
modint operator- () const {
return neg();
}
modint operator+ () const {
return modint(*this);
}
modint& operator ++ () {
v ++;
if (v == MOD) v = 0;
return *this;
}
modint& operator -- () {
if (v == 0) v = MOD;
v --;
return *this;
}
modint& operator += (const modint& o) {
v += o.v;
if (v >= MOD) v -= MOD;
return *this;
}
modint& operator -= (const modint& o) {
v -= o.v;
if (v < 0) v += MOD;
return *this;
}
modint& operator *= (const modint& o) {
v = int(ll(v) * ll(o.v) % MOD);
return *this;
}
modint& operator /= (const modint& o) {
return *this *= o.inv();
}
friend modint operator ++ (modint& a, int) { modint r = a; ++a; return r; }
friend modint operator -- (modint& a, int) { modint r = a; --a; return r; }
friend modint operator + (const modint& a, const modint& b) { return modint(a) += b; }
friend modint operator - (const modint& a, const modint& b) { return modint(a) -= b; }
friend modint operator * (const modint& a, const modint& b) { return modint(a) *= b; }
friend modint operator / (const modint& a, const modint& b) { return modint(a) /= b; }
};
namespace IO {
template<class T>
void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(ll &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { x = getchar(); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U>
void R(T &head, U &... tail) { _R(head), R(tail...); }
template<class T>
void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const ll &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T, class U>
void _W(const pair<T, U> &x) { _W(x.first), putchar(' '), _W(x.second); }
template<class T>
void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U>
void W(const T &head, const U &... tail) { _W(head), putchar(sizeof...(tail) ? ' ' : '\n'), W(tail...); }
}
using namespace IO;
template <typename T> T pow(T a, long long b) {
assert(b >= 0);
T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r;
}
const int maxn = 2e5+50;
const int maxp = maxn*32+5;
const int K = 20;
const int M = 1 << K;
int ls[maxp], rs[maxp], tot;
// 0, 1, cnt, XOR
int t0[maxp], t1[maxp], p[maxp], txor[maxp], tor[maxp];
inline void pushup(int x) {
p[x] = p[ls[x]] + p[rs[x]];
t0[x] = t0[ls[x]] | t0[rs[x]];
t1[x] = t1[ls[x]] | t1[rs[x]];
}
inline void XOR(int x, int t) {
if (!x) return;
txor[x] ^= t;
if (~tor[x] && t >> tor[x] & 1) swap(ls[x], rs[x]);
int a = (t0[x] & (~t)) | (t1[x] & t), b = (t1[x] & (~t)) | (t0[x] & t);
t0[x] = a, t1[x] = b;
}
inline void pushdown(int x) {
if (txor[x]) {
XOR(ls[x], txor[x]), XOR(rs[x], txor[x]);
txor[x] = 0;
}
}
inline void insert(int &x, int s, int k) {
if (!x) x = ++tot;
tor[x] = k;
if (k == -1) {
t1[x] = s, t0[x] = s ^ (M - 1);
p[x] = 1;
return;
}
insert((s >> k & 1) ? rs[x] : ls[x], s, k - 1);
pushup(x);
}
inline void split(int &x, int &y, int l, int r, int le, int re) {
if (!x || re < l || le > r) {
y = 0;
return;
}
if (le <= l && r <= re) {
y = x;
x = 0;
return;
}
int mid = l + r >> 1; pushdown(x);
tor[y = ++tot] = tor[x];
split(ls[x], ls[y], l, mid, le, re);
split(rs[x], rs[y], mid + 1, r, le, re);
pushup(x), pushup(y);
}
inline void merge(int &x, int y) {
if (!x || !y) {
x = x | y;
return;
}
pushdown(x), pushdown(y);
merge(ls[x], ls[y]), merge(rs[x], rs[y]);
if (~tor[x]) pushup(x);
}
inline void OR(int x, int s) {
if (!x) return;
if (!(s & t0[x] & t1[x])) {
XOR(x, s & t0[x]);
return;
}
pushdown(x);
if (s >> tor[x] & 1) XOR(ls[x], 1 << tor[x]), merge(rs[x], ls[x]), ls[x] = 0;
OR(ls[x], s), OR(rs[x], s);
pushup(x);
}
int main() {
int n, q, x = 0;
R(n, q);
for (int i = 1; i <= n; ++i) {
int v; R(v); insert(x, v, K - 1);
}
for (; q; --q) {
int oth;
int t, l, r, v; R(t, l, r);
split(x, oth, 0, M - 1, l, r);
if (t == 1) R(v),XOR(oth, M - 1),OR(oth, v ^ (M - 1)),XOR(oth, M - 1);
else if (t == 2) R(v),OR(oth, v);
else if (t == 3) R(v), XOR(oth, v);
else W(p[oth]);
merge(x, oth);
}
return 0;
} | ### Prompt
In cpp, your task is to solve the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int MOD;
struct modint {
private:
int v;
static int minv(int a, int m) {
a %= m;
assert(a);
return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a);
}
public:
modint() : v(0) {}
modint(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }
explicit operator int() const { return v; }
friend std::ostream& operator << (std::ostream& out, const modint& n) { return out << int(n); }
friend std::istream& operator >> (std::istream& in, modint& n) { ll v_; in >> v_; n = modint(v_); return in; }
friend bool operator == (const modint& a, const modint& b) { return a.v == b.v; }
friend bool operator != (const modint& a, const modint& b) { return a.v != b.v; }
modint inv() const {
modint res;
res.v = minv(v, MOD);
return res;
}
friend modint inv(const modint& m) { return m.inv(); }
modint neg() const {
modint res;
res.v = v ? MOD-v : 0;
return res;
}
friend modint neg(const modint& m) { return m.neg(); }
modint operator- () const {
return neg();
}
modint operator+ () const {
return modint(*this);
}
modint& operator ++ () {
v ++;
if (v == MOD) v = 0;
return *this;
}
modint& operator -- () {
if (v == 0) v = MOD;
v --;
return *this;
}
modint& operator += (const modint& o) {
v += o.v;
if (v >= MOD) v -= MOD;
return *this;
}
modint& operator -= (const modint& o) {
v -= o.v;
if (v < 0) v += MOD;
return *this;
}
modint& operator *= (const modint& o) {
v = int(ll(v) * ll(o.v) % MOD);
return *this;
}
modint& operator /= (const modint& o) {
return *this *= o.inv();
}
friend modint operator ++ (modint& a, int) { modint r = a; ++a; return r; }
friend modint operator -- (modint& a, int) { modint r = a; --a; return r; }
friend modint operator + (const modint& a, const modint& b) { return modint(a) += b; }
friend modint operator - (const modint& a, const modint& b) { return modint(a) -= b; }
friend modint operator * (const modint& a, const modint& b) { return modint(a) *= b; }
friend modint operator / (const modint& a, const modint& b) { return modint(a) /= b; }
};
namespace IO {
template<class T>
void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(ll &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { x = getchar(); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U>
void R(T &head, U &... tail) { _R(head), R(tail...); }
template<class T>
void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const ll &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T, class U>
void _W(const pair<T, U> &x) { _W(x.first), putchar(' '), _W(x.second); }
template<class T>
void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U>
void W(const T &head, const U &... tail) { _W(head), putchar(sizeof...(tail) ? ' ' : '\n'), W(tail...); }
}
using namespace IO;
template <typename T> T pow(T a, long long b) {
assert(b >= 0);
T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r;
}
const int maxn = 2e5+50;
const int maxp = maxn*32+5;
const int K = 20;
const int M = 1 << K;
int ls[maxp], rs[maxp], tot;
// 0, 1, cnt, XOR
int t0[maxp], t1[maxp], p[maxp], txor[maxp], tor[maxp];
inline void pushup(int x) {
p[x] = p[ls[x]] + p[rs[x]];
t0[x] = t0[ls[x]] | t0[rs[x]];
t1[x] = t1[ls[x]] | t1[rs[x]];
}
inline void XOR(int x, int t) {
if (!x) return;
txor[x] ^= t;
if (~tor[x] && t >> tor[x] & 1) swap(ls[x], rs[x]);
int a = (t0[x] & (~t)) | (t1[x] & t), b = (t1[x] & (~t)) | (t0[x] & t);
t0[x] = a, t1[x] = b;
}
inline void pushdown(int x) {
if (txor[x]) {
XOR(ls[x], txor[x]), XOR(rs[x], txor[x]);
txor[x] = 0;
}
}
inline void insert(int &x, int s, int k) {
if (!x) x = ++tot;
tor[x] = k;
if (k == -1) {
t1[x] = s, t0[x] = s ^ (M - 1);
p[x] = 1;
return;
}
insert((s >> k & 1) ? rs[x] : ls[x], s, k - 1);
pushup(x);
}
inline void split(int &x, int &y, int l, int r, int le, int re) {
if (!x || re < l || le > r) {
y = 0;
return;
}
if (le <= l && r <= re) {
y = x;
x = 0;
return;
}
int mid = l + r >> 1; pushdown(x);
tor[y = ++tot] = tor[x];
split(ls[x], ls[y], l, mid, le, re);
split(rs[x], rs[y], mid + 1, r, le, re);
pushup(x), pushup(y);
}
inline void merge(int &x, int y) {
if (!x || !y) {
x = x | y;
return;
}
pushdown(x), pushdown(y);
merge(ls[x], ls[y]), merge(rs[x], rs[y]);
if (~tor[x]) pushup(x);
}
inline void OR(int x, int s) {
if (!x) return;
if (!(s & t0[x] & t1[x])) {
XOR(x, s & t0[x]);
return;
}
pushdown(x);
if (s >> tor[x] & 1) XOR(ls[x], 1 << tor[x]), merge(rs[x], ls[x]), ls[x] = 0;
OR(ls[x], s), OR(rs[x], s);
pushup(x);
}
int main() {
int n, q, x = 0;
R(n, q);
for (int i = 1; i <= n; ++i) {
int v; R(v); insert(x, v, K - 1);
}
for (; q; --q) {
int oth;
int t, l, r, v; R(t, l, r);
split(x, oth, 0, M - 1, l, r);
if (t == 1) R(v),XOR(oth, M - 1),OR(oth, v ^ (M - 1)),XOR(oth, M - 1);
else if (t == 2) R(v),OR(oth, v);
else if (t == 3) R(v), XOR(oth, v);
else W(p[oth]);
merge(x, oth);
}
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
const int N=200005,f=(1<<20)-1;
int n,q,u,m,lc[N<<6],rc[N<<6],tr[N<<6],t0[N<<6],t1[N<<6],t[N<<6];
void pt(int u,int p,int x)
{
if(!u)
return;
if(x>>(p-1)&1)
swap(lc[u],rc[u]);
int v0=(t0[u]&(~x))|(t1[u]&x),v1=(t1[u]&(~x))|(t0[u]&x);
t0[u]=v0,t1[u]=v1,t[u]^=x;
}
void pd(int u,int p)
{
if(t[u])
{
pt(lc[u],p-1,t[u]);
pt(rc[u],p-1,t[u]);
t[u]=0;
}
}
void pu(int u)
{
tr[u]=tr[lc[u]]+tr[rc[u]];
t0[u]=t0[lc[u]]|t0[rc[u]];
t1[u]=t1[lc[u]]|t1[rc[u]];
}
int ins(int u,int p,int x)
{
if(!u)
u=++m;
if(!p)
{
tr[u]=1;
t0[u]=(x^f);
t1[u]=x;
return u;
}
if(!(x>>(p-1)&1))
lc[u]=ins(lc[u],p-1,x);
else
rc[u]=ins(rc[u],p-1,x);
pu(u);
return u;
}
void split(int &u,int &v,int p,int l,int r,int a,int b)
{
if(b<=l||r<=a||!u)
{
v=0;
return;
}
if(a<=l&&r<=b)
{
v=u;
u=0;
return;
}
pd(u,p);
v=++m;
int mid=l+r>>1;
split(lc[u],lc[v],p-1,l,mid,a,b),split(rc[u],rc[v],p-1,mid,r,a,b);
pu(u),pu(v);
}
void mg(int &u,int &v,int p)
{
if(!u)
{
u=v;
return;
}
if(v==0||p==0)
return;
pd(u,p),pd(v,p);
mg(lc[u],lc[v],p-1);
mg(rc[u],rc[v],p-1);
pu(u);
}
void upd(int u,int p,int x)
{
if(!u)
return;
if(!(x&t0[u]&t1[u]))
{
pt(u,p,x&t0[u]);
return;
}
pd(u,p);
if(x>>(p-1)&1)
{
pt(lc[u],p-1,1<<(p-1));
mg(rc[u],lc[u],p-1);
lc[u]=0;
}
upd(lc[u],p-1,x);
upd(rc[u],p-1,x);
pu(u);
}
int ask(int u,int p,int l,int r,int a,int b)
{
if(b<=l||r<=a||!u)
return 0;
if(a<=l&&r<=b)
return tr[u];
pd(u,p);
int mid=l+r>>1;
return ask(lc[u],p-1,l,mid,a,b)+ask(rc[u],p-1,mid,r,a,b);
}
int main()
{
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
u=ins(u,20,x);
}
while(q--)
{
int t,l,r,v,x;
scanf("%d%d%d",&t,&l,&r);
r++;
if(t<=3)
{
scanf("%d",&x);
split(u,v,20,0,f+1,l,r);
if(t==1)
pt(v,20,f),upd(v,20,x^f),pt(v,20,f);
if(t==2)
upd(v,20,x);
if(t==3)
pt(v,20,x);
mg(u,v,20);
}
else
printf("%d\n",ask(u,20,0,f+1,l,r));
}
return 0;
} | ### Prompt
In cpp, your task is to solve the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
const int N=200005,f=(1<<20)-1;
int n,q,u,m,lc[N<<6],rc[N<<6],tr[N<<6],t0[N<<6],t1[N<<6],t[N<<6];
void pt(int u,int p,int x)
{
if(!u)
return;
if(x>>(p-1)&1)
swap(lc[u],rc[u]);
int v0=(t0[u]&(~x))|(t1[u]&x),v1=(t1[u]&(~x))|(t0[u]&x);
t0[u]=v0,t1[u]=v1,t[u]^=x;
}
void pd(int u,int p)
{
if(t[u])
{
pt(lc[u],p-1,t[u]);
pt(rc[u],p-1,t[u]);
t[u]=0;
}
}
void pu(int u)
{
tr[u]=tr[lc[u]]+tr[rc[u]];
t0[u]=t0[lc[u]]|t0[rc[u]];
t1[u]=t1[lc[u]]|t1[rc[u]];
}
int ins(int u,int p,int x)
{
if(!u)
u=++m;
if(!p)
{
tr[u]=1;
t0[u]=(x^f);
t1[u]=x;
return u;
}
if(!(x>>(p-1)&1))
lc[u]=ins(lc[u],p-1,x);
else
rc[u]=ins(rc[u],p-1,x);
pu(u);
return u;
}
void split(int &u,int &v,int p,int l,int r,int a,int b)
{
if(b<=l||r<=a||!u)
{
v=0;
return;
}
if(a<=l&&r<=b)
{
v=u;
u=0;
return;
}
pd(u,p);
v=++m;
int mid=l+r>>1;
split(lc[u],lc[v],p-1,l,mid,a,b),split(rc[u],rc[v],p-1,mid,r,a,b);
pu(u),pu(v);
}
void mg(int &u,int &v,int p)
{
if(!u)
{
u=v;
return;
}
if(v==0||p==0)
return;
pd(u,p),pd(v,p);
mg(lc[u],lc[v],p-1);
mg(rc[u],rc[v],p-1);
pu(u);
}
void upd(int u,int p,int x)
{
if(!u)
return;
if(!(x&t0[u]&t1[u]))
{
pt(u,p,x&t0[u]);
return;
}
pd(u,p);
if(x>>(p-1)&1)
{
pt(lc[u],p-1,1<<(p-1));
mg(rc[u],lc[u],p-1);
lc[u]=0;
}
upd(lc[u],p-1,x);
upd(rc[u],p-1,x);
pu(u);
}
int ask(int u,int p,int l,int r,int a,int b)
{
if(b<=l||r<=a||!u)
return 0;
if(a<=l&&r<=b)
return tr[u];
pd(u,p);
int mid=l+r>>1;
return ask(lc[u],p-1,l,mid,a,b)+ask(rc[u],p-1,mid,r,a,b);
}
int main()
{
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
u=ins(u,20,x);
}
while(q--)
{
int t,l,r,v,x;
scanf("%d%d%d",&t,&l,&r);
r++;
if(t<=3)
{
scanf("%d",&x);
split(u,v,20,0,f+1,l,r);
if(t==1)
pt(v,20,f),upd(v,20,x^f),pt(v,20,f);
if(t==2)
upd(v,20,x);
if(t==3)
pt(v,20,x);
mg(u,v,20);
}
else
printf("%d\n",ask(u,20,0,f+1,l,r));
}
return 0;
}
``` |
/*
author: Maksim1744
created: 06.05.2021 16:01:27
*/
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define sum(a) ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
template<typename T> vector<T>& operator-- (vector<T> &v){for (auto& i : v) --i; return v;}
template<typename T> vector<T>& operator++ (vector<T> &v){for (auto& i : v) ++i; return v;}
template<typename T> istream& operator>>(istream& is, vector<T> &v){for (auto& i : v) is >> i; return is;}
template<typename T> ostream& operator<<(ostream& os, vector<T> v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> pair<T,U>& operator-- (pair<T, U> &p){--p.first; --p.second; return p;}
template<typename T, typename U> pair<T,U>& operator++ (pair<T, U> &p){++p.first; ++p.second; return p;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U> &p){is >> p.first >> p.second; return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U> p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}
#ifdef HOME
#define SHOW_COLORS
#else
#define show(...) void(0)
#define mclock void(0)
#define shows void(0)
#define debug if (false)
#endif
const int B = 20;
struct Node {
Node* to[2];
int sz = 0;
int mod = 0;
int def0 = (1 << B) - 1;
int def1 = (1 << B) - 1;
Node() {
to[0] = nullptr;
to[1] = nullptr;
}
};
ostream &operator << (ostream &o, const Node &n) {
return o << "[sz=" << n.sz << ", mod=" << n.mod
<< ", def0=" << n.def0 << ", def1=" << n.def1 << "]";
}
void add_num(Node *node, int x) {
for (int i = 0; i < B; ++i) {
node->sz++;
node->def1 &= x;
node->def0 &= ((1 << B) - 1) ^ x;
int b = ((x >> (B - i - 1)) & 1);
if (!node->to[b]) {
node->to[b] = new Node();
}
node = node->to[b];
}
node->sz++;
node->def1 &= x;
node->def0 &= ((1 << B) - 1) ^ x;
}
void update(Node *node, int level) {
if (!node) return;
node->sz = 0;
if (node->to[0]) node->sz += node->to[0]->sz;
if (node->to[1]) node->sz += node->to[1]->sz;
node->def0 = (1 << B) - 1;
node->def1 = (1 << B) - 1;
if (node->to[0]) {
node->def0 &= node->to[0]->def0;
node->def1 &= node->to[0]->def1;
}
if (node->to[1]) {
node->def0 &= node->to[1]->def0;
node->def1 &= node->to[1]->def1;
}
if (!node->to[0])
node->def1 |= (1 << (B - level - 1));
if (!node->to[1])
node->def0 |= (1 << (B - level - 1));
}
void modify(Node *node, int x) {
if (!node) return;
node->mod ^= x;
int mask = (x & (node->def0 | node->def1));
node->def0 ^= mask;
node->def1 ^= mask;
}
void push(Node *node, int level) {
if (!node) {
return;
}
if ((node->mod >> (B - level - 1)) & 1) {
swap(node->to[0], node->to[1]);
}
modify(node->to[0], node->mod);
modify(node->to[1], node->mod);
node->mod = 0;
}
pair<Node*, Node*> split(Node *node, int m, int l = 0, int r = (1 << B) - 1, int level = 0) {
push(node, level);
if (!node) return {nullptr, nullptr};
if (r <= m) return {node, nullptr};
if (l > m) return {nullptr, node};
int mid = (l + r) / 2;
auto L = new Node();
auto R = new Node();
if (mid < m) {
auto [a, b] = split(node->to[1], m, mid + 1, r, level + 1);
if (b) {
R->to[1] = b;
} else {
R = nullptr;
}
if (node->to[0] || a) {
L->to[0] = node->to[0];
L->to[1] = a;
} else {
L = nullptr;
}
update(L, level);
update(R, level);
return {L, R};
} else {
auto [a, b] = split(node->to[0], m, l, mid, level + 1);
if (node->to[1] || b) {
R->to[0] = b;
R->to[1] = node->to[1];
} else {
R = nullptr;
}
if (a) {
L->to[0] = a;
} else {
L = nullptr;
}
update(L, level);
update(R, level);
return {L, R};
}
}
Node* mrg(Node *a, Node *b, int level) {
if (!a) return b;
if (!b) return a;
if (level == B) {
assert(a->sz == 1 && b->sz == 1);
return a;
}
Node *root = a;
push(a, level);
push(b, level);
a->to[0] = mrg(a->to[0], b->to[0], level + 1);
a->to[1] = mrg(a->to[1], b->to[1], level + 1);
update(root, level);
return root;
}
tuple<Node*, Node*, Node*> cut(Node *root, int l, int r) {
auto [a, bc] = split(root, l - 1);
auto [b, c] = split(bc, r);
return {a, b, c};
}
Node* merge3(Node *a, Node *b, Node *c) {
return mrg(mrg(a, b, 0), c, 0);
}
void push_or_bit(Node *node, int b, int level) {
if (!node) return;
push(node, level);
if (level == B - b - 1) {
modify(node->to[0], 1 << b);
node->to[1] = mrg(node->to[0], node->to[1], level + 1);
node->to[0] = nullptr;
update(node, level);
return;
}
if ((node->def1 >> b) & 1) {
return;
}
if ((node->def0 >> b) & 1) {
modify(node, 1 << b);
return;
}
push_or_bit(node->to[0], b, level + 1);
push_or_bit(node->to[1], b, level + 1);
update(node, level);
}
void push_or(Node *root, int x) {
for (int b = 0; b < B; ++b) {
if ((x >> b) & 1) {
push_or_bit(root, b, 0);
}
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
Node *root = new Node();
auto call_xor = [&](int l, int r, int x) {
auto [a, b, c] = cut(root, l, r);
modify(b, x);
root = merge3(a, b, c);
};
auto call_or = [&](int l, int r, int x) {
auto [a, b, c] = cut(root, l, r);
push_or(b, x);
root = merge3(a, b, c);
};
auto call_ask = [&](int l, int r) {
auto [a, b, c] = cut(root, l, r);
int res = (b ? b->sz : 0);
return res;
};
auto call_and = [&](int l, int r, int x) {
call_xor(0, (1 << B) - 1, (1 << B) - 1);
call_or(r ^ ((1 << B) - 1), l ^ ((1 << B) - 1), ((1 << B) - 1) ^ x);
call_xor(0, (1 << B) - 1, (1 << B) - 1);
};
int n, q;
cin >> n >> q;
vector<int> a(n);
cin >> a;
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
n = a.size();
for (int k : a) {
add_num(root, k);
}
while (q--) {
int tp, l, r, x;
cin >> tp >> l >> r;
if (tp != 4) cin >> x;
if (tp == 1) {
call_and(l, r, x);
} else if (tp == 2) {
call_or(l, r, x);
} else if (tp == 3) {
call_xor(l, r, x);
} else if (tp == 4) {
cout << call_ask(l, r) << '\n';
} else {
assert(false);
}
}
return 0;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
/*
author: Maksim1744
created: 06.05.2021 16:01:27
*/
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define sum(a) ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
template<typename T> vector<T>& operator-- (vector<T> &v){for (auto& i : v) --i; return v;}
template<typename T> vector<T>& operator++ (vector<T> &v){for (auto& i : v) ++i; return v;}
template<typename T> istream& operator>>(istream& is, vector<T> &v){for (auto& i : v) is >> i; return is;}
template<typename T> ostream& operator<<(ostream& os, vector<T> v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> pair<T,U>& operator-- (pair<T, U> &p){--p.first; --p.second; return p;}
template<typename T, typename U> pair<T,U>& operator++ (pair<T, U> &p){++p.first; ++p.second; return p;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U> &p){is >> p.first >> p.second; return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U> p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}
#ifdef HOME
#define SHOW_COLORS
#else
#define show(...) void(0)
#define mclock void(0)
#define shows void(0)
#define debug if (false)
#endif
const int B = 20;
struct Node {
Node* to[2];
int sz = 0;
int mod = 0;
int def0 = (1 << B) - 1;
int def1 = (1 << B) - 1;
Node() {
to[0] = nullptr;
to[1] = nullptr;
}
};
ostream &operator << (ostream &o, const Node &n) {
return o << "[sz=" << n.sz << ", mod=" << n.mod
<< ", def0=" << n.def0 << ", def1=" << n.def1 << "]";
}
void add_num(Node *node, int x) {
for (int i = 0; i < B; ++i) {
node->sz++;
node->def1 &= x;
node->def0 &= ((1 << B) - 1) ^ x;
int b = ((x >> (B - i - 1)) & 1);
if (!node->to[b]) {
node->to[b] = new Node();
}
node = node->to[b];
}
node->sz++;
node->def1 &= x;
node->def0 &= ((1 << B) - 1) ^ x;
}
void update(Node *node, int level) {
if (!node) return;
node->sz = 0;
if (node->to[0]) node->sz += node->to[0]->sz;
if (node->to[1]) node->sz += node->to[1]->sz;
node->def0 = (1 << B) - 1;
node->def1 = (1 << B) - 1;
if (node->to[0]) {
node->def0 &= node->to[0]->def0;
node->def1 &= node->to[0]->def1;
}
if (node->to[1]) {
node->def0 &= node->to[1]->def0;
node->def1 &= node->to[1]->def1;
}
if (!node->to[0])
node->def1 |= (1 << (B - level - 1));
if (!node->to[1])
node->def0 |= (1 << (B - level - 1));
}
void modify(Node *node, int x) {
if (!node) return;
node->mod ^= x;
int mask = (x & (node->def0 | node->def1));
node->def0 ^= mask;
node->def1 ^= mask;
}
void push(Node *node, int level) {
if (!node) {
return;
}
if ((node->mod >> (B - level - 1)) & 1) {
swap(node->to[0], node->to[1]);
}
modify(node->to[0], node->mod);
modify(node->to[1], node->mod);
node->mod = 0;
}
pair<Node*, Node*> split(Node *node, int m, int l = 0, int r = (1 << B) - 1, int level = 0) {
push(node, level);
if (!node) return {nullptr, nullptr};
if (r <= m) return {node, nullptr};
if (l > m) return {nullptr, node};
int mid = (l + r) / 2;
auto L = new Node();
auto R = new Node();
if (mid < m) {
auto [a, b] = split(node->to[1], m, mid + 1, r, level + 1);
if (b) {
R->to[1] = b;
} else {
R = nullptr;
}
if (node->to[0] || a) {
L->to[0] = node->to[0];
L->to[1] = a;
} else {
L = nullptr;
}
update(L, level);
update(R, level);
return {L, R};
} else {
auto [a, b] = split(node->to[0], m, l, mid, level + 1);
if (node->to[1] || b) {
R->to[0] = b;
R->to[1] = node->to[1];
} else {
R = nullptr;
}
if (a) {
L->to[0] = a;
} else {
L = nullptr;
}
update(L, level);
update(R, level);
return {L, R};
}
}
Node* mrg(Node *a, Node *b, int level) {
if (!a) return b;
if (!b) return a;
if (level == B) {
assert(a->sz == 1 && b->sz == 1);
return a;
}
Node *root = a;
push(a, level);
push(b, level);
a->to[0] = mrg(a->to[0], b->to[0], level + 1);
a->to[1] = mrg(a->to[1], b->to[1], level + 1);
update(root, level);
return root;
}
tuple<Node*, Node*, Node*> cut(Node *root, int l, int r) {
auto [a, bc] = split(root, l - 1);
auto [b, c] = split(bc, r);
return {a, b, c};
}
Node* merge3(Node *a, Node *b, Node *c) {
return mrg(mrg(a, b, 0), c, 0);
}
void push_or_bit(Node *node, int b, int level) {
if (!node) return;
push(node, level);
if (level == B - b - 1) {
modify(node->to[0], 1 << b);
node->to[1] = mrg(node->to[0], node->to[1], level + 1);
node->to[0] = nullptr;
update(node, level);
return;
}
if ((node->def1 >> b) & 1) {
return;
}
if ((node->def0 >> b) & 1) {
modify(node, 1 << b);
return;
}
push_or_bit(node->to[0], b, level + 1);
push_or_bit(node->to[1], b, level + 1);
update(node, level);
}
void push_or(Node *root, int x) {
for (int b = 0; b < B; ++b) {
if ((x >> b) & 1) {
push_or_bit(root, b, 0);
}
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
Node *root = new Node();
auto call_xor = [&](int l, int r, int x) {
auto [a, b, c] = cut(root, l, r);
modify(b, x);
root = merge3(a, b, c);
};
auto call_or = [&](int l, int r, int x) {
auto [a, b, c] = cut(root, l, r);
push_or(b, x);
root = merge3(a, b, c);
};
auto call_ask = [&](int l, int r) {
auto [a, b, c] = cut(root, l, r);
int res = (b ? b->sz : 0);
return res;
};
auto call_and = [&](int l, int r, int x) {
call_xor(0, (1 << B) - 1, (1 << B) - 1);
call_or(r ^ ((1 << B) - 1), l ^ ((1 << B) - 1), ((1 << B) - 1) ^ x);
call_xor(0, (1 << B) - 1, (1 << B) - 1);
};
int n, q;
cin >> n >> q;
vector<int> a(n);
cin >> a;
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
n = a.size();
for (int k : a) {
add_num(root, k);
}
while (q--) {
int tp, l, r, x;
cin >> tp >> l >> r;
if (tp != 4) cin >> x;
if (tp == 1) {
call_and(l, r, x);
} else if (tp == 2) {
call_or(l, r, x);
} else if (tp == 3) {
call_xor(l, r, x);
} else if (tp == 4) {
cout << call_ask(l, r) << '\n';
} else {
assert(false);
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
const int LG = 20, U = (1 << LG) - 1;
struct Node {
int all0, all1;
int sz;
Node() : all0(U), all1(U), sz(0) {}
Node(int a0, int a1, int _sz) : all0(a0), all1(a1), sz(_sz) {}
};
Node operator+(const Node &a, const Node &b) {
return Node(a.all0 & b.all0, a.all1 & b.all1, a.sz + b.sz);
}
struct Trie {
Trie *son[2];
Node a;
int dep;
int tag;
Trie(int d) : dep(d), tag(0) {
son[0] = son[1] = NULL;
}
~Trie() {
delete son[0];
delete son[1];
}
};
void up(Trie *u) {
u->a = (u->son[0] != NULL ? u->son[0]->a : Node()) +
(u->son[1] != NULL ? u->son[1]->a : Node());
u->a.all0 |= (u->son[1] == NULL) << (u->dep - 1);
u->a.all1 |= (u->son[0] == NULL) << (u->dep - 1);
}
void work_xor(Trie *u, int v) {
if (u != NULL && u->dep > 0) {
int t = (u->a.all0 & v) ^ (u->a.all1 & v);
u->a.all0 ^= t, u->a.all1 ^= t;
if (v >> (u->dep - 1) & 1) {
std::swap(u->son[0], u->son[1]);
v ^= 1 << (u->dep - 1);
}
u->tag ^= v;
}
}
void down(Trie *u) {
work_xor(u->son[0], u->tag);
work_xor(u->son[1], u->tag);
u->tag = 0;
}
void insert(Trie *&u, int v, int d = LG) {
if (u == NULL) {
u = new Trie(d);
}
if (d == 0) {
u->a = Node(0, 0, 1);
return;
}
down(u);
insert(u->son[v >> (d - 1) & 1], v, d - 1);
up(u);
}
void split(Trie *u, int v, Trie *&x, Trie *&y) {
if (u == NULL) {
x = y = NULL;
return;
}
if (u->dep == 0) {
x = u;
y = NULL;
return;
}
down(u);
if (v >> (u->dep - 1) & 1) {
x = u;
y = new Trie(u->dep);
split(u->son[1], v, x->son[1], y->son[1]);
} else {
y = u;
x = new Trie(u->dep);
split(u->son[0], v, x->son[0], y->son[0]);
}
if (x->son[0] == NULL && x->son[1] == NULL) {
delete x;
x = NULL;
} else {
up(x);
}
if (y->son[0] == NULL && y->son[1] == NULL) {
delete y;
y = NULL;
} else {
up(y);
}
}
Trie *merge(Trie *u, Trie *v) {
if (u == NULL) {
return v;
}
if (v == NULL) {
return u;
}
if (u->dep == 0) {
u->a.sz = u->a.sz || v->a.sz;
delete v;
return u;
}
down(u), down(v);
u->son[0] = merge(u->son[0], v->son[0]);
u->son[1] = merge(u->son[1], v->son[1]);
up(u);
v->son[0] = v->son[1] = NULL;
delete v;
return u;
}
void split(Trie *u, int l, int r, Trie *&x, Trie *&y) {
if (l == 0) {
split(u, r, y, x);
} else {
split(u, l - 1, x, y);
Trie *z;
split(y, r, y, z);
x = merge(x, z);
}
}
void work_and(Trie *u, int v) {
if (u == NULL || u->dep == 0) {
return;
}
v &= (1 << u->dep) - 1;
if (((u->a.all0 | u->a.all1) & v) == v) {
work_xor(u, u->a.all1 & v);
return;
}
down(u);
if (v >> (u->dep - 1) & 1) {
u->son[0] = merge(u->son[0], u->son[1]);
u->son[1] = NULL;
}
work_and(u->son[0], v);
work_and(u->son[1], v);
up(u);
}
void work_or(Trie *u, int v) {
if (u == NULL || u->dep == 0) {
return;
}
v &= (1 << u->dep) - 1;
if (((u->a.all0 | u->a.all1) & v) == v) {
work_xor(u, u->a.all0 & v);
return;
}
down(u);
if (v >> (u->dep - 1) & 1) {
u->son[1] = merge(u->son[0], u->son[1]);
u->son[0] = NULL;
}
work_or(u->son[0], v);
work_or(u->son[1], v);
up(u);
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
int n, q;
std::cin >> n >> q;
Trie *rt = NULL;
for (int i = 0; i < n; ++i) {
int v;
std::cin >> v;
insert(rt, v);
}
while (q--) {
int op, l, r;
std::cin >> op >> l >> r;
Trie *tmp;
split(rt, l, r, tmp, rt);
if (op <= 3) {
int x;
std::cin >> x;
if (op == 1) {
work_and(rt, U ^ x);
} else if (op == 2) {
work_or(rt, x);
} else {
work_xor(rt, x);
}
} else {
std::cout << (rt == NULL ? 0 : rt->a.sz) << "\n";
}
rt = merge(rt, tmp);
}
}
| ### Prompt
Create a solution in Cpp for the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <bits/stdc++.h>
const int LG = 20, U = (1 << LG) - 1;
struct Node {
int all0, all1;
int sz;
Node() : all0(U), all1(U), sz(0) {}
Node(int a0, int a1, int _sz) : all0(a0), all1(a1), sz(_sz) {}
};
Node operator+(const Node &a, const Node &b) {
return Node(a.all0 & b.all0, a.all1 & b.all1, a.sz + b.sz);
}
struct Trie {
Trie *son[2];
Node a;
int dep;
int tag;
Trie(int d) : dep(d), tag(0) {
son[0] = son[1] = NULL;
}
~Trie() {
delete son[0];
delete son[1];
}
};
void up(Trie *u) {
u->a = (u->son[0] != NULL ? u->son[0]->a : Node()) +
(u->son[1] != NULL ? u->son[1]->a : Node());
u->a.all0 |= (u->son[1] == NULL) << (u->dep - 1);
u->a.all1 |= (u->son[0] == NULL) << (u->dep - 1);
}
void work_xor(Trie *u, int v) {
if (u != NULL && u->dep > 0) {
int t = (u->a.all0 & v) ^ (u->a.all1 & v);
u->a.all0 ^= t, u->a.all1 ^= t;
if (v >> (u->dep - 1) & 1) {
std::swap(u->son[0], u->son[1]);
v ^= 1 << (u->dep - 1);
}
u->tag ^= v;
}
}
void down(Trie *u) {
work_xor(u->son[0], u->tag);
work_xor(u->son[1], u->tag);
u->tag = 0;
}
void insert(Trie *&u, int v, int d = LG) {
if (u == NULL) {
u = new Trie(d);
}
if (d == 0) {
u->a = Node(0, 0, 1);
return;
}
down(u);
insert(u->son[v >> (d - 1) & 1], v, d - 1);
up(u);
}
void split(Trie *u, int v, Trie *&x, Trie *&y) {
if (u == NULL) {
x = y = NULL;
return;
}
if (u->dep == 0) {
x = u;
y = NULL;
return;
}
down(u);
if (v >> (u->dep - 1) & 1) {
x = u;
y = new Trie(u->dep);
split(u->son[1], v, x->son[1], y->son[1]);
} else {
y = u;
x = new Trie(u->dep);
split(u->son[0], v, x->son[0], y->son[0]);
}
if (x->son[0] == NULL && x->son[1] == NULL) {
delete x;
x = NULL;
} else {
up(x);
}
if (y->son[0] == NULL && y->son[1] == NULL) {
delete y;
y = NULL;
} else {
up(y);
}
}
Trie *merge(Trie *u, Trie *v) {
if (u == NULL) {
return v;
}
if (v == NULL) {
return u;
}
if (u->dep == 0) {
u->a.sz = u->a.sz || v->a.sz;
delete v;
return u;
}
down(u), down(v);
u->son[0] = merge(u->son[0], v->son[0]);
u->son[1] = merge(u->son[1], v->son[1]);
up(u);
v->son[0] = v->son[1] = NULL;
delete v;
return u;
}
void split(Trie *u, int l, int r, Trie *&x, Trie *&y) {
if (l == 0) {
split(u, r, y, x);
} else {
split(u, l - 1, x, y);
Trie *z;
split(y, r, y, z);
x = merge(x, z);
}
}
void work_and(Trie *u, int v) {
if (u == NULL || u->dep == 0) {
return;
}
v &= (1 << u->dep) - 1;
if (((u->a.all0 | u->a.all1) & v) == v) {
work_xor(u, u->a.all1 & v);
return;
}
down(u);
if (v >> (u->dep - 1) & 1) {
u->son[0] = merge(u->son[0], u->son[1]);
u->son[1] = NULL;
}
work_and(u->son[0], v);
work_and(u->son[1], v);
up(u);
}
void work_or(Trie *u, int v) {
if (u == NULL || u->dep == 0) {
return;
}
v &= (1 << u->dep) - 1;
if (((u->a.all0 | u->a.all1) & v) == v) {
work_xor(u, u->a.all0 & v);
return;
}
down(u);
if (v >> (u->dep - 1) & 1) {
u->son[1] = merge(u->son[0], u->son[1]);
u->son[0] = NULL;
}
work_or(u->son[0], v);
work_or(u->son[1], v);
up(u);
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
int n, q;
std::cin >> n >> q;
Trie *rt = NULL;
for (int i = 0; i < n; ++i) {
int v;
std::cin >> v;
insert(rt, v);
}
while (q--) {
int op, l, r;
std::cin >> op >> l >> r;
Trie *tmp;
split(rt, l, r, tmp, rt);
if (op <= 3) {
int x;
std::cin >> x;
if (op == 1) {
work_and(rt, U ^ x);
} else if (op == 2) {
work_or(rt, x);
} else {
work_xor(rt, x);
}
} else {
std::cout << (rt == NULL ? 0 : rt->a.sz) << "\n";
}
rt = merge(rt, tmp);
}
}
``` |
#include<stdio.h>
const int maxn=200005,maxk=maxn*40;
int n,q,k,rt,cnt;
int lc[maxk],rc[maxk],lazy[maxk],Vand[maxk],Vor[maxk],res[maxk];
inline void swp(int &a,int &b){
a+=b,b=a-b,a-=b;
}
inline void pushup(int now){
Vand[now]=Vand[lc[now]]|Vand[rc[now]];
Vor[now]=Vor[lc[now]]|Vor[rc[now]];
res[now]=res[lc[now]]+res[rc[now]];
}
void insert(int dep,int &now,int val){
if(now==0)
now=++cnt;
if(dep==0){
Vand[now]=(k^val),Vor[now]=val,res[now]=1;
return ;
}
if(((val>>(dep-1))&1)==0)
insert(dep-1,lc[now],val);
else insert(dep-1,rc[now],val);
pushup(now);
}
void getxor(int dep,int now,int val){
if(now==0)
return ;
if((val>>(dep-1))&1)
swp(lc[now],rc[now]);
int Va=Vand[now],Vo=Vor[now];
Vand[now]=(Va&(k^val))|(Vo&val);
Vor[now]=(Vo&(k^val))|(Va&val);
lazy[now]^=val;
}
void pushdown(int dep,int now){
if(lazy[now]==0)
return ;
getxor(dep-1,lc[now],lazy[now]),getxor(dep-1,rc[now],lazy[now]);
lazy[now]=0;
}
void split(int dep,int &x,int &y,int l,int r,int L,int R){
if(x==0||R<l||r<L){
y=0;
return ;
}
if(L<=l&&r<=R){
y=x,x=0;
return ;
}
int mid=(l+r)>>1;
pushdown(dep,x),y=++cnt;
split(dep-1,lc[x],lc[y],l,mid,L,R),split(dep-1,rc[x],rc[y],mid+1,r,L,R);
pushup(x),pushup(y);
}
void merge(int dep,int &x,int &y){
if(x==0){
x=y;
return ;
}
if(y==0||dep==0)
return ;
pushdown(dep,x),pushdown(dep,y);
merge(dep-1,lc[x],lc[y]),merge(dep-1,rc[x],rc[y]);
pushup(x);
}
void getor(int dep,int now,int val){
if(now==0)
return ;
int add=val&Vand[now];
if((add&Vor[now])==0){
getxor(dep,now,add);
return ;
}
pushdown(dep,now);
if((val>>(dep-1))&1){
getxor(dep-1,lc[now],1<<(dep-1));
merge(dep-1,rc[now],lc[now]);
lc[now]=0;
}
getor(dep-1,lc[now],val),getor(dep-1,rc[now],val);
pushup(now);
}
int query(int dep,int now,int l,int r,int L,int R){
if(now==0||R<l||r<L)
return 0;
if(L<=l&&r<=R)
return res[now];
int mid=(l+r)>>1;
pushdown(dep,now);
return query(dep-1,lc[now],l,mid,L,R)+query(dep-1,rc[now],mid+1,r,L,R);
}
void read(int &x){
x=0;
char c=getchar();
for(;c<'0'||c>'9';c=getchar());
for(;c>='0'&&c<='9';c=getchar())
x=x*10+c-48;
}
int main(){
scanf("%d%d",&n,&q),k=(1<<20)-1;
for(int i=1;i<=n;i++){
int x;
read(x);
insert(20,rt,x);
}
for(int i=1;i<=q;i++){
int t,x,y,z,now;
read(t);
if(t==4){
read(x),read(y);
printf("%d\n",query(20,rt,0,k,x,y));
continue;
}
read(x),read(y),read(z);
split(20,rt,now,0,k,x,y);
if(t==1)
getxor(20,now,k),getor(20,now,z^k),getxor(20,now,k);
if(t==2)
getor(20,now,z);
if(t==3)
getxor(20,now,z);
merge(20,rt,now);
}
return 0;
} | ### Prompt
Develop a solution in CPP to the problem described below:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include<stdio.h>
const int maxn=200005,maxk=maxn*40;
int n,q,k,rt,cnt;
int lc[maxk],rc[maxk],lazy[maxk],Vand[maxk],Vor[maxk],res[maxk];
inline void swp(int &a,int &b){
a+=b,b=a-b,a-=b;
}
inline void pushup(int now){
Vand[now]=Vand[lc[now]]|Vand[rc[now]];
Vor[now]=Vor[lc[now]]|Vor[rc[now]];
res[now]=res[lc[now]]+res[rc[now]];
}
void insert(int dep,int &now,int val){
if(now==0)
now=++cnt;
if(dep==0){
Vand[now]=(k^val),Vor[now]=val,res[now]=1;
return ;
}
if(((val>>(dep-1))&1)==0)
insert(dep-1,lc[now],val);
else insert(dep-1,rc[now],val);
pushup(now);
}
void getxor(int dep,int now,int val){
if(now==0)
return ;
if((val>>(dep-1))&1)
swp(lc[now],rc[now]);
int Va=Vand[now],Vo=Vor[now];
Vand[now]=(Va&(k^val))|(Vo&val);
Vor[now]=(Vo&(k^val))|(Va&val);
lazy[now]^=val;
}
void pushdown(int dep,int now){
if(lazy[now]==0)
return ;
getxor(dep-1,lc[now],lazy[now]),getxor(dep-1,rc[now],lazy[now]);
lazy[now]=0;
}
void split(int dep,int &x,int &y,int l,int r,int L,int R){
if(x==0||R<l||r<L){
y=0;
return ;
}
if(L<=l&&r<=R){
y=x,x=0;
return ;
}
int mid=(l+r)>>1;
pushdown(dep,x),y=++cnt;
split(dep-1,lc[x],lc[y],l,mid,L,R),split(dep-1,rc[x],rc[y],mid+1,r,L,R);
pushup(x),pushup(y);
}
void merge(int dep,int &x,int &y){
if(x==0){
x=y;
return ;
}
if(y==0||dep==0)
return ;
pushdown(dep,x),pushdown(dep,y);
merge(dep-1,lc[x],lc[y]),merge(dep-1,rc[x],rc[y]);
pushup(x);
}
void getor(int dep,int now,int val){
if(now==0)
return ;
int add=val&Vand[now];
if((add&Vor[now])==0){
getxor(dep,now,add);
return ;
}
pushdown(dep,now);
if((val>>(dep-1))&1){
getxor(dep-1,lc[now],1<<(dep-1));
merge(dep-1,rc[now],lc[now]);
lc[now]=0;
}
getor(dep-1,lc[now],val),getor(dep-1,rc[now],val);
pushup(now);
}
int query(int dep,int now,int l,int r,int L,int R){
if(now==0||R<l||r<L)
return 0;
if(L<=l&&r<=R)
return res[now];
int mid=(l+r)>>1;
pushdown(dep,now);
return query(dep-1,lc[now],l,mid,L,R)+query(dep-1,rc[now],mid+1,r,L,R);
}
void read(int &x){
x=0;
char c=getchar();
for(;c<'0'||c>'9';c=getchar());
for(;c>='0'&&c<='9';c=getchar())
x=x*10+c-48;
}
int main(){
scanf("%d%d",&n,&q),k=(1<<20)-1;
for(int i=1;i<=n;i++){
int x;
read(x);
insert(20,rt,x);
}
for(int i=1;i<=q;i++){
int t,x,y,z,now;
read(t);
if(t==4){
read(x),read(y);
printf("%d\n",query(20,rt,0,k,x,y));
continue;
}
read(x),read(y),read(z);
split(20,rt,now,0,k,x,y);
if(t==1)
getxor(20,now,k),getor(20,now,z^k),getxor(20,now,k);
if(t==2)
getor(20,now,z);
if(t==3)
getxor(20,now,z);
merge(20,rt,now);
}
return 0;
}
``` |
#include<bits/stdc++.h>
#define lson ls[x],l,mid
#define rson rs[x],mid+1,r
using namespace std;const int N=2e5+7,M=N*50;
int rt,rt1,x,n,m,i,j,l,r,t,tk[M],ls[M],rs[M],ts[M],ts0[M],ts1[M],U,tag[M],sz;
void ud(int x){ts[x]=ts[ls[x]]+ts[rs[x]];ts0[x]=ts0[ls[x]]|ts0[rs[x]];ts1[x]=ts1[ls[x]]|ts1[rs[x]];}
int newnode(int k){return tk[sz+1]=k,++sz;}
void A(int x,int val){
if(!x)return;
tag[x]^=val;if(~tk[x]&&val>>tk[x]&1)swap(ls[x],rs[x]);
int X=(ts0[x]&(U^val))|(ts1[x]&val),Y=(ts1[x]&(U^val))|(ts0[x]&val);
ts0[x]=X;ts1[x]=Y;
}
void pd(int x){if(tag[x])A(ls[x],tag[x]),A(rs[x],tag[x]),tag[x]=0;}
void ins(int&x,int l,int r,int pos,int k){
if(!x)x=newnode(k);if(l==r){ts[x]=1;ts0[x]=U^pos;ts1[x]=U&pos;return;}int mid=l+r>>1;
if(pos<=mid)ins(lson,pos,k-1);else ins(rson,pos,k-1);ud(x);
}
int merge(int x,int y){
if(x==0||y==0)return x+y;pd(x);pd(y);
ls[x]=merge(ls[x],ls[y]);rs[x]=merge(rs[x],rs[y]);
if(~tk[x])ud(x);return x;
}
void split(int&x,int l,int r,int&y,int a,int b){
if(!x||a<=l&&r<=b){y=x;x=0;return;}int mid=l+r>>1;y=newnode(tk[x]);pd(x);
if(a<=mid)split(lson,ls[y],a,b);if(b>mid)split(rson,rs[y],a,b);ud(y);ud(x);
}
void modify(int x,int val){
if(!x)return;
if(!(val&ts0[x]&ts1[x]))return A(x,val&ts0[x]);pd(x);int k=tk[x];
if((val>>k&1))A(ls[x],1<<k),rs[x]=merge(rs[x],ls[x]),ls[x]=0;
modify(ls[x],val),modify(rs[x],val);if(k>=0)ud(x);
}
int main(){
for(scanf("%d%d",&n,&m),U=(1<<20)-1,i=1;i<=n;++i)scanf("%d",&j),ins(rt,0,U,j,19);
for(;m--;){
scanf("%d%d%d",&t,&l,&r);split(rt,0,U,rt1,l,r);
if(t==1)scanf("%d",&x),A(rt1,U),modify(rt1,x^U),A(rt1,U);
if(t==2)scanf("%d",&x),modify(rt1,x);
if(t==3)scanf("%d",&x),A(rt1,x);
if(t==4)printf("%d\n",ts[rt1]);
rt=merge(rt,rt1);
}
}
| ### Prompt
In Cpp, your task is to solve the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include<bits/stdc++.h>
#define lson ls[x],l,mid
#define rson rs[x],mid+1,r
using namespace std;const int N=2e5+7,M=N*50;
int rt,rt1,x,n,m,i,j,l,r,t,tk[M],ls[M],rs[M],ts[M],ts0[M],ts1[M],U,tag[M],sz;
void ud(int x){ts[x]=ts[ls[x]]+ts[rs[x]];ts0[x]=ts0[ls[x]]|ts0[rs[x]];ts1[x]=ts1[ls[x]]|ts1[rs[x]];}
int newnode(int k){return tk[sz+1]=k,++sz;}
void A(int x,int val){
if(!x)return;
tag[x]^=val;if(~tk[x]&&val>>tk[x]&1)swap(ls[x],rs[x]);
int X=(ts0[x]&(U^val))|(ts1[x]&val),Y=(ts1[x]&(U^val))|(ts0[x]&val);
ts0[x]=X;ts1[x]=Y;
}
void pd(int x){if(tag[x])A(ls[x],tag[x]),A(rs[x],tag[x]),tag[x]=0;}
void ins(int&x,int l,int r,int pos,int k){
if(!x)x=newnode(k);if(l==r){ts[x]=1;ts0[x]=U^pos;ts1[x]=U&pos;return;}int mid=l+r>>1;
if(pos<=mid)ins(lson,pos,k-1);else ins(rson,pos,k-1);ud(x);
}
int merge(int x,int y){
if(x==0||y==0)return x+y;pd(x);pd(y);
ls[x]=merge(ls[x],ls[y]);rs[x]=merge(rs[x],rs[y]);
if(~tk[x])ud(x);return x;
}
void split(int&x,int l,int r,int&y,int a,int b){
if(!x||a<=l&&r<=b){y=x;x=0;return;}int mid=l+r>>1;y=newnode(tk[x]);pd(x);
if(a<=mid)split(lson,ls[y],a,b);if(b>mid)split(rson,rs[y],a,b);ud(y);ud(x);
}
void modify(int x,int val){
if(!x)return;
if(!(val&ts0[x]&ts1[x]))return A(x,val&ts0[x]);pd(x);int k=tk[x];
if((val>>k&1))A(ls[x],1<<k),rs[x]=merge(rs[x],ls[x]),ls[x]=0;
modify(ls[x],val),modify(rs[x],val);if(k>=0)ud(x);
}
int main(){
for(scanf("%d%d",&n,&m),U=(1<<20)-1,i=1;i<=n;++i)scanf("%d",&j),ins(rt,0,U,j,19);
for(;m--;){
scanf("%d%d%d",&t,&l,&r);split(rt,0,U,rt1,l,r);
if(t==1)scanf("%d",&x),A(rt1,U),modify(rt1,x^U),A(rt1,U);
if(t==2)scanf("%d",&x),modify(rt1,x);
if(t==3)scanf("%d",&x),A(rt1,x);
if(t==4)printf("%d\n",ts[rt1]);
rt=merge(rt,rt1);
}
}
``` |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 200005;
const int maxd = 19;
const int S = (1 << (maxd + 1)) - 1;
const int maxp = 10000005;
int ch[maxp][2], sz[maxp], sta[maxp][2];
inline void pushup(int x) {
sz[x] = sz[ch[x][0]] + sz[ch[x][1]];
for(int i = 0; i <= 1; i++)
sta[x][i] = sta[ch[x][0]][i] | sta[ch[x][1]][i];
}
int lazy[maxp];
inline void puttag(int x, int y, int d) {
if((y >> d) & 1) swap(ch[x][0], ch[x][1]);
int s0 = (sta[x][0] & (S ^ y)) | (sta[x][1] & y);
int s1 = (sta[x][1] & (S ^ y)) | (sta[x][0] & y);
sta[x][0] = s0, sta[x][1] = s1;
lazy[x] ^= y;
}
inline void putdown(int x, int d) {
if(!lazy[x]) return;
puttag(ch[x][0], lazy[x], d - 1);
puttag(ch[x][1], lazy[x], d - 1);
lazy[x] = 0;
}
int rt = 1, totp = 1, st[maxp], top;
inline int newnode() {
int x = (top) ? st[top--] : ++totp;
ch[x][0] = ch[x][1] = sz[x] = lazy[x] = sta[x][0] = sta[x][1] = 0;
return x;
}
void insert(int &x, int y, int d) {
if(!x) x = newnode();
if(d == -1) {
sz[x] = 1;
sta[x][0] = S ^ y;
sta[x][1] = y;
return;
}
int nxt = (y >> d) & 1;
insert(ch[x][nxt], y, d - 1);
pushup(x);
}
void split(int &x, int &y, int l, int r, int L, int R, int d) {
if(L <= l && r <= R) {
y = x, x = 0;
return;
}
putdown(x, d);
int mid = (l + r) >> 1;
y = newnode();
if(L <= mid)
split(ch[x][0], ch[y][0], l, mid, L, R, d - 1);
if(R > mid)
split(ch[x][1], ch[y][1], mid + 1, r, L, R, d - 1);
pushup(x), pushup(y);
}
int merge(int x, int y, int d) {
if(!x || !y) return x + y;
if(d == -1) {
st[++top] = y;
return x;
}
putdown(x, d), putdown(y, d);
sz[x] += sz[y];
ch[x][0] = merge(ch[x][0], ch[y][0], d - 1);
ch[x][1] = merge(ch[x][1], ch[y][1], d - 1);
pushup(x);
st[++top] = y;
return x;
}
int query(int x, int l, int r, int L, int R, int d) {
if(!x) return 0;
if(L <= l && r <= R) return sz[x];
putdown(x, d);
int mid = (l + r) >> 1, res = 0;
if(L <= mid)
res += query(ch[x][0], l, mid, L, R, d - 1);
if(R > mid)
res += query(ch[x][1], mid + 1, r, L, R, d - 1);
return res;
}
void modify(int x, int y, int d) {
if(!x) return;
if((y & sta[x][0] & sta[x][1]) == 0) {
puttag(x, (y & sta[x][0]), d);
return;
}
putdown(x, d);
if((y >> d) & 1) {
puttag(ch[x][0], 1 << d, d - 1);
ch[x][1] = merge(ch[x][0], ch[x][1], d - 1);
ch[x][0] = 0;
}
modify(ch[x][0], y, d - 1), modify(ch[x][1], y, d - 1);
pushup(x);
}
int n, q;
int main() {
scanf("%d%d", &n, &q);
for(int i = 1, x; i <= n; i++)
scanf("%d", &x), insert(rt, x, maxd);
for(int i = 1, opt, l, r, x; i <= q; i++) {
scanf("%d%d%d", &opt, &l, &r);
if(opt == 4)
printf("%d\n", query(rt, 0, S, l, r, maxd));
else {
scanf("%d", &x);
int nrt = 0;
split(rt, nrt, 0, S, l, r, maxd);
if(opt == 1) {
puttag(nrt, S, maxd);
modify(nrt, x ^ S, maxd);
puttag(nrt, S, maxd);
}
if(opt == 2)
modify(nrt, x, maxd);
if(opt == 3)
puttag(nrt, x, maxd);
rt = merge(rt, nrt, maxd);
}
}
return 0;
} | ### Prompt
Please create a solution in CPP to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 200005;
const int maxd = 19;
const int S = (1 << (maxd + 1)) - 1;
const int maxp = 10000005;
int ch[maxp][2], sz[maxp], sta[maxp][2];
inline void pushup(int x) {
sz[x] = sz[ch[x][0]] + sz[ch[x][1]];
for(int i = 0; i <= 1; i++)
sta[x][i] = sta[ch[x][0]][i] | sta[ch[x][1]][i];
}
int lazy[maxp];
inline void puttag(int x, int y, int d) {
if((y >> d) & 1) swap(ch[x][0], ch[x][1]);
int s0 = (sta[x][0] & (S ^ y)) | (sta[x][1] & y);
int s1 = (sta[x][1] & (S ^ y)) | (sta[x][0] & y);
sta[x][0] = s0, sta[x][1] = s1;
lazy[x] ^= y;
}
inline void putdown(int x, int d) {
if(!lazy[x]) return;
puttag(ch[x][0], lazy[x], d - 1);
puttag(ch[x][1], lazy[x], d - 1);
lazy[x] = 0;
}
int rt = 1, totp = 1, st[maxp], top;
inline int newnode() {
int x = (top) ? st[top--] : ++totp;
ch[x][0] = ch[x][1] = sz[x] = lazy[x] = sta[x][0] = sta[x][1] = 0;
return x;
}
void insert(int &x, int y, int d) {
if(!x) x = newnode();
if(d == -1) {
sz[x] = 1;
sta[x][0] = S ^ y;
sta[x][1] = y;
return;
}
int nxt = (y >> d) & 1;
insert(ch[x][nxt], y, d - 1);
pushup(x);
}
void split(int &x, int &y, int l, int r, int L, int R, int d) {
if(L <= l && r <= R) {
y = x, x = 0;
return;
}
putdown(x, d);
int mid = (l + r) >> 1;
y = newnode();
if(L <= mid)
split(ch[x][0], ch[y][0], l, mid, L, R, d - 1);
if(R > mid)
split(ch[x][1], ch[y][1], mid + 1, r, L, R, d - 1);
pushup(x), pushup(y);
}
int merge(int x, int y, int d) {
if(!x || !y) return x + y;
if(d == -1) {
st[++top] = y;
return x;
}
putdown(x, d), putdown(y, d);
sz[x] += sz[y];
ch[x][0] = merge(ch[x][0], ch[y][0], d - 1);
ch[x][1] = merge(ch[x][1], ch[y][1], d - 1);
pushup(x);
st[++top] = y;
return x;
}
int query(int x, int l, int r, int L, int R, int d) {
if(!x) return 0;
if(L <= l && r <= R) return sz[x];
putdown(x, d);
int mid = (l + r) >> 1, res = 0;
if(L <= mid)
res += query(ch[x][0], l, mid, L, R, d - 1);
if(R > mid)
res += query(ch[x][1], mid + 1, r, L, R, d - 1);
return res;
}
void modify(int x, int y, int d) {
if(!x) return;
if((y & sta[x][0] & sta[x][1]) == 0) {
puttag(x, (y & sta[x][0]), d);
return;
}
putdown(x, d);
if((y >> d) & 1) {
puttag(ch[x][0], 1 << d, d - 1);
ch[x][1] = merge(ch[x][0], ch[x][1], d - 1);
ch[x][0] = 0;
}
modify(ch[x][0], y, d - 1), modify(ch[x][1], y, d - 1);
pushup(x);
}
int n, q;
int main() {
scanf("%d%d", &n, &q);
for(int i = 1, x; i <= n; i++)
scanf("%d", &x), insert(rt, x, maxd);
for(int i = 1, opt, l, r, x; i <= q; i++) {
scanf("%d%d%d", &opt, &l, &r);
if(opt == 4)
printf("%d\n", query(rt, 0, S, l, r, maxd));
else {
scanf("%d", &x);
int nrt = 0;
split(rt, nrt, 0, S, l, r, maxd);
if(opt == 1) {
puttag(nrt, S, maxd);
modify(nrt, x ^ S, maxd);
puttag(nrt, S, maxd);
}
if(opt == 2)
modify(nrt, x, maxd);
if(opt == 3)
puttag(nrt, x, maxd);
rt = merge(rt, nrt, maxd);
}
}
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
const int maxb=20;
const int maxs=1<<maxb;
int n,q,tot,top,rt;
int st[maxs<<1],ls[maxs<<1],rs[maxs<<1],tag0[maxs<<1],tag1[maxs<<1],tagx[maxs<<1],sdep[maxs<<1],cnt[maxs<<1];
vector<pair<pair<int,int>,int> >todo;
void apply0(int cur,int mask){
tag1[cur]&=~mask;
tagx[cur]&=~mask;
tag0[cur]|=mask;
}
void apply1(int cur,int mask){
tag0[cur]&=~mask;
tagx[cur]&=~mask;
tag1[cur]|=mask;
}
void applyx(int cur,int mask){
int tmp=(tag0[cur]^tag1[cur])&mask;
tag0[cur]^=tmp;
tag1[cur]^=tmp;
tagx[cur]^=mask&~tag0[cur]&~tag1[cur];
}
int newnode(int _cnt){
int cur=top?st[--top]:tot++;
ls[cur]=rs[cur]=-1;
tag0[cur]=tag1[cur]=tagx[cur]=sdep[cur]=0;
cnt[cur]=_cnt;
return cur;
}
void merge(int &cur,int cur1,int cur2,int dep,int l,int r);
void pushdown(int cur,int dep,int l,int r){
int mid=l+r>>1;
if((tag0[cur]>>dep)&1){
merge(ls[cur],ls[cur],rs[cur],dep-1,l,mid);
rs[cur]=-1;
}
else if((tag1[cur]>>dep)&1){
merge(rs[cur],ls[cur],rs[cur],dep-1,mid+1,r);
ls[cur]=-1;
}
else if((tagx[cur]>>dep)&1)
swap(ls[cur],rs[cur]);
if(ls[cur]>=0)
apply0(ls[cur],tag0[cur]),apply1(ls[cur],tag1[cur]),applyx(ls[cur],tagx[cur]);
if(rs[cur]>=0)
apply0(rs[cur],tag0[cur]),apply1(rs[cur],tag1[cur]),applyx(rs[cur],tagx[cur]);
tag0[cur]=tag1[cur]=tagx[cur]=0;
}
void pushup(int cur,int dep){
cnt[cur]=(ls[cur]>=0?cnt[ls[cur]]:0)+(rs[cur]>=0?cnt[rs[cur]]:0);
sdep[cur]=(ls[cur]>=0?sdep[ls[cur]]:0)|(rs[cur]>=0?sdep[rs[cur]]:0);
if(ls[cur]>=0&&rs[cur]>=0)sdep[cur]|=1<<dep;
}
void check(int cur,int dep,int l,int r){
if(dep<0)return;
if(sdep[cur]&(tag0[cur]|tag1[cur])){
pushdown(cur,dep,l,r);
int mid=l+r>>1;
check(ls[cur],dep-1,l,mid);
check(rs[cur],dep-1,mid+1,r);
pushup(cur,dep);
}
}
void merge(int &cur,int cur1,int cur2,int dep,int l,int r){
if(cur1<0)cur=cur2;
else if(cur2<0)cur=cur1;
else{
cur=cur1;
if(dep>=0){
pushdown(cur1,dep,l,r);
pushdown(cur2,dep,l,r);
int mid=l+r>>1;
merge(ls[cur],ls[cur1],ls[cur2],dep-1,l,mid);
merge(rs[cur],rs[cur1],rs[cur2],dep-1,mid+1,r);
pushup(cur,dep);
}
st[top++]=cur2;
}
}
void insert(int &cur,int dep,int edep,int l,int r,int k,int val){
if(dep==edep){
merge(cur,cur,val,dep,l,r);
return;
}
if(cur<0)cur=newnode(0);
pushdown(cur,dep,l,r);
int mid=l+r>>1;
if(k<=mid)insert(ls[cur],dep-1,edep,l,mid,k,val);
else insert(rs[cur],dep-1,edep,mid+1,r,k,val);
pushup(cur,dep);
}
void update(int &cur,int dep,int l,int r,int vl,int vr,int op,int mask){
if(cur<0)return;
if(l>=vl&&r<=vr){
if(op==0){
apply0(cur,mask);
todo.push_back(make_pair(make_pair(l&((maxs-1)^mask),cur),dep));
}
else if(op==1){
apply1(cur,mask);
todo.push_back(make_pair(make_pair(l|mask,cur),dep));
}
else{
applyx(cur,mask);
todo.push_back(make_pair(make_pair(l^mask,cur),dep));
}
check(cur,dep,l,r);
cur=-1;
return;
}
pushdown(cur,dep,l,r);
int mid=l+r>>1;
if(mid>=vl)update(ls[cur],dep-1,l,mid,vl,vr,op,mask);
if(mid<vr)update(rs[cur],dep-1,mid+1,r,vl,vr,op,mask);
pushup(cur,dep);
}
int query(int cur,int dep,int l,int r,int vl,int vr){
if(cur<0)return 0;
if(l>=vl&&r<=vr)return cnt[cur];
pushdown(cur,dep,l,r);
int mid=l+r>>1;
int sl=mid>=vl?query(ls[cur],dep-1,l,mid,vl,vr):0;
int sr=mid<vr?query(rs[cur],dep-1,mid+1,r,vl,vr):0;
return sl+sr;
}
int main(){
rt=-1;
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++){
int x;
scanf("%d",&x);
insert(rt,maxb-1,-1,0,maxs-1,x,newnode(1));
}
while(q--){
int op,l,r;
scanf("%d%d%d",&op,&l,&r);
op--;
if(op<=2){
int mask;
scanf("%d",&mask);
todo.clear();
update(rt,maxb-1,0,maxs-1,l,r,op,op==0?(maxs-1)^mask:mask);
for(int i=0;i<int(todo.size());i++)
insert(rt,maxb-1,todo[i].second,0,maxs-1,todo[i].first.first,todo[i].first.second);
}
else
printf("%d\n",query(rt,maxb-1,0,maxs-1,l,r));
}
return 0;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
const int maxb=20;
const int maxs=1<<maxb;
int n,q,tot,top,rt;
int st[maxs<<1],ls[maxs<<1],rs[maxs<<1],tag0[maxs<<1],tag1[maxs<<1],tagx[maxs<<1],sdep[maxs<<1],cnt[maxs<<1];
vector<pair<pair<int,int>,int> >todo;
void apply0(int cur,int mask){
tag1[cur]&=~mask;
tagx[cur]&=~mask;
tag0[cur]|=mask;
}
void apply1(int cur,int mask){
tag0[cur]&=~mask;
tagx[cur]&=~mask;
tag1[cur]|=mask;
}
void applyx(int cur,int mask){
int tmp=(tag0[cur]^tag1[cur])&mask;
tag0[cur]^=tmp;
tag1[cur]^=tmp;
tagx[cur]^=mask&~tag0[cur]&~tag1[cur];
}
int newnode(int _cnt){
int cur=top?st[--top]:tot++;
ls[cur]=rs[cur]=-1;
tag0[cur]=tag1[cur]=tagx[cur]=sdep[cur]=0;
cnt[cur]=_cnt;
return cur;
}
void merge(int &cur,int cur1,int cur2,int dep,int l,int r);
void pushdown(int cur,int dep,int l,int r){
int mid=l+r>>1;
if((tag0[cur]>>dep)&1){
merge(ls[cur],ls[cur],rs[cur],dep-1,l,mid);
rs[cur]=-1;
}
else if((tag1[cur]>>dep)&1){
merge(rs[cur],ls[cur],rs[cur],dep-1,mid+1,r);
ls[cur]=-1;
}
else if((tagx[cur]>>dep)&1)
swap(ls[cur],rs[cur]);
if(ls[cur]>=0)
apply0(ls[cur],tag0[cur]),apply1(ls[cur],tag1[cur]),applyx(ls[cur],tagx[cur]);
if(rs[cur]>=0)
apply0(rs[cur],tag0[cur]),apply1(rs[cur],tag1[cur]),applyx(rs[cur],tagx[cur]);
tag0[cur]=tag1[cur]=tagx[cur]=0;
}
void pushup(int cur,int dep){
cnt[cur]=(ls[cur]>=0?cnt[ls[cur]]:0)+(rs[cur]>=0?cnt[rs[cur]]:0);
sdep[cur]=(ls[cur]>=0?sdep[ls[cur]]:0)|(rs[cur]>=0?sdep[rs[cur]]:0);
if(ls[cur]>=0&&rs[cur]>=0)sdep[cur]|=1<<dep;
}
void check(int cur,int dep,int l,int r){
if(dep<0)return;
if(sdep[cur]&(tag0[cur]|tag1[cur])){
pushdown(cur,dep,l,r);
int mid=l+r>>1;
check(ls[cur],dep-1,l,mid);
check(rs[cur],dep-1,mid+1,r);
pushup(cur,dep);
}
}
void merge(int &cur,int cur1,int cur2,int dep,int l,int r){
if(cur1<0)cur=cur2;
else if(cur2<0)cur=cur1;
else{
cur=cur1;
if(dep>=0){
pushdown(cur1,dep,l,r);
pushdown(cur2,dep,l,r);
int mid=l+r>>1;
merge(ls[cur],ls[cur1],ls[cur2],dep-1,l,mid);
merge(rs[cur],rs[cur1],rs[cur2],dep-1,mid+1,r);
pushup(cur,dep);
}
st[top++]=cur2;
}
}
void insert(int &cur,int dep,int edep,int l,int r,int k,int val){
if(dep==edep){
merge(cur,cur,val,dep,l,r);
return;
}
if(cur<0)cur=newnode(0);
pushdown(cur,dep,l,r);
int mid=l+r>>1;
if(k<=mid)insert(ls[cur],dep-1,edep,l,mid,k,val);
else insert(rs[cur],dep-1,edep,mid+1,r,k,val);
pushup(cur,dep);
}
void update(int &cur,int dep,int l,int r,int vl,int vr,int op,int mask){
if(cur<0)return;
if(l>=vl&&r<=vr){
if(op==0){
apply0(cur,mask);
todo.push_back(make_pair(make_pair(l&((maxs-1)^mask),cur),dep));
}
else if(op==1){
apply1(cur,mask);
todo.push_back(make_pair(make_pair(l|mask,cur),dep));
}
else{
applyx(cur,mask);
todo.push_back(make_pair(make_pair(l^mask,cur),dep));
}
check(cur,dep,l,r);
cur=-1;
return;
}
pushdown(cur,dep,l,r);
int mid=l+r>>1;
if(mid>=vl)update(ls[cur],dep-1,l,mid,vl,vr,op,mask);
if(mid<vr)update(rs[cur],dep-1,mid+1,r,vl,vr,op,mask);
pushup(cur,dep);
}
int query(int cur,int dep,int l,int r,int vl,int vr){
if(cur<0)return 0;
if(l>=vl&&r<=vr)return cnt[cur];
pushdown(cur,dep,l,r);
int mid=l+r>>1;
int sl=mid>=vl?query(ls[cur],dep-1,l,mid,vl,vr):0;
int sr=mid<vr?query(rs[cur],dep-1,mid+1,r,vl,vr):0;
return sl+sr;
}
int main(){
rt=-1;
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++){
int x;
scanf("%d",&x);
insert(rt,maxb-1,-1,0,maxs-1,x,newnode(1));
}
while(q--){
int op,l,r;
scanf("%d%d%d",&op,&l,&r);
op--;
if(op<=2){
int mask;
scanf("%d",&mask);
todo.clear();
update(rt,maxb-1,0,maxs-1,l,r,op,op==0?(maxs-1)^mask:mask);
for(int i=0;i<int(todo.size());i++)
insert(rt,maxb-1,todo[i].second,0,maxs-1,todo[i].first.first,todo[i].first.second);
}
else
printf("%d\n",query(rt,maxb-1,0,maxs-1,l,r));
}
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
# define ll long long
# define read read1<ll>()
# define Type template<typename T>
Type T read1(){
T t=0;
char k;
bool vis=0;
do (k=getchar())=='-'&&(vis=1);while('0'>k||k>'9');
while('0'<=k&&k<='9')t=(t<<3)+(t<<1)+(k^'0'),k=getchar();
return vis?-t:t;
}
# define fre(k) freopen(k".in","r",stdin);freopen(k".out","w",stdout)
int s,q;
const int Depth=19;
struct node;
int size(node*);
int True(node*);
int False(node*);
struct node{
node *l,*r;
int _xor,s,k,or_0,or_1;//ORsum of the space of 0/1
node(int d){l=r=NULL;_xor=0;s=0;k=d;or_0=0;or_1=0;}
node(node *l_,node *r_,int d){
l=l_;r=r_;s=0;or_0=0;or_1=0;k=d;
if(l_){l_->down();
s+=l_->s;
or_0|=1<<k|l->or_0;
or_1|=l->or_1;
}if(r_){r_->down();
s+=r_->s;
or_0|=r->or_0;
or_1|=1<<k|r->or_1;
}if(!~d)s=1;
_xor=0;
}
void down(){
if(!_xor)return;
if(_xor>>k&1){
swap(l,r);
if((or_0^or_1)&(1<<k))or_0^=1<<k,or_1^=1<<k;
_xor^=1<<k;
}
if(l)l->_xor^=_xor;
if(r)r->_xor^=_xor;
int u=(or_0^or_1)&_xor;
or_0^=u;or_1^=u;
_xor=0;
}
void up(){
s=0;or_0=0;or_1=0;
if(l){l->down();
s+=l->s;
or_0|=1<<k|l->or_0;
or_1|=l->or_1;
}if(r){r->down();
s+=r->s;
or_0|=r->or_0;
or_1|=1<<k|r->or_1;
}if(!~k)s=1;
_xor=0;
}
}*root;
int size(node *x){return x?x->s:0;}
int True(node *x){return x?x->down(),x->or_1:0;}
int False(node *x){return x?x->down(),x->or_0:0;}
void insert(node *&x,int v,int d=Depth){
if(!x)x=new node(d),++x->s;
if(!~d)return;
if(v>>d&1)insert(x->r,v,d-1);
else insert(x->l,v,d-1);
x->up();
}
node* merge(node *x,node *y){
if(!x||!y)return x?x:y;
x->down();y->down();
x->l=merge(x->l,y->l);
x->r=merge(x->r,y->r);
x->up();delete y;
return x;
}
typedef pair<node*,node*> Homology;
Homology split(node *x,int n){
if(n<0)return Homology(NULL,x);
if(!x)return Homology(NULL,NULL);
if(!~x->k)return Homology(x,NULL);
Homology y;x->down();
node *z=NULL;
if(n>>x->k&1){
y=split(x->r,n);
y=Homology(new node(x->l,y.first,x->k),new node(NULL,y.second,x->k));
}else{
y=split(x->l,n);
y=Homology(new node(y.first,NULL,x->k),new node(y.second,x->r,x->k));
}
delete x;
return y;
}
void pr(node *&x,int v=0){
if(!x)return;x->down();
if(!~x->k)printf("-->%d %d\n",v,x->s);
else pr(x->l,v),pr(x->r,v|1<<x->k);
}
void trie_or(node *&x,int n){
if(!x)return;
if(!~x->k)return;
x->down();
// cout<<x->k<<"::"<<x->or_0<<endl;
if(!(n&x->or_0&x->or_1)){
x->_xor^=n&x->or_0;
return;
}
trie_or(x->l,n&~(1<<x->k));
trie_or(x->r,n&~(1<<x->k));
if(n>>x->k&1){
x->r=merge(x->l,x->r);
x->l=NULL;
}
x->up();
}
void Mtrie_or(node *&x,int n){
if(!x)return;
if(!~x->k)return;
x->down();
if(n>>x->k&1){
x->r=merge(x->l,x->r);x->l=NULL;
Mtrie_or(x->r,n^(1<<x->k));x->up();return;
}else Mtrie_or(x->l,n),Mtrie_or(x->r,n),x->up();
}
void trie_xor(node *&x,int n){
if(!x)return;
x->down();
// printf("-->%d\n",x->or_0);
x->_xor^=n;
}
void Mtrie_xor(node *&x,int n){
if(!x)return;
if(!~x->k)return;
x->down();
if(n>>x->k&1){
swap(x->l,x->r);
Mtrie_xor(x->l,n);
Mtrie_xor(x->r,n);
x->up();
}else Mtrie_xor(x->l,n),Mtrie_xor(x->r,n),x->up();
}
int main(){
s=read;q=read;
for(int i=1;i<=s;++i)insert(root,read);
// printf("(%d)\n",root->or_0);
for(int i=1;i<=q;++i){//puts("----");
int type=read,l=read,r=read,v;
Homology x=split(root,r),y=split(x.first,l-1);
if(type==4)printf("%d\n",size(y.second));
else{v=read;
switch(type){
case 1:trie_xor(y.second,(1<<20)-1);trie_or(y.second,(1<<20)-1^v);trie_xor(y.second,(1<<20)-1);break;
case 2:trie_or(y.second,v);break;
default:trie_xor(y.second,v);
}
}root=merge(merge(y.first,y.second),x.second);
// pr(root);
}
return 0;
} | ### Prompt
Construct a CPP code solution to the problem outlined:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
# define ll long long
# define read read1<ll>()
# define Type template<typename T>
Type T read1(){
T t=0;
char k;
bool vis=0;
do (k=getchar())=='-'&&(vis=1);while('0'>k||k>'9');
while('0'<=k&&k<='9')t=(t<<3)+(t<<1)+(k^'0'),k=getchar();
return vis?-t:t;
}
# define fre(k) freopen(k".in","r",stdin);freopen(k".out","w",stdout)
int s,q;
const int Depth=19;
struct node;
int size(node*);
int True(node*);
int False(node*);
struct node{
node *l,*r;
int _xor,s,k,or_0,or_1;//ORsum of the space of 0/1
node(int d){l=r=NULL;_xor=0;s=0;k=d;or_0=0;or_1=0;}
node(node *l_,node *r_,int d){
l=l_;r=r_;s=0;or_0=0;or_1=0;k=d;
if(l_){l_->down();
s+=l_->s;
or_0|=1<<k|l->or_0;
or_1|=l->or_1;
}if(r_){r_->down();
s+=r_->s;
or_0|=r->or_0;
or_1|=1<<k|r->or_1;
}if(!~d)s=1;
_xor=0;
}
void down(){
if(!_xor)return;
if(_xor>>k&1){
swap(l,r);
if((or_0^or_1)&(1<<k))or_0^=1<<k,or_1^=1<<k;
_xor^=1<<k;
}
if(l)l->_xor^=_xor;
if(r)r->_xor^=_xor;
int u=(or_0^or_1)&_xor;
or_0^=u;or_1^=u;
_xor=0;
}
void up(){
s=0;or_0=0;or_1=0;
if(l){l->down();
s+=l->s;
or_0|=1<<k|l->or_0;
or_1|=l->or_1;
}if(r){r->down();
s+=r->s;
or_0|=r->or_0;
or_1|=1<<k|r->or_1;
}if(!~k)s=1;
_xor=0;
}
}*root;
int size(node *x){return x?x->s:0;}
int True(node *x){return x?x->down(),x->or_1:0;}
int False(node *x){return x?x->down(),x->or_0:0;}
void insert(node *&x,int v,int d=Depth){
if(!x)x=new node(d),++x->s;
if(!~d)return;
if(v>>d&1)insert(x->r,v,d-1);
else insert(x->l,v,d-1);
x->up();
}
node* merge(node *x,node *y){
if(!x||!y)return x?x:y;
x->down();y->down();
x->l=merge(x->l,y->l);
x->r=merge(x->r,y->r);
x->up();delete y;
return x;
}
typedef pair<node*,node*> Homology;
Homology split(node *x,int n){
if(n<0)return Homology(NULL,x);
if(!x)return Homology(NULL,NULL);
if(!~x->k)return Homology(x,NULL);
Homology y;x->down();
node *z=NULL;
if(n>>x->k&1){
y=split(x->r,n);
y=Homology(new node(x->l,y.first,x->k),new node(NULL,y.second,x->k));
}else{
y=split(x->l,n);
y=Homology(new node(y.first,NULL,x->k),new node(y.second,x->r,x->k));
}
delete x;
return y;
}
void pr(node *&x,int v=0){
if(!x)return;x->down();
if(!~x->k)printf("-->%d %d\n",v,x->s);
else pr(x->l,v),pr(x->r,v|1<<x->k);
}
void trie_or(node *&x,int n){
if(!x)return;
if(!~x->k)return;
x->down();
// cout<<x->k<<"::"<<x->or_0<<endl;
if(!(n&x->or_0&x->or_1)){
x->_xor^=n&x->or_0;
return;
}
trie_or(x->l,n&~(1<<x->k));
trie_or(x->r,n&~(1<<x->k));
if(n>>x->k&1){
x->r=merge(x->l,x->r);
x->l=NULL;
}
x->up();
}
void Mtrie_or(node *&x,int n){
if(!x)return;
if(!~x->k)return;
x->down();
if(n>>x->k&1){
x->r=merge(x->l,x->r);x->l=NULL;
Mtrie_or(x->r,n^(1<<x->k));x->up();return;
}else Mtrie_or(x->l,n),Mtrie_or(x->r,n),x->up();
}
void trie_xor(node *&x,int n){
if(!x)return;
x->down();
// printf("-->%d\n",x->or_0);
x->_xor^=n;
}
void Mtrie_xor(node *&x,int n){
if(!x)return;
if(!~x->k)return;
x->down();
if(n>>x->k&1){
swap(x->l,x->r);
Mtrie_xor(x->l,n);
Mtrie_xor(x->r,n);
x->up();
}else Mtrie_xor(x->l,n),Mtrie_xor(x->r,n),x->up();
}
int main(){
s=read;q=read;
for(int i=1;i<=s;++i)insert(root,read);
// printf("(%d)\n",root->or_0);
for(int i=1;i<=q;++i){//puts("----");
int type=read,l=read,r=read,v;
Homology x=split(root,r),y=split(x.first,l-1);
if(type==4)printf("%d\n",size(y.second));
else{v=read;
switch(type){
case 1:trie_xor(y.second,(1<<20)-1);trie_or(y.second,(1<<20)-1^v);trie_xor(y.second,(1<<20)-1);break;
case 2:trie_or(y.second,v);break;
default:trie_xor(y.second,v);
}
}root=merge(merge(y.first,y.second),x.second);
// pr(root);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int MOD;
struct modint {
private:
int v;
static int minv(int a, int m) {
a %= m;
assert(a);
return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a);
}
public:
modint() : v(0) {}
modint(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }
explicit operator int() const { return v; }
friend std::ostream& operator << (std::ostream& out, const modint& n) { return out << int(n); }
friend std::istream& operator >> (std::istream& in, modint& n) { ll v_; in >> v_; n = modint(v_); return in; }
friend bool operator == (const modint& a, const modint& b) { return a.v == b.v; }
friend bool operator != (const modint& a, const modint& b) { return a.v != b.v; }
modint inv() const {
modint res;
res.v = minv(v, MOD);
return res;
}
friend modint inv(const modint& m) { return m.inv(); }
modint neg() const {
modint res;
res.v = v ? MOD-v : 0;
return res;
}
friend modint neg(const modint& m) { return m.neg(); }
modint operator- () const {
return neg();
}
modint operator+ () const {
return modint(*this);
}
modint& operator ++ () {
v ++;
if (v == MOD) v = 0;
return *this;
}
modint& operator -- () {
if (v == 0) v = MOD;
v --;
return *this;
}
modint& operator += (const modint& o) {
v += o.v;
if (v >= MOD) v -= MOD;
return *this;
}
modint& operator -= (const modint& o) {
v -= o.v;
if (v < 0) v += MOD;
return *this;
}
modint& operator *= (const modint& o) {
v = int(ll(v) * ll(o.v) % MOD);
return *this;
}
modint& operator /= (const modint& o) {
return *this *= o.inv();
}
friend modint operator ++ (modint& a, int) { modint r = a; ++a; return r; }
friend modint operator -- (modint& a, int) { modint r = a; --a; return r; }
friend modint operator + (const modint& a, const modint& b) { return modint(a) += b; }
friend modint operator - (const modint& a, const modint& b) { return modint(a) -= b; }
friend modint operator * (const modint& a, const modint& b) { return modint(a) *= b; }
friend modint operator / (const modint& a, const modint& b) { return modint(a) /= b; }
};
namespace IO {
template<class T>
void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(ll &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { x = getchar(); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U>
void R(T &head, U &... tail) { _R(head), R(tail...); }
template<class T>
void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const ll &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T, class U>
void _W(const pair<T, U> &x) { _W(x.first), putchar(' '), _W(x.second); }
template<class T>
void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U>
void W(const T &head, const U &... tail) { _W(head), putchar(sizeof...(tail) ? ' ' : '\n'), W(tail...); }
}
using namespace IO;
template <typename T> T pow(T a, long long b) {
assert(b >= 0);
T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r;
}
const int maxn = 2e5+50;
const int maxp = maxn*32+5;
const int K = 20;
const int M = 1 << K;
int ls[maxp], rs[maxp], tot;
// 0, 1, cnt, XOR
int t0[maxp], t1[maxp], p[maxp], txor[maxp], tor[maxp];
inline void pushup(int x) {
p[x] = p[ls[x]] + p[rs[x]];
t0[x] = t0[ls[x]] | t0[rs[x]];
t1[x] = t1[ls[x]] | t1[rs[x]];
}
inline void XOR(int x, int t) {
if (!x) return;
txor[x] ^= t;
if (~tor[x] && t >> tor[x] & 1) swap(ls[x], rs[x]);
int a = (t0[x] & (~t)) | (t1[x] & t), b = (t1[x] & (~t)) | (t0[x] & t);
t0[x] = a, t1[x] = b;
}
inline void pushdown(int x) {
if (txor[x]) {
XOR(ls[x], txor[x]), XOR(rs[x], txor[x]);
txor[x] = 0;
}
}
inline void insert(int &x, int s, int k) {
if (!x) x = ++tot;
tor[x] = k;
if (k == -1) {
t1[x] = s & (M - 1), t0[x] = t1[x] ^ (M - 1);
p[x] = 1;
return;
}
insert((s >> k & 1) ? rs[x] : ls[x], s, k - 1);
pushup(x);
}
inline void split(int &x, int &y, int l, int r, int le, int re) {
if (!x || re < l || le > r) {
y = 0;
return;
}
if (le <= l && r <= re) {
y = x;
x = 0;
return;
}
int mid = l + r >> 1; pushdown(x);
tor[y = ++tot] = tor[x];
split(ls[x], ls[y], l, mid, le, re);
split(rs[x], rs[y], mid + 1, r, le, re);
pushup(x), pushup(y);
}
inline void merge(int &x, int y) {
if (!x || !y) {
x = x | y;
return;
}
pushdown(x), pushdown(y);
merge(ls[x], ls[y]), merge(rs[x], rs[y]);
if (~tor[x]) pushup(x);
}
inline void OR(int x, int s) {
if (!x) return;
if (!(s & t0[x] & t1[x])) {
XOR(x, s & t0[x]);
return;
}
pushdown(x);
if (s >> tor[x] & 1) XOR(ls[x], 1 << tor[x]), merge(rs[x], ls[x]), ls[x] = 0;
OR(ls[x], s), OR(rs[x], s);
pushup(x);
}
int main() {
int n, q, x = 0;
R(n, q);
for (int i = 1; i <= n; ++i) {
int v; R(v); insert(x, v, K - 1);
}
for (; q; --q) {
int oth;
int t, l, r, v; R(t, l, r);
split(x, oth, 0, M - 1, l, r);
if (t == 1) R(v),XOR(oth, M - 1),OR(oth, v ^ (M - 1)),XOR(oth, M - 1);
else if (t == 2) R(v),OR(oth, v);
else if (t == 3) R(v), XOR(oth, v);
else W(p[oth]);
merge(x, oth);
}
return 0;
} | ### Prompt
Construct a CPP code solution to the problem outlined:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int MOD;
struct modint {
private:
int v;
static int minv(int a, int m) {
a %= m;
assert(a);
return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a);
}
public:
modint() : v(0) {}
modint(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }
explicit operator int() const { return v; }
friend std::ostream& operator << (std::ostream& out, const modint& n) { return out << int(n); }
friend std::istream& operator >> (std::istream& in, modint& n) { ll v_; in >> v_; n = modint(v_); return in; }
friend bool operator == (const modint& a, const modint& b) { return a.v == b.v; }
friend bool operator != (const modint& a, const modint& b) { return a.v != b.v; }
modint inv() const {
modint res;
res.v = minv(v, MOD);
return res;
}
friend modint inv(const modint& m) { return m.inv(); }
modint neg() const {
modint res;
res.v = v ? MOD-v : 0;
return res;
}
friend modint neg(const modint& m) { return m.neg(); }
modint operator- () const {
return neg();
}
modint operator+ () const {
return modint(*this);
}
modint& operator ++ () {
v ++;
if (v == MOD) v = 0;
return *this;
}
modint& operator -- () {
if (v == 0) v = MOD;
v --;
return *this;
}
modint& operator += (const modint& o) {
v += o.v;
if (v >= MOD) v -= MOD;
return *this;
}
modint& operator -= (const modint& o) {
v -= o.v;
if (v < 0) v += MOD;
return *this;
}
modint& operator *= (const modint& o) {
v = int(ll(v) * ll(o.v) % MOD);
return *this;
}
modint& operator /= (const modint& o) {
return *this *= o.inv();
}
friend modint operator ++ (modint& a, int) { modint r = a; ++a; return r; }
friend modint operator -- (modint& a, int) { modint r = a; --a; return r; }
friend modint operator + (const modint& a, const modint& b) { return modint(a) += b; }
friend modint operator - (const modint& a, const modint& b) { return modint(a) -= b; }
friend modint operator * (const modint& a, const modint& b) { return modint(a) *= b; }
friend modint operator / (const modint& a, const modint& b) { return modint(a) /= b; }
};
namespace IO {
template<class T>
void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(ll &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { x = getchar(); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U>
void R(T &head, U &... tail) { _R(head), R(tail...); }
template<class T>
void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const ll &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T, class U>
void _W(const pair<T, U> &x) { _W(x.first), putchar(' '), _W(x.second); }
template<class T>
void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U>
void W(const T &head, const U &... tail) { _W(head), putchar(sizeof...(tail) ? ' ' : '\n'), W(tail...); }
}
using namespace IO;
template <typename T> T pow(T a, long long b) {
assert(b >= 0);
T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r;
}
const int maxn = 2e5+50;
const int maxp = maxn*32+5;
const int K = 20;
const int M = 1 << K;
int ls[maxp], rs[maxp], tot;
// 0, 1, cnt, XOR
int t0[maxp], t1[maxp], p[maxp], txor[maxp], tor[maxp];
inline void pushup(int x) {
p[x] = p[ls[x]] + p[rs[x]];
t0[x] = t0[ls[x]] | t0[rs[x]];
t1[x] = t1[ls[x]] | t1[rs[x]];
}
inline void XOR(int x, int t) {
if (!x) return;
txor[x] ^= t;
if (~tor[x] && t >> tor[x] & 1) swap(ls[x], rs[x]);
int a = (t0[x] & (~t)) | (t1[x] & t), b = (t1[x] & (~t)) | (t0[x] & t);
t0[x] = a, t1[x] = b;
}
inline void pushdown(int x) {
if (txor[x]) {
XOR(ls[x], txor[x]), XOR(rs[x], txor[x]);
txor[x] = 0;
}
}
inline void insert(int &x, int s, int k) {
if (!x) x = ++tot;
tor[x] = k;
if (k == -1) {
t1[x] = s & (M - 1), t0[x] = t1[x] ^ (M - 1);
p[x] = 1;
return;
}
insert((s >> k & 1) ? rs[x] : ls[x], s, k - 1);
pushup(x);
}
inline void split(int &x, int &y, int l, int r, int le, int re) {
if (!x || re < l || le > r) {
y = 0;
return;
}
if (le <= l && r <= re) {
y = x;
x = 0;
return;
}
int mid = l + r >> 1; pushdown(x);
tor[y = ++tot] = tor[x];
split(ls[x], ls[y], l, mid, le, re);
split(rs[x], rs[y], mid + 1, r, le, re);
pushup(x), pushup(y);
}
inline void merge(int &x, int y) {
if (!x || !y) {
x = x | y;
return;
}
pushdown(x), pushdown(y);
merge(ls[x], ls[y]), merge(rs[x], rs[y]);
if (~tor[x]) pushup(x);
}
inline void OR(int x, int s) {
if (!x) return;
if (!(s & t0[x] & t1[x])) {
XOR(x, s & t0[x]);
return;
}
pushdown(x);
if (s >> tor[x] & 1) XOR(ls[x], 1 << tor[x]), merge(rs[x], ls[x]), ls[x] = 0;
OR(ls[x], s), OR(rs[x], s);
pushup(x);
}
int main() {
int n, q, x = 0;
R(n, q);
for (int i = 1; i <= n; ++i) {
int v; R(v); insert(x, v, K - 1);
}
for (; q; --q) {
int oth;
int t, l, r, v; R(t, l, r);
split(x, oth, 0, M - 1, l, r);
if (t == 1) R(v),XOR(oth, M - 1),OR(oth, v ^ (M - 1)),XOR(oth, M - 1);
else if (t == 2) R(v),OR(oth, v);
else if (t == 3) R(v), XOR(oth, v);
else W(p[oth]);
merge(x, oth);
}
return 0;
}
``` |
#include <stdio.h>
#include <stdlib.h>
#define N 200000
#define Q 100000
#define N_ ((N + Q * 2) * (LG + 1))
#define LG 20
#define B (1 << LG)
int ll[1 + N_], rr[1 + N_], sz[1 + N_], msk0[1 + N_], msk1[1 + N_], lz[1 + N_], _ = 1;
void put(int u, int lg, int x) {int tmp, x0, x1;if (u == 0)return;if ((x & 1 << lg - 1) != 0)tmp = ll[u], ll[u] = rr[u], rr[u] = tmp;
x0 = msk0[u] & ~x | msk1[u] & x, x1 = msk1[u] & ~x | msk0[u] & x;msk0[u] = x0, msk1[u] = x1;lz[u] ^= x;}
void pus(int u, int lg) {if (lz[u])put(ll[u], lg - 1, lz[u]), put(rr[u], lg - 1, lz[u]), lz[u] = 0;}
void pul(int u) {int l = ll[u], r = rr[u];sz[u] = sz[l] + sz[r], msk0[u] = msk0[l] | msk0[r], msk1[u] = msk1[l] | msk1[r];}
int add(int u, int lg, int a) {if (u == 0)u = _++;if (lg == 0)sz[u] = 1, msk0[u] = a ^ B - 1, msk1[u] = a;
else {if ((a & 1 << lg - 1) == 0)ll[u] = add(ll[u], lg - 1, a);else rr[u] = add(rr[u], lg - 1, a);pul(u);}return u;}
void split(int *u, int *v, int lg, int l, int r, int ql, int qr) {
int m;
if (qr <= l || r <= ql || *u == 0) {
*v = 0;
return;
}
if (ql <= l && r <= qr) {
*v = *u, *u = 0;
return;
}
pus(*u, lg), *v = _++;
m = (l + r) / 2;
split(&ll[*u], &ll[*v], lg - 1, l, m, ql, qr), split(&rr[*u], &rr[*v], lg - 1, m, r, ql, qr);
pul(*u), pul(*v);
}
void merge(int *u, int *v, int lg) {if (*u == 0) {*u = *v;return;}if (*v == 0 || lg == 0)return;pus(*u, lg), pus(*v, lg);merge(&ll[*u], &ll[*v], lg - 1), merge(&rr[*u], &rr[*v], lg - 1);pul(*u);}
void update_or(int u, int lg, int x) {
if (u == 0)
return;
if ((x & msk0[u] & msk1[u]) == 0) {
put(u, lg, x & msk0[u]);
return;
}
pus(u, lg);
if ((x & 1 << lg - 1) != 0)
put(ll[u], lg - 1, 1 << lg - 1), merge(&rr[u], &ll[u], lg - 1), ll[u] = 0;
update_or(ll[u], lg - 1, x), update_or(rr[u], lg - 1, x);
pul(u);
}
int query(int u, int lg, int l, int r, int ql, int qr) {
int m;
if (qr <= l || r <= ql || u == 0)
return 0;
if (ql <= l && r <= qr)
return sz[u];
pus(u, lg);
m = (l + r) / 2;
return query(ll[u], lg - 1, l, m, ql, qr) + query(rr[u], lg - 1, m, r, ql, qr);
}
int main() {
int n, q, i, u;
scanf("%d%d", &n, &q);
u = 0;
for (i = 0; i < n; i++) {
int a;
scanf("%d", &a);
u = add(u, LG, a);
}
while (q--) {
int type, l, r;
scanf("%d%d%d", &type, &l, &r), r++;
if (type != 4) {
int x, v;
scanf("%d", &x);
split(&u, &v, LG, 0, B, l, r);
if (type == 1)
put(v, LG, B - 1), update_or(v, LG, x ^ B - 1), put(v, LG, B - 1);
else if (type == 2)
update_or(v, LG, x);
else
put(v, LG, x);
merge(&u, &v, LG);
} else
printf("%d\n", query(u, LG, 0, B, l, r));
}
return 0;
} | ### Prompt
Construct a cpp code solution to the problem outlined:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <stdio.h>
#include <stdlib.h>
#define N 200000
#define Q 100000
#define N_ ((N + Q * 2) * (LG + 1))
#define LG 20
#define B (1 << LG)
int ll[1 + N_], rr[1 + N_], sz[1 + N_], msk0[1 + N_], msk1[1 + N_], lz[1 + N_], _ = 1;
void put(int u, int lg, int x) {int tmp, x0, x1;if (u == 0)return;if ((x & 1 << lg - 1) != 0)tmp = ll[u], ll[u] = rr[u], rr[u] = tmp;
x0 = msk0[u] & ~x | msk1[u] & x, x1 = msk1[u] & ~x | msk0[u] & x;msk0[u] = x0, msk1[u] = x1;lz[u] ^= x;}
void pus(int u, int lg) {if (lz[u])put(ll[u], lg - 1, lz[u]), put(rr[u], lg - 1, lz[u]), lz[u] = 0;}
void pul(int u) {int l = ll[u], r = rr[u];sz[u] = sz[l] + sz[r], msk0[u] = msk0[l] | msk0[r], msk1[u] = msk1[l] | msk1[r];}
int add(int u, int lg, int a) {if (u == 0)u = _++;if (lg == 0)sz[u] = 1, msk0[u] = a ^ B - 1, msk1[u] = a;
else {if ((a & 1 << lg - 1) == 0)ll[u] = add(ll[u], lg - 1, a);else rr[u] = add(rr[u], lg - 1, a);pul(u);}return u;}
void split(int *u, int *v, int lg, int l, int r, int ql, int qr) {
int m;
if (qr <= l || r <= ql || *u == 0) {
*v = 0;
return;
}
if (ql <= l && r <= qr) {
*v = *u, *u = 0;
return;
}
pus(*u, lg), *v = _++;
m = (l + r) / 2;
split(&ll[*u], &ll[*v], lg - 1, l, m, ql, qr), split(&rr[*u], &rr[*v], lg - 1, m, r, ql, qr);
pul(*u), pul(*v);
}
void merge(int *u, int *v, int lg) {if (*u == 0) {*u = *v;return;}if (*v == 0 || lg == 0)return;pus(*u, lg), pus(*v, lg);merge(&ll[*u], &ll[*v], lg - 1), merge(&rr[*u], &rr[*v], lg - 1);pul(*u);}
void update_or(int u, int lg, int x) {
if (u == 0)
return;
if ((x & msk0[u] & msk1[u]) == 0) {
put(u, lg, x & msk0[u]);
return;
}
pus(u, lg);
if ((x & 1 << lg - 1) != 0)
put(ll[u], lg - 1, 1 << lg - 1), merge(&rr[u], &ll[u], lg - 1), ll[u] = 0;
update_or(ll[u], lg - 1, x), update_or(rr[u], lg - 1, x);
pul(u);
}
int query(int u, int lg, int l, int r, int ql, int qr) {
int m;
if (qr <= l || r <= ql || u == 0)
return 0;
if (ql <= l && r <= qr)
return sz[u];
pus(u, lg);
m = (l + r) / 2;
return query(ll[u], lg - 1, l, m, ql, qr) + query(rr[u], lg - 1, m, r, ql, qr);
}
int main() {
int n, q, i, u;
scanf("%d%d", &n, &q);
u = 0;
for (i = 0; i < n; i++) {
int a;
scanf("%d", &a);
u = add(u, LG, a);
}
while (q--) {
int type, l, r;
scanf("%d%d%d", &type, &l, &r), r++;
if (type != 4) {
int x, v;
scanf("%d", &x);
split(&u, &v, LG, 0, B, l, r);
if (type == 1)
put(v, LG, B - 1), update_or(v, LG, x ^ B - 1), put(v, LG, B - 1);
else if (type == 2)
update_or(v, LG, x);
else
put(v, LG, x);
merge(&u, &v, LG);
} else
printf("%d\n", query(u, LG, 0, B, l, r));
}
return 0;
}
``` |
/* https://codeforces.com/blog/entry/90236 (segment tree beats) */
#include <stdio.h>
#include <stdlib.h>
#define N 200000
#define Q 100000
#define N_ ((N + Q * 2) * (LG + 1))
#define LG 20
#define B (1 << LG)
int ll[1 + N_], rr[1 + N_], sz[1 + N_], msk0[1 + N_], msk1[1 + N_], lz[1 + N_], _ = 1;
void put(int u, int lg, int x) {
int tmp, x0, x1;
if (u == 0)
return;
if ((x & 1 << lg - 1) != 0)
tmp = ll[u], ll[u] = rr[u], rr[u] = tmp;
x0 = msk0[u] & ~x | msk1[u] & x, x1 = msk1[u] & ~x | msk0[u] & x;
msk0[u] = x0, msk1[u] = x1;
lz[u] ^= x;
}
void pus(int u, int lg) {
if (lz[u])
put(ll[u], lg - 1, lz[u]), put(rr[u], lg - 1, lz[u]), lz[u] = 0;
}
void pul(int u) {
int l = ll[u], r = rr[u];
sz[u] = sz[l] + sz[r], msk0[u] = msk0[l] | msk0[r], msk1[u] = msk1[l] | msk1[r];
}
int add(int u, int lg, int a) {
if (u == 0)
u = _++;
if (lg == 0)
sz[u] = 1, msk0[u] = a ^ B - 1, msk1[u] = a;
else {
if ((a & 1 << lg - 1) == 0)
ll[u] = add(ll[u], lg - 1, a);
else
rr[u] = add(rr[u], lg - 1, a);
pul(u);
}
return u;
}
void split(int *u, int *v, int lg, int l, int r, int ql, int qr) {
int m;
if (qr <= l || r <= ql || *u == 0) {
*v = 0;
return;
}
if (ql <= l && r <= qr) {
*v = *u, *u = 0;
return;
}
pus(*u, lg), *v = _++;
m = (l + r) / 2;
split(&ll[*u], &ll[*v], lg - 1, l, m, ql, qr), split(&rr[*u], &rr[*v], lg - 1, m, r, ql, qr);
pul(*u), pul(*v);
}
void merge(int *u, int *v, int lg) {
if (*u == 0) {
*u = *v;
return;
}
if (*v == 0 || lg == 0)
return;
pus(*u, lg), pus(*v, lg);
merge(&ll[*u], &ll[*v], lg - 1), merge(&rr[*u], &rr[*v], lg - 1);
pul(*u);
}
void update_or(int u, int lg, int x) {
if (u == 0)
return;
if ((x & msk0[u] & msk1[u]) == 0) {
put(u, lg, x & msk0[u]);
return;
}
pus(u, lg);
if ((x & 1 << lg - 1) != 0)
put(ll[u], lg - 1, 1 << lg - 1), merge(&rr[u], &ll[u], lg - 1), ll[u] = 0;
update_or(ll[u], lg - 1, x), update_or(rr[u], lg - 1, x);
pul(u);
}
int query(int u, int lg, int l, int r, int ql, int qr) {
int m;
if (qr <= l || r <= ql || u == 0)
return 0;
if (ql <= l && r <= qr)
return sz[u];
pus(u, lg);
m = (l + r) / 2;
return query(ll[u], lg - 1, l, m, ql, qr) + query(rr[u], lg - 1, m, r, ql, qr);
}
int main() {
int n, q, i, u;
scanf("%d%d", &n, &q);
u = 0;
for (i = 0; i < n; i++) {
int a;
scanf("%d", &a);
u = add(u, LG, a);
}
while (q--) {
int type, l, r;
scanf("%d%d%d", &type, &l, &r), r++;
if (type != 4) {
int x, v;
scanf("%d", &x);
split(&u, &v, LG, 0, B, l, r);
if (type == 1)
put(v, LG, B - 1), update_or(v, LG, x ^ B - 1), put(v, LG, B - 1);
else if (type == 2)
update_or(v, LG, x);
else
put(v, LG, x);
merge(&u, &v, LG);
} else
printf("%d\n", query(u, LG, 0, B, l, r));
}
return 0;
}
| ### Prompt
In cpp, your task is to solve the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
/* https://codeforces.com/blog/entry/90236 (segment tree beats) */
#include <stdio.h>
#include <stdlib.h>
#define N 200000
#define Q 100000
#define N_ ((N + Q * 2) * (LG + 1))
#define LG 20
#define B (1 << LG)
int ll[1 + N_], rr[1 + N_], sz[1 + N_], msk0[1 + N_], msk1[1 + N_], lz[1 + N_], _ = 1;
void put(int u, int lg, int x) {
int tmp, x0, x1;
if (u == 0)
return;
if ((x & 1 << lg - 1) != 0)
tmp = ll[u], ll[u] = rr[u], rr[u] = tmp;
x0 = msk0[u] & ~x | msk1[u] & x, x1 = msk1[u] & ~x | msk0[u] & x;
msk0[u] = x0, msk1[u] = x1;
lz[u] ^= x;
}
void pus(int u, int lg) {
if (lz[u])
put(ll[u], lg - 1, lz[u]), put(rr[u], lg - 1, lz[u]), lz[u] = 0;
}
void pul(int u) {
int l = ll[u], r = rr[u];
sz[u] = sz[l] + sz[r], msk0[u] = msk0[l] | msk0[r], msk1[u] = msk1[l] | msk1[r];
}
int add(int u, int lg, int a) {
if (u == 0)
u = _++;
if (lg == 0)
sz[u] = 1, msk0[u] = a ^ B - 1, msk1[u] = a;
else {
if ((a & 1 << lg - 1) == 0)
ll[u] = add(ll[u], lg - 1, a);
else
rr[u] = add(rr[u], lg - 1, a);
pul(u);
}
return u;
}
void split(int *u, int *v, int lg, int l, int r, int ql, int qr) {
int m;
if (qr <= l || r <= ql || *u == 0) {
*v = 0;
return;
}
if (ql <= l && r <= qr) {
*v = *u, *u = 0;
return;
}
pus(*u, lg), *v = _++;
m = (l + r) / 2;
split(&ll[*u], &ll[*v], lg - 1, l, m, ql, qr), split(&rr[*u], &rr[*v], lg - 1, m, r, ql, qr);
pul(*u), pul(*v);
}
void merge(int *u, int *v, int lg) {
if (*u == 0) {
*u = *v;
return;
}
if (*v == 0 || lg == 0)
return;
pus(*u, lg), pus(*v, lg);
merge(&ll[*u], &ll[*v], lg - 1), merge(&rr[*u], &rr[*v], lg - 1);
pul(*u);
}
void update_or(int u, int lg, int x) {
if (u == 0)
return;
if ((x & msk0[u] & msk1[u]) == 0) {
put(u, lg, x & msk0[u]);
return;
}
pus(u, lg);
if ((x & 1 << lg - 1) != 0)
put(ll[u], lg - 1, 1 << lg - 1), merge(&rr[u], &ll[u], lg - 1), ll[u] = 0;
update_or(ll[u], lg - 1, x), update_or(rr[u], lg - 1, x);
pul(u);
}
int query(int u, int lg, int l, int r, int ql, int qr) {
int m;
if (qr <= l || r <= ql || u == 0)
return 0;
if (ql <= l && r <= qr)
return sz[u];
pus(u, lg);
m = (l + r) / 2;
return query(ll[u], lg - 1, l, m, ql, qr) + query(rr[u], lg - 1, m, r, ql, qr);
}
int main() {
int n, q, i, u;
scanf("%d%d", &n, &q);
u = 0;
for (i = 0; i < n; i++) {
int a;
scanf("%d", &a);
u = add(u, LG, a);
}
while (q--) {
int type, l, r;
scanf("%d%d%d", &type, &l, &r), r++;
if (type != 4) {
int x, v;
scanf("%d", &x);
split(&u, &v, LG, 0, B, l, r);
if (type == 1)
put(v, LG, B - 1), update_or(v, LG, x ^ B - 1), put(v, LG, B - 1);
else if (type == 2)
update_or(v, LG, x);
else
put(v, LG, x);
merge(&u, &v, LG);
} else
printf("%d\n", query(u, LG, 0, B, l, r));
}
return 0;
}
``` |
#include<stdio.h>
const int maxn=200005,maxk=maxn*50;
int n,q,k,rt,cnt;
int lc[maxk],rc[maxk],lazy[maxk],Vand[maxk],Vor[maxk],res[maxk];
inline void swp(int &a,int &b){
a+=b,b=a-b,a-=b;
}
inline void pushup(int now){
Vand[now]=Vand[lc[now]]|Vand[rc[now]];
Vor[now]=Vor[lc[now]]|Vor[rc[now]];
res[now]=res[lc[now]]+res[rc[now]];
}
void insert(int dep,int &now,int val){
if(now==0)
now=++cnt;
if(dep==0){
Vand[now]=(k^val),Vor[now]=val,res[now]=1;
return ;
}
if(((val>>(dep-1))&1)==0)
insert(dep-1,lc[now],val);
else insert(dep-1,rc[now],val);
pushup(now);
}
void getxor(int dep,int now,int val){
if(now==0)
return ;
if((val>>(dep-1))&1)
swp(lc[now],rc[now]);
int Va=Vand[now],Vo=Vor[now];
Vand[now]=(Va&(k^val))|(Vo&val);//
Vor[now]=(Vo&(k^val))|(Va&val);
lazy[now]^=val;
}
void pushdown(int dep,int now){
if(lazy[now]==0)
return ;
getxor(dep-1,lc[now],lazy[now]),getxor(dep-1,rc[now],lazy[now]);
lazy[now]=0;
}
void split(int dep,int &x,int &y,int l,int r,int L,int R){
if(x==0||R<l||r<L){
y=0;
return ;
}
if(L<=l&&r<=R){
y=x,x=0;
return ;
}
int mid=(l+r)>>1;
pushdown(dep,x),y=++cnt;
split(dep-1,lc[x],lc[y],l,mid,L,R),split(dep-1,rc[x],rc[y],mid+1,r,L,R);
pushup(x),pushup(y);
}
void merge(int dep,int &x,int &y){
if(x==0){
x=y;
return ;
}
if(y==0||dep==0)
return ;
pushdown(dep,x),pushdown(dep,y);
merge(dep-1,lc[x],lc[y]),merge(dep-1,rc[x],rc[y]);
pushup(x);
}
void getor(int dep,int now,int val){
if(now==0)
return ;
if((val&Vand[now]&Vor[now])==0){
getxor(dep,now,val&Vand[now]);
return ;
}
pushdown(dep,now);
if((val>>(dep-1))&1){
getxor(dep-1,lc[now],1<<(dep-1));
merge(dep-1,rc[now],lc[now]);
lc[now]=0;
}
getor(dep-1,lc[now],val),getor(dep-1,rc[now],val);
pushup(now);
}
int query(int dep,int now,int l,int r,int L,int R){
if(now==0||R<l||r<L)
return 0;
if(L<=l&&r<=R)
return res[now];
int mid=(l+r)>>1;
pushdown(dep,now);
return query(dep-1,lc[now],l,mid,L,R)+query(dep-1,rc[now],mid+1,r,L,R);
}
int main(){
scanf("%d%d",&n,&q),k=(1<<20)-1;
for(int i=1;i<=n;i++){
int x;
scanf("%d",&x);
insert(20,rt,x);
}
for(int i=1;i<=q;i++){
int t,x,y,z;
scanf("%d",&t);
if(t==4){
scanf("%d%d",&x,&y);
printf("%d\n",query(20,rt,0,k,x,y));
continue;
}
int now;
scanf("%d%d%d",&x,&y,&z);
split(20,rt,now,0,k,x,y);
if(t==1)
getxor(20,now,k),getor(20,now,z^k),getxor(20,now,k);
if(t==2)
getor(20,now,z);
if(t==3)
getxor(20,now,z);
merge(20,rt,now);
}
return 0;
} | ### Prompt
Generate a cpp solution to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include<stdio.h>
const int maxn=200005,maxk=maxn*50;
int n,q,k,rt,cnt;
int lc[maxk],rc[maxk],lazy[maxk],Vand[maxk],Vor[maxk],res[maxk];
inline void swp(int &a,int &b){
a+=b,b=a-b,a-=b;
}
inline void pushup(int now){
Vand[now]=Vand[lc[now]]|Vand[rc[now]];
Vor[now]=Vor[lc[now]]|Vor[rc[now]];
res[now]=res[lc[now]]+res[rc[now]];
}
void insert(int dep,int &now,int val){
if(now==0)
now=++cnt;
if(dep==0){
Vand[now]=(k^val),Vor[now]=val,res[now]=1;
return ;
}
if(((val>>(dep-1))&1)==0)
insert(dep-1,lc[now],val);
else insert(dep-1,rc[now],val);
pushup(now);
}
void getxor(int dep,int now,int val){
if(now==0)
return ;
if((val>>(dep-1))&1)
swp(lc[now],rc[now]);
int Va=Vand[now],Vo=Vor[now];
Vand[now]=(Va&(k^val))|(Vo&val);//
Vor[now]=(Vo&(k^val))|(Va&val);
lazy[now]^=val;
}
void pushdown(int dep,int now){
if(lazy[now]==0)
return ;
getxor(dep-1,lc[now],lazy[now]),getxor(dep-1,rc[now],lazy[now]);
lazy[now]=0;
}
void split(int dep,int &x,int &y,int l,int r,int L,int R){
if(x==0||R<l||r<L){
y=0;
return ;
}
if(L<=l&&r<=R){
y=x,x=0;
return ;
}
int mid=(l+r)>>1;
pushdown(dep,x),y=++cnt;
split(dep-1,lc[x],lc[y],l,mid,L,R),split(dep-1,rc[x],rc[y],mid+1,r,L,R);
pushup(x),pushup(y);
}
void merge(int dep,int &x,int &y){
if(x==0){
x=y;
return ;
}
if(y==0||dep==0)
return ;
pushdown(dep,x),pushdown(dep,y);
merge(dep-1,lc[x],lc[y]),merge(dep-1,rc[x],rc[y]);
pushup(x);
}
void getor(int dep,int now,int val){
if(now==0)
return ;
if((val&Vand[now]&Vor[now])==0){
getxor(dep,now,val&Vand[now]);
return ;
}
pushdown(dep,now);
if((val>>(dep-1))&1){
getxor(dep-1,lc[now],1<<(dep-1));
merge(dep-1,rc[now],lc[now]);
lc[now]=0;
}
getor(dep-1,lc[now],val),getor(dep-1,rc[now],val);
pushup(now);
}
int query(int dep,int now,int l,int r,int L,int R){
if(now==0||R<l||r<L)
return 0;
if(L<=l&&r<=R)
return res[now];
int mid=(l+r)>>1;
pushdown(dep,now);
return query(dep-1,lc[now],l,mid,L,R)+query(dep-1,rc[now],mid+1,r,L,R);
}
int main(){
scanf("%d%d",&n,&q),k=(1<<20)-1;
for(int i=1;i<=n;i++){
int x;
scanf("%d",&x);
insert(20,rt,x);
}
for(int i=1;i<=q;i++){
int t,x,y,z;
scanf("%d",&t);
if(t==4){
scanf("%d%d",&x,&y);
printf("%d\n",query(20,rt,0,k,x,y));
continue;
}
int now;
scanf("%d%d%d",&x,&y,&z);
split(20,rt,now,0,k,x,y);
if(t==1)
getxor(20,now,k),getor(20,now,z^k),getxor(20,now,k);
if(t==2)
getor(20,now,z);
if(t==3)
getxor(20,now,z);
merge(20,rt,now);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int B=20;
int n,q,rt=1,cnt=1;
int t0[15000000],t1[15000000],sz[15000000],s[2][15000000],lz[15000000];
void pushup(int id,int d){
sz[id]=sz[s[0][id]]+sz[s[1][id]];
t0[id]=t0[s[0][id]]|t0[s[1][id]];
t1[id]=t1[s[0][id]]|t1[s[1][id]];
if(s[0][id])t0[id]|=(1<<d);
if(s[1][id])t1[id]|=(1<<d);
}
void tag(int id,int x,int d){
if(!id||d==-1)return;
if(x&(1<<d))swap(s[0][id],s[1][id]);
int tmp=(t0[id]^t1[id])&x;
t0[id]^=tmp,t1[id]^=tmp;
lz[id]^=x;
}
void pushdown(int id,int d){
if(lz[id]){
tag(s[0][id],lz[id],d-1);
tag(s[1][id],lz[id],d-1);
lz[id]=0;
}
}
void ins(int id,int x,int d){
if(d==-1){
sz[id]=1;
return;
}
if((1<<d)&x){
if(!s[1][id])s[1][id]=++cnt;
ins(s[1][id],x,d-1);
}else{
if(!s[0][id])s[0][id]=++cnt;
ins(s[0][id],x,d-1);
}
pushup(id,d);
}
void split(int id,int d,int p,int &x,int &y){
if(id==0){
x=y=0;
return;
}
if(d==-1){
x=id,y=0;
return;
}
pushdown(id,d);
if(p&(1<<d)){
x=id,y=++cnt;
split(s[1][x],d-1,p,s[1][x],s[1][y]);
}else{
x=++cnt,y=id;
split(s[0][y],d-1,p,s[0][x],s[0][y]);
}
pushup(x,d),pushup(y,d);
}
int merge(int id1,int id2,int d){
if(!id1||!id2)return id1|id2;
if(d==-1)return id1;
pushdown(id1,d),pushdown(id2,d);
s[0][id1]=merge(s[0][id1],s[0][id2],d-1);
s[1][id1]=merge(s[1][id1],s[1][id2],d-1);
pushup(id1,d);
return id1;
}
void AND(int id,int p,int d){
if(!id||(!((1<<p)&t1[id])))return;
if(!((1<<p)&t0[id])){
tag(id,1<<p,d);
return;
}
pushdown(id,d);
if(d==p){
s[0][id]=merge(s[0][id],s[1][id],d-1);
s[1][id]=0;
pushup(id,d);
return;
}
AND(s[0][id],p,d-1);
AND(s[1][id],p,d-1);
pushup(id,d);
}
void OR(int id,int p,int d){
if(!id||(!((1<<p)&t0[id])))return;
if(!((1<<p)&t1[id])){
tag(id,1<<p,d);
return;
}
pushdown(id,d);
if(d==p){
s[1][id]=merge(s[0][id],s[1][id],d-1);
s[0][id]=0;
pushup(id,d);
return;
}
OR(s[0][id],p,d-1);
OR(s[1][id],p,d-1);
pushup(id,d);
}
int main(){
scanf("%d%d",&n,&q);
while(n--){
int x;scanf("%d",&x);
ins(1,x,B-1);
}
while(q--){
int t,l,r,x,lr=0,rr=0;
scanf("%d%d%d",&t,&l,&r);
if(l>0)split(rt,B-1,l-1,lr,rt);
split(rt,B-1,r,rt,rr);
if(t==1){
scanf("%d",&x);
for(int i=0;i<B;i++)if(!(x&(1<<i)))AND(rt,i,B-1);
}
if(t==2){
scanf("%d",&x);
for(int i=0;i<B;i++)if(x&(1<<i))OR(rt,i,B-1);
}
if(t==3){
scanf("%d",&x);
tag(rt,x,B-1);
}
if(t==4)printf("%d\n",sz[rt]);
rt=merge(merge(lr,rt,B-1),rr,B-1);
}
} | ### Prompt
Construct a cpp code solution to the problem outlined:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int B=20;
int n,q,rt=1,cnt=1;
int t0[15000000],t1[15000000],sz[15000000],s[2][15000000],lz[15000000];
void pushup(int id,int d){
sz[id]=sz[s[0][id]]+sz[s[1][id]];
t0[id]=t0[s[0][id]]|t0[s[1][id]];
t1[id]=t1[s[0][id]]|t1[s[1][id]];
if(s[0][id])t0[id]|=(1<<d);
if(s[1][id])t1[id]|=(1<<d);
}
void tag(int id,int x,int d){
if(!id||d==-1)return;
if(x&(1<<d))swap(s[0][id],s[1][id]);
int tmp=(t0[id]^t1[id])&x;
t0[id]^=tmp,t1[id]^=tmp;
lz[id]^=x;
}
void pushdown(int id,int d){
if(lz[id]){
tag(s[0][id],lz[id],d-1);
tag(s[1][id],lz[id],d-1);
lz[id]=0;
}
}
void ins(int id,int x,int d){
if(d==-1){
sz[id]=1;
return;
}
if((1<<d)&x){
if(!s[1][id])s[1][id]=++cnt;
ins(s[1][id],x,d-1);
}else{
if(!s[0][id])s[0][id]=++cnt;
ins(s[0][id],x,d-1);
}
pushup(id,d);
}
void split(int id,int d,int p,int &x,int &y){
if(id==0){
x=y=0;
return;
}
if(d==-1){
x=id,y=0;
return;
}
pushdown(id,d);
if(p&(1<<d)){
x=id,y=++cnt;
split(s[1][x],d-1,p,s[1][x],s[1][y]);
}else{
x=++cnt,y=id;
split(s[0][y],d-1,p,s[0][x],s[0][y]);
}
pushup(x,d),pushup(y,d);
}
int merge(int id1,int id2,int d){
if(!id1||!id2)return id1|id2;
if(d==-1)return id1;
pushdown(id1,d),pushdown(id2,d);
s[0][id1]=merge(s[0][id1],s[0][id2],d-1);
s[1][id1]=merge(s[1][id1],s[1][id2],d-1);
pushup(id1,d);
return id1;
}
void AND(int id,int p,int d){
if(!id||(!((1<<p)&t1[id])))return;
if(!((1<<p)&t0[id])){
tag(id,1<<p,d);
return;
}
pushdown(id,d);
if(d==p){
s[0][id]=merge(s[0][id],s[1][id],d-1);
s[1][id]=0;
pushup(id,d);
return;
}
AND(s[0][id],p,d-1);
AND(s[1][id],p,d-1);
pushup(id,d);
}
void OR(int id,int p,int d){
if(!id||(!((1<<p)&t0[id])))return;
if(!((1<<p)&t1[id])){
tag(id,1<<p,d);
return;
}
pushdown(id,d);
if(d==p){
s[1][id]=merge(s[0][id],s[1][id],d-1);
s[0][id]=0;
pushup(id,d);
return;
}
OR(s[0][id],p,d-1);
OR(s[1][id],p,d-1);
pushup(id,d);
}
int main(){
scanf("%d%d",&n,&q);
while(n--){
int x;scanf("%d",&x);
ins(1,x,B-1);
}
while(q--){
int t,l,r,x,lr=0,rr=0;
scanf("%d%d%d",&t,&l,&r);
if(l>0)split(rt,B-1,l-1,lr,rt);
split(rt,B-1,r,rt,rr);
if(t==1){
scanf("%d",&x);
for(int i=0;i<B;i++)if(!(x&(1<<i)))AND(rt,i,B-1);
}
if(t==2){
scanf("%d",&x);
for(int i=0;i<B;i++)if(x&(1<<i))OR(rt,i,B-1);
}
if(t==3){
scanf("%d",&x);
tag(rt,x,B-1);
}
if(t==4)printf("%d\n",sz[rt]);
rt=merge(merge(lr,rt,B-1),rr,B-1);
}
}
``` |
#include<bits/stdc++.h>
#define ll long long
#define N 200015
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define lowbit(i) ((i)&(-i))
#define VI vector<int>
#define all(x) x.begin(),x.end()
#define SZ(x) ((int)x.size())
using namespace std;
int n,q,a[N];
const int all = (1<<20)-1;
int ls[N<<5],rs[N<<5],lz[N<<5],t0[N<<5],t1[N<<5],tr[N<<5],rt,tot;
void pushup(int p){
tr[p] = tr[ls[p]]+tr[rs[p]];
t0[p] = t0[ls[p]]|t0[rs[p]];
t1[p] = t1[ls[p]]|t1[rs[p]];
}
void Xor(int p,int dep,int x){
if(!p) return;
if(x>>(dep-1)&1) swap(ls[p],rs[p]);
int v0 = (t1[p]&x)|(t0[p]&(~x)),v1 = (t0[p]&x)|(t1[p]&(~x));
t0[p] = v0; t1[p] = v1; lz[p] ^= x;
}
void pushdown(int p,int dep){
if(lz[p]){
Xor(ls[p],dep-1,lz[p]);
Xor(rs[p],dep-1,lz[p]);
lz[p] = 0;
}
}
void Split(int &u,int &v,int dep,int l,int r,int a,int b){
if(a <= l && r <= b){
v = u; u = 0;
return;
}
pushdown(u,dep);
v = ++tot;
int mid = (l+r)>>1;
if(a <= mid) Split(ls[u],ls[v],dep-1,l,mid,a,b);
if(b > mid) Split(rs[u],rs[v],dep-1,mid+1,r,a,b);
pushup(u); pushup(v);
}
void merge(int &u,int &v,int dep){
if(!u) return u = v,void();
if(!v || !dep) return;
pushdown(u,dep); pushdown(v,dep);
merge(ls[u],ls[v],dep-1); merge(rs[u],rs[v],dep-1);
pushup(u);
}
void ins(int &p,int x,int dep){
if(!p) p = ++tot;
if(!dep){
tr[p] = 1;
t0[p] = x^all;
t1[p] = x;
return;
}
if(x>>(dep-1)&1) ins(rs[p],x,dep-1);
else ins(ls[p],x,dep-1);
pushup(p);
}
void Or(int p,int dep,int x){
if(!p || !dep) return;
if(!(x&t0[p]&t1[p])){
Xor(p,dep,x&t0[p]);
return;
}
pushdown(p,dep);
if(x>>(dep-1)&1){
Xor(ls[p],dep-1,1<<(dep-1));
merge(rs[p],ls[p],dep-1);
ls[p] = 0;
}
Or(ls[p],dep-1,x);
Or(rs[p],dep-1,x);
pushup(p);
}
int query(int p,int dep,int l,int r,int x,int y){
if(x <= l && r <= y) return tr[p];
int mid = (l+r)>>1;
pushdown(p,dep);
int res = 0;
if(x <= mid) res += query(ls[p],dep-1,l,mid,x,y);
if(y > mid) res += query(rs[p],dep-1,mid+1,r,x,y);
return res;
}
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
scanf("%d%d",&n,&q);
rep(i,1,n){
int x; scanf("%d",&x);
ins(rt,x,20);
}
while(q--){
int typ,l,r,x;
scanf("%d%d%d",&typ,&l,&r);
if(typ <= 3){
int v;
scanf("%d",&x);
Split(rt,v,20,0,all,l,r);
if(typ == 1) Xor(v,20,all),Or(v,20,x^all),Xor(v,20,all);
if(typ == 2) Or(v,20,x);
if(typ == 3) Xor(v,20,x);
merge(rt,v,20);
}else{
printf("%d\n",query(rt,20,0,all,l,r));
}
}
return 0;
} | ### Prompt
Your challenge is to write a CPP solution to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include<bits/stdc++.h>
#define ll long long
#define N 200015
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define lowbit(i) ((i)&(-i))
#define VI vector<int>
#define all(x) x.begin(),x.end()
#define SZ(x) ((int)x.size())
using namespace std;
int n,q,a[N];
const int all = (1<<20)-1;
int ls[N<<5],rs[N<<5],lz[N<<5],t0[N<<5],t1[N<<5],tr[N<<5],rt,tot;
void pushup(int p){
tr[p] = tr[ls[p]]+tr[rs[p]];
t0[p] = t0[ls[p]]|t0[rs[p]];
t1[p] = t1[ls[p]]|t1[rs[p]];
}
void Xor(int p,int dep,int x){
if(!p) return;
if(x>>(dep-1)&1) swap(ls[p],rs[p]);
int v0 = (t1[p]&x)|(t0[p]&(~x)),v1 = (t0[p]&x)|(t1[p]&(~x));
t0[p] = v0; t1[p] = v1; lz[p] ^= x;
}
void pushdown(int p,int dep){
if(lz[p]){
Xor(ls[p],dep-1,lz[p]);
Xor(rs[p],dep-1,lz[p]);
lz[p] = 0;
}
}
void Split(int &u,int &v,int dep,int l,int r,int a,int b){
if(a <= l && r <= b){
v = u; u = 0;
return;
}
pushdown(u,dep);
v = ++tot;
int mid = (l+r)>>1;
if(a <= mid) Split(ls[u],ls[v],dep-1,l,mid,a,b);
if(b > mid) Split(rs[u],rs[v],dep-1,mid+1,r,a,b);
pushup(u); pushup(v);
}
void merge(int &u,int &v,int dep){
if(!u) return u = v,void();
if(!v || !dep) return;
pushdown(u,dep); pushdown(v,dep);
merge(ls[u],ls[v],dep-1); merge(rs[u],rs[v],dep-1);
pushup(u);
}
void ins(int &p,int x,int dep){
if(!p) p = ++tot;
if(!dep){
tr[p] = 1;
t0[p] = x^all;
t1[p] = x;
return;
}
if(x>>(dep-1)&1) ins(rs[p],x,dep-1);
else ins(ls[p],x,dep-1);
pushup(p);
}
void Or(int p,int dep,int x){
if(!p || !dep) return;
if(!(x&t0[p]&t1[p])){
Xor(p,dep,x&t0[p]);
return;
}
pushdown(p,dep);
if(x>>(dep-1)&1){
Xor(ls[p],dep-1,1<<(dep-1));
merge(rs[p],ls[p],dep-1);
ls[p] = 0;
}
Or(ls[p],dep-1,x);
Or(rs[p],dep-1,x);
pushup(p);
}
int query(int p,int dep,int l,int r,int x,int y){
if(x <= l && r <= y) return tr[p];
int mid = (l+r)>>1;
pushdown(p,dep);
int res = 0;
if(x <= mid) res += query(ls[p],dep-1,l,mid,x,y);
if(y > mid) res += query(rs[p],dep-1,mid+1,r,x,y);
return res;
}
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
scanf("%d%d",&n,&q);
rep(i,1,n){
int x; scanf("%d",&x);
ins(rt,x,20);
}
while(q--){
int typ,l,r,x;
scanf("%d%d%d",&typ,&l,&r);
if(typ <= 3){
int v;
scanf("%d",&x);
Split(rt,v,20,0,all,l,r);
if(typ == 1) Xor(v,20,all),Or(v,20,x^all),Xor(v,20,all);
if(typ == 2) Or(v,20,x);
if(typ == 3) Xor(v,20,x);
merge(rt,v,20);
}else{
printf("%d\n",query(rt,20,0,all,l,r));
}
}
return 0;
}
``` |
#include <cstdio>
#include <cassert>
#include <utility>
#include <functional>
//numbers up to 2^MAXLOGX-1
const int MAXLOGX=20;
template<int k>
struct Trie{
Trie<k-1>* chd[2];
int cnt;
int lazy;
int has[2];
int get_cnt(){
assert(this!=NULL);
return cnt;
}
int get_has(int d){
assert(this!=NULL);
push();
assert(has[d]<(1<<(k+1)));
return has[d];
}
Trie(Trie<k-1>* l,Trie<k-1>* r):chd{l,r},cnt(0),lazy(0),has{0,0}{
if(l){
cnt+=l->get_cnt();
has[0]|=l->get_has(0)|(1<<k);
has[1]|=l->get_has(1);
}
if(r){
cnt+=r->get_cnt();
has[0]|=r->get_has(0);
has[1]|=r->get_has(1)|(1<<k);
}
assert(has[0]<(1<<(k+1)));
assert(has[1]<(1<<(k+1)));
}
void push(){
assert(lazy<(1<<(k+1)));
if(!lazy) return;
//handle kth bit
if(lazy&(1<<k)){
std::swap(chd[0],chd[1]);
if((has[0]^has[1])&(1<<k)){
has[0]^=(1<<k);
has[1]^=(1<<k);
}
lazy^=(1<<k);
}
//handle rest of bits
int flip=(has[0]^has[1])&lazy;
has[0]^=flip;
has[1]^=flip;
if(chd[0]) chd[0]->lazy^=lazy;
if(chd[1]) chd[1]->lazy^=lazy;
lazy=0;
assert(has[0]<(1<<(k+1)));
assert(has[1]<(1<<(k+1)));
}
};
template<>
struct Trie<-1>{
int lazy;
Trie():lazy(0){
}
int get_cnt(){
assert(this!=NULL);
return 1;
}
int get_has(int d){
assert(this!=NULL);
return 0;
}
};
template<int k>
Trie<k>* create(int x){
if(x&(1<<k)){
return new Trie<k>(NULL,create<k-1>(x));
}else{
return new Trie<k>(create<k-1>(x),NULL);
}
}
template<>
Trie<-1>* create(int x){
return new Trie<-1>();
}
template<int k>
std::pair<Trie<k-1>*,Trie<k-1>*> destruct(Trie<k>* a){
assert(a!=NULL);
a->push();
auto res=std::make_pair(a->chd[0],a->chd[1]);
delete a;
return res;
}
template<int k>
Trie<k>* join(Trie<k-1>* l,Trie<k-1>* r){
if(l==NULL&&r==NULL) return NULL;
return new Trie<k>(l,r);
}
template<int k>
Trie<k>* merge(Trie<k>* a,Trie<k>* b){
if(!a) return b;
if(!b) return a;
auto aa=destruct(a);
auto bb=destruct(b);
Trie<k-1>* l=merge<k-1>(aa.first,bb.first);
Trie<k-1>* r=merge<k-1>(aa.second,bb.second);
return join<k>(l,r);
}
template<>
Trie<-1>* merge<-1>(Trie<-1>* a,Trie<-1>* b){
if(!a) return b;
if(!b) return a;
delete b;
return a;
}
template<int k>
//<thres and >=thres
std::pair<Trie<k>*,Trie<k>*> split(Trie<k>* a,int thres){
if(a==NULL){
return {NULL,NULL};
}
if(thres<=0) return {NULL,a};
if(thres>=(1<<(k+1))) return {a,NULL};
assert(k>=0);
auto aa=destruct(a);
if(thres<(1<<k)){
Trie<k-1>* l,*r;
std::tie(l,r)=split<k-1>(aa.first,thres);
return std::make_pair(join<k>(l,NULL),join<k>(r,aa.second));
}else if(thres>(1<<k)){
Trie<k-1>* l,*r;
std::tie(l,r)=split<k-1>(aa.second,thres-(1<<k));
return std::make_pair(join<k>(aa.first,l),join<k>(NULL,r));
}else{
return std::make_pair(join<k>(aa.first,NULL),join<k>(NULL,aa.second));
}
}
template<>
std::pair<Trie<-1>*,Trie<-1>*> split<-1>(Trie<-1>* a,int thres){
assert(0);
}
template<int k>
Trie<k>* update(Trie<k>* a,int val){
if(a==NULL) return NULL;
a->push();
assert(val<(1<<(k+1)));
if((val&a->has[0]&a->has[1])==0){
a->lazy^=(val&a->has[0]);
return a;
}
Trie<k-1>* l,*r;
std::tie(l,r)=destruct(a);
l=update<k-1>(l,val&~(1<<k));
r=update<k-1>(r,val&~(1<<k));
if(val&(1<<k)){
return join<k>(NULL,merge<k-1>(l,r));
}else{
return join<k>(l,r);
}
}
template<>
Trie<-1>* update<-1>(Trie<-1>* a,int val){
return a;
}
int main(){
Trie<MAXLOGX-1>* root=NULL;
int N,Q;
scanf("%d %d",&N,&Q);
for(int i=0;i<N;i++){
int A;
scanf("%d",&A);
root=merge(root,create<MAXLOGX-1>(A));
}
for(int i=0;i<Q;i++){
int T,L,R;
scanf("%d %d %d",&T,&L,&R);
Trie<MAXLOGX-1>* left,*right;
std::tie(left,root)=split(root,L);
std::tie(root,right)=split(root,R+1);
if(T==4){
printf("%d\n",root?root->cnt:0);
}else{
int X;
scanf("%d",&X);
if(root!=NULL){
if(T==1){
root->lazy^=((1<<MAXLOGX)-1);
root=update(root,X^((1<<MAXLOGX)-1));
root->lazy^=((1<<MAXLOGX)-1);
}else if(T==2){
root=update(root,X);
}else if(T==3){
assert(X<(1<<MAXLOGX));
root->lazy^=X;
}
}
}
root=merge(root,left);
root=merge(root,right);
}
} | ### Prompt
Your challenge is to write a Cpp solution to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <cstdio>
#include <cassert>
#include <utility>
#include <functional>
//numbers up to 2^MAXLOGX-1
const int MAXLOGX=20;
template<int k>
struct Trie{
Trie<k-1>* chd[2];
int cnt;
int lazy;
int has[2];
int get_cnt(){
assert(this!=NULL);
return cnt;
}
int get_has(int d){
assert(this!=NULL);
push();
assert(has[d]<(1<<(k+1)));
return has[d];
}
Trie(Trie<k-1>* l,Trie<k-1>* r):chd{l,r},cnt(0),lazy(0),has{0,0}{
if(l){
cnt+=l->get_cnt();
has[0]|=l->get_has(0)|(1<<k);
has[1]|=l->get_has(1);
}
if(r){
cnt+=r->get_cnt();
has[0]|=r->get_has(0);
has[1]|=r->get_has(1)|(1<<k);
}
assert(has[0]<(1<<(k+1)));
assert(has[1]<(1<<(k+1)));
}
void push(){
assert(lazy<(1<<(k+1)));
if(!lazy) return;
//handle kth bit
if(lazy&(1<<k)){
std::swap(chd[0],chd[1]);
if((has[0]^has[1])&(1<<k)){
has[0]^=(1<<k);
has[1]^=(1<<k);
}
lazy^=(1<<k);
}
//handle rest of bits
int flip=(has[0]^has[1])&lazy;
has[0]^=flip;
has[1]^=flip;
if(chd[0]) chd[0]->lazy^=lazy;
if(chd[1]) chd[1]->lazy^=lazy;
lazy=0;
assert(has[0]<(1<<(k+1)));
assert(has[1]<(1<<(k+1)));
}
};
template<>
struct Trie<-1>{
int lazy;
Trie():lazy(0){
}
int get_cnt(){
assert(this!=NULL);
return 1;
}
int get_has(int d){
assert(this!=NULL);
return 0;
}
};
template<int k>
Trie<k>* create(int x){
if(x&(1<<k)){
return new Trie<k>(NULL,create<k-1>(x));
}else{
return new Trie<k>(create<k-1>(x),NULL);
}
}
template<>
Trie<-1>* create(int x){
return new Trie<-1>();
}
template<int k>
std::pair<Trie<k-1>*,Trie<k-1>*> destruct(Trie<k>* a){
assert(a!=NULL);
a->push();
auto res=std::make_pair(a->chd[0],a->chd[1]);
delete a;
return res;
}
template<int k>
Trie<k>* join(Trie<k-1>* l,Trie<k-1>* r){
if(l==NULL&&r==NULL) return NULL;
return new Trie<k>(l,r);
}
template<int k>
Trie<k>* merge(Trie<k>* a,Trie<k>* b){
if(!a) return b;
if(!b) return a;
auto aa=destruct(a);
auto bb=destruct(b);
Trie<k-1>* l=merge<k-1>(aa.first,bb.first);
Trie<k-1>* r=merge<k-1>(aa.second,bb.second);
return join<k>(l,r);
}
template<>
Trie<-1>* merge<-1>(Trie<-1>* a,Trie<-1>* b){
if(!a) return b;
if(!b) return a;
delete b;
return a;
}
template<int k>
//<thres and >=thres
std::pair<Trie<k>*,Trie<k>*> split(Trie<k>* a,int thres){
if(a==NULL){
return {NULL,NULL};
}
if(thres<=0) return {NULL,a};
if(thres>=(1<<(k+1))) return {a,NULL};
assert(k>=0);
auto aa=destruct(a);
if(thres<(1<<k)){
Trie<k-1>* l,*r;
std::tie(l,r)=split<k-1>(aa.first,thres);
return std::make_pair(join<k>(l,NULL),join<k>(r,aa.second));
}else if(thres>(1<<k)){
Trie<k-1>* l,*r;
std::tie(l,r)=split<k-1>(aa.second,thres-(1<<k));
return std::make_pair(join<k>(aa.first,l),join<k>(NULL,r));
}else{
return std::make_pair(join<k>(aa.first,NULL),join<k>(NULL,aa.second));
}
}
template<>
std::pair<Trie<-1>*,Trie<-1>*> split<-1>(Trie<-1>* a,int thres){
assert(0);
}
template<int k>
Trie<k>* update(Trie<k>* a,int val){
if(a==NULL) return NULL;
a->push();
assert(val<(1<<(k+1)));
if((val&a->has[0]&a->has[1])==0){
a->lazy^=(val&a->has[0]);
return a;
}
Trie<k-1>* l,*r;
std::tie(l,r)=destruct(a);
l=update<k-1>(l,val&~(1<<k));
r=update<k-1>(r,val&~(1<<k));
if(val&(1<<k)){
return join<k>(NULL,merge<k-1>(l,r));
}else{
return join<k>(l,r);
}
}
template<>
Trie<-1>* update<-1>(Trie<-1>* a,int val){
return a;
}
int main(){
Trie<MAXLOGX-1>* root=NULL;
int N,Q;
scanf("%d %d",&N,&Q);
for(int i=0;i<N;i++){
int A;
scanf("%d",&A);
root=merge(root,create<MAXLOGX-1>(A));
}
for(int i=0;i<Q;i++){
int T,L,R;
scanf("%d %d %d",&T,&L,&R);
Trie<MAXLOGX-1>* left,*right;
std::tie(left,root)=split(root,L);
std::tie(root,right)=split(root,R+1);
if(T==4){
printf("%d\n",root?root->cnt:0);
}else{
int X;
scanf("%d",&X);
if(root!=NULL){
if(T==1){
root->lazy^=((1<<MAXLOGX)-1);
root=update(root,X^((1<<MAXLOGX)-1));
root->lazy^=((1<<MAXLOGX)-1);
}else if(T==2){
root=update(root,X);
}else if(T==3){
assert(X<(1<<MAXLOGX));
root->lazy^=X;
}
}
}
root=merge(root,left);
root=merge(root,right);
}
}
``` |
//因为,已经我不想写代码了啊
#include<stdio.h>
const int maxn=200005,maxk=maxn*40;
int n,q,k,rt,cnt;
int lc[maxk],rc[maxk],lazy[maxk],Vand[maxk],Vor[maxk],res[maxk];
inline void swp(int &a,int &b){
a+=b,b=a-b,a-=b;
}
inline void pushup(int now){
Vand[now]=Vand[lc[now]]|Vand[rc[now]];
Vor[now]=Vor[lc[now]]|Vor[rc[now]];
res[now]=res[lc[now]]+res[rc[now]];
}
void insert(int dep,int &now,int val){
if(now==0)
now=++cnt;
if(dep==0){
Vand[now]=(k^val),Vor[now]=val,res[now]=1;
return ;
}
if(((val>>(dep-1))&1)==0)
insert(dep-1,lc[now],val);
else insert(dep-1,rc[now],val);
pushup(now);
}
void getxor(int dep,int now,int val){
if(now==0)
return ;
if((val>>(dep-1))&1)
swp(lc[now],rc[now]);
int Va=Vand[now],Vo=Vor[now];
Vand[now]=(Va&(k^val))|(Vo&val);
Vor[now]=(Vo&(k^val))|(Va&val);
lazy[now]^=val;
}
void pushdown(int dep,int now){
if(lazy[now]==0)
return ;
getxor(dep-1,lc[now],lazy[now]),getxor(dep-1,rc[now],lazy[now]);
lazy[now]=0;
}
void split(int dep,int &x,int &y,int l,int r,int L,int R){
if(x==0||R<l||r<L){
y=0;
return ;
}
if(L<=l&&r<=R){
y=x,x=0;
return ;
}
int mid=(l+r)>>1;
pushdown(dep,x),y=++cnt;
split(dep-1,lc[x],lc[y],l,mid,L,R),split(dep-1,rc[x],rc[y],mid+1,r,L,R);
pushup(x),pushup(y);
}
void merge(int dep,int &x,int &y){
if(x==0){
x=y;
return ;
}
if(y==0||dep==0)
return ;
pushdown(dep,x),pushdown(dep,y);
merge(dep-1,lc[x],lc[y]),merge(dep-1,rc[x],rc[y]);
pushup(x);
}
void getor(int dep,int now,int val){
if(now==0)
return ;
int add=val&Vand[now];
if((add&Vor[now])==0){
getxor(dep,now,add);
return ;
}
pushdown(dep,now);
if((val>>(dep-1))&1){
//getxor(dep-1,lc[now],1<<(dep-1));
merge(dep-1,rc[now],lc[now]);
lc[now]=0;
}
getor(dep-1,lc[now],val),getor(dep-1,rc[now],val);
pushup(now);
}
int query(int dep,int now,int l,int r,int L,int R){
if(now==0||R<l||r<L)
return 0;
if(L<=l&&r<=R)
return res[now];
int mid=(l+r)>>1;
pushdown(dep,now);
return query(dep-1,lc[now],l,mid,L,R)+query(dep-1,rc[now],mid+1,r,L,R);
}
void read(int &x){
x=0;
char c=getchar();
for(;c<'0'||c>'9';c=getchar());
for(;c>='0'&&c<='9';c=getchar())
x=x*10+c-48;
}
int main(){
scanf("%d%d",&n,&q),k=(1<<20)-1;
for(int i=1;i<=n;i++){
int x;
read(x);
insert(20,rt,x);
}
for(int i=1;i<=q;i++){
int t,x,y,z,now;
read(t);
if(t==4){
read(x),read(y);
printf("%d\n",query(20,rt,0,k,x,y));
continue;
}
read(x),read(y),read(z);
split(20,rt,now,0,k,x,y);
if(t==1)
getxor(20,now,k),getor(20,now,z^k),getxor(20,now,k);
if(t==2)
getor(20,now,z);
if(t==3)
getxor(20,now,z);
merge(20,rt,now);
}
return 0;
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
//因为,已经我不想写代码了啊
#include<stdio.h>
const int maxn=200005,maxk=maxn*40;
int n,q,k,rt,cnt;
int lc[maxk],rc[maxk],lazy[maxk],Vand[maxk],Vor[maxk],res[maxk];
inline void swp(int &a,int &b){
a+=b,b=a-b,a-=b;
}
inline void pushup(int now){
Vand[now]=Vand[lc[now]]|Vand[rc[now]];
Vor[now]=Vor[lc[now]]|Vor[rc[now]];
res[now]=res[lc[now]]+res[rc[now]];
}
void insert(int dep,int &now,int val){
if(now==0)
now=++cnt;
if(dep==0){
Vand[now]=(k^val),Vor[now]=val,res[now]=1;
return ;
}
if(((val>>(dep-1))&1)==0)
insert(dep-1,lc[now],val);
else insert(dep-1,rc[now],val);
pushup(now);
}
void getxor(int dep,int now,int val){
if(now==0)
return ;
if((val>>(dep-1))&1)
swp(lc[now],rc[now]);
int Va=Vand[now],Vo=Vor[now];
Vand[now]=(Va&(k^val))|(Vo&val);
Vor[now]=(Vo&(k^val))|(Va&val);
lazy[now]^=val;
}
void pushdown(int dep,int now){
if(lazy[now]==0)
return ;
getxor(dep-1,lc[now],lazy[now]),getxor(dep-1,rc[now],lazy[now]);
lazy[now]=0;
}
void split(int dep,int &x,int &y,int l,int r,int L,int R){
if(x==0||R<l||r<L){
y=0;
return ;
}
if(L<=l&&r<=R){
y=x,x=0;
return ;
}
int mid=(l+r)>>1;
pushdown(dep,x),y=++cnt;
split(dep-1,lc[x],lc[y],l,mid,L,R),split(dep-1,rc[x],rc[y],mid+1,r,L,R);
pushup(x),pushup(y);
}
void merge(int dep,int &x,int &y){
if(x==0){
x=y;
return ;
}
if(y==0||dep==0)
return ;
pushdown(dep,x),pushdown(dep,y);
merge(dep-1,lc[x],lc[y]),merge(dep-1,rc[x],rc[y]);
pushup(x);
}
void getor(int dep,int now,int val){
if(now==0)
return ;
int add=val&Vand[now];
if((add&Vor[now])==0){
getxor(dep,now,add);
return ;
}
pushdown(dep,now);
if((val>>(dep-1))&1){
//getxor(dep-1,lc[now],1<<(dep-1));
merge(dep-1,rc[now],lc[now]);
lc[now]=0;
}
getor(dep-1,lc[now],val),getor(dep-1,rc[now],val);
pushup(now);
}
int query(int dep,int now,int l,int r,int L,int R){
if(now==0||R<l||r<L)
return 0;
if(L<=l&&r<=R)
return res[now];
int mid=(l+r)>>1;
pushdown(dep,now);
return query(dep-1,lc[now],l,mid,L,R)+query(dep-1,rc[now],mid+1,r,L,R);
}
void read(int &x){
x=0;
char c=getchar();
for(;c<'0'||c>'9';c=getchar());
for(;c>='0'&&c<='9';c=getchar())
x=x*10+c-48;
}
int main(){
scanf("%d%d",&n,&q),k=(1<<20)-1;
for(int i=1;i<=n;i++){
int x;
read(x);
insert(20,rt,x);
}
for(int i=1;i<=q;i++){
int t,x,y,z,now;
read(t);
if(t==4){
read(x),read(y);
printf("%d\n",query(20,rt,0,k,x,y));
continue;
}
read(x),read(y),read(z);
split(20,rt,now,0,k,x,y);
if(t==1)
getxor(20,now,k),getor(20,now,z^k),getxor(20,now,k);
if(t==2)
getor(20,now,z);
if(t==3)
getxor(20,now,z);
merge(20,rt,now);
}
return 0;
}
``` |
#include<stdio.h>
const int maxn=200005,maxk=maxn*40;
int n,q,k,rt,cnt;
int lc[maxk],rc[maxk],lazy[maxk],Vand[maxk],Vor[maxk],res[maxk];
inline void swp(int &a,int &b){
a+=b,b=a-b,a-=b;
}
inline void pushup(int now){
Vand[now]=Vand[lc[now]]|Vand[rc[now]];
Vor[now]=Vor[lc[now]]|Vor[rc[now]];
res[now]=res[lc[now]]+res[rc[now]];
}
void insert(int dep,int &now,int val){
if(now==0)
now=++cnt;
if(dep==0){
Vand[now]=(k^val),Vor[now]=val,res[now]=1;
return ;
}
if(((val>>(dep-1))&1)==0)
insert(dep-1,lc[now],val);
else insert(dep-1,rc[now],val);
pushup(now);
}
void getxor(int dep,int now,int val){
if(now==0)
return ;
if((val>>(dep-1))&1)
swp(lc[now],rc[now]);
int Va=Vand[now],Vo=Vor[now];
Vand[now]=(Va&(k^val))|(Vo&val);
Vor[now]=(Vo&(k^val))|(Va&val);
lazy[now]^=val;
}
void pushdown(int dep,int now){
if(lazy[now]==0)
return ;
getxor(dep-1,lc[now],lazy[now]),getxor(dep-1,rc[now],lazy[now]);
lazy[now]=0;
}
void split(int dep,int &x,int &y,int l,int r,int L,int R){
if(x==0||R<l||r<L){
y=0;
return ;
}
if(L<=l&&r<=R){
y=x,x=0;
return ;
}
int mid=(l+r)>>1;
pushdown(dep,x),y=++cnt;
split(dep-1,lc[x],lc[y],l,mid,L,R),split(dep-1,rc[x],rc[y],mid+1,r,L,R);
pushup(x),pushup(y);
}
void merge(int dep,int &x,int &y){
if(x==0){
x=y;
return ;
}
if(y==0||dep==0)
return ;
pushdown(dep,x),pushdown(dep,y);
merge(dep-1,lc[x],lc[y]),merge(dep-1,rc[x],rc[y]);
pushup(x);
}
void getor(int dep,int now,int val){
if(now==0)
return ;
int add=val&Vand[now];
if((add&Vor[now])==0){
getxor(dep,now,add);
return ;
}
pushdown(dep,now);
if((val>>(dep-1))&1){
//getxor(dep-1,lc[now],1<<(dep-1));
merge(dep-1,rc[now],lc[now]);
lc[now]=0;
}
getor(dep-1,lc[now],val),getor(dep-1,rc[now],val);
pushup(now);
}
int query(int dep,int now,int l,int r,int L,int R){
if(now==0||R<l||r<L)
return 0;
if(L<=l&&r<=R)
return res[now];
int mid=(l+r)>>1;
pushdown(dep,now);
return query(dep-1,lc[now],l,mid,L,R)+query(dep-1,rc[now],mid+1,r,L,R);
}
void read(int &x){
x=0;
char c=getchar();
for(;c<'0'||c>'9';c=getchar());
for(;c>='0'&&c<='9';c=getchar())
x=x*10+c-48;
}
int main(){
scanf("%d%d",&n,&q),k=(1<<20)-1;
for(int i=1;i<=n;i++){
int x;
read(x);
insert(20,rt,x);
}
for(int i=1;i<=q;i++){
int t,x,y,z,now;
read(t);
if(t==4){
read(x),read(y);
printf("%d\n",query(20,rt,0,k,x,y));
continue;
}
read(x),read(y),read(z);
split(20,rt,now,0,k,x,y);
if(t==1)
getxor(20,now,k),getor(20,now,z^k),getxor(20,now,k);
if(t==2)
getor(20,now,z);
if(t==3)
getxor(20,now,z);
merge(20,rt,now);
}
return 0;
} | ### Prompt
Generate a CPP solution to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include<stdio.h>
const int maxn=200005,maxk=maxn*40;
int n,q,k,rt,cnt;
int lc[maxk],rc[maxk],lazy[maxk],Vand[maxk],Vor[maxk],res[maxk];
inline void swp(int &a,int &b){
a+=b,b=a-b,a-=b;
}
inline void pushup(int now){
Vand[now]=Vand[lc[now]]|Vand[rc[now]];
Vor[now]=Vor[lc[now]]|Vor[rc[now]];
res[now]=res[lc[now]]+res[rc[now]];
}
void insert(int dep,int &now,int val){
if(now==0)
now=++cnt;
if(dep==0){
Vand[now]=(k^val),Vor[now]=val,res[now]=1;
return ;
}
if(((val>>(dep-1))&1)==0)
insert(dep-1,lc[now],val);
else insert(dep-1,rc[now],val);
pushup(now);
}
void getxor(int dep,int now,int val){
if(now==0)
return ;
if((val>>(dep-1))&1)
swp(lc[now],rc[now]);
int Va=Vand[now],Vo=Vor[now];
Vand[now]=(Va&(k^val))|(Vo&val);
Vor[now]=(Vo&(k^val))|(Va&val);
lazy[now]^=val;
}
void pushdown(int dep,int now){
if(lazy[now]==0)
return ;
getxor(dep-1,lc[now],lazy[now]),getxor(dep-1,rc[now],lazy[now]);
lazy[now]=0;
}
void split(int dep,int &x,int &y,int l,int r,int L,int R){
if(x==0||R<l||r<L){
y=0;
return ;
}
if(L<=l&&r<=R){
y=x,x=0;
return ;
}
int mid=(l+r)>>1;
pushdown(dep,x),y=++cnt;
split(dep-1,lc[x],lc[y],l,mid,L,R),split(dep-1,rc[x],rc[y],mid+1,r,L,R);
pushup(x),pushup(y);
}
void merge(int dep,int &x,int &y){
if(x==0){
x=y;
return ;
}
if(y==0||dep==0)
return ;
pushdown(dep,x),pushdown(dep,y);
merge(dep-1,lc[x],lc[y]),merge(dep-1,rc[x],rc[y]);
pushup(x);
}
void getor(int dep,int now,int val){
if(now==0)
return ;
int add=val&Vand[now];
if((add&Vor[now])==0){
getxor(dep,now,add);
return ;
}
pushdown(dep,now);
if((val>>(dep-1))&1){
//getxor(dep-1,lc[now],1<<(dep-1));
merge(dep-1,rc[now],lc[now]);
lc[now]=0;
}
getor(dep-1,lc[now],val),getor(dep-1,rc[now],val);
pushup(now);
}
int query(int dep,int now,int l,int r,int L,int R){
if(now==0||R<l||r<L)
return 0;
if(L<=l&&r<=R)
return res[now];
int mid=(l+r)>>1;
pushdown(dep,now);
return query(dep-1,lc[now],l,mid,L,R)+query(dep-1,rc[now],mid+1,r,L,R);
}
void read(int &x){
x=0;
char c=getchar();
for(;c<'0'||c>'9';c=getchar());
for(;c>='0'&&c<='9';c=getchar())
x=x*10+c-48;
}
int main(){
scanf("%d%d",&n,&q),k=(1<<20)-1;
for(int i=1;i<=n;i++){
int x;
read(x);
insert(20,rt,x);
}
for(int i=1;i<=q;i++){
int t,x,y,z,now;
read(t);
if(t==4){
read(x),read(y);
printf("%d\n",query(20,rt,0,k,x,y));
continue;
}
read(x),read(y),read(z);
split(20,rt,now,0,k,x,y);
if(t==1)
getxor(20,now,k),getor(20,now,z^k),getxor(20,now,k);
if(t==2)
getor(20,now,z);
if(t==3)
getxor(20,now,z);
merge(20,rt,now);
}
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
const int M = 2111111, all = (1<<20)-1;
template<typename T>
void read(T &x){
int ch = getchar(); x = 0; bool f = false;
for(;ch < '0' || ch > '9';ch = getchar()) f |= ch == '-';
for(;ch >= '0' && ch <= '9';ch = getchar()) x = x * 10 + ch - '0';
if(f) x = -x;
}
int n, q, rt, cnt, stk[M], tp, ch[M][2], seg[M][2], siz[M], tag[M];
int crep(){
int x = tp ? stk[tp--] : ++cnt;
ch[x][0] = ch[x][1] = seg[x][0] = seg[x][1] = tag[x] = siz[x] = 0;
return x;
}
void pup(int x){
siz[x] = siz[ch[x][0]] + siz[ch[x][1]];
for(int _ = 0;_ < 2;++ _)
seg[x][_] = seg[ch[x][0]][_] | seg[ch[x][1]][_];
}
void ptag(int x, int val, int d = 19){
if(val >> d & 1) swap(ch[x][0], ch[x][1]);
int _0 = (seg[x][0] & ~val) | (seg[x][1] & val), _1 = (seg[x][0] & val) | (seg[x][1] & ~val);
seg[x][0] = _0; seg[x][1] = _1; tag[x] ^= val;
}
void pdown(int x, int d){
if(tag[x]){ptag(ch[x][0], tag[x], d-1); ptag(ch[x][1], tag[x], d-1); tag[x] = 0;}
}
void ins(int val, int &x = rt, int d = 19){
if(!x) x = crep();
if(!~d){siz[x] = 1; seg[x][0] = all ^ val; seg[x][1] = val; return;}
ins(val, ch[x][val>>d&1], d-1); pup(x);
}
int qry(int ql, int qr, int x = rt, int L = 0, int R = all, int d = 19){
if(!x) return 0;
if(ql <= L && R <= qr) return siz[x];
int mid = L+R>>1, ans = 0; pdown(x, d);
if(ql <= mid) ans = qry(ql, qr, ch[x][0], L, mid, d-1);
if(mid < qr) ans += qry(ql, qr, ch[x][1], mid+1, R, d-1);
return ans;
}
void split(int &x, int &y, int ql, int qr, int L = 0, int R = all, int d = 19){
if(!x) return;
if(ql <= L && R <= qr){y = x; x = 0; return;}
int mid = L+R>>1; y = crep(); pdown(x, d);
if(ql <= mid) split(ch[x][0], ch[y][0], ql, qr, L, mid, d-1);
if(mid < qr) split(ch[x][1], ch[y][1], ql, qr, mid+1, R, d-1);
pup(x); pup(y);
}
void merge(int &x, int y, int d = 19){
if(!x || !y){x |= y; return;}
if(d >= 0){
pdown(x, d); pdown(y, d);
merge(ch[x][0], ch[y][0], d-1);
merge(ch[x][1], ch[y][1], d-1);
pup(x);
} stk[++tp] = y;
}
void chan(int x, int val, int d = 19){
if(!x) return;
if(!(val & seg[x][0] & seg[x][1])){
ptag(x, val & seg[x][0], d); return;
} pdown(x, d);
if(val >> d & 1){
ptag(ch[x][0], 1<<d, d-1);
merge(ch[x][1], ch[x][0], d-1);
ch[x][0] = 0;
} else chan(ch[x][0], val, d-1);
chan(ch[x][1], val, d-1); pup(x);
}
int main(){
read(n); read(q);
while(n --){int x; read(x); ins(x);}
while(q --){
int op, l, r, x, u; read(op); read(l); read(r);
if(op == 4) printf("%d\n", qry(l, r));
else {
read(x); split(rt, u, l, r);
if(op == 1){
ptag(u, all); chan(u, x ^ all); ptag(u, all);
} else if(op == 2) chan(u, x); else ptag(u, x);
merge(rt, u);
}
}
} | ### Prompt
Please create a solution in cpp to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
const int M = 2111111, all = (1<<20)-1;
template<typename T>
void read(T &x){
int ch = getchar(); x = 0; bool f = false;
for(;ch < '0' || ch > '9';ch = getchar()) f |= ch == '-';
for(;ch >= '0' && ch <= '9';ch = getchar()) x = x * 10 + ch - '0';
if(f) x = -x;
}
int n, q, rt, cnt, stk[M], tp, ch[M][2], seg[M][2], siz[M], tag[M];
int crep(){
int x = tp ? stk[tp--] : ++cnt;
ch[x][0] = ch[x][1] = seg[x][0] = seg[x][1] = tag[x] = siz[x] = 0;
return x;
}
void pup(int x){
siz[x] = siz[ch[x][0]] + siz[ch[x][1]];
for(int _ = 0;_ < 2;++ _)
seg[x][_] = seg[ch[x][0]][_] | seg[ch[x][1]][_];
}
void ptag(int x, int val, int d = 19){
if(val >> d & 1) swap(ch[x][0], ch[x][1]);
int _0 = (seg[x][0] & ~val) | (seg[x][1] & val), _1 = (seg[x][0] & val) | (seg[x][1] & ~val);
seg[x][0] = _0; seg[x][1] = _1; tag[x] ^= val;
}
void pdown(int x, int d){
if(tag[x]){ptag(ch[x][0], tag[x], d-1); ptag(ch[x][1], tag[x], d-1); tag[x] = 0;}
}
void ins(int val, int &x = rt, int d = 19){
if(!x) x = crep();
if(!~d){siz[x] = 1; seg[x][0] = all ^ val; seg[x][1] = val; return;}
ins(val, ch[x][val>>d&1], d-1); pup(x);
}
int qry(int ql, int qr, int x = rt, int L = 0, int R = all, int d = 19){
if(!x) return 0;
if(ql <= L && R <= qr) return siz[x];
int mid = L+R>>1, ans = 0; pdown(x, d);
if(ql <= mid) ans = qry(ql, qr, ch[x][0], L, mid, d-1);
if(mid < qr) ans += qry(ql, qr, ch[x][1], mid+1, R, d-1);
return ans;
}
void split(int &x, int &y, int ql, int qr, int L = 0, int R = all, int d = 19){
if(!x) return;
if(ql <= L && R <= qr){y = x; x = 0; return;}
int mid = L+R>>1; y = crep(); pdown(x, d);
if(ql <= mid) split(ch[x][0], ch[y][0], ql, qr, L, mid, d-1);
if(mid < qr) split(ch[x][1], ch[y][1], ql, qr, mid+1, R, d-1);
pup(x); pup(y);
}
void merge(int &x, int y, int d = 19){
if(!x || !y){x |= y; return;}
if(d >= 0){
pdown(x, d); pdown(y, d);
merge(ch[x][0], ch[y][0], d-1);
merge(ch[x][1], ch[y][1], d-1);
pup(x);
} stk[++tp] = y;
}
void chan(int x, int val, int d = 19){
if(!x) return;
if(!(val & seg[x][0] & seg[x][1])){
ptag(x, val & seg[x][0], d); return;
} pdown(x, d);
if(val >> d & 1){
ptag(ch[x][0], 1<<d, d-1);
merge(ch[x][1], ch[x][0], d-1);
ch[x][0] = 0;
} else chan(ch[x][0], val, d-1);
chan(ch[x][1], val, d-1); pup(x);
}
int main(){
read(n); read(q);
while(n --){int x; read(x); ins(x);}
while(q --){
int op, l, r, x, u; read(op); read(l); read(r);
if(op == 4) printf("%d\n", qry(l, r));
else {
read(x); split(rt, u, l, r);
if(op == 1){
ptag(u, all); chan(u, x ^ all); ptag(u, all);
} else if(op == 2) chan(u, x); else ptag(u, x);
merge(rt, u);
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
struct Node {
int cnt;
int type;
int tag;
int both;
int d;
Node *lc, *rc;
};
Node *null = new Node();
Node *newNode(int d) {
Node *t = new Node();
*t = *null;
t->d = d;
return t;
}
Node *merge(Node *, Node *);
void maintain(Node *);
void pull(Node *t) {
if (t->d == 0) {
return;
} else {
t->cnt = t->lc->cnt + t->rc->cnt;
t->both = t->lc->both | t->rc->both;
if (t->lc != null && t->rc != null) {
t->both |= 1 << t->d - 1;
}
maintain(t);
}
}
void apply(Node *t, int type, int tag) {
if (t == null) {
return;
} else if (t->d == 0) {
return;
} else {
t->type |= type;
t->tag |= tag & type;
t->tag &= tag | ~type;
t->tag ^= tag & ~type;
maintain(t);
}
}
void push(Node *t) {
if (t->type >> t->d - 1 & 1) {
if (t->tag >> t->d - 1 & 1) {
t->rc = merge(t->lc, t->rc);
t->lc = null;
apply(t->rc, t->type, t->tag);
} else {
t->lc = merge(t->lc, t->rc);
t->rc = null;
apply(t->lc, t->type, t->tag);
}
} else {
if (t->tag >> t->d - 1 & 1) {
swap(t->lc, t->rc);
}
apply(t->lc, t->type, t->tag);
apply(t->rc, t->type, t->tag);
}
t->type = t->tag = 0;
}
void maintain(Node *t) {
if (t == null) {
return;
} else if (!(t->both & t->type)) {
return;
} else {
push(t);
maintain(t->lc);
maintain(t->rc);
pull(t);
}
}
Node *merge(Node *a, Node *b) {
if (a == null) {
return b;
}
if (b == null) {
return a;
}
if (a->d == 0) {
a->cnt = a->cnt | b->cnt;
delete b;
return a;
}
push(a);
push(b);
a->lc = merge(a->lc, b->lc);
a->rc = merge(a->rc, b->rc);
delete b;
pull(a);
return a;
}
void add(Node *&t, int v, int d) {
if (t == null) {
t = newNode(d);
}
if (d == 0) {
t->cnt = 1;
} else {
if (~v >> d - 1 & 1) {
add(t->lc, v, d - 1);
} else {
add(t->rc, v, d - 1);
}
pull(t);
}
}
vector<pair<int, Node*>> nodes;
void modify1(Node *&t, int L, int R, int l, int r, int x, int type) {
if (L >= r || R <= l) {
return;
}
if (t == null) {
return;
}
if (L >= l && R <= r) {
Node *backUp = t;
t = null;
if (type == 1) {
apply(backUp, ~x, 0);
nodes.emplace_back(L & x, backUp);
} else if (type == 2) {
apply(backUp, x, x);
nodes.emplace_back(L | x, backUp);
} else {
apply(backUp, 0, x);
nodes.emplace_back(L ^ x, backUp);
}
return;
}
int m = (L + R) / 2;
push(t);
modify1(t->lc, L, m, l, r, x, type);
modify1(t->rc, m, R, l, r, x, type);
pull(t);
}
void modify2(Node *&t, int l, int r, int d) {
if (t == null) {
t = newNode(d);
}
while (l < r && nodes[l].second->d == d) {
t = merge(t, nodes[l].second);
l++;
}
if (l == r) {
return;
}
int mid = l;
while (mid < r && (~nodes[mid].first >> d - 1 & 1)) {
mid++;
}
push(t);
modify2(t->lc, l, mid, d - 1);
modify2(t->rc, mid, r, d - 1);
pull(t);
}
int query(Node *t, int L, int R, int l, int r) {
if (t == null) {
return 0;
}
if (L >= r || R <= l) {
return 0;
}
if (L >= l && R <= r) {
return t->cnt;
}
int m = (L + R) / 2;
push(t);
int leftCnt = query(t->lc, L, m, l, r);
int rightCnt = query(t->rc, m, R, l, r);
return leftCnt + rightCnt;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
null->cnt = null->type = null->tag = null->both = 0;
null->lc = null->rc = null;
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
Node *root = null;
for (int i = 0; i < n; i++) {
add(root, a[i], 20);
}
while (q--) {
int op, l, r;
cin >> op >> l >> r;
r++;
if (op < 4) {
int x;
cin >> x;
modify1(root, 0, 1 << 20, l, r, x, op);
sort(nodes.begin(), nodes.end(), [&](const pair<int, Node*> &a, const pair<int, Node*> &b) {
int va = a.first & ~((1 << a.second->d) - 1);
int vb = b.first & ~((1 << b.second->d) - 1);
if (va != vb) {
return va < vb;
} else {
return a.second->d > b.second->d;
}
});
modify2(root, 0, nodes.size(), 20);
nodes.clear();
} else {
cout << query(root, 0, 1 << 20, l, r) << "\n";
}
}
} | ### Prompt
Your task is to create a Cpp solution to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct Node {
int cnt;
int type;
int tag;
int both;
int d;
Node *lc, *rc;
};
Node *null = new Node();
Node *newNode(int d) {
Node *t = new Node();
*t = *null;
t->d = d;
return t;
}
Node *merge(Node *, Node *);
void maintain(Node *);
void pull(Node *t) {
if (t->d == 0) {
return;
} else {
t->cnt = t->lc->cnt + t->rc->cnt;
t->both = t->lc->both | t->rc->both;
if (t->lc != null && t->rc != null) {
t->both |= 1 << t->d - 1;
}
maintain(t);
}
}
void apply(Node *t, int type, int tag) {
if (t == null) {
return;
} else if (t->d == 0) {
return;
} else {
t->type |= type;
t->tag |= tag & type;
t->tag &= tag | ~type;
t->tag ^= tag & ~type;
maintain(t);
}
}
void push(Node *t) {
if (t->type >> t->d - 1 & 1) {
if (t->tag >> t->d - 1 & 1) {
t->rc = merge(t->lc, t->rc);
t->lc = null;
apply(t->rc, t->type, t->tag);
} else {
t->lc = merge(t->lc, t->rc);
t->rc = null;
apply(t->lc, t->type, t->tag);
}
} else {
if (t->tag >> t->d - 1 & 1) {
swap(t->lc, t->rc);
}
apply(t->lc, t->type, t->tag);
apply(t->rc, t->type, t->tag);
}
t->type = t->tag = 0;
}
void maintain(Node *t) {
if (t == null) {
return;
} else if (!(t->both & t->type)) {
return;
} else {
push(t);
maintain(t->lc);
maintain(t->rc);
pull(t);
}
}
Node *merge(Node *a, Node *b) {
if (a == null) {
return b;
}
if (b == null) {
return a;
}
if (a->d == 0) {
a->cnt = a->cnt | b->cnt;
delete b;
return a;
}
push(a);
push(b);
a->lc = merge(a->lc, b->lc);
a->rc = merge(a->rc, b->rc);
delete b;
pull(a);
return a;
}
void add(Node *&t, int v, int d) {
if (t == null) {
t = newNode(d);
}
if (d == 0) {
t->cnt = 1;
} else {
if (~v >> d - 1 & 1) {
add(t->lc, v, d - 1);
} else {
add(t->rc, v, d - 1);
}
pull(t);
}
}
vector<pair<int, Node*>> nodes;
void modify1(Node *&t, int L, int R, int l, int r, int x, int type) {
if (L >= r || R <= l) {
return;
}
if (t == null) {
return;
}
if (L >= l && R <= r) {
Node *backUp = t;
t = null;
if (type == 1) {
apply(backUp, ~x, 0);
nodes.emplace_back(L & x, backUp);
} else if (type == 2) {
apply(backUp, x, x);
nodes.emplace_back(L | x, backUp);
} else {
apply(backUp, 0, x);
nodes.emplace_back(L ^ x, backUp);
}
return;
}
int m = (L + R) / 2;
push(t);
modify1(t->lc, L, m, l, r, x, type);
modify1(t->rc, m, R, l, r, x, type);
pull(t);
}
void modify2(Node *&t, int l, int r, int d) {
if (t == null) {
t = newNode(d);
}
while (l < r && nodes[l].second->d == d) {
t = merge(t, nodes[l].second);
l++;
}
if (l == r) {
return;
}
int mid = l;
while (mid < r && (~nodes[mid].first >> d - 1 & 1)) {
mid++;
}
push(t);
modify2(t->lc, l, mid, d - 1);
modify2(t->rc, mid, r, d - 1);
pull(t);
}
int query(Node *t, int L, int R, int l, int r) {
if (t == null) {
return 0;
}
if (L >= r || R <= l) {
return 0;
}
if (L >= l && R <= r) {
return t->cnt;
}
int m = (L + R) / 2;
push(t);
int leftCnt = query(t->lc, L, m, l, r);
int rightCnt = query(t->rc, m, R, l, r);
return leftCnt + rightCnt;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
null->cnt = null->type = null->tag = null->both = 0;
null->lc = null->rc = null;
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
Node *root = null;
for (int i = 0; i < n; i++) {
add(root, a[i], 20);
}
while (q--) {
int op, l, r;
cin >> op >> l >> r;
r++;
if (op < 4) {
int x;
cin >> x;
modify1(root, 0, 1 << 20, l, r, x, op);
sort(nodes.begin(), nodes.end(), [&](const pair<int, Node*> &a, const pair<int, Node*> &b) {
int va = a.first & ~((1 << a.second->d) - 1);
int vb = b.first & ~((1 << b.second->d) - 1);
if (va != vb) {
return va < vb;
} else {
return a.second->d > b.second->d;
}
});
modify2(root, 0, nodes.size(), 20);
nodes.clear();
} else {
cout << query(root, 0, 1 << 20, l, r) << "\n";
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7 + 5, K = 20, W = 1 << K;
int n, q, tot;
int ls[N], rs[N], sum[N], or0[N], or1[N], tag[N], dep[N];
void pushup(int p) {
sum[p] = sum[ls[p]] + sum[rs[p]];
or0[p] = or0[ls[p]] | or0[rs[p]];
or1[p] = or1[ls[p]] | or1[rs[p]];
}
void Tag(int p, int k) {
if (!p) return ;
tag[p] ^= k;
if (dep[p] != -1 && k >> dep[p] & 1) swap(ls[p], rs[p]);
int tmp0 = (or0[p] & (~k)) | (or1[p] & k);
int tmp1 = (or1[p] & (~k)) | (or0[p] & k);
or0[p] = tmp0, or1[p] = tmp1;
}
void pushdown(int p) {
if (!tag[p]) return ;
Tag(ls[p], tag[p]), Tag(rs[p], tag[p]);
tag[p] = 0;
}
void insert(int &p, int k, int d) {
if (!p) p = ++tot;
dep[p] = d;
if (d == -1) {
or1[p] = k, or0[p] = k ^ (W - 1), sum[p] = 1;
return ;
}
insert((k >> d & 1) ? rs[p] : ls[p], k, d - 1);
pushup(p);
}
void split(int &p, int &q, int l, int r, int x, int y) {
if (!p || y < l || x > r) return q = 0, void();
if (x <= l && r <= y) {
q = p, p = 0;
return ;
}
int mid = (l + r) >> 1;
pushdown(p), q = ++tot;
dep[q] = dep[p];
split(ls[p], ls[q], l, mid, x, y);
split(rs[p], rs[q], mid + 1, r, x, y);
pushup(p), pushup(q);
}
void merge(int &p, int q) {
if (!p || !q) return p = p + q, void();
pushdown(p), pushdown(q);
merge(ls[p], ls[q]);
merge(rs[p], rs[q]);
if (dep[p] != -1) pushup(p);
}
void modify(int p, int k) {
if (!p) return ;
if ((k & or0[p] & or1[p]) == 0) {
return Tag(p, k & or0[p]), void();
}
pushdown(p);
if (dep[p] != -1 && k >> dep[p] & 1) {
Tag(ls[p], 1 << dep[p]);
merge(rs[p], ls[p]), ls[p] = 0;
}
modify(ls[p], k), modify(rs[p], k);
pushup(p);
}
int main() {
scanf("%d%d", &n, &q);
int x = 0;
for (int i = 1; i <= n; i++) {
int v; scanf("%d", &v);
insert(x, v, K - 1);
}
while (q--) {
int t, l, r, y, k;
scanf("%d%d%d", &t, &l, &r);
split(x, y, 0, W - 1, l, r);
if (t == 1) {
scanf("%d", &k);
Tag(y, W - 1);
modify(y, k ^ (W - 1));
Tag(y, W - 1);
} else if (t == 2) {
scanf("%d", &k);
modify(y, k);
} else if (t == 3) {
scanf("%d", &k);
Tag(y, k);
} else {
printf("%d\n", sum[y]);
}
merge(x, y);
}
return 0;
} | ### Prompt
Create a solution in Cpp for the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7 + 5, K = 20, W = 1 << K;
int n, q, tot;
int ls[N], rs[N], sum[N], or0[N], or1[N], tag[N], dep[N];
void pushup(int p) {
sum[p] = sum[ls[p]] + sum[rs[p]];
or0[p] = or0[ls[p]] | or0[rs[p]];
or1[p] = or1[ls[p]] | or1[rs[p]];
}
void Tag(int p, int k) {
if (!p) return ;
tag[p] ^= k;
if (dep[p] != -1 && k >> dep[p] & 1) swap(ls[p], rs[p]);
int tmp0 = (or0[p] & (~k)) | (or1[p] & k);
int tmp1 = (or1[p] & (~k)) | (or0[p] & k);
or0[p] = tmp0, or1[p] = tmp1;
}
void pushdown(int p) {
if (!tag[p]) return ;
Tag(ls[p], tag[p]), Tag(rs[p], tag[p]);
tag[p] = 0;
}
void insert(int &p, int k, int d) {
if (!p) p = ++tot;
dep[p] = d;
if (d == -1) {
or1[p] = k, or0[p] = k ^ (W - 1), sum[p] = 1;
return ;
}
insert((k >> d & 1) ? rs[p] : ls[p], k, d - 1);
pushup(p);
}
void split(int &p, int &q, int l, int r, int x, int y) {
if (!p || y < l || x > r) return q = 0, void();
if (x <= l && r <= y) {
q = p, p = 0;
return ;
}
int mid = (l + r) >> 1;
pushdown(p), q = ++tot;
dep[q] = dep[p];
split(ls[p], ls[q], l, mid, x, y);
split(rs[p], rs[q], mid + 1, r, x, y);
pushup(p), pushup(q);
}
void merge(int &p, int q) {
if (!p || !q) return p = p + q, void();
pushdown(p), pushdown(q);
merge(ls[p], ls[q]);
merge(rs[p], rs[q]);
if (dep[p] != -1) pushup(p);
}
void modify(int p, int k) {
if (!p) return ;
if ((k & or0[p] & or1[p]) == 0) {
return Tag(p, k & or0[p]), void();
}
pushdown(p);
if (dep[p] != -1 && k >> dep[p] & 1) {
Tag(ls[p], 1 << dep[p]);
merge(rs[p], ls[p]), ls[p] = 0;
}
modify(ls[p], k), modify(rs[p], k);
pushup(p);
}
int main() {
scanf("%d%d", &n, &q);
int x = 0;
for (int i = 1; i <= n; i++) {
int v; scanf("%d", &v);
insert(x, v, K - 1);
}
while (q--) {
int t, l, r, y, k;
scanf("%d%d%d", &t, &l, &r);
split(x, y, 0, W - 1, l, r);
if (t == 1) {
scanf("%d", &k);
Tag(y, W - 1);
modify(y, k ^ (W - 1));
Tag(y, W - 1);
} else if (t == 2) {
scanf("%d", &k);
modify(y, k);
} else if (t == 3) {
scanf("%d", &k);
Tag(y, k);
} else {
printf("%d\n", sum[y]);
}
merge(x, y);
}
return 0;
}
``` |
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <list>
#include <cassert>
#include <climits>
#include <bitset>
#include <chrono>
#include <random>
using namespace std;
#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n) FORE(i,0,n)
#define FORSZ(i,a,v) FOR(i,a,SZ(v))
#define REPSZ(i,v) REP(i,SZ(v))
std::mt19937 rnd((int)std::chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 200000;
const int MAXQ = 100000;
const int NBIT = 20;
struct Node {
int prefix, bit;
int lazyset, lazyclear, lazyflip;
int cntleaf, bothmask;
int ch[2];
Node() { lazyset = lazyclear = lazyflip = 0; ch[0] = ch[1] = -1; }
};
int n, nq;
int a[MAXN];
int qkind[MAXQ], ql[MAXQ], qr[MAXQ], qval[MAXQ];
int qans[MAXQ];
vector<Node> nodes;
queue<int> pool;
void print(int x,int v=-1) {
REP(i, NBIT - 1 - nodes[x].bit) printf(" ");
printf("%d:", x); if (v != -1) printf("%d", v); printf("[pref=%d cntleaf=%d", nodes[x].prefix, nodes[x].cntleaf);
if (nodes[x].lazyset != 0) printf(" lazyset=%x", nodes[x].lazyset);
if (nodes[x].lazyclear != 0) printf(" lazyclear=%x", nodes[x].lazyclear);
if (nodes[x].lazyflip != 0) printf(" lazyflip=%x", nodes[x].lazyflip);
printf("]\n");
REP(v, 2) if (nodes[x].ch[v] != -1) assert(nodes[nodes[x].ch[v]].bit == nodes[x].bit - 1), print(nodes[x].ch[v], v);
}
void update(int x) {
nodes[x].cntleaf = (nodes[x].bit == -1 ? 1 : 0) + (nodes[x].ch[0] != -1 ? nodes[nodes[x].ch[0]].cntleaf : 0) + (nodes[x].ch[1] != -1 ? nodes[nodes[x].ch[1]].cntleaf : 0);
nodes[x].bothmask = (nodes[x].ch[0] != -1 && nodes[x].ch[1] != -1 ? (1 << nodes[x].bit) : 0) | (nodes[x].ch[0] != -1 ? nodes[nodes[x].ch[0]].bothmask : 0) | (nodes[x].ch[1] != -1 ? nodes[nodes[x].ch[1]].bothmask : 0);
}
int createnode() {
if (!pool.empty()) { int ret = pool.front(); pool.pop(); nodes[ret] = Node(); return ret; }
int ret = SZ(nodes); nodes.PB(Node()); return ret;
}
int createnode(int prefix, int bit) {
int ret = createnode();
nodes[ret].prefix = prefix, nodes[ret].bit = bit;
update(ret);
return ret;
}
void releasenode(int x) {
pool.push(x);
}
void applyset(int x, int val) {
nodes[x].prefix |= val & ~((1 << (nodes[x].bit + 1)) - 1);
assert((nodes[x].bothmask & val) == 0);
if (nodes[x].bit == -1) return;
if (val & (1 << nodes[x].bit)) {
if (nodes[x].ch[0] != -1) { assert(nodes[x].ch[1] == -1); swap(nodes[x].ch[0], nodes[x].ch[1]); }
}
nodes[x].lazyset |= val;
nodes[x].lazyclear &= ~val;
nodes[x].lazyflip &= ~val;
}
void applyclear(int x, int val) {
nodes[x].prefix &= ~(val & ~((1 << (nodes[x].bit + 1)) - 1));
assert((nodes[x].bothmask & val) == 0);
if (nodes[x].bit == -1) return;
if (val & (1 << nodes[x].bit)) {
if (nodes[x].ch[1] != -1) { assert(nodes[x].ch[0] == -1); swap(nodes[x].ch[0], nodes[x].ch[1]); }
}
nodes[x].lazyclear |= val;
nodes[x].lazyset &= ~val;
nodes[x].lazyflip &= ~val;
}
void applyflip(int x, int val) {
nodes[x].prefix ^= val & ~((1 << (nodes[x].bit + 1)) - 1);
if (nodes[x].bit == -1) return;
if (val & (1 << nodes[x].bit)) {
swap(nodes[x].ch[0], nodes[x].ch[1]);
}
nodes[x].lazyflip ^= val;
}
void push(int x) {
if (nodes[x].lazyset != 0) { REP(v, 2) if (nodes[x].ch[v] != -1) applyset(nodes[x].ch[v], nodes[x].lazyset); nodes[x].lazyset = 0; }
if (nodes[x].lazyclear != 0) { REP(v, 2) if (nodes[x].ch[v] != -1) applyclear(nodes[x].ch[v], nodes[x].lazyclear); nodes[x].lazyclear = 0; }
if (nodes[x].lazyflip != 0) { REP(v, 2) if (nodes[x].ch[v] != -1) applyflip(nodes[x].ch[v], nodes[x].lazyflip); nodes[x].lazyflip = 0; }
}
void merge(int x, int y) {
//printf("merge(%d,%d)\n", nodes[x].bit, nodes[y].bit);
//print(x);
//print(y);
push(x); push(y);
assert(nodes[x].bit == nodes[y].bit && nodes[x].prefix == nodes[y].prefix);
REP(v, 2) {
int xx = nodes[x].ch[v], yy = nodes[y].ch[v];
if (yy == -1) continue;
if (xx == -1) { nodes[x].ch[v] = yy; continue; }
merge(xx, yy);
}
update(x);
releasenode(y);
}
void insert(int x, int y) {
//printf("insert(%d,%d)\n", nodes[x].bit, nodes[y].bit);
assert(nodes[x].bit >= nodes[y].bit);
push(x); push(y);
if (nodes[x].bit == nodes[y].bit) {
merge(x, y);
} else {
int v = (nodes[y].prefix >> nodes[x].bit) & 1;
if (nodes[x].ch[v] == -1) {
int z = createnode(nodes[x].prefix | (v << nodes[x].bit), nodes[x].bit - 1);
nodes[x].ch[v] = z;
}
assert(nodes[nodes[x].ch[v]].bit == nodes[x].bit - 1);
insert(nodes[x].ch[v], y);
update(x);
}
}
bool extract(int x, int l, int r, int L, int R, vector<int>& lst) { // [l,r) and [L,R)
push(x);
if (L <= l && r <= R) {
lst.PB(x);
return true;
} else {
int m = l + (r - l) / 2;
if (nodes[x].ch[0] != -1 && L < m) {
bool rem = extract(nodes[x].ch[0], l, m, L, R, lst);
if (rem) nodes[x].ch[0] = -1;
}
if (nodes[x].ch[1] != -1 && m < R) {
bool rem = extract(nodes[x].ch[1], m, r, L, R, lst);
if (rem) nodes[x].ch[1] = -1;
}
if (nodes[x].ch[0] == -1 && nodes[x].ch[1] == -1) {
releasenode(x);
return true;
}
update(x);
return false;
}
}
void resolveclear(int x, int val) {
nodes[x].prefix &= ~(val & ~((1 << (nodes[x].bit + 1)) - 1));
if (nodes[x].bit == -1) return;
if ((nodes[x].bothmask & val) == 0) { applyclear(x, val); return; }
push(x);
if (val & (1 << nodes[x].bit)) {
if (nodes[x].ch[1] != -1) {
applyclear(nodes[x].ch[1], 1 << nodes[x].bit);
if (nodes[x].ch[0] == -1) swap(nodes[x].ch[0], nodes[x].ch[1]);
else { merge(nodes[x].ch[0], nodes[x].ch[1]); nodes[x].ch[1] = -1; }
}
}
REP(v, 2) if (nodes[x].ch[v] != -1) resolveclear(nodes[x].ch[v], val);
update(x);
}
void resolveset(int x, int val) {
nodes[x].prefix |= val & ~((1 << (nodes[x].bit + 1)) - 1);
if (nodes[x].bit == -1) return;
if ((nodes[x].bothmask & val) == 0) { applyset(x, val); return; }
push(x);
if (val & (1 << nodes[x].bit)) {
if (nodes[x].ch[0] != -1) {
applyset(nodes[x].ch[0], 1 << nodes[x].bit);
if (nodes[x].ch[1] == -1) swap(nodes[x].ch[0], nodes[x].ch[1]);
else { merge(nodes[x].ch[1], nodes[x].ch[0]); nodes[x].ch[0] = -1; }
}
}
REP(v, 2) if (nodes[x].ch[v] != -1) resolveset(nodes[x].ch[v], val);
update(x);
}
void resolveflip(int x, int val) {
applyflip(x, val);
}
void solve() {
sort(a, a + n);
n = unique(a, a + n) - a;
nodes.clear();
pool = queue<int>();
int root = createnode(0, NBIT - 1);
REP(i, n) insert(root, createnode(a[i], -1));
REP(i, nq) {
//printf("q%d\n", i);
//print(root);
vector<int> lst;
bool newroot = extract(root, 0, (1 << NBIT), ql[i], qr[i] + 1, lst);
//printf("SZ(lst)=%d\n", SZ(lst));
//for (int x : lst) print(x);
//if (!newroot) printf("root\n"), print(root), printf("done\n");
if (qkind[i] == 1) {
for (int x : lst) {
resolveclear(x, (1 << NBIT) - 1 - qval[i]);
}
}
if (qkind[i] == 2) {
for (int x : lst) {
resolveset(x, qval[i]);
}
}
if (qkind[i] == 3) {
for (int x : lst) {
resolveflip(x, qval[i]);
}
}
//if (qkind[i] != 4) for (int x : lst) print(x);
if (qkind[i] == 4) {
qans[i] = 0;
for (int x : lst) qans[i] += nodes[x].cntleaf;
}
if (newroot) root = createnode(0, NBIT - 1);
for (int x : lst) insert(root, x);
}
}
void run() {
scanf("%d%d", &n, &nq);
REP(i, n) scanf("%d", &a[i]);
REP(i, nq) {
scanf("%d%d%d", &qkind[i], &ql[i], &qr[i]);
if (qkind[i] != 4) scanf("%d", &qval[i]);
}
solve();
REP(i, nq) if (qkind[i] == 4) printf("%d\n", qans[i]);
}
int qansstupid[MAXQ];
void solvestupid() {
set<int> cur;
REP(i, n) cur.insert(a[i]);
REP(i, nq) {
int l = ql[i], r = qr[i];
auto it = cur.lower_bound(l);
qansstupid[i] = 0;
vector<int> rem;
while (it != cur.end() && *it <= r) {
++qansstupid[i];
rem.PB(*it);
cur.erase(it++);
}
for (int x : rem) {
if (qkind[i] == 1) x = x&qval[i];
if (qkind[i] == 2) x = x | qval[i];
if (qkind[i] == 3) x = x^qval[i];
cur.insert(x);
}
}
}
void stress() {
int mxbit = 20, mxn = 1000, mxq = 1000;
//int mxbit = 4, mxn = 10, mxq = 5;
REP(rep, 10000) {
int nbit = rnd() % mxbit + 1;
n = rnd() % min(mxn, 1 << nbit) + 1;
set<int> seen;
REP(i, n) while (true) { int x = rnd() % (1 << nbit); if (seen.count(x)) continue; seen.insert(x); a[i] = x; break; }
nq = rnd() % mxq + 1;
REP(i, nq) {
qkind[i] = rnd() % 4 + 1;
ql[i] = rnd() % (1 << nbit), qr[i] = rnd() % (1 << nbit); if (ql[i] > qr[i]) swap(ql[i], qr[i]);
if (qkind[i] != 4) qval[i] = rnd() % (1 << nbit);
}
//printf("%d %d\n", n, nq); REP(i, n) { if (i != 0) printf(" "); printf("%d", a[i]); } puts(""); REP(i, nq) { printf("%d %d %d", qkind[i], ql[i], qr[i]); if (qkind[i] != 4) printf(" %d", qval[i]); puts(""); }
solve();
solvestupid();
bool ok = true; REP(i, nq) if (qkind[i] == 4 && qans[i] != qansstupid[i]) ok = false;
if (ok) { printf("."); continue; }
printf("err\n");
}
}
int main() {
run();
//stress();
return 0;
}
| ### Prompt
Please formulate a CPP solution to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <list>
#include <cassert>
#include <climits>
#include <bitset>
#include <chrono>
#include <random>
using namespace std;
#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n) FORE(i,0,n)
#define FORSZ(i,a,v) FOR(i,a,SZ(v))
#define REPSZ(i,v) REP(i,SZ(v))
std::mt19937 rnd((int)std::chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 200000;
const int MAXQ = 100000;
const int NBIT = 20;
struct Node {
int prefix, bit;
int lazyset, lazyclear, lazyflip;
int cntleaf, bothmask;
int ch[2];
Node() { lazyset = lazyclear = lazyflip = 0; ch[0] = ch[1] = -1; }
};
int n, nq;
int a[MAXN];
int qkind[MAXQ], ql[MAXQ], qr[MAXQ], qval[MAXQ];
int qans[MAXQ];
vector<Node> nodes;
queue<int> pool;
void print(int x,int v=-1) {
REP(i, NBIT - 1 - nodes[x].bit) printf(" ");
printf("%d:", x); if (v != -1) printf("%d", v); printf("[pref=%d cntleaf=%d", nodes[x].prefix, nodes[x].cntleaf);
if (nodes[x].lazyset != 0) printf(" lazyset=%x", nodes[x].lazyset);
if (nodes[x].lazyclear != 0) printf(" lazyclear=%x", nodes[x].lazyclear);
if (nodes[x].lazyflip != 0) printf(" lazyflip=%x", nodes[x].lazyflip);
printf("]\n");
REP(v, 2) if (nodes[x].ch[v] != -1) assert(nodes[nodes[x].ch[v]].bit == nodes[x].bit - 1), print(nodes[x].ch[v], v);
}
void update(int x) {
nodes[x].cntleaf = (nodes[x].bit == -1 ? 1 : 0) + (nodes[x].ch[0] != -1 ? nodes[nodes[x].ch[0]].cntleaf : 0) + (nodes[x].ch[1] != -1 ? nodes[nodes[x].ch[1]].cntleaf : 0);
nodes[x].bothmask = (nodes[x].ch[0] != -1 && nodes[x].ch[1] != -1 ? (1 << nodes[x].bit) : 0) | (nodes[x].ch[0] != -1 ? nodes[nodes[x].ch[0]].bothmask : 0) | (nodes[x].ch[1] != -1 ? nodes[nodes[x].ch[1]].bothmask : 0);
}
int createnode() {
if (!pool.empty()) { int ret = pool.front(); pool.pop(); nodes[ret] = Node(); return ret; }
int ret = SZ(nodes); nodes.PB(Node()); return ret;
}
int createnode(int prefix, int bit) {
int ret = createnode();
nodes[ret].prefix = prefix, nodes[ret].bit = bit;
update(ret);
return ret;
}
void releasenode(int x) {
pool.push(x);
}
void applyset(int x, int val) {
nodes[x].prefix |= val & ~((1 << (nodes[x].bit + 1)) - 1);
assert((nodes[x].bothmask & val) == 0);
if (nodes[x].bit == -1) return;
if (val & (1 << nodes[x].bit)) {
if (nodes[x].ch[0] != -1) { assert(nodes[x].ch[1] == -1); swap(nodes[x].ch[0], nodes[x].ch[1]); }
}
nodes[x].lazyset |= val;
nodes[x].lazyclear &= ~val;
nodes[x].lazyflip &= ~val;
}
void applyclear(int x, int val) {
nodes[x].prefix &= ~(val & ~((1 << (nodes[x].bit + 1)) - 1));
assert((nodes[x].bothmask & val) == 0);
if (nodes[x].bit == -1) return;
if (val & (1 << nodes[x].bit)) {
if (nodes[x].ch[1] != -1) { assert(nodes[x].ch[0] == -1); swap(nodes[x].ch[0], nodes[x].ch[1]); }
}
nodes[x].lazyclear |= val;
nodes[x].lazyset &= ~val;
nodes[x].lazyflip &= ~val;
}
void applyflip(int x, int val) {
nodes[x].prefix ^= val & ~((1 << (nodes[x].bit + 1)) - 1);
if (nodes[x].bit == -1) return;
if (val & (1 << nodes[x].bit)) {
swap(nodes[x].ch[0], nodes[x].ch[1]);
}
nodes[x].lazyflip ^= val;
}
void push(int x) {
if (nodes[x].lazyset != 0) { REP(v, 2) if (nodes[x].ch[v] != -1) applyset(nodes[x].ch[v], nodes[x].lazyset); nodes[x].lazyset = 0; }
if (nodes[x].lazyclear != 0) { REP(v, 2) if (nodes[x].ch[v] != -1) applyclear(nodes[x].ch[v], nodes[x].lazyclear); nodes[x].lazyclear = 0; }
if (nodes[x].lazyflip != 0) { REP(v, 2) if (nodes[x].ch[v] != -1) applyflip(nodes[x].ch[v], nodes[x].lazyflip); nodes[x].lazyflip = 0; }
}
void merge(int x, int y) {
//printf("merge(%d,%d)\n", nodes[x].bit, nodes[y].bit);
//print(x);
//print(y);
push(x); push(y);
assert(nodes[x].bit == nodes[y].bit && nodes[x].prefix == nodes[y].prefix);
REP(v, 2) {
int xx = nodes[x].ch[v], yy = nodes[y].ch[v];
if (yy == -1) continue;
if (xx == -1) { nodes[x].ch[v] = yy; continue; }
merge(xx, yy);
}
update(x);
releasenode(y);
}
void insert(int x, int y) {
//printf("insert(%d,%d)\n", nodes[x].bit, nodes[y].bit);
assert(nodes[x].bit >= nodes[y].bit);
push(x); push(y);
if (nodes[x].bit == nodes[y].bit) {
merge(x, y);
} else {
int v = (nodes[y].prefix >> nodes[x].bit) & 1;
if (nodes[x].ch[v] == -1) {
int z = createnode(nodes[x].prefix | (v << nodes[x].bit), nodes[x].bit - 1);
nodes[x].ch[v] = z;
}
assert(nodes[nodes[x].ch[v]].bit == nodes[x].bit - 1);
insert(nodes[x].ch[v], y);
update(x);
}
}
bool extract(int x, int l, int r, int L, int R, vector<int>& lst) { // [l,r) and [L,R)
push(x);
if (L <= l && r <= R) {
lst.PB(x);
return true;
} else {
int m = l + (r - l) / 2;
if (nodes[x].ch[0] != -1 && L < m) {
bool rem = extract(nodes[x].ch[0], l, m, L, R, lst);
if (rem) nodes[x].ch[0] = -1;
}
if (nodes[x].ch[1] != -1 && m < R) {
bool rem = extract(nodes[x].ch[1], m, r, L, R, lst);
if (rem) nodes[x].ch[1] = -1;
}
if (nodes[x].ch[0] == -1 && nodes[x].ch[1] == -1) {
releasenode(x);
return true;
}
update(x);
return false;
}
}
void resolveclear(int x, int val) {
nodes[x].prefix &= ~(val & ~((1 << (nodes[x].bit + 1)) - 1));
if (nodes[x].bit == -1) return;
if ((nodes[x].bothmask & val) == 0) { applyclear(x, val); return; }
push(x);
if (val & (1 << nodes[x].bit)) {
if (nodes[x].ch[1] != -1) {
applyclear(nodes[x].ch[1], 1 << nodes[x].bit);
if (nodes[x].ch[0] == -1) swap(nodes[x].ch[0], nodes[x].ch[1]);
else { merge(nodes[x].ch[0], nodes[x].ch[1]); nodes[x].ch[1] = -1; }
}
}
REP(v, 2) if (nodes[x].ch[v] != -1) resolveclear(nodes[x].ch[v], val);
update(x);
}
void resolveset(int x, int val) {
nodes[x].prefix |= val & ~((1 << (nodes[x].bit + 1)) - 1);
if (nodes[x].bit == -1) return;
if ((nodes[x].bothmask & val) == 0) { applyset(x, val); return; }
push(x);
if (val & (1 << nodes[x].bit)) {
if (nodes[x].ch[0] != -1) {
applyset(nodes[x].ch[0], 1 << nodes[x].bit);
if (nodes[x].ch[1] == -1) swap(nodes[x].ch[0], nodes[x].ch[1]);
else { merge(nodes[x].ch[1], nodes[x].ch[0]); nodes[x].ch[0] = -1; }
}
}
REP(v, 2) if (nodes[x].ch[v] != -1) resolveset(nodes[x].ch[v], val);
update(x);
}
void resolveflip(int x, int val) {
applyflip(x, val);
}
void solve() {
sort(a, a + n);
n = unique(a, a + n) - a;
nodes.clear();
pool = queue<int>();
int root = createnode(0, NBIT - 1);
REP(i, n) insert(root, createnode(a[i], -1));
REP(i, nq) {
//printf("q%d\n", i);
//print(root);
vector<int> lst;
bool newroot = extract(root, 0, (1 << NBIT), ql[i], qr[i] + 1, lst);
//printf("SZ(lst)=%d\n", SZ(lst));
//for (int x : lst) print(x);
//if (!newroot) printf("root\n"), print(root), printf("done\n");
if (qkind[i] == 1) {
for (int x : lst) {
resolveclear(x, (1 << NBIT) - 1 - qval[i]);
}
}
if (qkind[i] == 2) {
for (int x : lst) {
resolveset(x, qval[i]);
}
}
if (qkind[i] == 3) {
for (int x : lst) {
resolveflip(x, qval[i]);
}
}
//if (qkind[i] != 4) for (int x : lst) print(x);
if (qkind[i] == 4) {
qans[i] = 0;
for (int x : lst) qans[i] += nodes[x].cntleaf;
}
if (newroot) root = createnode(0, NBIT - 1);
for (int x : lst) insert(root, x);
}
}
void run() {
scanf("%d%d", &n, &nq);
REP(i, n) scanf("%d", &a[i]);
REP(i, nq) {
scanf("%d%d%d", &qkind[i], &ql[i], &qr[i]);
if (qkind[i] != 4) scanf("%d", &qval[i]);
}
solve();
REP(i, nq) if (qkind[i] == 4) printf("%d\n", qans[i]);
}
int qansstupid[MAXQ];
void solvestupid() {
set<int> cur;
REP(i, n) cur.insert(a[i]);
REP(i, nq) {
int l = ql[i], r = qr[i];
auto it = cur.lower_bound(l);
qansstupid[i] = 0;
vector<int> rem;
while (it != cur.end() && *it <= r) {
++qansstupid[i];
rem.PB(*it);
cur.erase(it++);
}
for (int x : rem) {
if (qkind[i] == 1) x = x&qval[i];
if (qkind[i] == 2) x = x | qval[i];
if (qkind[i] == 3) x = x^qval[i];
cur.insert(x);
}
}
}
void stress() {
int mxbit = 20, mxn = 1000, mxq = 1000;
//int mxbit = 4, mxn = 10, mxq = 5;
REP(rep, 10000) {
int nbit = rnd() % mxbit + 1;
n = rnd() % min(mxn, 1 << nbit) + 1;
set<int> seen;
REP(i, n) while (true) { int x = rnd() % (1 << nbit); if (seen.count(x)) continue; seen.insert(x); a[i] = x; break; }
nq = rnd() % mxq + 1;
REP(i, nq) {
qkind[i] = rnd() % 4 + 1;
ql[i] = rnd() % (1 << nbit), qr[i] = rnd() % (1 << nbit); if (ql[i] > qr[i]) swap(ql[i], qr[i]);
if (qkind[i] != 4) qval[i] = rnd() % (1 << nbit);
}
//printf("%d %d\n", n, nq); REP(i, n) { if (i != 0) printf(" "); printf("%d", a[i]); } puts(""); REP(i, nq) { printf("%d %d %d", qkind[i], ql[i], qr[i]); if (qkind[i] != 4) printf(" %d", qval[i]); puts(""); }
solve();
solvestupid();
bool ok = true; REP(i, nq) if (qkind[i] == 4 && qans[i] != qansstupid[i]) ok = false;
if (ok) { printf("."); continue; }
printf("err\n");
}
}
int main() {
run();
//stress();
return 0;
}
``` |
#include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); i++)
#define R(i, j, k) for(int i = (j); i >= (k); i--)
#define ll long long
#define sz(a) a.size()
#define vi vector<int>
using namespace std;
const int N = 2e5 + 7, M = N * 80, rk = 20, mx = (1 << rk) - 1;
int n, q, a[N];
int ch[M][2], to0[M], to1[M], only[M], v[M], tot;
int Nw() {
++tot, only[tot] = mx, to0[tot] = 0, to1[tot] = mx, v[tot] = 0;
return tot;
}
void upd(int x, int l) {
only[x] = (only[ch[x][0]] & only[ch[x][1]]), v[x] = v[ch[x][0]] + v[ch[x][1]];
if(ch[x][0] && ch[x][1]) only[x] ^= (only[x] & (1 << l));
}
void mk(int x, int t0, int t1) {
if(x)
to0[x] = (t0 & (to0[x] ^ mx)) | (t1 & to0[x]),
to1[x] = (t0 & (to1[x] ^ mx)) | (t1 & to1[x]);
}
void push(int x, int l) {
int c0 = ch[x][0], c1 = ch[x][1];
ch[x][0] = ch[x][1] = 0;
if(c0) ch[x][to0[x] >> l & 1] = c0;
if(c1) ch[x][to1[x] >> l & 1] = c1;
to0[x] = (to0[x] | (1 << l)) ^ (1 << l), to1[x] = to1[x] | (1 << l);
mk(ch[x][0], to0[x], to1[x]), mk(ch[x][1], to0[x], to1[x]), to0[x] = 0, to1[x] = mx;
}
void split(int now, int &x, int &y, int k, int l = rk - 1) {
if(!now || l < 0) return x = now, y = 0, void();
push(now, l);
if(k >> l & 1) x = now, y = Nw(), split(ch[x][1], ch[x][1], ch[y][1], k, l - 1);
else y = now, x = Nw(), split(ch[y][0], ch[x][0], ch[y][0], k, l - 1);
upd(x, l), upd(y, l);
}
void ins (int &rt, int k, int l = rk - 1) {
if(!rt) rt = Nw();
if(l < 0) return v[rt] = 1, void();
ins(ch[rt][k >> l & 1], k, l - 1), upd(rt, l);
}
int merge(int x, int y, int l = rk - 1) {
if(!x || !y) return x | y;
if(l == -1) return x;
push(x, l), push(y, l);
ch[x][0] = merge(ch[x][0], ch[y][0], l - 1);
ch[x][1] = merge(ch[x][1], ch[y][1], l - 1);
upd(x, l);
return x;
}
void AND(int now, int k, int l = rk - 1) {
if(!now || l < 0) return;
if((only[now] & (mx ^ k)) == (mx ^ k)) return to0[now] &= k, to1[now] &= k, void();
push(now, l);
if(! (k >> l & 1)) ch[now][0] = merge(ch[now][0], ch[now][1], l - 1), ch[now][1] = 0;
AND(ch[now][0], k, l - 1), AND(ch[now][1], k, l - 1), upd(now, l);
}
void OR(int now, int k, int l = rk - 1) {
if(!now || l < 0) return;
if((only[now] & k) == k) return to0[now] |= k, to1[now] |= k, void();
push(now, l);
if(k >> l & 1) ch[now][1] = merge(ch[now][0], ch[now][1], l - 1), ch[now][0] = 0;
OR(ch[now][0], k, l - 1), OR(ch[now][1], k, l - 1), upd(now, l);
}
void dfs(int now, int l = rk - 1) {
if(!now || l < 0) return;
push(now, l), dfs(ch[now][0], l - 1), dfs(ch[now][1], l - 1), upd(now, l);
}
int rt;
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
only[0] = mx, cin >> n >> q;
L(i, 1, n) cin >> a[i], ins(rt, a[i]);
while(q--) {
int op, l, r, x, ox, now, oy;
cin >> op >> l >> r;
if(!l) ox = 0, oy = rt;
else split(rt, ox, oy, l - 1); //, cout << v[ox] << " and " << v[oy] << "\n";
split(oy, now, oy, r);
// cout << v[ox] << " " << v[now] << " " << v[oy] << "\n";
if(op == 4)
cout << v[now] << "\n";
else {
cin >> x;
if(op == 1) AND(now, x); //, dfs(now);
if(op == 2) OR(now, x);
if(op == 3) to0[now] ^= x, to1[now] ^= x;
}
// cout << "v = " << v[now] << "\n";
rt = merge(merge(ox, now), oy);
// cout << " rtv = " << v[rt] << "\n";
}
return 0;
} | ### Prompt
Please provide a cpp coded solution to the problem described below:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); i++)
#define R(i, j, k) for(int i = (j); i >= (k); i--)
#define ll long long
#define sz(a) a.size()
#define vi vector<int>
using namespace std;
const int N = 2e5 + 7, M = N * 80, rk = 20, mx = (1 << rk) - 1;
int n, q, a[N];
int ch[M][2], to0[M], to1[M], only[M], v[M], tot;
int Nw() {
++tot, only[tot] = mx, to0[tot] = 0, to1[tot] = mx, v[tot] = 0;
return tot;
}
void upd(int x, int l) {
only[x] = (only[ch[x][0]] & only[ch[x][1]]), v[x] = v[ch[x][0]] + v[ch[x][1]];
if(ch[x][0] && ch[x][1]) only[x] ^= (only[x] & (1 << l));
}
void mk(int x, int t0, int t1) {
if(x)
to0[x] = (t0 & (to0[x] ^ mx)) | (t1 & to0[x]),
to1[x] = (t0 & (to1[x] ^ mx)) | (t1 & to1[x]);
}
void push(int x, int l) {
int c0 = ch[x][0], c1 = ch[x][1];
ch[x][0] = ch[x][1] = 0;
if(c0) ch[x][to0[x] >> l & 1] = c0;
if(c1) ch[x][to1[x] >> l & 1] = c1;
to0[x] = (to0[x] | (1 << l)) ^ (1 << l), to1[x] = to1[x] | (1 << l);
mk(ch[x][0], to0[x], to1[x]), mk(ch[x][1], to0[x], to1[x]), to0[x] = 0, to1[x] = mx;
}
void split(int now, int &x, int &y, int k, int l = rk - 1) {
if(!now || l < 0) return x = now, y = 0, void();
push(now, l);
if(k >> l & 1) x = now, y = Nw(), split(ch[x][1], ch[x][1], ch[y][1], k, l - 1);
else y = now, x = Nw(), split(ch[y][0], ch[x][0], ch[y][0], k, l - 1);
upd(x, l), upd(y, l);
}
void ins (int &rt, int k, int l = rk - 1) {
if(!rt) rt = Nw();
if(l < 0) return v[rt] = 1, void();
ins(ch[rt][k >> l & 1], k, l - 1), upd(rt, l);
}
int merge(int x, int y, int l = rk - 1) {
if(!x || !y) return x | y;
if(l == -1) return x;
push(x, l), push(y, l);
ch[x][0] = merge(ch[x][0], ch[y][0], l - 1);
ch[x][1] = merge(ch[x][1], ch[y][1], l - 1);
upd(x, l);
return x;
}
void AND(int now, int k, int l = rk - 1) {
if(!now || l < 0) return;
if((only[now] & (mx ^ k)) == (mx ^ k)) return to0[now] &= k, to1[now] &= k, void();
push(now, l);
if(! (k >> l & 1)) ch[now][0] = merge(ch[now][0], ch[now][1], l - 1), ch[now][1] = 0;
AND(ch[now][0], k, l - 1), AND(ch[now][1], k, l - 1), upd(now, l);
}
void OR(int now, int k, int l = rk - 1) {
if(!now || l < 0) return;
if((only[now] & k) == k) return to0[now] |= k, to1[now] |= k, void();
push(now, l);
if(k >> l & 1) ch[now][1] = merge(ch[now][0], ch[now][1], l - 1), ch[now][0] = 0;
OR(ch[now][0], k, l - 1), OR(ch[now][1], k, l - 1), upd(now, l);
}
void dfs(int now, int l = rk - 1) {
if(!now || l < 0) return;
push(now, l), dfs(ch[now][0], l - 1), dfs(ch[now][1], l - 1), upd(now, l);
}
int rt;
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
only[0] = mx, cin >> n >> q;
L(i, 1, n) cin >> a[i], ins(rt, a[i]);
while(q--) {
int op, l, r, x, ox, now, oy;
cin >> op >> l >> r;
if(!l) ox = 0, oy = rt;
else split(rt, ox, oy, l - 1); //, cout << v[ox] << " and " << v[oy] << "\n";
split(oy, now, oy, r);
// cout << v[ox] << " " << v[now] << " " << v[oy] << "\n";
if(op == 4)
cout << v[now] << "\n";
else {
cin >> x;
if(op == 1) AND(now, x); //, dfs(now);
if(op == 2) OR(now, x);
if(op == 3) to0[now] ^= x, to1[now] ^= x;
}
// cout << "v = " << v[now] << "\n";
rt = merge(merge(ox, now), oy);
// cout << " rtv = " << v[rt] << "\n";
}
return 0;
}
``` |
#include <cstdio>
#include <cassert>
#include <algorithm>
int n,q;
int rt,cnt;
int ch[2][8000001],size[8000001],diff[8000001],val[8000001],lazy[8000001];
bool vis[8000001];
void pushup(int x){
if(vis[x])return;
if(!size[ch[0][x]]){
size[x]=size[ch[1][x]];
diff[x]=diff[ch[1][x]];
val[x]=val[ch[1][x]];
}
else if(!size[ch[1][x]]){
size[x]=size[ch[0][x]];
diff[x]=diff[ch[0][x]];
val[x]=val[ch[0][x]];
}
else{
size[x]=size[ch[0][x]]+size[ch[1][x]];
diff[x]=diff[ch[0][x]]|diff[ch[1][x]]|(val[ch[0][x]]^val[ch[1][x]]);
val[x]=val[ch[0][x]]&val[ch[1][x]];
}
}
void pushdown(int x,int y,int dep){
if(~dep&&(y&(1<<dep)))std::swap(ch[0][x],ch[1][x]);
val[x]=val[x]^y^(diff[x]&y);
lazy[x]^=y;
}
void spread(int x,int dep){
if(lazy[x]){
pushdown(ch[0][x],lazy[x],dep-1);
pushdown(ch[1][x],lazy[x],dep-1);
lazy[x]=0;
}
}
void add(int &pos,int dep,int v){
if(!pos)pos=++cnt;
if(!~dep){
diff[pos]=0;
val[pos]=v;
size[pos]=1;
vis[pos]=1;
return;
}
add(ch[((v&(1<<dep))!=0)][pos],dep-1,v);
pushup(pos);
}
void split(int now,int &x,int &y,int v,int dep){
if(v<0){
x=0,y=now;
return;
}
spread(now,dep);
if(!now)x=y=0;
else if(!~dep)x=now,y=0;
else{
if(!(v&(1<<dep))){
y=now;
x=++cnt;
split(ch[0][now],ch[0][x],ch[0][y],v,dep-1);
pushup(x);
pushup(y);
}
else{
x=now;
y=++cnt;
split(ch[1][now],ch[1][x],ch[1][y],v,dep-1);
pushup(x);
pushup(y);
}
}
}
int merge(int a,int b,int dep){
spread(a,dep),spread(b,dep);
if(!a||!b)return a|b;
if(!~dep)return a;
ch[0][a]=merge(ch[0][a],ch[0][b],dep-1);
ch[1][a]=merge(ch[1][a],ch[1][b],dep-1);
pushup(a);
return a;
}
int solve(int a,int dep,int val){
spread(a,dep);
if(!a)return a;
if(!~dep)return a;
int x=diff[a]&val;
if(!x){
pushdown(a,val^(val&::val[a]),dep);
return a;
}
if(val&(1<<dep)){
pushdown(ch[0][a],(1<<dep),dep-1);
ch[1][a]=merge(ch[0][a],ch[1][a],dep-1);
ch[0][a]=0;
}
solve(ch[0][a],dep-1,val);
solve(ch[1][a],dep-1,val);
pushup(a);
return a;
}
void debug(int x,int dep){
if(!x||!size[x])return;
spread(x,dep);
if(!~dep){
printf("%d ",val[x]);
return;
}
debug(ch[0][x],dep-1);
debug(ch[1][x],dep-1);
}
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
scanf("%d%d",&n,&q);
for(int i=1,tem;i<=n;++i){
scanf("%d",&tem);
add(rt,19,tem);
}
for(int i=1,t,l,r,x;i<=q;++i){
scanf("%d%d%d",&t,&l,&r);
int t1,t2,t3;
// debug(rt,19);putchar('\n');
split(rt,t1,t2,l-1,19);
split(t2,t2,t3,r,19);
rt=merge(t1,t3,19);
// debug(t2,19);putchar('\n');
if(t<=3)scanf("%d",&x);
if(t==1){
x^=1048575;
pushdown(t2,1048575,19);
solve(t2,19,x);
for(int i=0;i<20;++i)
if(x&(1<<i)&&!(val[t2]&(1<<i)))
pushdown(t2,1<<i,19);
pushdown(t2,1048575,19);
}
else if(t==2){
solve(t2,19,x);
// debug(t2,19);putchar('\n');
}
else if(t==3){
pushdown(t2,x,19);
}
else{
printf("%d\n",size[t2]);
}
rt=merge(rt,t2,19);
// debug(rt,19);putchar('\n');
// putchar('\n');
}
} | ### Prompt
Generate a Cpp solution to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <cstdio>
#include <cassert>
#include <algorithm>
int n,q;
int rt,cnt;
int ch[2][8000001],size[8000001],diff[8000001],val[8000001],lazy[8000001];
bool vis[8000001];
void pushup(int x){
if(vis[x])return;
if(!size[ch[0][x]]){
size[x]=size[ch[1][x]];
diff[x]=diff[ch[1][x]];
val[x]=val[ch[1][x]];
}
else if(!size[ch[1][x]]){
size[x]=size[ch[0][x]];
diff[x]=diff[ch[0][x]];
val[x]=val[ch[0][x]];
}
else{
size[x]=size[ch[0][x]]+size[ch[1][x]];
diff[x]=diff[ch[0][x]]|diff[ch[1][x]]|(val[ch[0][x]]^val[ch[1][x]]);
val[x]=val[ch[0][x]]&val[ch[1][x]];
}
}
void pushdown(int x,int y,int dep){
if(~dep&&(y&(1<<dep)))std::swap(ch[0][x],ch[1][x]);
val[x]=val[x]^y^(diff[x]&y);
lazy[x]^=y;
}
void spread(int x,int dep){
if(lazy[x]){
pushdown(ch[0][x],lazy[x],dep-1);
pushdown(ch[1][x],lazy[x],dep-1);
lazy[x]=0;
}
}
void add(int &pos,int dep,int v){
if(!pos)pos=++cnt;
if(!~dep){
diff[pos]=0;
val[pos]=v;
size[pos]=1;
vis[pos]=1;
return;
}
add(ch[((v&(1<<dep))!=0)][pos],dep-1,v);
pushup(pos);
}
void split(int now,int &x,int &y,int v,int dep){
if(v<0){
x=0,y=now;
return;
}
spread(now,dep);
if(!now)x=y=0;
else if(!~dep)x=now,y=0;
else{
if(!(v&(1<<dep))){
y=now;
x=++cnt;
split(ch[0][now],ch[0][x],ch[0][y],v,dep-1);
pushup(x);
pushup(y);
}
else{
x=now;
y=++cnt;
split(ch[1][now],ch[1][x],ch[1][y],v,dep-1);
pushup(x);
pushup(y);
}
}
}
int merge(int a,int b,int dep){
spread(a,dep),spread(b,dep);
if(!a||!b)return a|b;
if(!~dep)return a;
ch[0][a]=merge(ch[0][a],ch[0][b],dep-1);
ch[1][a]=merge(ch[1][a],ch[1][b],dep-1);
pushup(a);
return a;
}
int solve(int a,int dep,int val){
spread(a,dep);
if(!a)return a;
if(!~dep)return a;
int x=diff[a]&val;
if(!x){
pushdown(a,val^(val&::val[a]),dep);
return a;
}
if(val&(1<<dep)){
pushdown(ch[0][a],(1<<dep),dep-1);
ch[1][a]=merge(ch[0][a],ch[1][a],dep-1);
ch[0][a]=0;
}
solve(ch[0][a],dep-1,val);
solve(ch[1][a],dep-1,val);
pushup(a);
return a;
}
void debug(int x,int dep){
if(!x||!size[x])return;
spread(x,dep);
if(!~dep){
printf("%d ",val[x]);
return;
}
debug(ch[0][x],dep-1);
debug(ch[1][x],dep-1);
}
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
scanf("%d%d",&n,&q);
for(int i=1,tem;i<=n;++i){
scanf("%d",&tem);
add(rt,19,tem);
}
for(int i=1,t,l,r,x;i<=q;++i){
scanf("%d%d%d",&t,&l,&r);
int t1,t2,t3;
// debug(rt,19);putchar('\n');
split(rt,t1,t2,l-1,19);
split(t2,t2,t3,r,19);
rt=merge(t1,t3,19);
// debug(t2,19);putchar('\n');
if(t<=3)scanf("%d",&x);
if(t==1){
x^=1048575;
pushdown(t2,1048575,19);
solve(t2,19,x);
for(int i=0;i<20;++i)
if(x&(1<<i)&&!(val[t2]&(1<<i)))
pushdown(t2,1<<i,19);
pushdown(t2,1048575,19);
}
else if(t==2){
solve(t2,19,x);
// debug(t2,19);putchar('\n');
}
else if(t==3){
pushdown(t2,x,19);
}
else{
printf("%d\n",size[t2]);
}
rt=merge(rt,t2,19);
// debug(rt,19);putchar('\n');
// putchar('\n');
}
}
``` |
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1048576,C=20;
int Ri(){
int x=0;
char c=getchar();
for (;c<'0'||c>'9';c=getchar());
for (;c<='9'&&c>='0';c=getchar()) x=x*10+c-'0';
return x;
}
int n,cq;
struct tree{
int s[2],cnt,all[2],rev;
}tr[N*2+9];
int sta[N*2+9],cst;
int rot,cn;
void Pushup(int k,int d){
int x=tr[k].s[0],y=tr[k].s[1];
tr[k].cnt=tr[x].cnt+tr[y].cnt;
tr[k].all[0]=tr[x].all[0]&tr[y].all[0];
tr[k].all[1]=tr[x].all[1]&tr[y].all[1];
tr[k].all[0]|=!y<<d;
tr[k].all[1]|=!x<<d;
}
void Update_rev(int k,int d,int x){
if (x>>d&1) swap(tr[k].s[0],tr[k].s[1]);
int u=tr[k].all[0]&x,v=tr[k].all[1]&x;
tr[k].all[0]+=v-u;
tr[k].all[1]+=u-v;
tr[k].rev^=x&(1<<d)-1;
}
void Pushdown(int k,int d){
if (d<=0||!tr[k].rev) return;
if (tr[k].s[0]) Update_rev(tr[k].s[0],d-1,tr[k].rev);
if (tr[k].s[1]) Update_rev(tr[k].s[1],d-1,tr[k].rev);
tr[k].rev=0;
}
void Change_ins(int p,int d,int &k){
if (!k) k=++cn;
if (d==-1) {tr[k].cnt=1;return;}
Pushdown(k,d);
Change_ins(p,d-1,tr[k].s[p>>d&1]);
Pushup(k,d);
}
int Merge(int d,int x,int y){
if (!x||!y) return x+y;
if (d==-1) {tr[x].cnt|=tr[y].cnt;return x;}
Pushdown(x,d);
Pushdown(y,d);
tr[x].s[0]=Merge(d-1,tr[x].s[0],tr[y].s[0]);
tr[x].s[1]=Merge(d-1,tr[x].s[1],tr[y].s[1]);
sta[++cst]=y;
Pushup(x,d);
return x;
}
void Split(int L,int R,int l,int r,int d,int &x,int &y){
if (!x) {x=y=0;return;}
if (L<=l&&R>=r) {y=x;x=0;return;}
int mid=l+r>>1;
Pushdown(x,d);
tr[y=cst?sta[cst--]:++cn]=tree();
if (L<=mid) Split(L,R,l,mid,d-1,tr[x].s[0],tr[y].s[0]);
if (R>mid) Split(L,R,mid+1,r,d-1,tr[x].s[1],tr[y].s[1]);
if (x) Pushup(x,d);
if (y) Pushup(y,d);
}
void Change_or(int x,int d,int k){
if (!x||!k||d==-1) return;
if ((x&(tr[k].all[0]|tr[k].all[1]))==x){
Update_rev(k,d,x&tr[k].all[0]);
return;
}
Pushdown(k,d);
if (x>>d&1){
tr[k].s[1]=Merge(d-1,tr[k].s[0],tr[k].s[1]);
tr[k].s[0]=0;
}
x&=(1<<d)-1;
Change_or(x,d-1,tr[k].s[0]);
Change_or(x,d-1,tr[k].s[1]);
Pushup(k,d);
}
int Query_cnt(int L,int R,int l,int r,int d,int k){
if (!k) return 0;
if (L<=l&&R>=r) return tr[k].cnt;
int mid=l+r>>1,res=0;
Pushdown(k,d);
if (L<=mid) res+=Query_cnt(L,R,l,mid,d-1,tr[k].s[0]);
if (R>mid) res+=Query_cnt(L,R,mid+1,r,d-1,tr[k].s[1]);
return res;
}
int flag;
void into(){
tr[0].all[0]=tr[0].all[1]=N-1;
n=Ri();cq=Ri();
for (int i=1;i<=n;++i){
int x=Ri();
Change_ins(x,C-1,rot);
}
}
void work(){
}
void outo(){
for (;cq--;){
int opt,l,r;
opt=Ri();l=Ri();r=Ri();
if (opt<=3){
int x=Ri(),k=0;
Split(l,r,0,N-1,C-1,rot,k);
switch (opt){
case 1:
Update_rev(k,C-1,N-1);
Change_or(x^N-1,C-1,k);
Update_rev(k,C-1,N-1);
break;
case 2:Change_or(x,C-1,k);break;
case 3:Update_rev(k,C-1,x);break;
}
rot=Merge(C-1,rot,k);
}else printf("%d\n",Query_cnt(l,r,0,N-1,C-1,rot));
}
}
int main(){
into();
work();
outo();
return 0;
} | ### Prompt
Construct a cpp code solution to the problem outlined:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1048576,C=20;
int Ri(){
int x=0;
char c=getchar();
for (;c<'0'||c>'9';c=getchar());
for (;c<='9'&&c>='0';c=getchar()) x=x*10+c-'0';
return x;
}
int n,cq;
struct tree{
int s[2],cnt,all[2],rev;
}tr[N*2+9];
int sta[N*2+9],cst;
int rot,cn;
void Pushup(int k,int d){
int x=tr[k].s[0],y=tr[k].s[1];
tr[k].cnt=tr[x].cnt+tr[y].cnt;
tr[k].all[0]=tr[x].all[0]&tr[y].all[0];
tr[k].all[1]=tr[x].all[1]&tr[y].all[1];
tr[k].all[0]|=!y<<d;
tr[k].all[1]|=!x<<d;
}
void Update_rev(int k,int d,int x){
if (x>>d&1) swap(tr[k].s[0],tr[k].s[1]);
int u=tr[k].all[0]&x,v=tr[k].all[1]&x;
tr[k].all[0]+=v-u;
tr[k].all[1]+=u-v;
tr[k].rev^=x&(1<<d)-1;
}
void Pushdown(int k,int d){
if (d<=0||!tr[k].rev) return;
if (tr[k].s[0]) Update_rev(tr[k].s[0],d-1,tr[k].rev);
if (tr[k].s[1]) Update_rev(tr[k].s[1],d-1,tr[k].rev);
tr[k].rev=0;
}
void Change_ins(int p,int d,int &k){
if (!k) k=++cn;
if (d==-1) {tr[k].cnt=1;return;}
Pushdown(k,d);
Change_ins(p,d-1,tr[k].s[p>>d&1]);
Pushup(k,d);
}
int Merge(int d,int x,int y){
if (!x||!y) return x+y;
if (d==-1) {tr[x].cnt|=tr[y].cnt;return x;}
Pushdown(x,d);
Pushdown(y,d);
tr[x].s[0]=Merge(d-1,tr[x].s[0],tr[y].s[0]);
tr[x].s[1]=Merge(d-1,tr[x].s[1],tr[y].s[1]);
sta[++cst]=y;
Pushup(x,d);
return x;
}
void Split(int L,int R,int l,int r,int d,int &x,int &y){
if (!x) {x=y=0;return;}
if (L<=l&&R>=r) {y=x;x=0;return;}
int mid=l+r>>1;
Pushdown(x,d);
tr[y=cst?sta[cst--]:++cn]=tree();
if (L<=mid) Split(L,R,l,mid,d-1,tr[x].s[0],tr[y].s[0]);
if (R>mid) Split(L,R,mid+1,r,d-1,tr[x].s[1],tr[y].s[1]);
if (x) Pushup(x,d);
if (y) Pushup(y,d);
}
void Change_or(int x,int d,int k){
if (!x||!k||d==-1) return;
if ((x&(tr[k].all[0]|tr[k].all[1]))==x){
Update_rev(k,d,x&tr[k].all[0]);
return;
}
Pushdown(k,d);
if (x>>d&1){
tr[k].s[1]=Merge(d-1,tr[k].s[0],tr[k].s[1]);
tr[k].s[0]=0;
}
x&=(1<<d)-1;
Change_or(x,d-1,tr[k].s[0]);
Change_or(x,d-1,tr[k].s[1]);
Pushup(k,d);
}
int Query_cnt(int L,int R,int l,int r,int d,int k){
if (!k) return 0;
if (L<=l&&R>=r) return tr[k].cnt;
int mid=l+r>>1,res=0;
Pushdown(k,d);
if (L<=mid) res+=Query_cnt(L,R,l,mid,d-1,tr[k].s[0]);
if (R>mid) res+=Query_cnt(L,R,mid+1,r,d-1,tr[k].s[1]);
return res;
}
int flag;
void into(){
tr[0].all[0]=tr[0].all[1]=N-1;
n=Ri();cq=Ri();
for (int i=1;i<=n;++i){
int x=Ri();
Change_ins(x,C-1,rot);
}
}
void work(){
}
void outo(){
for (;cq--;){
int opt,l,r;
opt=Ri();l=Ri();r=Ri();
if (opt<=3){
int x=Ri(),k=0;
Split(l,r,0,N-1,C-1,rot,k);
switch (opt){
case 1:
Update_rev(k,C-1,N-1);
Change_or(x^N-1,C-1,k);
Update_rev(k,C-1,N-1);
break;
case 2:Change_or(x,C-1,k);break;
case 3:Update_rev(k,C-1,x);break;
}
rot=Merge(C-1,rot,k);
}else printf("%d\n",Query_cnt(l,r,0,N-1,C-1,rot));
}
}
int main(){
into();
work();
outo();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int MOD;
struct modint {
private:
int v;
static int minv(int a, int m) {
a %= m;
assert(a);
return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a);
}
public:
modint() : v(0) {}
modint(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }
explicit operator int() const { return v; }
friend std::ostream& operator << (std::ostream& out, const modint& n) { return out << int(n); }
friend std::istream& operator >> (std::istream& in, modint& n) { ll v_; in >> v_; n = modint(v_); return in; }
friend bool operator == (const modint& a, const modint& b) { return a.v == b.v; }
friend bool operator != (const modint& a, const modint& b) { return a.v != b.v; }
modint inv() const {
modint res;
res.v = minv(v, MOD);
return res;
}
friend modint inv(const modint& m) { return m.inv(); }
modint neg() const {
modint res;
res.v = v ? MOD-v : 0;
return res;
}
friend modint neg(const modint& m) { return m.neg(); }
modint operator- () const {
return neg();
}
modint operator+ () const {
return modint(*this);
}
modint& operator ++ () {
v ++;
if (v == MOD) v = 0;
return *this;
}
modint& operator -- () {
if (v == 0) v = MOD;
v --;
return *this;
}
modint& operator += (const modint& o) {
v += o.v;
if (v >= MOD) v -= MOD;
return *this;
}
modint& operator -= (const modint& o) {
v -= o.v;
if (v < 0) v += MOD;
return *this;
}
modint& operator *= (const modint& o) {
v = int(ll(v) * ll(o.v) % MOD);
return *this;
}
modint& operator /= (const modint& o) {
return *this *= o.inv();
}
friend modint operator ++ (modint& a, int) { modint r = a; ++a; return r; }
friend modint operator -- (modint& a, int) { modint r = a; --a; return r; }
friend modint operator + (const modint& a, const modint& b) { return modint(a) += b; }
friend modint operator - (const modint& a, const modint& b) { return modint(a) -= b; }
friend modint operator * (const modint& a, const modint& b) { return modint(a) *= b; }
friend modint operator / (const modint& a, const modint& b) { return modint(a) /= b; }
};
namespace IO {
template<class T>
void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(ll &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { x = getchar(); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U>
void R(T &head, U &... tail) { _R(head), R(tail...); }
template<class T>
void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const ll &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T, class U>
void _W(const pair<T, U> &x) { _W(x.first), putchar(' '), _W(x.second); }
template<class T>
void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U>
void W(const T &head, const U &... tail) { _W(head), putchar(sizeof...(tail) ? ' ' : '\n'), W(tail...); }
}
using namespace IO;
template <typename T> T pow(T a, long long b) {
assert(b >= 0);
T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r;
}
const int maxn = 2e5+50;
const int maxp = maxn*32+5;
const int K = 20;
const int M = 1 << K;
int ls[maxp], rs[maxp], tot;
// 0, 1, cnt, XOR
int t0[maxp], t1[maxp], p[maxp], txor[maxp];
inline void pushup(int x) {
p[x] = p[ls[x]] + p[rs[x]];
t0[x] = t0[ls[x]] | t0[rs[x]];
t1[x] = t1[ls[x]] | t1[rs[x]];
}
inline void XOR(int x, int t, int dep = K) {
if (!x) return;
txor[x] ^= t;
if (t >> (dep - 1) & 1) swap(ls[x], rs[x]);
int a = (t0[x] & (~t)) | (t1[x] & t), b = (t1[x] & (~t)) | (t0[x] & t);
t0[x] = a, t1[x] = b;
}
inline void pushdown(int x, int dep) {
if (txor[x]) {
XOR(ls[x], txor[x], dep - 1), XOR(rs[x], txor[x], dep - 1);
txor[x] = 0;
}
}
inline void insert(int &x, int s, int dep = K) {
if (!x) x = ++tot;
if (!dep) {
t1[x] = s, t0[x] = s ^ (M - 1);
p[x] = 1;
return;
}
insert((s >> (dep - 1) & 1) ? rs[x] : ls[x], s, dep - 1);
pushup(x);
}
inline void split(int &x, int &y, int l, int r, int le, int re, int dep = K) {
if (le <= l && r <= re) {
y = x; x = 0;
return;
}
int mid = l + r >> 1; pushdown(x, dep);
y = ++tot;
if (le <= mid) split(ls[x], ls[y], l, mid, le, re, dep - 1);
if (re > mid) split(rs[x], rs[y], mid + 1, r, le, re, dep - 1);
pushup(x), pushup(y);
}
inline void merge(int &x, int y, int dep = K) {
if (!x || !y) {
x = x + y;
return;
}
if (!dep) return;
pushdown(x, dep), pushdown(y, dep);
merge(ls[x], ls[y], dep - 1), merge(rs[x], rs[y], dep - 1);
pushup(x);
}
inline void OR(int x, int s, int dep = K) {
if (!x) return;
if (!(s & t0[x] & t1[x])) {
XOR(x, s & t0[x], dep);
return;
}
pushdown(x, dep);
if (s >> (dep - 1) & 1) {
XOR(ls[x], 1 << (dep - 1), dep - 1);
merge(rs[x], ls[x], dep - 1);
ls[x] = 0;
}
OR(ls[x], s, dep - 1), OR(rs[x], s, dep - 1);
pushup(x);
}
int main() {
int n, q, x = 0;
R(n, q);
for (int i = 1; i <= n; ++i) {
int v; R(v); insert(x, v);
}
for (; q; --q) {
int oth;
int t, l, r, v; R(t, l, r);
split(x, oth, 0, M - 1, l, r);
if (t == 1) R(v),XOR(oth, M - 1),OR(oth, v ^ (M - 1)),XOR(oth, M - 1);
else if (t == 2) R(v),OR(oth, v);
else if (t == 3) R(v), XOR(oth, v);
else W(p[oth]);
merge(x, oth);
}
return 0;
} | ### Prompt
Your task is to create a cpp solution to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int MOD;
struct modint {
private:
int v;
static int minv(int a, int m) {
a %= m;
assert(a);
return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a);
}
public:
modint() : v(0) {}
modint(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }
explicit operator int() const { return v; }
friend std::ostream& operator << (std::ostream& out, const modint& n) { return out << int(n); }
friend std::istream& operator >> (std::istream& in, modint& n) { ll v_; in >> v_; n = modint(v_); return in; }
friend bool operator == (const modint& a, const modint& b) { return a.v == b.v; }
friend bool operator != (const modint& a, const modint& b) { return a.v != b.v; }
modint inv() const {
modint res;
res.v = minv(v, MOD);
return res;
}
friend modint inv(const modint& m) { return m.inv(); }
modint neg() const {
modint res;
res.v = v ? MOD-v : 0;
return res;
}
friend modint neg(const modint& m) { return m.neg(); }
modint operator- () const {
return neg();
}
modint operator+ () const {
return modint(*this);
}
modint& operator ++ () {
v ++;
if (v == MOD) v = 0;
return *this;
}
modint& operator -- () {
if (v == 0) v = MOD;
v --;
return *this;
}
modint& operator += (const modint& o) {
v += o.v;
if (v >= MOD) v -= MOD;
return *this;
}
modint& operator -= (const modint& o) {
v -= o.v;
if (v < 0) v += MOD;
return *this;
}
modint& operator *= (const modint& o) {
v = int(ll(v) * ll(o.v) % MOD);
return *this;
}
modint& operator /= (const modint& o) {
return *this *= o.inv();
}
friend modint operator ++ (modint& a, int) { modint r = a; ++a; return r; }
friend modint operator -- (modint& a, int) { modint r = a; --a; return r; }
friend modint operator + (const modint& a, const modint& b) { return modint(a) += b; }
friend modint operator - (const modint& a, const modint& b) { return modint(a) -= b; }
friend modint operator * (const modint& a, const modint& b) { return modint(a) *= b; }
friend modint operator / (const modint& a, const modint& b) { return modint(a) /= b; }
};
namespace IO {
template<class T>
void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(ll &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { x = getchar(); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U>
void R(T &head, U &... tail) { _R(head), R(tail...); }
template<class T>
void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const ll &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T, class U>
void _W(const pair<T, U> &x) { _W(x.first), putchar(' '), _W(x.second); }
template<class T>
void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U>
void W(const T &head, const U &... tail) { _W(head), putchar(sizeof...(tail) ? ' ' : '\n'), W(tail...); }
}
using namespace IO;
template <typename T> T pow(T a, long long b) {
assert(b >= 0);
T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r;
}
const int maxn = 2e5+50;
const int maxp = maxn*32+5;
const int K = 20;
const int M = 1 << K;
int ls[maxp], rs[maxp], tot;
// 0, 1, cnt, XOR
int t0[maxp], t1[maxp], p[maxp], txor[maxp];
inline void pushup(int x) {
p[x] = p[ls[x]] + p[rs[x]];
t0[x] = t0[ls[x]] | t0[rs[x]];
t1[x] = t1[ls[x]] | t1[rs[x]];
}
inline void XOR(int x, int t, int dep = K) {
if (!x) return;
txor[x] ^= t;
if (t >> (dep - 1) & 1) swap(ls[x], rs[x]);
int a = (t0[x] & (~t)) | (t1[x] & t), b = (t1[x] & (~t)) | (t0[x] & t);
t0[x] = a, t1[x] = b;
}
inline void pushdown(int x, int dep) {
if (txor[x]) {
XOR(ls[x], txor[x], dep - 1), XOR(rs[x], txor[x], dep - 1);
txor[x] = 0;
}
}
inline void insert(int &x, int s, int dep = K) {
if (!x) x = ++tot;
if (!dep) {
t1[x] = s, t0[x] = s ^ (M - 1);
p[x] = 1;
return;
}
insert((s >> (dep - 1) & 1) ? rs[x] : ls[x], s, dep - 1);
pushup(x);
}
inline void split(int &x, int &y, int l, int r, int le, int re, int dep = K) {
if (le <= l && r <= re) {
y = x; x = 0;
return;
}
int mid = l + r >> 1; pushdown(x, dep);
y = ++tot;
if (le <= mid) split(ls[x], ls[y], l, mid, le, re, dep - 1);
if (re > mid) split(rs[x], rs[y], mid + 1, r, le, re, dep - 1);
pushup(x), pushup(y);
}
inline void merge(int &x, int y, int dep = K) {
if (!x || !y) {
x = x + y;
return;
}
if (!dep) return;
pushdown(x, dep), pushdown(y, dep);
merge(ls[x], ls[y], dep - 1), merge(rs[x], rs[y], dep - 1);
pushup(x);
}
inline void OR(int x, int s, int dep = K) {
if (!x) return;
if (!(s & t0[x] & t1[x])) {
XOR(x, s & t0[x], dep);
return;
}
pushdown(x, dep);
if (s >> (dep - 1) & 1) {
XOR(ls[x], 1 << (dep - 1), dep - 1);
merge(rs[x], ls[x], dep - 1);
ls[x] = 0;
}
OR(ls[x], s, dep - 1), OR(rs[x], s, dep - 1);
pushup(x);
}
int main() {
int n, q, x = 0;
R(n, q);
for (int i = 1; i <= n; ++i) {
int v; R(v); insert(x, v);
}
for (; q; --q) {
int oth;
int t, l, r, v; R(t, l, r);
split(x, oth, 0, M - 1, l, r);
if (t == 1) R(v),XOR(oth, M - 1),OR(oth, v ^ (M - 1)),XOR(oth, M - 1);
else if (t == 2) R(v),OR(oth, v);
else if (t == 3) R(v), XOR(oth, v);
else W(p[oth]);
merge(x, oth);
}
return 0;
}
``` |
#include <bits/stdc++.h>
const int N = 8e6 + 5, K = 20, M = 1 << K;
int n, q, tot, ls[N], rs[N], tk[N], t0[N], t1[N], ts[N], tt[N];
inline void pushup(int p) {
ts[p] = ts[ls[p]] + ts[rs[p]];
t0[p] = t0[ls[p]] | t0[rs[p]];
t1[p] = t1[ls[p]] | t1[rs[p]];
}
inline void tag(int p, int t) {
if (!p) return; tt[p] ^= t;
if (~tk[p] && t >> tk[p] & 1) std::swap(ls[p], rs[p]);
int x = (t0[p] & (~t)) | (t1[p] & t), y = (t1[p] & (~t)) | (t0[p] & t);
t0[p] = x, t1[p] = y;
}
inline void pushdown(int p) {
if (tt[p]) {
tag(ls[p], tt[p]), tag(rs[p], tt[p]);
tt[p] = 0;
}
}
void insert(int &p, int s, int k) {
if (!p) p = ++tot;
tk[p] = k;
if (k == -1) {
t1[p] = s & (M - 1), t0[p] = t1[p] ^ (M - 1);
ts[p] = 1;
return;
}
insert((s >> k & 1) ? rs[p] : ls[p], s, k - 1);
pushup(p);
}
void split(int &p, int &q, int l, int r, int x, int y) {
if (!p || y < l || x > r) {q = 0; return;}
if (x <= l && r <= y) {q = p, p = 0; return;}
int mid = l + r >> 1; pushdown(p);
tk[q = ++tot] = tk[p];
split(ls[p], ls[q], l, mid, x, y);
split(rs[p], rs[q], mid + 1, r, x, y);
pushup(p), pushup(q);
}
void merge(int &p, int q) {
if (!p || !q) {p = p | q; return;}
pushdown(p), pushdown(q);
merge(ls[p], ls[q]), merge(rs[p], rs[q]);
if (~tk[p]) pushup(p);
}
void modify(int p, int s) {
if (!p) return;
if (!(s & t0[p] & t1[p])) {tag(p, s & t0[p]); return;}
pushdown(p);
if (s >> tk[p] & 1) tag(ls[p], 1 << tk[p]), merge(rs[p], ls[p]), ls[p] = 0;
modify(ls[p], s), modify(rs[p], s);
pushup(p);
}
int main() {
scanf("%d%d", &n, &q); int p = 0;
for (int i = 1; i <= n; ++i) {
int x; scanf("%d", &x);
insert(p, x, K - 1);
}
for (; q; --q) {
int t, l, r, q, x; scanf("%d%d%d", &t, &l, &r);
split(p, q, 0, M - 1, l, r);
if (t == 1) scanf("%d", &x), tag(q, M - 1), modify(q, x ^ (M - 1)), tag(q, M - 1);
else if (t == 2) scanf("%d", &x), modify(q, x);
else if (t == 3) scanf("%d", &x), tag(q, x);
else printf("%d\n", ts[q]);
merge(p, q);
}
return 0;
} | ### Prompt
In Cpp, your task is to solve the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <bits/stdc++.h>
const int N = 8e6 + 5, K = 20, M = 1 << K;
int n, q, tot, ls[N], rs[N], tk[N], t0[N], t1[N], ts[N], tt[N];
inline void pushup(int p) {
ts[p] = ts[ls[p]] + ts[rs[p]];
t0[p] = t0[ls[p]] | t0[rs[p]];
t1[p] = t1[ls[p]] | t1[rs[p]];
}
inline void tag(int p, int t) {
if (!p) return; tt[p] ^= t;
if (~tk[p] && t >> tk[p] & 1) std::swap(ls[p], rs[p]);
int x = (t0[p] & (~t)) | (t1[p] & t), y = (t1[p] & (~t)) | (t0[p] & t);
t0[p] = x, t1[p] = y;
}
inline void pushdown(int p) {
if (tt[p]) {
tag(ls[p], tt[p]), tag(rs[p], tt[p]);
tt[p] = 0;
}
}
void insert(int &p, int s, int k) {
if (!p) p = ++tot;
tk[p] = k;
if (k == -1) {
t1[p] = s & (M - 1), t0[p] = t1[p] ^ (M - 1);
ts[p] = 1;
return;
}
insert((s >> k & 1) ? rs[p] : ls[p], s, k - 1);
pushup(p);
}
void split(int &p, int &q, int l, int r, int x, int y) {
if (!p || y < l || x > r) {q = 0; return;}
if (x <= l && r <= y) {q = p, p = 0; return;}
int mid = l + r >> 1; pushdown(p);
tk[q = ++tot] = tk[p];
split(ls[p], ls[q], l, mid, x, y);
split(rs[p], rs[q], mid + 1, r, x, y);
pushup(p), pushup(q);
}
void merge(int &p, int q) {
if (!p || !q) {p = p | q; return;}
pushdown(p), pushdown(q);
merge(ls[p], ls[q]), merge(rs[p], rs[q]);
if (~tk[p]) pushup(p);
}
void modify(int p, int s) {
if (!p) return;
if (!(s & t0[p] & t1[p])) {tag(p, s & t0[p]); return;}
pushdown(p);
if (s >> tk[p] & 1) tag(ls[p], 1 << tk[p]), merge(rs[p], ls[p]), ls[p] = 0;
modify(ls[p], s), modify(rs[p], s);
pushup(p);
}
int main() {
scanf("%d%d", &n, &q); int p = 0;
for (int i = 1; i <= n; ++i) {
int x; scanf("%d", &x);
insert(p, x, K - 1);
}
for (; q; --q) {
int t, l, r, q, x; scanf("%d%d%d", &t, &l, &r);
split(p, q, 0, M - 1, l, r);
if (t == 1) scanf("%d", &x), tag(q, M - 1), modify(q, x ^ (M - 1)), tag(q, M - 1);
else if (t == 2) scanf("%d", &x), modify(q, x);
else if (t == 3) scanf("%d", &x), tag(q, x);
else printf("%d\n", ts[q]);
merge(p, q);
}
return 0;
}
``` |
/*
{
######################
# Author #
# Gary #
# 2021 #
######################
*/
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define check_min(a,b) a=min(a,b)
#define check_max(a,b) a=max(a,b)
using namespace std;
//inline int read(){
// int x=0;
// char ch=getchar();
// while(ch<'0'||ch>'9'){
// ch=getchar();
// }
// while(ch>='0'&&ch<='9'){
// x=(x<<1)+(x<<3)+(ch^48);
// ch=getchar();
// }
// return x;
//}
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
/*}
*/
int root=1,cnt=1,ans[200000*41+233],lc[200000*41+233],rc[200000*41+233],depth[200000*41+233],r[200000*41+233],l[200000*41+233],tag[200000*41+233];
bool have[200000*41+233];
void pushdown(int idx){
if((tag[idx]>>depth[idx])&1) swap(lc[idx],rc[idx]);
if(lc[idx]) tag[lc[idx]]^=tag[idx];
if(rc[idx]) tag[rc[idx]]^=tag[idx];
int oldl=l[idx],oldr=r[idx];
r[idx]^=r[idx]&tag[idx];
l[idx]^=l[idx]&tag[idx];
r[idx]|=oldl&tag[idx];
l[idx]|=oldr&tag[idx];
tag[idx]=0;
assert(!(l[idx]&r[idx]));
}
void upd(int idx){
ans[idx]=have[idx];
if(depth[idx]==20) return ;
l[idx]=r[idx]=0;
assert(lc[idx]||rc[idx]);
if(lc[idx]) pushdown(lc[idx]),ans[idx]+=ans[lc[idx]],assert(!tag[lc[idx]]);
else r[idx]|=1<<depth[idx];
if(rc[idx]) pushdown(rc[idx]),ans[idx]+=ans[rc[idx]],assert(!tag[rc[idx]]);
else l[idx]|=1<<depth[idx];
if(lc[idx]&&rc[idx]) l[idx]|=l[lc[idx]]&l[rc[idx]],r[idx]|=r[lc[idx]]&r[rc[idx]];
else l[idx]|=l[max(lc[idx],rc[idx])],r[idx]|=r[max(lc[idx],rc[idx])];
assert(!(l[idx]&r[idx]));
}
void merge(int& u,int& v){//merge(u,v) -> v
if(!u||!v){
if(u){
v=u;
u=0;
}
if(v) pushdown(v);
return ;
}
pushdown(u),pushdown(v);
have[v]|=have[u];
merge(rc[u],rc[v]);
merge(lc[u],lc[v]);
// cout<<u<<" "<<v<<' '<<rc[v]<<' '<<lc[v]<<' '<<endl;
u=0;
upd(v);
}
int Rev(int x){
int ret=0;
rep(i,20){
ret|=((x>>i)&1)<<(19-i);
}
return ret;
}
void split(int rt,int val,int & x,int & y){
if(!rt){
x=y=0;
return ;
}
pushdown(rt);
if(depth[rt]==20){
x=rt,y=0;
return;
}
if((val>>depth[rt])&1){
x=rt;
y=++cnt;
depth[cnt]=depth[rt];
split(rc[rt],val,rc[x],rc[y]);
}
else{
x=++cnt;
y=rt;
depth[cnt]=depth[rt];
split(lc[rt],val,lc[x],lc[y]);
}
if(!lc[x]&&!rc[x]){
x=0;
}
if(!lc[y]&&!rc[y]){
y=0;
}
if(x) upd(x);
if(y) upd(y);
}
void getor(int idx,int x){
if(depth[idx]==20) return ;
pushdown(idx);
if((x&(l[idx]|r[idx]))==x){
tag[idx]^=x&l[idx];
pushdown(idx);
return ;
}
if((x>>depth[idx])&1);
else{
if(lc[idx]) getor(lc[idx],x);
if(rc[idx]) getor(rc[idx],x);
upd(idx);
return ;
}
if(lc[idx]&&rc[idx]){
merge(lc[idx],rc[idx]);
getor(rc[idx],x^(1<<depth[idx]));
upd(idx);
return ;
}
if(lc[idx]){
swap(lc[idx],rc[idx]);
getor(rc[idx],x^(1<<depth[idx]));
upd(idx);
return ;
}
assert(rc[idx]);
getor(rc[idx],x^(1<<depth[idx]));
upd(idx);
}
void insert(int rt,int val){
if(depth[rt]==20){
have[rt]=1;
upd(rt);
return ;
}
bool bt=(val>>depth[rt])&1;
if(bt){
if(!rc[rt]) rc[rt]=++cnt,depth[cnt]=depth[rt]+1;
insert(rc[rt],val);
}
else{
if(!lc[rt]) lc[rt]=++cnt,depth[cnt]=depth[rt]+1;
insert(lc[rt],val);
}
upd(rt);
}
int main(){
int n,q;
scanf("%d%d",&n,&q);
rb(i,1,n){
int ai;
scanf("%d",&ai);
insert(root,Rev(ai));
}
rb(i,1,q){
int ty,l,r;
scanf("%d%d%d",&ty,&l,&r);
int x=0,y=0,z=0;
split(root,Rev(r),y,z);
if(l)
split(y,Rev(l-1),x,y);
else x=0;
if(ty<=3){
int X;
scanf("%d",&X);
// if(n==1000) cout<<ty<<' '<<l<<' '<<r<<" "<<X<<endl;
if(y){
if(ty==1){
tag[y]^=(1<<20)-1;
getor(y,((1<<20)-1)^Rev(X));
tag[y]^=(1<<20)-1;
}
if(ty==2){
getor(y,Rev(X));
}
if(ty==3){
tag[y]^=Rev(X);
}
pushdown(y);
}
}
else{
// if(n==1000) cout<<ty<<' '<<l<<' '<.<r<<endl;
printf("%d\n",ans[y]);
}
merge(x,y);
merge(y,z);
root=z;
}
return 0;
} | ### Prompt
Generate a Cpp solution to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
/*
{
######################
# Author #
# Gary #
# 2021 #
######################
*/
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define check_min(a,b) a=min(a,b)
#define check_max(a,b) a=max(a,b)
using namespace std;
//inline int read(){
// int x=0;
// char ch=getchar();
// while(ch<'0'||ch>'9'){
// ch=getchar();
// }
// while(ch>='0'&&ch<='9'){
// x=(x<<1)+(x<<3)+(ch^48);
// ch=getchar();
// }
// return x;
//}
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
/*}
*/
int root=1,cnt=1,ans[200000*41+233],lc[200000*41+233],rc[200000*41+233],depth[200000*41+233],r[200000*41+233],l[200000*41+233],tag[200000*41+233];
bool have[200000*41+233];
void pushdown(int idx){
if((tag[idx]>>depth[idx])&1) swap(lc[idx],rc[idx]);
if(lc[idx]) tag[lc[idx]]^=tag[idx];
if(rc[idx]) tag[rc[idx]]^=tag[idx];
int oldl=l[idx],oldr=r[idx];
r[idx]^=r[idx]&tag[idx];
l[idx]^=l[idx]&tag[idx];
r[idx]|=oldl&tag[idx];
l[idx]|=oldr&tag[idx];
tag[idx]=0;
assert(!(l[idx]&r[idx]));
}
void upd(int idx){
ans[idx]=have[idx];
if(depth[idx]==20) return ;
l[idx]=r[idx]=0;
assert(lc[idx]||rc[idx]);
if(lc[idx]) pushdown(lc[idx]),ans[idx]+=ans[lc[idx]],assert(!tag[lc[idx]]);
else r[idx]|=1<<depth[idx];
if(rc[idx]) pushdown(rc[idx]),ans[idx]+=ans[rc[idx]],assert(!tag[rc[idx]]);
else l[idx]|=1<<depth[idx];
if(lc[idx]&&rc[idx]) l[idx]|=l[lc[idx]]&l[rc[idx]],r[idx]|=r[lc[idx]]&r[rc[idx]];
else l[idx]|=l[max(lc[idx],rc[idx])],r[idx]|=r[max(lc[idx],rc[idx])];
assert(!(l[idx]&r[idx]));
}
void merge(int& u,int& v){//merge(u,v) -> v
if(!u||!v){
if(u){
v=u;
u=0;
}
if(v) pushdown(v);
return ;
}
pushdown(u),pushdown(v);
have[v]|=have[u];
merge(rc[u],rc[v]);
merge(lc[u],lc[v]);
// cout<<u<<" "<<v<<' '<<rc[v]<<' '<<lc[v]<<' '<<endl;
u=0;
upd(v);
}
int Rev(int x){
int ret=0;
rep(i,20){
ret|=((x>>i)&1)<<(19-i);
}
return ret;
}
void split(int rt,int val,int & x,int & y){
if(!rt){
x=y=0;
return ;
}
pushdown(rt);
if(depth[rt]==20){
x=rt,y=0;
return;
}
if((val>>depth[rt])&1){
x=rt;
y=++cnt;
depth[cnt]=depth[rt];
split(rc[rt],val,rc[x],rc[y]);
}
else{
x=++cnt;
y=rt;
depth[cnt]=depth[rt];
split(lc[rt],val,lc[x],lc[y]);
}
if(!lc[x]&&!rc[x]){
x=0;
}
if(!lc[y]&&!rc[y]){
y=0;
}
if(x) upd(x);
if(y) upd(y);
}
void getor(int idx,int x){
if(depth[idx]==20) return ;
pushdown(idx);
if((x&(l[idx]|r[idx]))==x){
tag[idx]^=x&l[idx];
pushdown(idx);
return ;
}
if((x>>depth[idx])&1);
else{
if(lc[idx]) getor(lc[idx],x);
if(rc[idx]) getor(rc[idx],x);
upd(idx);
return ;
}
if(lc[idx]&&rc[idx]){
merge(lc[idx],rc[idx]);
getor(rc[idx],x^(1<<depth[idx]));
upd(idx);
return ;
}
if(lc[idx]){
swap(lc[idx],rc[idx]);
getor(rc[idx],x^(1<<depth[idx]));
upd(idx);
return ;
}
assert(rc[idx]);
getor(rc[idx],x^(1<<depth[idx]));
upd(idx);
}
void insert(int rt,int val){
if(depth[rt]==20){
have[rt]=1;
upd(rt);
return ;
}
bool bt=(val>>depth[rt])&1;
if(bt){
if(!rc[rt]) rc[rt]=++cnt,depth[cnt]=depth[rt]+1;
insert(rc[rt],val);
}
else{
if(!lc[rt]) lc[rt]=++cnt,depth[cnt]=depth[rt]+1;
insert(lc[rt],val);
}
upd(rt);
}
int main(){
int n,q;
scanf("%d%d",&n,&q);
rb(i,1,n){
int ai;
scanf("%d",&ai);
insert(root,Rev(ai));
}
rb(i,1,q){
int ty,l,r;
scanf("%d%d%d",&ty,&l,&r);
int x=0,y=0,z=0;
split(root,Rev(r),y,z);
if(l)
split(y,Rev(l-1),x,y);
else x=0;
if(ty<=3){
int X;
scanf("%d",&X);
// if(n==1000) cout<<ty<<' '<<l<<' '<<r<<" "<<X<<endl;
if(y){
if(ty==1){
tag[y]^=(1<<20)-1;
getor(y,((1<<20)-1)^Rev(X));
tag[y]^=(1<<20)-1;
}
if(ty==2){
getor(y,Rev(X));
}
if(ty==3){
tag[y]^=Rev(X);
}
pushdown(y);
}
}
else{
// if(n==1000) cout<<ty<<' '<<l<<' '<.<r<<endl;
printf("%d\n",ans[y]);
}
merge(x,y);
merge(y,z);
root=z;
}
return 0;
}
``` |
#include <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<iomanip>
#include<ctime>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<bitset>
#define sqr(x) ((x)*(x))
#define fz1(i,n) for ((i)=1;(i)<=(n);(i)++)
#define fd1(i,n) for ((i)=(n);(i)>=1;(i)--)
#define fz0g(i,n) for ((i)=0;(i)<=(n);(i)++)
#define fd0g(i,n) for ((i)=(n);(i)>=0;(i)--)
#define fz0k(i,n) for ((i)=0;(i)<(n);(i)++)
#define fd0k(i,n) for ((i)=(long long)((n)-1);(i)>=0;(i)--)
#define fz(i,x,y) for ((i)=(x);(i)<=(y);(i)++)
#define fd(i,y,x) for ((i)=(y);(i)>=(x);(i)--)
#define fzin fz1(i,n)
#define fzim fz1(i,m)
#define fzjn fz1(j,n)
#define fzjm fz1(j,m)
#define ff(c,itr) for (__typeof((c).begin()) itr=(c).begin();itr!=(c).end();++itr)
#define rdst(st,len){static char ss[len];scanf(" %s",ss);(st)=ss;}
#define incm(x,y) {x=((x)+(y))%mod;}
#define spln(i,n) (i==n?'\n':' ')
#define fac_init(n){fac[0]=fac[1]=inv[1]=fi[0]=fi[1]=1;fz(i,2,n){fac[i]=1ll*fac[i-1]*i%mod;inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;fi[i]=1ll*fi[i-1]*inv[i]%mod;}}
#define fi first
#define se second
#define mk make_pair
using namespace std;
inline void read(int &x)
{
char c;int f=1;
while(!isdigit(c=getchar()))if(c=='-')f=-1;
x=(c&15);while(isdigit(c=getchar()))x=(x<<1)+(x<<3)+(c&15);
x*=f;
}
const int lim=(1<<20)-1;
int n,q,i,j,rt,cnt;
int lc[16000005],rc[16000005];
int tagx[16000005];
int sz[16000005],f[16000005][2];
vector<pair<int,pair<int,int> > > v;
void pushup(int x,int d)
{
sz[x]=sz[lc[x]]+sz[rc[x]];
f[x][0]=f[lc[x]][0]|f[rc[x]][0];
f[x][1]=f[lc[x]][1]|f[rc[x]][1];
if(sz[lc[x]]==0) lc[x]=0;
if(sz[rc[x]]==0) rc[x]=0;
if(lc[x]) f[x][0]|=(1<<d);
if(rc[x]) f[x][1]|=(1<<d);
if(sz[x]&&!(f[x][0]||f[x][1])){
int a;
a++;
}
}
void update(int &x,int l,int r,int d,int y)
{
if(!x){
x=++cnt;
}
if(l==r){
sz[x]=1;
f[x][0]|=(lim^y);
f[x][1]|=y;
return;
}
int mid=(l+r)/2;
if(y<=mid) update(lc[x],l,mid,d-1,y); else update(rc[x],mid+1,r,d-1,y);
pushup(x,d);
}
void merge(int &x,int y,int d);
void updx(int x,int d,int t)
{
if(d==-1) return;
int tt=((f[x][0]^f[x][1])&t);
f[x][0]^=tt;f[x][1]^=tt;
tagx[x]^=t;
if(((t>>d)&1)){
swap(lc[x],rc[x]);
}
}
void pushdo(int x,int d)
{
if(lc[x]){
if(tagx[x]!=0) updx(lc[x],d-1,tagx[x]);
}
if(rc[x]){
if(tagx[x]!=0) updx(rc[x],d-1,tagx[x]);
}
tagx[x]=0;
}
void upda(int x,int k,int d)
{
if(!x||!((f[x][1]>>k)&1)) return;
if(!((f[x][0]>>k)&1)){
updx(x,d,(1<<k));
return;
}
pushdo(x,d);
if(d==k){
merge(lc[x],rc[x],d-1);
rc[x]=0;
pushup(x,d);
return;
}
upda(lc[x],k,d-1);
upda(rc[x],k,d-1);
pushup(x,d);
}
void updo(int x,int k,int d)
{
if(!x||!((f[x][0]>>k)&1)) return;
if(!((f[x][1]>>k)&1)){
updx(x,d,(1<<k));
return;
}
pushdo(x,d);
if(d==k){
merge(rc[x],lc[x],d-1);
lc[x]=0;
pushup(x,d);
return;
}
updo(lc[x],k,d-1);
updo(rc[x],k,d-1);
pushup(x,d);
}
void merge(int &x,int y,int d)
{
if(!x||!y){
x=x+y;
return;
}
if(d==-1){
sz[x]=1;
return;
}
pushdo(x,d);
pushdo(y,d);
merge(lc[x],lc[y],d-1);
merge(rc[x],rc[y],d-1);
pushup(x,d);
}
int count(int x,int l,int r,int d,int ql,int qr)
{
if(!x) return 0;
if(ql<=l&&r<=qr){
return sz[x];
}
pushdo(x,d);
int mid=(l+r)/2,s=0;
if(ql<=mid) s+=count(lc[x],l,mid,d-1,ql,qr);
if(qr>mid) s+=count(rc[x],mid+1,r,d-1,ql,qr);
return s;
}
void pushin(int &x,int l,int r,int d,int ql,int qr)
{
if(!x) return;
if(ql<=l&&r<=qr){
v.push_back(make_pair(x,mk(l,r)));
x=0;return;
}
pushdo(x,d);
int mid=(l+r)/2;
if(ql<=mid) pushin(lc[x],l,mid,d-1,ql,qr);
if(qr>mid) pushin(rc[x],mid+1,r,d-1,ql,qr);
pushup(x,d);
}
void retin(int &x,int l,int r,int d,int ql,int qr,int y)
{
if(!x){
x=++cnt;
}
if(ql==l&&r==qr){
merge(x,y,d);
return;
}
pushdo(x,d);
int mid=(l+r)/2;
if(ql<=mid) retin(lc[x],l,mid,d-1,ql,qr,y);
if(qr>mid) retin(rc[x],mid+1,r,d-1,ql,qr,y);
pushup(x,d);
}
int main()
{
read(n);read(q);
fz1(i,n){
int x;
read(x);
update(rt,0,lim,19,x);
}
while(q--){
int op,l,r;
read(op);read(l);read(r);
if(op==4){
printf("%d\n",count(rt,0,lim,19,l,r));
continue;
}
v.clear();
pushin(rt,0,lim,19,l,r);
int val;read(val);
ff(v,it){
int l=it->se.fi,r=it->se.se,x=it->fi;
for(i=19;i>=0;i--){
if(((l>>i)&1)==((r>>i)&1)){
if(op==1){
l&=(lim^(1<<i)^((1<<i)&val));
r&=(lim^(1<<i)^((1<<i)&val));
}
if(op==2){
l|=((1<<i)&val);
r|=((1<<i)&val);
}
if(op==3){
l^=((1<<i)&val);
r^=((1<<i)&val);
}
}
else break;
}
if(op==1){
fz0k(j,20) if(!((val>>j)&1))upda(x,j,i);
}
if(op==2){
fz0k(j,20) if(((val>>j)&1))updo(x,j,i);
}
if(op==3) updx(x,i,val);
retin(rt,0,lim,19,l,r,x);
}
}
return 0;
} | ### Prompt
Create a solution in cpp for the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<iomanip>
#include<ctime>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<bitset>
#define sqr(x) ((x)*(x))
#define fz1(i,n) for ((i)=1;(i)<=(n);(i)++)
#define fd1(i,n) for ((i)=(n);(i)>=1;(i)--)
#define fz0g(i,n) for ((i)=0;(i)<=(n);(i)++)
#define fd0g(i,n) for ((i)=(n);(i)>=0;(i)--)
#define fz0k(i,n) for ((i)=0;(i)<(n);(i)++)
#define fd0k(i,n) for ((i)=(long long)((n)-1);(i)>=0;(i)--)
#define fz(i,x,y) for ((i)=(x);(i)<=(y);(i)++)
#define fd(i,y,x) for ((i)=(y);(i)>=(x);(i)--)
#define fzin fz1(i,n)
#define fzim fz1(i,m)
#define fzjn fz1(j,n)
#define fzjm fz1(j,m)
#define ff(c,itr) for (__typeof((c).begin()) itr=(c).begin();itr!=(c).end();++itr)
#define rdst(st,len){static char ss[len];scanf(" %s",ss);(st)=ss;}
#define incm(x,y) {x=((x)+(y))%mod;}
#define spln(i,n) (i==n?'\n':' ')
#define fac_init(n){fac[0]=fac[1]=inv[1]=fi[0]=fi[1]=1;fz(i,2,n){fac[i]=1ll*fac[i-1]*i%mod;inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;fi[i]=1ll*fi[i-1]*inv[i]%mod;}}
#define fi first
#define se second
#define mk make_pair
using namespace std;
inline void read(int &x)
{
char c;int f=1;
while(!isdigit(c=getchar()))if(c=='-')f=-1;
x=(c&15);while(isdigit(c=getchar()))x=(x<<1)+(x<<3)+(c&15);
x*=f;
}
const int lim=(1<<20)-1;
int n,q,i,j,rt,cnt;
int lc[16000005],rc[16000005];
int tagx[16000005];
int sz[16000005],f[16000005][2];
vector<pair<int,pair<int,int> > > v;
void pushup(int x,int d)
{
sz[x]=sz[lc[x]]+sz[rc[x]];
f[x][0]=f[lc[x]][0]|f[rc[x]][0];
f[x][1]=f[lc[x]][1]|f[rc[x]][1];
if(sz[lc[x]]==0) lc[x]=0;
if(sz[rc[x]]==0) rc[x]=0;
if(lc[x]) f[x][0]|=(1<<d);
if(rc[x]) f[x][1]|=(1<<d);
if(sz[x]&&!(f[x][0]||f[x][1])){
int a;
a++;
}
}
void update(int &x,int l,int r,int d,int y)
{
if(!x){
x=++cnt;
}
if(l==r){
sz[x]=1;
f[x][0]|=(lim^y);
f[x][1]|=y;
return;
}
int mid=(l+r)/2;
if(y<=mid) update(lc[x],l,mid,d-1,y); else update(rc[x],mid+1,r,d-1,y);
pushup(x,d);
}
void merge(int &x,int y,int d);
void updx(int x,int d,int t)
{
if(d==-1) return;
int tt=((f[x][0]^f[x][1])&t);
f[x][0]^=tt;f[x][1]^=tt;
tagx[x]^=t;
if(((t>>d)&1)){
swap(lc[x],rc[x]);
}
}
void pushdo(int x,int d)
{
if(lc[x]){
if(tagx[x]!=0) updx(lc[x],d-1,tagx[x]);
}
if(rc[x]){
if(tagx[x]!=0) updx(rc[x],d-1,tagx[x]);
}
tagx[x]=0;
}
void upda(int x,int k,int d)
{
if(!x||!((f[x][1]>>k)&1)) return;
if(!((f[x][0]>>k)&1)){
updx(x,d,(1<<k));
return;
}
pushdo(x,d);
if(d==k){
merge(lc[x],rc[x],d-1);
rc[x]=0;
pushup(x,d);
return;
}
upda(lc[x],k,d-1);
upda(rc[x],k,d-1);
pushup(x,d);
}
void updo(int x,int k,int d)
{
if(!x||!((f[x][0]>>k)&1)) return;
if(!((f[x][1]>>k)&1)){
updx(x,d,(1<<k));
return;
}
pushdo(x,d);
if(d==k){
merge(rc[x],lc[x],d-1);
lc[x]=0;
pushup(x,d);
return;
}
updo(lc[x],k,d-1);
updo(rc[x],k,d-1);
pushup(x,d);
}
void merge(int &x,int y,int d)
{
if(!x||!y){
x=x+y;
return;
}
if(d==-1){
sz[x]=1;
return;
}
pushdo(x,d);
pushdo(y,d);
merge(lc[x],lc[y],d-1);
merge(rc[x],rc[y],d-1);
pushup(x,d);
}
int count(int x,int l,int r,int d,int ql,int qr)
{
if(!x) return 0;
if(ql<=l&&r<=qr){
return sz[x];
}
pushdo(x,d);
int mid=(l+r)/2,s=0;
if(ql<=mid) s+=count(lc[x],l,mid,d-1,ql,qr);
if(qr>mid) s+=count(rc[x],mid+1,r,d-1,ql,qr);
return s;
}
void pushin(int &x,int l,int r,int d,int ql,int qr)
{
if(!x) return;
if(ql<=l&&r<=qr){
v.push_back(make_pair(x,mk(l,r)));
x=0;return;
}
pushdo(x,d);
int mid=(l+r)/2;
if(ql<=mid) pushin(lc[x],l,mid,d-1,ql,qr);
if(qr>mid) pushin(rc[x],mid+1,r,d-1,ql,qr);
pushup(x,d);
}
void retin(int &x,int l,int r,int d,int ql,int qr,int y)
{
if(!x){
x=++cnt;
}
if(ql==l&&r==qr){
merge(x,y,d);
return;
}
pushdo(x,d);
int mid=(l+r)/2;
if(ql<=mid) retin(lc[x],l,mid,d-1,ql,qr,y);
if(qr>mid) retin(rc[x],mid+1,r,d-1,ql,qr,y);
pushup(x,d);
}
int main()
{
read(n);read(q);
fz1(i,n){
int x;
read(x);
update(rt,0,lim,19,x);
}
while(q--){
int op,l,r;
read(op);read(l);read(r);
if(op==4){
printf("%d\n",count(rt,0,lim,19,l,r));
continue;
}
v.clear();
pushin(rt,0,lim,19,l,r);
int val;read(val);
ff(v,it){
int l=it->se.fi,r=it->se.se,x=it->fi;
for(i=19;i>=0;i--){
if(((l>>i)&1)==((r>>i)&1)){
if(op==1){
l&=(lim^(1<<i)^((1<<i)&val));
r&=(lim^(1<<i)^((1<<i)&val));
}
if(op==2){
l|=((1<<i)&val);
r|=((1<<i)&val);
}
if(op==3){
l^=((1<<i)&val);
r^=((1<<i)&val);
}
}
else break;
}
if(op==1){
fz0k(j,20) if(!((val>>j)&1))upda(x,j,i);
}
if(op==2){
fz0k(j,20) if(((val>>j)&1))updo(x,j,i);
}
if(op==3) updx(x,i,val);
retin(rt,0,lim,19,l,r,x);
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
typedef long long i64;
#define sz(a) int((a).size())
#define all(a) (a).begin(), (a).end()
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b) - 1; i >= (a); --i)
using namespace std;
const int xn = 8e6, xk = 20, xs = 1 << xk;
int n = 1, ls[xn], rs[xn], h[xn];
int sz[xn], lw[xn], rw[xn], fw[xn];
int nw(int k) {int u = n++; h[u] = k; return u;}
void pu(int u) {
if (-1 != h[u]) {
sz[u] = sz[ls[u]] + sz[rs[u]];
lw[u] = lw[ls[u]] | lw[rs[u]];
rw[u] = rw[ls[u]] | rw[rs[u]];
}
}
void rev(int u, int x) {
if (!u) {return;} fw[u] ^= x;
lw[u] ^= (rw[u] ^= (lw[u] ^= rw[u] & x) & x) & x;
if (~h[u] and x >> h[u] & 1) {swap(ls[u], rs[u]);}
}
void pd(int u) {if (fw[u]) {rev(ls[u], fw[u]),
rev(rs[u], fw[u]), fw[u] = 0;}}
void ist(int &u, int x, int k = xk - 1) {
if (!u) {u = nw(k);}
if (k == -1) {lw[u] = (xs - 1) ^ x; rw[u] = x; sz[u] = 1;}
else {ist(x >> k & 1 ? rs[u] : ls[u], x, k - 1); pu(u);}
}
void spl(int &u, int &v, int x, int y, int l = 0, int r = xs) {
if (!u or y <= l or r <= x) {v = 0; return;}
if (x <= l and r <= y) {v = u; u = 0; return;}
int mi = (l + r) / 2; pd(u); v = nw(h[u]);
spl(ls[u], ls[v], x, y, l, mi);
spl(rs[u], rs[v], x, y, mi, r); pu(u); pu(v);
}
void mge(int &u, int v) {
if (!u or !v) {u |= v; return;}
pd(u); pd(v); mge(ls[u], ls[v]); mge(rs[u], rs[v]); pu(u);
}
void wor(int u, int x) {
if (!u) {return;}
if (!(x & lw[u] & rw[u])) {return rev(u, x & lw[u]);}
pd(u);
if (x >> h[u] & 1) {
rev(ls[u], 1 << h[u]), mge(rs[u], ls[u]), ls[u] = 0;
}
wor(ls[u], x); wor(rs[u], x); pu(u);
}
void out(int u) {
if (!u) {return;} pd(u);
cout << u << " -> " << ls[u] << " -> "
<< rs[u] << " : " << h[u] << ' ' << lw[u]
<< ' ' << rw[u] << ' ' << fw[u] << ' ' << sz[u] << '\n';
out(ls[u]), out(rs[u]);
}
int main() {
// freopen("CF1515H.in", "r", stdin); // @
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n, nq, rt = 0, u; cin >> n >> nq;
rep(i, 0, n) {int x; cin >> x; ist(rt, x);}
// cout << "[OUT RT]\n"; out(rt); // @
rep(iq, 0, nq) {
int o, l, r; cin >> o >> l >> r, ++r;
spl(rt, u, l, r);
// cout << "[OUT U]\n"; out(u); // @
if (o == 4) {cout << sz[u] << '\n';}
else {
int x; cin >> x;
if (o == 3) {rev(u, x);}
else if (o == 2) {wor(u, x);}
else {rev(u, xs - 1), wor(u, (xs - 1) ^ x),
rev(u, xs - 1);}
}
// cout << "[OUT U]\n"; out(u); // @
mge(rt, u);
// cout << "[OUT RT]\n"; out(rt); // @
}
// cout << "hem, hem, aaaaaaaaaaa" << endl; // @
return 0;
}
/* 📌https://www.cnblogs.com/George1123/p/tips.html */ | ### Prompt
Your challenge is to write a cpp solution to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <bits/stdc++.h>
typedef long long i64;
#define sz(a) int((a).size())
#define all(a) (a).begin(), (a).end()
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b) - 1; i >= (a); --i)
using namespace std;
const int xn = 8e6, xk = 20, xs = 1 << xk;
int n = 1, ls[xn], rs[xn], h[xn];
int sz[xn], lw[xn], rw[xn], fw[xn];
int nw(int k) {int u = n++; h[u] = k; return u;}
void pu(int u) {
if (-1 != h[u]) {
sz[u] = sz[ls[u]] + sz[rs[u]];
lw[u] = lw[ls[u]] | lw[rs[u]];
rw[u] = rw[ls[u]] | rw[rs[u]];
}
}
void rev(int u, int x) {
if (!u) {return;} fw[u] ^= x;
lw[u] ^= (rw[u] ^= (lw[u] ^= rw[u] & x) & x) & x;
if (~h[u] and x >> h[u] & 1) {swap(ls[u], rs[u]);}
}
void pd(int u) {if (fw[u]) {rev(ls[u], fw[u]),
rev(rs[u], fw[u]), fw[u] = 0;}}
void ist(int &u, int x, int k = xk - 1) {
if (!u) {u = nw(k);}
if (k == -1) {lw[u] = (xs - 1) ^ x; rw[u] = x; sz[u] = 1;}
else {ist(x >> k & 1 ? rs[u] : ls[u], x, k - 1); pu(u);}
}
void spl(int &u, int &v, int x, int y, int l = 0, int r = xs) {
if (!u or y <= l or r <= x) {v = 0; return;}
if (x <= l and r <= y) {v = u; u = 0; return;}
int mi = (l + r) / 2; pd(u); v = nw(h[u]);
spl(ls[u], ls[v], x, y, l, mi);
spl(rs[u], rs[v], x, y, mi, r); pu(u); pu(v);
}
void mge(int &u, int v) {
if (!u or !v) {u |= v; return;}
pd(u); pd(v); mge(ls[u], ls[v]); mge(rs[u], rs[v]); pu(u);
}
void wor(int u, int x) {
if (!u) {return;}
if (!(x & lw[u] & rw[u])) {return rev(u, x & lw[u]);}
pd(u);
if (x >> h[u] & 1) {
rev(ls[u], 1 << h[u]), mge(rs[u], ls[u]), ls[u] = 0;
}
wor(ls[u], x); wor(rs[u], x); pu(u);
}
void out(int u) {
if (!u) {return;} pd(u);
cout << u << " -> " << ls[u] << " -> "
<< rs[u] << " : " << h[u] << ' ' << lw[u]
<< ' ' << rw[u] << ' ' << fw[u] << ' ' << sz[u] << '\n';
out(ls[u]), out(rs[u]);
}
int main() {
// freopen("CF1515H.in", "r", stdin); // @
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n, nq, rt = 0, u; cin >> n >> nq;
rep(i, 0, n) {int x; cin >> x; ist(rt, x);}
// cout << "[OUT RT]\n"; out(rt); // @
rep(iq, 0, nq) {
int o, l, r; cin >> o >> l >> r, ++r;
spl(rt, u, l, r);
// cout << "[OUT U]\n"; out(u); // @
if (o == 4) {cout << sz[u] << '\n';}
else {
int x; cin >> x;
if (o == 3) {rev(u, x);}
else if (o == 2) {wor(u, x);}
else {rev(u, xs - 1), wor(u, (xs - 1) ^ x),
rev(u, xs - 1);}
}
// cout << "[OUT U]\n"; out(u); // @
mge(rt, u);
// cout << "[OUT RT]\n"; out(rt); // @
}
// cout << "hem, hem, aaaaaaaaaaa" << endl; // @
return 0;
}
/* 📌https://www.cnblogs.com/George1123/p/tips.html */
``` |
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
//#define int ll
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif
template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}
template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;
using pi=pair<int,int>;
using vi=vc<int>;
template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
return os<<"{"<<p.a<<","<<p.b<<"}";
}
template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
os<<"{";
for(auto e:v)os<<e<<",";
return os<<"}";
}
#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))
#ifdef LOCAL
void dmpr(ostream&os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
os<<t<<" ";
dmpr(os,args...);
}
#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)
#else
#define dmp2(...) void(0)
#endif
using uint=unsigned;
using ull=unsigned long long;
template<class t,size_t n>
ostream& operator<<(ostream&os,const array<t,n>&a){
return os<<vc<t>(all(a));
}
template<int i,class T>
void print_tuple(ostream&,const T&){
}
template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
if(i)os<<",";
os<<get<i>(t);
print_tuple<i+1,T,Args...>(os,t);
}
template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
os<<"{";
print_tuple<0,tuple<Args...>,Args...>(os,t);
return os<<"}";
}
template<class t>
void print(t x,int suc=1){
cout<<x;
if(suc==1)
cout<<"\n";
if(suc==2)
cout<<" ";
}
ll read(){
ll i;
cin>>i;
return i;
}
vi readvi(int n,int off=0){
vi v(n);
rep(i,n)v[i]=read()+off;
return v;
}
pi readpi(int off=0){
int a,b;cin>>a>>b;
return pi(a+off,b+off);
}
template<class t,class u>
void print(const pair<t,u>&p,int suc=1){
print(p.a,2);
print(p.b,suc);
}
template<class T>
void print(const vector<T>&v,int suc=1){
rep(i,v.size())
print(v[i],i==int(v.size())-1?suc:2);
}
string readString(){
string s;
cin>>s;
return s;
}
template<class T>
T sq(const T& t){
return t*t;
}
//#define CAPITAL
void yes(bool ex=true){
#ifdef CAPITAL
cout<<"YES"<<"\n";
#else
cout<<"Yes"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void no(bool ex=true){
#ifdef CAPITAL
cout<<"NO"<<"\n";
#else
cout<<"No"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void possible(bool ex=true){
#ifdef CAPITAL
cout<<"POSSIBLE"<<"\n";
#else
cout<<"Possible"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void impossible(bool ex=true){
#ifdef CAPITAL
cout<<"IMPOSSIBLE"<<"\n";
#else
cout<<"Impossible"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
constexpr ll ten(int n){
return n==0?1:ten(n-1)*10;
}
const ll infLL=LLONG_MAX/3;
#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif
int topbit(signed t){
return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
return __builtin_popcount(t);
}
int popcount(ll t){
return __builtin_popcountll(t);
}
bool ispow2(int i){
return i&&(i&-i)==i;
}
ll mask(int i){
return (ll(1)<<i)-1;
}
bool inc(int a,int b,int c){
return a<=b&&b<=c;
}
template<class t> void mkuni(vc<t>&v){
sort(all(v));
v.erase(unique(all(v)),v.ed);
}
ll rand_int(ll l, ll r) { //[l, r]
#ifdef LOCAL
static mt19937_64 gen;
#else
static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
#endif
return uniform_int_distribution<ll>(l, r)(gen);
}
template<class t>
void myshuffle(vc<t>&a){
rep(i,si(a))swap(a[i],a[rand_int(0,i)]);
}
template<class t>
int lwb(const vc<t>&v,const t&a){
return lower_bound(all(v),a)-v.bg;
}
vvc<int> readGraph(int n,int m){
vvc<int> g(n);
rep(i,m){
int a,b;
cin>>a>>b;
//sc.read(a,b);
a--;b--;
g[a].pb(b);
g[b].pb(a);
}
return g;
}
vvc<int> readTree(int n){
return readGraph(n,n-1);
}
const int L=20;
const int M=mask(L);
/*
void clear(int&a,int i){
a&=~(1<<i);
}
*/
int action(int i,int fix,int off){
return ((i^off)&~fix)|(fix&off);
}
struct N{
N*ch[2];
int fix,off,cnt,both;
N():ch{nullptr,nullptr},fix(0),off(0),cnt(0),both(0){}
void work(int f,int o,int lv){
f&=mask(lv);
o&=mask(lv);
off=action(off,f,o);
fix|=f;
}
void update(int lv){
fix=0;
off=0;
cnt=0;
both=0;
rep(k,2)if(ch[k]){
cnt+=ch[k]->cnt;
both|=ch[k]->both;
}
if(ch[0]&&ch[1])both|=1<<(lv-1);
}
void propagate(int lv){
if(fix&1<<(lv-1)){
int k=(off>>(lv-1))&1;
if(ch[k^1])swap(ch[0],ch[1]);
assert(!ch[k^1]);
}else if(off&1<<(lv-1)){
swap(ch[0],ch[1]);
}
rep(k,2)if(ch[k])ch[k]->work(fix,off,lv-1);
fix=0;
off=0;
}
};
N buf[ten(7)];
using np=N*;
N* nn(){
static int head=0;
return buf+head++;
}
np mg(np a,np b,int lv){
if(!a&&!b)return nullptr;
np n=nn();
n->ch[0]=a;
n->ch[1]=b;
n->update(lv);
return n;
}
np insert(np x,int pos,int lv){
if(!x)x=nn();
if(lv==0){
x->cnt=1;
return x;
}
int k=(pos>>(lv-1))&1;
x->propagate(lv);
x->ch[k]=insert(x->ch[k],pos,lv-1);
x->update(lv);
return x;
}
//l,lv,ptr
vc<tuple<int,int,np>> memo;
np meld(np x,np y,int lv){
if(!x)return y;
if(!y)return x;
if(lv==0){
x->cnt|=y->cnt;
}else{
x->propagate(lv);
y->propagate(lv);
rep(k,2)x->ch[k]=meld(x->ch[k],y->ch[k],lv-1);
x->update(lv);
}
return x;
}
np modify(np x,int lv){
if(!x)return x;
if((x->fix&x->both)==0)return x;
assert(lv>0);
if(x->fix&1<<(lv-1)){
int k=(x->off>>(lv-1))&1;
x->ch[k]=meld(x->ch[k],x->ch[k^1],lv-1);
x->ch[k^1]=nullptr;
}
x->propagate(lv);
rep(k,2)x->ch[k]=modify(x->ch[k],lv-1);
x->update(lv);
return x;
}
np dfs1(np x,int l,int r,int lv,int b,int e,int fix,int off){
if(!x||e<=l||r<=b)return x;
if(b<=l&&r<=e){
x->work(fix,off,lv);
int pos=(action(l,fix,off)>>lv)<<lv;
memo.eb(pos,lv,modify(x,lv));
return nullptr;
}
int m=(l+r)/2;
x->propagate(lv);
return mg(
dfs1(x->ch[0],l,m,lv-1,b,e,fix,off),
dfs1(x->ch[1],m,r,lv-1,b,e,fix,off),
lv);
}
np dfs2(np x,int l,int r,int lv,int tarl,int tarlv,np y){
assert(l<=tarl&&tarl<r);
if(lv==tarlv){
assert(l==tarl);
return meld(x,y,lv);
}
if(!x)x=nn();
x->propagate(lv);
int m=(l+r)/2;
if(tarl<m){
x->ch[0]=dfs2(x->ch[0],l,m,lv-1,tarl,tarlv,y);
}else{
x->ch[1]=dfs2(x->ch[1],m,r,lv-1,tarl,tarlv,y);
}
x->update(lv);
return x;
}
np make_change(np root,int b,int e,int fix,int off){
root=dfs1(root,0,1<<L,L,b,e,fix,off);
for(auto [l,lv,ptr]:memo){
root=dfs2(root,0,1<<L,L,l,lv,ptr);
}
memo.clear();
return root;
}
int query(np x,int l,int r,int lv,int b,int e){
if(!x||e<=l||r<=b)return 0;
if(b<=l&&r<=e)return x->cnt;
int m=(l+r)/2;
x->propagate(lv);
return query(x->ch[0],l,m,lv-1,b,e)+query(x->ch[1],m,r,lv-1,b,e);
}
void slv(){
int n,q;cin>>n>>q;
np root=nullptr;
rep(i,n){
int a=read();
root=insert(root,a,L);
}
rep(_,q){
int t;cin>>t;
int l,r;cin>>l>>r;
r++;
if(t<=3){
int x=read(),fix=0,off=0;
if(t==1)fix=mask(L)^x;
else if(t==2)fix=x,off=x;
else off=x;
root=make_change(root,l,r,fix,off);
}else{
int ans=query(root,0,1<<L,L,l,r);
print(ans);
}
}
}
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
cout<<fixed<<setprecision(20);
//int t;cin>>t;rep(_,t)
slv();
}
| ### Prompt
Create a solution in cpp for the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
//#define int ll
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif
template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}
template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;
using pi=pair<int,int>;
using vi=vc<int>;
template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
return os<<"{"<<p.a<<","<<p.b<<"}";
}
template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
os<<"{";
for(auto e:v)os<<e<<",";
return os<<"}";
}
#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))
#ifdef LOCAL
void dmpr(ostream&os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
os<<t<<" ";
dmpr(os,args...);
}
#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)
#else
#define dmp2(...) void(0)
#endif
using uint=unsigned;
using ull=unsigned long long;
template<class t,size_t n>
ostream& operator<<(ostream&os,const array<t,n>&a){
return os<<vc<t>(all(a));
}
template<int i,class T>
void print_tuple(ostream&,const T&){
}
template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
if(i)os<<",";
os<<get<i>(t);
print_tuple<i+1,T,Args...>(os,t);
}
template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
os<<"{";
print_tuple<0,tuple<Args...>,Args...>(os,t);
return os<<"}";
}
template<class t>
void print(t x,int suc=1){
cout<<x;
if(suc==1)
cout<<"\n";
if(suc==2)
cout<<" ";
}
ll read(){
ll i;
cin>>i;
return i;
}
vi readvi(int n,int off=0){
vi v(n);
rep(i,n)v[i]=read()+off;
return v;
}
pi readpi(int off=0){
int a,b;cin>>a>>b;
return pi(a+off,b+off);
}
template<class t,class u>
void print(const pair<t,u>&p,int suc=1){
print(p.a,2);
print(p.b,suc);
}
template<class T>
void print(const vector<T>&v,int suc=1){
rep(i,v.size())
print(v[i],i==int(v.size())-1?suc:2);
}
string readString(){
string s;
cin>>s;
return s;
}
template<class T>
T sq(const T& t){
return t*t;
}
//#define CAPITAL
void yes(bool ex=true){
#ifdef CAPITAL
cout<<"YES"<<"\n";
#else
cout<<"Yes"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void no(bool ex=true){
#ifdef CAPITAL
cout<<"NO"<<"\n";
#else
cout<<"No"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void possible(bool ex=true){
#ifdef CAPITAL
cout<<"POSSIBLE"<<"\n";
#else
cout<<"Possible"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void impossible(bool ex=true){
#ifdef CAPITAL
cout<<"IMPOSSIBLE"<<"\n";
#else
cout<<"Impossible"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
constexpr ll ten(int n){
return n==0?1:ten(n-1)*10;
}
const ll infLL=LLONG_MAX/3;
#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif
int topbit(signed t){
return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
return __builtin_popcount(t);
}
int popcount(ll t){
return __builtin_popcountll(t);
}
bool ispow2(int i){
return i&&(i&-i)==i;
}
ll mask(int i){
return (ll(1)<<i)-1;
}
bool inc(int a,int b,int c){
return a<=b&&b<=c;
}
template<class t> void mkuni(vc<t>&v){
sort(all(v));
v.erase(unique(all(v)),v.ed);
}
ll rand_int(ll l, ll r) { //[l, r]
#ifdef LOCAL
static mt19937_64 gen;
#else
static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
#endif
return uniform_int_distribution<ll>(l, r)(gen);
}
template<class t>
void myshuffle(vc<t>&a){
rep(i,si(a))swap(a[i],a[rand_int(0,i)]);
}
template<class t>
int lwb(const vc<t>&v,const t&a){
return lower_bound(all(v),a)-v.bg;
}
vvc<int> readGraph(int n,int m){
vvc<int> g(n);
rep(i,m){
int a,b;
cin>>a>>b;
//sc.read(a,b);
a--;b--;
g[a].pb(b);
g[b].pb(a);
}
return g;
}
vvc<int> readTree(int n){
return readGraph(n,n-1);
}
const int L=20;
const int M=mask(L);
/*
void clear(int&a,int i){
a&=~(1<<i);
}
*/
int action(int i,int fix,int off){
return ((i^off)&~fix)|(fix&off);
}
struct N{
N*ch[2];
int fix,off,cnt,both;
N():ch{nullptr,nullptr},fix(0),off(0),cnt(0),both(0){}
void work(int f,int o,int lv){
f&=mask(lv);
o&=mask(lv);
off=action(off,f,o);
fix|=f;
}
void update(int lv){
fix=0;
off=0;
cnt=0;
both=0;
rep(k,2)if(ch[k]){
cnt+=ch[k]->cnt;
both|=ch[k]->both;
}
if(ch[0]&&ch[1])both|=1<<(lv-1);
}
void propagate(int lv){
if(fix&1<<(lv-1)){
int k=(off>>(lv-1))&1;
if(ch[k^1])swap(ch[0],ch[1]);
assert(!ch[k^1]);
}else if(off&1<<(lv-1)){
swap(ch[0],ch[1]);
}
rep(k,2)if(ch[k])ch[k]->work(fix,off,lv-1);
fix=0;
off=0;
}
};
N buf[ten(7)];
using np=N*;
N* nn(){
static int head=0;
return buf+head++;
}
np mg(np a,np b,int lv){
if(!a&&!b)return nullptr;
np n=nn();
n->ch[0]=a;
n->ch[1]=b;
n->update(lv);
return n;
}
np insert(np x,int pos,int lv){
if(!x)x=nn();
if(lv==0){
x->cnt=1;
return x;
}
int k=(pos>>(lv-1))&1;
x->propagate(lv);
x->ch[k]=insert(x->ch[k],pos,lv-1);
x->update(lv);
return x;
}
//l,lv,ptr
vc<tuple<int,int,np>> memo;
np meld(np x,np y,int lv){
if(!x)return y;
if(!y)return x;
if(lv==0){
x->cnt|=y->cnt;
}else{
x->propagate(lv);
y->propagate(lv);
rep(k,2)x->ch[k]=meld(x->ch[k],y->ch[k],lv-1);
x->update(lv);
}
return x;
}
np modify(np x,int lv){
if(!x)return x;
if((x->fix&x->both)==0)return x;
assert(lv>0);
if(x->fix&1<<(lv-1)){
int k=(x->off>>(lv-1))&1;
x->ch[k]=meld(x->ch[k],x->ch[k^1],lv-1);
x->ch[k^1]=nullptr;
}
x->propagate(lv);
rep(k,2)x->ch[k]=modify(x->ch[k],lv-1);
x->update(lv);
return x;
}
np dfs1(np x,int l,int r,int lv,int b,int e,int fix,int off){
if(!x||e<=l||r<=b)return x;
if(b<=l&&r<=e){
x->work(fix,off,lv);
int pos=(action(l,fix,off)>>lv)<<lv;
memo.eb(pos,lv,modify(x,lv));
return nullptr;
}
int m=(l+r)/2;
x->propagate(lv);
return mg(
dfs1(x->ch[0],l,m,lv-1,b,e,fix,off),
dfs1(x->ch[1],m,r,lv-1,b,e,fix,off),
lv);
}
np dfs2(np x,int l,int r,int lv,int tarl,int tarlv,np y){
assert(l<=tarl&&tarl<r);
if(lv==tarlv){
assert(l==tarl);
return meld(x,y,lv);
}
if(!x)x=nn();
x->propagate(lv);
int m=(l+r)/2;
if(tarl<m){
x->ch[0]=dfs2(x->ch[0],l,m,lv-1,tarl,tarlv,y);
}else{
x->ch[1]=dfs2(x->ch[1],m,r,lv-1,tarl,tarlv,y);
}
x->update(lv);
return x;
}
np make_change(np root,int b,int e,int fix,int off){
root=dfs1(root,0,1<<L,L,b,e,fix,off);
for(auto [l,lv,ptr]:memo){
root=dfs2(root,0,1<<L,L,l,lv,ptr);
}
memo.clear();
return root;
}
int query(np x,int l,int r,int lv,int b,int e){
if(!x||e<=l||r<=b)return 0;
if(b<=l&&r<=e)return x->cnt;
int m=(l+r)/2;
x->propagate(lv);
return query(x->ch[0],l,m,lv-1,b,e)+query(x->ch[1],m,r,lv-1,b,e);
}
void slv(){
int n,q;cin>>n>>q;
np root=nullptr;
rep(i,n){
int a=read();
root=insert(root,a,L);
}
rep(_,q){
int t;cin>>t;
int l,r;cin>>l>>r;
r++;
if(t<=3){
int x=read(),fix=0,off=0;
if(t==1)fix=mask(L)^x;
else if(t==2)fix=x,off=x;
else off=x;
root=make_change(root,l,r,fix,off);
}else{
int ans=query(root,0,1<<L,L,l,r);
print(ans);
}
}
}
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
cout<<fixed<<setprecision(20);
//int t;cin>>t;rep(_,t)
slv();
}
``` |
#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
struct node {
node *l,*r;
int z,f,s;
int a,b;
node() { l = r = NULL,z = f = a = b = 0,s = 1; }
};
int c = 0;
node mem[1 << 22];
node *all = NULL;
node *newNode() {
if (all != NULL) {
node *t = all;
all = all->l;
t->l = t->r = NULL,t->z = t->f = t->a = t->b = 0,t->s = 1;
return t;
}
else return &mem[c++];
}
int delNode(node *n) {
n->l = all,all = n;
return 0;
}
node *root = NULL;
int size(node *n) {
if (n != NULL) return n->s;
else return 0;
}
int update(node *n,int d) {
if (n != NULL) {
n->s = size(n->l) + size(n->r);
if (n->s == 0) n->s = 1;
n->a = ((n->l == NULL) ? 0:n->l->a|(1 << d)) | ((n->r == NULL) ? 0:n->r->a);
n->b = ((n->r == NULL) ? 0:n->r->b|(1 << d)) | ((n->l == NULL) ? 0:n->l->b);
}
return 0;
}
int prop(node *n,int d);
int merge2(node *l,node *r,node *&n,int d) {
prop(l,d),prop(r,d);
if (l == NULL) n = r;
else if (r == NULL) n = l;
else {
if (n == NULL) n = newNode();
merge2(l->l,r->l,n->l,d-1);
merge2(l->r,r->r,n->r,d-1);
delNode(l);
delNode(r);
}
update(n,d);
return 0;
}
int merge(node *l,node *r,node *&n,int d) {
node *t = NULL;
merge2(l,r,t,d);
n = t;
return 0;
}
int prop(node *n,int d) {
if (n != NULL) {
if (n->l != NULL) {
n->l->f &= ~n->z;
n->l->z |= n->z;
n->l->f ^= n->f;
}
if (n->r != NULL) {
n->r->f &= ~n->z;
n->r->z |= n->z;
n->r->f ^= n->f;
}
if (d >= 0) {
if (n->z & (1 << d)) merge(n->l,n->r,n->l,d-1),n->r = NULL;
if (n->f & (1 << d)) swap(n->l,n->r);
}
if ((n->a & n->b & n->z) & ((1 << (d+1))-1)) prop(n->l,d-1),prop(n->r,d-1);
n->z = n->f = 0;
update(n,d);
}
return 0;
}
int split(node *n,node *&l,node *&r,int s,int d) {
prop(n,d);
if (n == NULL) l = r = NULL;
else if (d < 0) l = n,r = NULL;
else if (s & (1 << d)) {
split(n->r,n->r,r,s,d-1);
if ((n->l == NULL) && (n->r == NULL)) {
delNode(n);
l = NULL;
}
else l = n;
if (r != NULL) {
node *t = newNode();
t->r = r,r = t;
}
}
else {
split(n->l,l,n->l,s,d-1);
if ((n->l == NULL) && (n->r == NULL)) {
delNode(n);
r = NULL;
}
else r = n;
if (l != NULL) {
node *t = newNode();
t->l = l,l = t;
}
}
update(l,d),update(r,d);
return 0;
}
int main() {
int i,j;
int n,q,a;
int t,l,r,x;
scanf("%d %d",&n,&q);
for (i = 0; i < n; i++) {
scanf("%d",&a);
node *n = newNode(),*r = n;
for (j = 19; j >= 0; j--) {
if (a & (1 << j)) n->r = newNode(),n = n->r;
else n->l = newNode(),n = n->l;
}
merge(root,r,root,19);
}
for (i = 0; i < q; i++) {
scanf("%d",&t);
if (t == 1) {
scanf("%d %d %d",&l,&r,&x);
node *L,*M,*R;
if (l > 0) split(root,L,M,l-1,19);
else L = NULL,M = root;
split(M,M,R,r,19);
if (M != NULL) M->z = ~x,M->f = 0;
merge(L,M,M,19);
merge(M,R,root,19);
}
else if (t == 2) {
scanf("%d %d %d",&l,&r,&x);
if (l > r) swap(l,r);
node *L,*M,*R;
if (l > 0) split(root,L,M,l-1,19);
else L = NULL,M = root;
split(M,M,R,r,19);
if (M != NULL) M->z = x,M->f = x;
merge(L,M,M,19);
merge(M,R,root,19);
}
else if (t == 3) {
scanf("%d %d %d",&l,&r,&x);
node *L,*M,*R;
if (l > 0) split(root,L,M,l-1,19);
else L = NULL,M = root;
split(M,M,R,r,19);
if (M != NULL) M->z = 0,M->f = x;
merge(L,M,M,19);
merge(M,R,root,19);
}
else {
scanf("%d %d",&l,&r);
if (l > r) swap(l,r);
node *L,*M,*R;
if (l > 0) split(root,L,M,l-1,19);
else L = NULL,M = root;
split(M,M,R,r,19);
printf("%d\n",size(M));
merge(L,M,M,19);
merge(M,R,root,19);
}
}
return 0;
}
| ### Prompt
Create a solution in cpp for the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
struct node {
node *l,*r;
int z,f,s;
int a,b;
node() { l = r = NULL,z = f = a = b = 0,s = 1; }
};
int c = 0;
node mem[1 << 22];
node *all = NULL;
node *newNode() {
if (all != NULL) {
node *t = all;
all = all->l;
t->l = t->r = NULL,t->z = t->f = t->a = t->b = 0,t->s = 1;
return t;
}
else return &mem[c++];
}
int delNode(node *n) {
n->l = all,all = n;
return 0;
}
node *root = NULL;
int size(node *n) {
if (n != NULL) return n->s;
else return 0;
}
int update(node *n,int d) {
if (n != NULL) {
n->s = size(n->l) + size(n->r);
if (n->s == 0) n->s = 1;
n->a = ((n->l == NULL) ? 0:n->l->a|(1 << d)) | ((n->r == NULL) ? 0:n->r->a);
n->b = ((n->r == NULL) ? 0:n->r->b|(1 << d)) | ((n->l == NULL) ? 0:n->l->b);
}
return 0;
}
int prop(node *n,int d);
int merge2(node *l,node *r,node *&n,int d) {
prop(l,d),prop(r,d);
if (l == NULL) n = r;
else if (r == NULL) n = l;
else {
if (n == NULL) n = newNode();
merge2(l->l,r->l,n->l,d-1);
merge2(l->r,r->r,n->r,d-1);
delNode(l);
delNode(r);
}
update(n,d);
return 0;
}
int merge(node *l,node *r,node *&n,int d) {
node *t = NULL;
merge2(l,r,t,d);
n = t;
return 0;
}
int prop(node *n,int d) {
if (n != NULL) {
if (n->l != NULL) {
n->l->f &= ~n->z;
n->l->z |= n->z;
n->l->f ^= n->f;
}
if (n->r != NULL) {
n->r->f &= ~n->z;
n->r->z |= n->z;
n->r->f ^= n->f;
}
if (d >= 0) {
if (n->z & (1 << d)) merge(n->l,n->r,n->l,d-1),n->r = NULL;
if (n->f & (1 << d)) swap(n->l,n->r);
}
if ((n->a & n->b & n->z) & ((1 << (d+1))-1)) prop(n->l,d-1),prop(n->r,d-1);
n->z = n->f = 0;
update(n,d);
}
return 0;
}
int split(node *n,node *&l,node *&r,int s,int d) {
prop(n,d);
if (n == NULL) l = r = NULL;
else if (d < 0) l = n,r = NULL;
else if (s & (1 << d)) {
split(n->r,n->r,r,s,d-1);
if ((n->l == NULL) && (n->r == NULL)) {
delNode(n);
l = NULL;
}
else l = n;
if (r != NULL) {
node *t = newNode();
t->r = r,r = t;
}
}
else {
split(n->l,l,n->l,s,d-1);
if ((n->l == NULL) && (n->r == NULL)) {
delNode(n);
r = NULL;
}
else r = n;
if (l != NULL) {
node *t = newNode();
t->l = l,l = t;
}
}
update(l,d),update(r,d);
return 0;
}
int main() {
int i,j;
int n,q,a;
int t,l,r,x;
scanf("%d %d",&n,&q);
for (i = 0; i < n; i++) {
scanf("%d",&a);
node *n = newNode(),*r = n;
for (j = 19; j >= 0; j--) {
if (a & (1 << j)) n->r = newNode(),n = n->r;
else n->l = newNode(),n = n->l;
}
merge(root,r,root,19);
}
for (i = 0; i < q; i++) {
scanf("%d",&t);
if (t == 1) {
scanf("%d %d %d",&l,&r,&x);
node *L,*M,*R;
if (l > 0) split(root,L,M,l-1,19);
else L = NULL,M = root;
split(M,M,R,r,19);
if (M != NULL) M->z = ~x,M->f = 0;
merge(L,M,M,19);
merge(M,R,root,19);
}
else if (t == 2) {
scanf("%d %d %d",&l,&r,&x);
if (l > r) swap(l,r);
node *L,*M,*R;
if (l > 0) split(root,L,M,l-1,19);
else L = NULL,M = root;
split(M,M,R,r,19);
if (M != NULL) M->z = x,M->f = x;
merge(L,M,M,19);
merge(M,R,root,19);
}
else if (t == 3) {
scanf("%d %d %d",&l,&r,&x);
node *L,*M,*R;
if (l > 0) split(root,L,M,l-1,19);
else L = NULL,M = root;
split(M,M,R,r,19);
if (M != NULL) M->z = 0,M->f = x;
merge(L,M,M,19);
merge(M,R,root,19);
}
else {
scanf("%d %d",&l,&r);
if (l > r) swap(l,r);
node *L,*M,*R;
if (l > 0) split(root,L,M,l-1,19);
else L = NULL,M = root;
split(M,M,R,r,19);
printf("%d\n",size(M));
merge(L,M,M,19);
merge(M,R,root,19);
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
t=0; char ch=getchar(); int f=1;
while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
const int maxn=(1e7)+10;
int n,q,a[200010];
int rt,tot,N;
int ls[maxn],rs[maxn];
int lazy[maxn];
int And[maxn],Or[maxn],tr[maxn];
void pushup(int root) {
And[root]=And[ls[root]]|And[rs[root]];
Or[root]=Or[ls[root]]|Or[rs[root]];
tr[root]=tr[ls[root]]+tr[rs[root]];
}
void insert(int dep,int &root,int x) {
if (!root) root=++tot;
if (dep==0) { tr[root]=1; And[root]=N-x,Or[root]=x; return; }
if (x>>(dep-1)&1) insert(dep-1,rs[root],x);
else insert(dep-1,ls[root],x);
pushup(root);
}
void update_xor(int dep,int root,int x) {
if (root==0) return;
//printf("%d %d %d\n",dep,root,x);
if (x>>(dep-1)&1) swap(ls[root],rs[root]);
int A=And[root],O=Or[root];
And[root]=(A&(N-x))|(O&x);
Or[root]=(O&(N-x))|(A&x);
lazy[root]^=x;
}
void pushdown(int dep,int root) {
if (lazy[root]==0) return;
update_xor(dep-1,ls[root],lazy[root]);
update_xor(dep-1,rs[root],lazy[root]);
lazy[root]=0;
}
void merge(int dep,int &x,int &y) {
//printf("%d %d %d,%d %d\n",dep,x,y,ls[x],rs[x]);
if (!x) { x=y; return; }
if (!y||!dep) return;
pushdown(dep,x),pushdown(dep,y);
merge(dep-1,ls[x],ls[y]);
merge(dep-1,rs[x],rs[y]);
pushup(x);
}
int query(int dep,int L,int R,int l,int r,int root) {
if (!root) return 0;
if (L<=l&&r<=R) return tr[root];
pushdown(dep,root);
int mid=(l+r)>>1,res=0;
if (L<=mid) res=query(dep-1,L,R,l,mid,ls[root]);
if (mid<R) res+=query(dep-1,L,R,mid+1,r,rs[root]);
return res;
}
void update_or(int dep,int root,int x) {
if (!root) return;
if ((x&And[root]&Or[root])==0) {
update_xor(dep,root,x&And[root]);
return;
}
pushdown(dep,root);
if (x>>(dep-1)&1) {
update_xor(dep-1,ls[root],1<<(dep-1));
merge(dep-1,rs[root],ls[root]);
ls[root]=0;
}
update_or(dep-1,ls[root],x);
update_or(dep-1,rs[root],x);
pushup(root);
}
void split(int dep,int L,int R,int &x,int &y,int l,int r) {
if (x==0||l>R||r<L) { y=0; return; }
if (L<=l&&r<=R) { y=x; x=0; return; }
int mid=(l+r)>>1;
pushdown(dep,x); y=++tot;
split(dep-1,L,R,ls[x],ls[y],l,mid);
split(dep-1,L,R,rs[x],rs[y],mid+1,r);
pushup(x),pushup(y);
}
int main() {
//freopen("1.txt","r",stdin);
read(n),read(q); N=(1<<20)-1;
for (int i=1;i<=n;i++) read(a[i]);
for (int i=1;i<=n;i++) insert(20,rt,a[i]);
int op,l,r,x;
while (q--) {
read(op),read(l),read(r);
if (op==4) {
printf("%d\n",query(20,l,r,0,N,rt));
continue;
}
read(x); int now;
split(20,l,r,rt,now,0,N);
if (op==1) update_xor(20,now,N),update_or(20,now,N-x),update_xor(20,now,N);
else if (op==2) update_or(20,now,x);
else update_xor(20,now,x);
merge(20,rt,now);
}
return 0;
}
/*
REMEMBER:
1. Think TWICE, Code ONCE!
Are there any counterexamples to your algo?
2. Be careful about the BOUNDARIES!
N=1? P=1? Something about 0?
3. Do not make STUPID MISTAKES!
Array size? Integer overflow? Time complexity? Memory usage? Precision error?
*/ | ### Prompt
Your challenge is to write a CPP solution to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
t=0; char ch=getchar(); int f=1;
while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
const int maxn=(1e7)+10;
int n,q,a[200010];
int rt,tot,N;
int ls[maxn],rs[maxn];
int lazy[maxn];
int And[maxn],Or[maxn],tr[maxn];
void pushup(int root) {
And[root]=And[ls[root]]|And[rs[root]];
Or[root]=Or[ls[root]]|Or[rs[root]];
tr[root]=tr[ls[root]]+tr[rs[root]];
}
void insert(int dep,int &root,int x) {
if (!root) root=++tot;
if (dep==0) { tr[root]=1; And[root]=N-x,Or[root]=x; return; }
if (x>>(dep-1)&1) insert(dep-1,rs[root],x);
else insert(dep-1,ls[root],x);
pushup(root);
}
void update_xor(int dep,int root,int x) {
if (root==0) return;
//printf("%d %d %d\n",dep,root,x);
if (x>>(dep-1)&1) swap(ls[root],rs[root]);
int A=And[root],O=Or[root];
And[root]=(A&(N-x))|(O&x);
Or[root]=(O&(N-x))|(A&x);
lazy[root]^=x;
}
void pushdown(int dep,int root) {
if (lazy[root]==0) return;
update_xor(dep-1,ls[root],lazy[root]);
update_xor(dep-1,rs[root],lazy[root]);
lazy[root]=0;
}
void merge(int dep,int &x,int &y) {
//printf("%d %d %d,%d %d\n",dep,x,y,ls[x],rs[x]);
if (!x) { x=y; return; }
if (!y||!dep) return;
pushdown(dep,x),pushdown(dep,y);
merge(dep-1,ls[x],ls[y]);
merge(dep-1,rs[x],rs[y]);
pushup(x);
}
int query(int dep,int L,int R,int l,int r,int root) {
if (!root) return 0;
if (L<=l&&r<=R) return tr[root];
pushdown(dep,root);
int mid=(l+r)>>1,res=0;
if (L<=mid) res=query(dep-1,L,R,l,mid,ls[root]);
if (mid<R) res+=query(dep-1,L,R,mid+1,r,rs[root]);
return res;
}
void update_or(int dep,int root,int x) {
if (!root) return;
if ((x&And[root]&Or[root])==0) {
update_xor(dep,root,x&And[root]);
return;
}
pushdown(dep,root);
if (x>>(dep-1)&1) {
update_xor(dep-1,ls[root],1<<(dep-1));
merge(dep-1,rs[root],ls[root]);
ls[root]=0;
}
update_or(dep-1,ls[root],x);
update_or(dep-1,rs[root],x);
pushup(root);
}
void split(int dep,int L,int R,int &x,int &y,int l,int r) {
if (x==0||l>R||r<L) { y=0; return; }
if (L<=l&&r<=R) { y=x; x=0; return; }
int mid=(l+r)>>1;
pushdown(dep,x); y=++tot;
split(dep-1,L,R,ls[x],ls[y],l,mid);
split(dep-1,L,R,rs[x],rs[y],mid+1,r);
pushup(x),pushup(y);
}
int main() {
//freopen("1.txt","r",stdin);
read(n),read(q); N=(1<<20)-1;
for (int i=1;i<=n;i++) read(a[i]);
for (int i=1;i<=n;i++) insert(20,rt,a[i]);
int op,l,r,x;
while (q--) {
read(op),read(l),read(r);
if (op==4) {
printf("%d\n",query(20,l,r,0,N,rt));
continue;
}
read(x); int now;
split(20,l,r,rt,now,0,N);
if (op==1) update_xor(20,now,N),update_or(20,now,N-x),update_xor(20,now,N);
else if (op==2) update_or(20,now,x);
else update_xor(20,now,x);
merge(20,rt,now);
}
return 0;
}
/*
REMEMBER:
1. Think TWICE, Code ONCE!
Are there any counterexamples to your algo?
2. Be careful about the BOUNDARIES!
N=1? P=1? Something about 0?
3. Do not make STUPID MISTAKES!
Array size? Integer overflow? Time complexity? Memory usage? Precision error?
*/
``` |
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=200010,D=20,M=N*D,Mx=(1<<D)-1;
int n,m;
namespace trie{
int val[M],ls[M],rs[M],tot,tag[M];
int h0[M],h1[M];
void setg(int u,int v,int d=D-1)
{
if(!u || d<0) return;
if(v>>d&1) swap(ls[u],rs[u]);
int p=(h0[u]^h1[u])&v;
h0[u]^=p;h1[u]^=p;
tag[u]^=v;
}
int ton[M],tp;
void del(int u)
{
h0[u]=h1[u]=ls[u]=rs[u]=tag[u]=val[u]=0;
ton[++tp]=u;
}
int node(){return tp?ton[tp--]:++tot;}
void push(int u,int d)
{
if(tag[u]) setg(ls[u],tag[u],d-1),setg(rs[u],tag[u],d-1),tag[u]=0;
}
void upd(int u,int d)
{
val[u]=val[ls[u]]+val[rs[u]];
h0[u]=h0[ls[u]]|h0[rs[u]],h1[u]=h1[ls[u]]|h1[rs[u]];
if(ls[u]) h0[u]|=1<<d;
if(rs[u]) h1[u]|=1<<d;
}
void insert(int &u,int x,int d=D-1)
{
if(!u) u=++tot;
if(d<0){val[u]=1;return;}
if(x>>d&1) insert(rs[u],x,d-1);
else insert(ls[u],x,d-1);
upd(u,d);
}
int merge(int x,int y,int d=D-1)
{
if(!x || !y) return x+y;
if(d<0){val[x]|=val[y];del(y);return x;}
push(x,d);push(y,d);
ls[x]=merge(ls[x],ls[y],d-1);
rs[x]=merge(rs[x],rs[y],d-1);
upd(x,d);del(y);
return x;
}
void split(int u,int k,int &l,int &r,int d=D-1)
{
if(d==-1){l=u;r=0;return;}
if(!u){l=r=0;return;}
push(u,d);
if(k>>d&1) l=u,r=node(),split(rs[u],k,rs[l],rs[r],d-1);
else l=node(),r=u,split(ls[u],k,ls[l],ls[r],d-1);
upd(l,d);upd(r,d);
}
void set_k(int u,int k,int d=D-1)//set all 0 in k to 1
{
if(!u || !(h0[u]>>k&1)) return;
if(!(h1[u]>>k&1)){setg(u,1<<k,d);return;}
push(u,d);
if(d==k){rs[u]=merge(rs[u],ls[u],d-1);ls[u]=0;upd(u,d);return;}
set_k(ls[u],k,d-1);set_k(rs[u],k,d-1);
upd(u,d);
}
void reset_k(int u,int k,int d=D-1)//set all 1 in k to 0
{
if(!u || !(h1[u]>>k&1)) return;
if(!(h0[u]>>k&1)){setg(u,1<<k,d);return;}
push(u,d);
if(d==k){ls[u]=merge(rs[u],ls[u],d-1);rs[u]=0;upd(u,d);return;}
reset_k(ls[u],k,d-1);reset_k(rs[u],k,d-1);
upd(u,d);
}
}
using trie::insert;using trie::merge;using trie::split;
using trie::setg;using trie::set_k;using trie::reset_k;
int root;
void set_xor(int l,int r,int x)
{
int lt=0,rt=0;
if(l) split(root,l-1,lt,root);
split(root,r,root,rt);
setg(root,x);
root=merge(merge(lt,root),rt);
}
void set_or(int l,int r,int x)
{
int lt=0,rt=0;
if(l) split(root,l-1,lt,root);
split(root,r,root,rt);
for(int i=0;i<D;i++) if(x>>i&1) set_k(root,i);
root=merge(merge(lt,root),rt);
}
void set_and(int l,int r,int x)
{
int lt=0,rt=0;
if(l) split(root,l-1,lt,root);
split(root,r,root,rt);
for(int i=0;i<D;i++) if(!(x>>i&1)) reset_k(root,i);
root=merge(merge(lt,root),rt);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1,x;i<=n;i++) scanf("%d",&x),trie::insert(root,x);
while(m --> 0)
{
int op,l,r,x;
scanf("%d%d%d",&op,&l,&r);
if(op==1) scanf("%d",&x),set_and(l,r,x);
else if(op==2) scanf("%d",&x),set_or(l,r,x);
else if(op==3) scanf("%d",&x),set_xor(l,r,x);
else
{
int lt=0,rt=0;
if(l) split(root,l-1,lt,root);
split(root,r,root,rt);
printf("%d\n",trie::val[root]);
root=merge(merge(lt,root),rt);
}
}
return 0;
} | ### Prompt
Create a solution in CPP for the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=200010,D=20,M=N*D,Mx=(1<<D)-1;
int n,m;
namespace trie{
int val[M],ls[M],rs[M],tot,tag[M];
int h0[M],h1[M];
void setg(int u,int v,int d=D-1)
{
if(!u || d<0) return;
if(v>>d&1) swap(ls[u],rs[u]);
int p=(h0[u]^h1[u])&v;
h0[u]^=p;h1[u]^=p;
tag[u]^=v;
}
int ton[M],tp;
void del(int u)
{
h0[u]=h1[u]=ls[u]=rs[u]=tag[u]=val[u]=0;
ton[++tp]=u;
}
int node(){return tp?ton[tp--]:++tot;}
void push(int u,int d)
{
if(tag[u]) setg(ls[u],tag[u],d-1),setg(rs[u],tag[u],d-1),tag[u]=0;
}
void upd(int u,int d)
{
val[u]=val[ls[u]]+val[rs[u]];
h0[u]=h0[ls[u]]|h0[rs[u]],h1[u]=h1[ls[u]]|h1[rs[u]];
if(ls[u]) h0[u]|=1<<d;
if(rs[u]) h1[u]|=1<<d;
}
void insert(int &u,int x,int d=D-1)
{
if(!u) u=++tot;
if(d<0){val[u]=1;return;}
if(x>>d&1) insert(rs[u],x,d-1);
else insert(ls[u],x,d-1);
upd(u,d);
}
int merge(int x,int y,int d=D-1)
{
if(!x || !y) return x+y;
if(d<0){val[x]|=val[y];del(y);return x;}
push(x,d);push(y,d);
ls[x]=merge(ls[x],ls[y],d-1);
rs[x]=merge(rs[x],rs[y],d-1);
upd(x,d);del(y);
return x;
}
void split(int u,int k,int &l,int &r,int d=D-1)
{
if(d==-1){l=u;r=0;return;}
if(!u){l=r=0;return;}
push(u,d);
if(k>>d&1) l=u,r=node(),split(rs[u],k,rs[l],rs[r],d-1);
else l=node(),r=u,split(ls[u],k,ls[l],ls[r],d-1);
upd(l,d);upd(r,d);
}
void set_k(int u,int k,int d=D-1)//set all 0 in k to 1
{
if(!u || !(h0[u]>>k&1)) return;
if(!(h1[u]>>k&1)){setg(u,1<<k,d);return;}
push(u,d);
if(d==k){rs[u]=merge(rs[u],ls[u],d-1);ls[u]=0;upd(u,d);return;}
set_k(ls[u],k,d-1);set_k(rs[u],k,d-1);
upd(u,d);
}
void reset_k(int u,int k,int d=D-1)//set all 1 in k to 0
{
if(!u || !(h1[u]>>k&1)) return;
if(!(h0[u]>>k&1)){setg(u,1<<k,d);return;}
push(u,d);
if(d==k){ls[u]=merge(rs[u],ls[u],d-1);rs[u]=0;upd(u,d);return;}
reset_k(ls[u],k,d-1);reset_k(rs[u],k,d-1);
upd(u,d);
}
}
using trie::insert;using trie::merge;using trie::split;
using trie::setg;using trie::set_k;using trie::reset_k;
int root;
void set_xor(int l,int r,int x)
{
int lt=0,rt=0;
if(l) split(root,l-1,lt,root);
split(root,r,root,rt);
setg(root,x);
root=merge(merge(lt,root),rt);
}
void set_or(int l,int r,int x)
{
int lt=0,rt=0;
if(l) split(root,l-1,lt,root);
split(root,r,root,rt);
for(int i=0;i<D;i++) if(x>>i&1) set_k(root,i);
root=merge(merge(lt,root),rt);
}
void set_and(int l,int r,int x)
{
int lt=0,rt=0;
if(l) split(root,l-1,lt,root);
split(root,r,root,rt);
for(int i=0;i<D;i++) if(!(x>>i&1)) reset_k(root,i);
root=merge(merge(lt,root),rt);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1,x;i<=n;i++) scanf("%d",&x),trie::insert(root,x);
while(m --> 0)
{
int op,l,r,x;
scanf("%d%d%d",&op,&l,&r);
if(op==1) scanf("%d",&x),set_and(l,r,x);
else if(op==2) scanf("%d",&x),set_or(l,r,x);
else if(op==3) scanf("%d",&x),set_xor(l,r,x);
else
{
int lt=0,rt=0;
if(l) split(root,l-1,lt,root);
split(root,r,root,rt);
printf("%d\n",trie::val[root]);
root=merge(merge(lt,root),rt);
}
}
return 0;
}
``` |
#include <cstdio>
#include <algorithm>
const int Maxn=200000;
const int Maxv=(1<<20);
const int Maxk=20;
int n,q;
int a[Maxn+5];
int Root,id_tot;
struct Trie_Node{
int ch[2];
int val_0,val_1;
int sum;
int lazy;
}seg[Maxn*Maxk+5];
int st[Maxn*Maxk+5],st_top;
int new_node(){
int u;
if(st_top>0){
u=st[st_top--];
}
else{
u=++id_tot;
}
seg[u].ch[0]=seg[u].ch[1]=0;
seg[u].val_0=seg[u].val_1=0;
seg[u].sum=0;
seg[u].lazy=0;
return u;
}
void del_node(int u){
st[++st_top]=u;
}
void push_up(int root,int dep){
seg[root].sum=seg[seg[root].ch[0]].sum+seg[seg[root].ch[1]].sum;
seg[root].val_0=seg[seg[root].ch[0]].val_0|seg[seg[root].ch[1]].val_0;
seg[root].val_1=seg[seg[root].ch[0]].val_1|seg[seg[root].ch[1]].val_1;
if(seg[root].ch[0]){
seg[root].val_0|=(1<<dep);
}
if(seg[root].ch[1]){
seg[root].val_1|=(1<<dep);
}
}
void insert(int val,int &root=Root,int dep=Maxk-1){
if(root==0){
root=new_node();
}
if(dep==-1){
seg[root].sum=1;
return;
}
insert(val,seg[root].ch[(val>>dep)&1],dep-1);
push_up(root,dep);
}
void update_tag(int root,int v,int dep){
if(root==0||dep==-1){
return;
}
if((v>>dep)&1){
std::swap(seg[root].ch[0],seg[root].ch[1]);
}
int tmp=(seg[root].val_0^seg[root].val_1)&v;
seg[root].val_0^=tmp,seg[root].val_1^=tmp;
seg[root].lazy^=v;
}
void push_down(int root,int dep){
if(seg[root].lazy){
update_tag(seg[root].ch[0],seg[root].lazy,dep-1);
update_tag(seg[root].ch[1],seg[root].lazy,dep-1);
seg[root].lazy=0;
}
}
int trie_merge(int root_1,int root_2,int dep=Maxk-1){
if(root_1==0||root_2==0){
return root_1^root_2;
}
if(dep==-1){
del_node(root_2);
return root_1;
}
push_down(root_1,dep);
push_down(root_2,dep);
seg[root_1].ch[0]=trie_merge(seg[root_1].ch[0],seg[root_2].ch[0],dep-1);
seg[root_1].ch[1]=trie_merge(seg[root_1].ch[1],seg[root_2].ch[1],dep-1);
del_node(root_2);
push_up(root_1,dep);
return root_1;
}
void trie_split(int root,int k,int &l_root,int &r_root,int dep=Maxk-1){
if(root==0){
l_root=r_root=0;
return;
}
if(dep==-1){
l_root=root;
r_root=0;
return;
}
push_down(root,dep);
if((k>>dep)&1){
l_root=root;
r_root=new_node();
trie_split(seg[root].ch[1],k,seg[l_root].ch[1],seg[r_root].ch[1],dep-1);
}
else{
l_root=new_node();
r_root=root;
trie_split(seg[root].ch[0],k,seg[l_root].ch[0],seg[r_root].ch[0],dep-1);
}
push_up(l_root,dep);
push_up(r_root,dep);
}
void push_or(int root,int k,int dep=Maxk-1){
if(root==0||((seg[root].val_0>>k)&1)==0){
return;
}
if(((seg[root].val_1>>k)&1)==0){
update_tag(root,1<<k,dep);
return;
}
push_down(root,dep);
if(dep==k){
seg[root].ch[1]=trie_merge(seg[root].ch[0],seg[root].ch[1],dep-1);
seg[root].ch[0]=0;
push_up(root,dep);
return;
}
push_or(seg[root].ch[0],k,dep-1);
push_or(seg[root].ch[1],k,dep-1);
push_up(root,dep);
}
void push_and(int root,int k,int dep=Maxk-1){
if(root==0||((seg[root].val_1>>k)&1)==0){
return;
}
if(((seg[root].val_0>>k)&1)==0){
update_tag(root,1<<k,dep);
return;
}
push_down(root,dep);
if(dep==k){
seg[root].ch[0]=trie_merge(seg[root].ch[0],seg[root].ch[1],dep-1);
seg[root].ch[1]=0;
push_up(root,dep);
return;
}
push_and(seg[root].ch[0],k,dep-1);
push_and(seg[root].ch[1],k,dep-1);
push_up(root,dep);
}
int main(){
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
insert(a[i]);
}
for(int t=1;t<=q;t++){
int op;
scanf("%d",&op);
int l,r;
scanf("%d%d",&l,&r);
int l_root=0,r_root=0;
if(l){
trie_split(Root,l-1,l_root,Root);
}
trie_split(Root,r,Root,r_root);
if(op==1){
int x;
scanf("%d",&x);
for(int i=0;i<Maxk;i++){
if(((x>>i)&1)==0){
push_and(Root,i);
}
}
}
else if(op==2){
int x;
scanf("%d",&x);
for(int i=0;i<Maxk;i++){
if(((x>>i)&1)==1){
push_or(Root,i);
}
}
}
else if(op==3){
int x;
scanf("%d",&x);
update_tag(Root,x,Maxk-1);
}
else{
printf("%d\n",seg[Root].sum);
}
Root=trie_merge(trie_merge(l_root,Root),r_root);
}
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <cstdio>
#include <algorithm>
const int Maxn=200000;
const int Maxv=(1<<20);
const int Maxk=20;
int n,q;
int a[Maxn+5];
int Root,id_tot;
struct Trie_Node{
int ch[2];
int val_0,val_1;
int sum;
int lazy;
}seg[Maxn*Maxk+5];
int st[Maxn*Maxk+5],st_top;
int new_node(){
int u;
if(st_top>0){
u=st[st_top--];
}
else{
u=++id_tot;
}
seg[u].ch[0]=seg[u].ch[1]=0;
seg[u].val_0=seg[u].val_1=0;
seg[u].sum=0;
seg[u].lazy=0;
return u;
}
void del_node(int u){
st[++st_top]=u;
}
void push_up(int root,int dep){
seg[root].sum=seg[seg[root].ch[0]].sum+seg[seg[root].ch[1]].sum;
seg[root].val_0=seg[seg[root].ch[0]].val_0|seg[seg[root].ch[1]].val_0;
seg[root].val_1=seg[seg[root].ch[0]].val_1|seg[seg[root].ch[1]].val_1;
if(seg[root].ch[0]){
seg[root].val_0|=(1<<dep);
}
if(seg[root].ch[1]){
seg[root].val_1|=(1<<dep);
}
}
void insert(int val,int &root=Root,int dep=Maxk-1){
if(root==0){
root=new_node();
}
if(dep==-1){
seg[root].sum=1;
return;
}
insert(val,seg[root].ch[(val>>dep)&1],dep-1);
push_up(root,dep);
}
void update_tag(int root,int v,int dep){
if(root==0||dep==-1){
return;
}
if((v>>dep)&1){
std::swap(seg[root].ch[0],seg[root].ch[1]);
}
int tmp=(seg[root].val_0^seg[root].val_1)&v;
seg[root].val_0^=tmp,seg[root].val_1^=tmp;
seg[root].lazy^=v;
}
void push_down(int root,int dep){
if(seg[root].lazy){
update_tag(seg[root].ch[0],seg[root].lazy,dep-1);
update_tag(seg[root].ch[1],seg[root].lazy,dep-1);
seg[root].lazy=0;
}
}
int trie_merge(int root_1,int root_2,int dep=Maxk-1){
if(root_1==0||root_2==0){
return root_1^root_2;
}
if(dep==-1){
del_node(root_2);
return root_1;
}
push_down(root_1,dep);
push_down(root_2,dep);
seg[root_1].ch[0]=trie_merge(seg[root_1].ch[0],seg[root_2].ch[0],dep-1);
seg[root_1].ch[1]=trie_merge(seg[root_1].ch[1],seg[root_2].ch[1],dep-1);
del_node(root_2);
push_up(root_1,dep);
return root_1;
}
void trie_split(int root,int k,int &l_root,int &r_root,int dep=Maxk-1){
if(root==0){
l_root=r_root=0;
return;
}
if(dep==-1){
l_root=root;
r_root=0;
return;
}
push_down(root,dep);
if((k>>dep)&1){
l_root=root;
r_root=new_node();
trie_split(seg[root].ch[1],k,seg[l_root].ch[1],seg[r_root].ch[1],dep-1);
}
else{
l_root=new_node();
r_root=root;
trie_split(seg[root].ch[0],k,seg[l_root].ch[0],seg[r_root].ch[0],dep-1);
}
push_up(l_root,dep);
push_up(r_root,dep);
}
void push_or(int root,int k,int dep=Maxk-1){
if(root==0||((seg[root].val_0>>k)&1)==0){
return;
}
if(((seg[root].val_1>>k)&1)==0){
update_tag(root,1<<k,dep);
return;
}
push_down(root,dep);
if(dep==k){
seg[root].ch[1]=trie_merge(seg[root].ch[0],seg[root].ch[1],dep-1);
seg[root].ch[0]=0;
push_up(root,dep);
return;
}
push_or(seg[root].ch[0],k,dep-1);
push_or(seg[root].ch[1],k,dep-1);
push_up(root,dep);
}
void push_and(int root,int k,int dep=Maxk-1){
if(root==0||((seg[root].val_1>>k)&1)==0){
return;
}
if(((seg[root].val_0>>k)&1)==0){
update_tag(root,1<<k,dep);
return;
}
push_down(root,dep);
if(dep==k){
seg[root].ch[0]=trie_merge(seg[root].ch[0],seg[root].ch[1],dep-1);
seg[root].ch[1]=0;
push_up(root,dep);
return;
}
push_and(seg[root].ch[0],k,dep-1);
push_and(seg[root].ch[1],k,dep-1);
push_up(root,dep);
}
int main(){
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
insert(a[i]);
}
for(int t=1;t<=q;t++){
int op;
scanf("%d",&op);
int l,r;
scanf("%d%d",&l,&r);
int l_root=0,r_root=0;
if(l){
trie_split(Root,l-1,l_root,Root);
}
trie_split(Root,r,Root,r_root);
if(op==1){
int x;
scanf("%d",&x);
for(int i=0;i<Maxk;i++){
if(((x>>i)&1)==0){
push_and(Root,i);
}
}
}
else if(op==2){
int x;
scanf("%d",&x);
for(int i=0;i<Maxk;i++){
if(((x>>i)&1)==1){
push_or(Root,i);
}
}
}
else if(op==3){
int x;
scanf("%d",&x);
update_tag(Root,x,Maxk-1);
}
else{
printf("%d\n",seg[Root].sum);
}
Root=trie_merge(trie_merge(l_root,Root),r_root);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int MOD;
struct modint {
private:
int v;
static int minv(int a, int m) {
a %= m;
assert(a);
return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a);
}
public:
modint() : v(0) {}
modint(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }
explicit operator int() const { return v; }
friend std::ostream& operator << (std::ostream& out, const modint& n) { return out << int(n); }
friend std::istream& operator >> (std::istream& in, modint& n) { ll v_; in >> v_; n = modint(v_); return in; }
friend bool operator == (const modint& a, const modint& b) { return a.v == b.v; }
friend bool operator != (const modint& a, const modint& b) { return a.v != b.v; }
modint inv() const {
modint res;
res.v = minv(v, MOD);
return res;
}
friend modint inv(const modint& m) { return m.inv(); }
modint neg() const {
modint res;
res.v = v ? MOD-v : 0;
return res;
}
friend modint neg(const modint& m) { return m.neg(); }
modint operator- () const {
return neg();
}
modint operator+ () const {
return modint(*this);
}
modint& operator ++ () {
v ++;
if (v == MOD) v = 0;
return *this;
}
modint& operator -- () {
if (v == 0) v = MOD;
v --;
return *this;
}
modint& operator += (const modint& o) {
v += o.v;
if (v >= MOD) v -= MOD;
return *this;
}
modint& operator -= (const modint& o) {
v -= o.v;
if (v < 0) v += MOD;
return *this;
}
modint& operator *= (const modint& o) {
v = int(ll(v) * ll(o.v) % MOD);
return *this;
}
modint& operator /= (const modint& o) {
return *this *= o.inv();
}
friend modint operator ++ (modint& a, int) { modint r = a; ++a; return r; }
friend modint operator -- (modint& a, int) { modint r = a; --a; return r; }
friend modint operator + (const modint& a, const modint& b) { return modint(a) += b; }
friend modint operator - (const modint& a, const modint& b) { return modint(a) -= b; }
friend modint operator * (const modint& a, const modint& b) { return modint(a) *= b; }
friend modint operator / (const modint& a, const modint& b) { return modint(a) /= b; }
};
namespace IO {
template<class T>
void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(ll &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { x = getchar(); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U>
void R(T &head, U &... tail) { _R(head), R(tail...); }
template<class T>
void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const ll &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T, class U>
void _W(const pair<T, U> &x) { _W(x.first), putchar(' '), _W(x.second); }
template<class T>
void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U>
void W(const T &head, const U &... tail) { _W(head), putchar(sizeof...(tail) ? ' ' : '\n'), W(tail...); }
}
using namespace IO;
template <typename T> T pow(T a, long long b) {
assert(b >= 0);
T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r;
}
const int maxn = 2e5+50;
const int maxp = maxn*32+5;
const int K = 20;
const int M = 1 << K;
int ls[maxp], rs[maxp], tot;
// 0, 1, cnt, XOR
int t0[maxp], t1[maxp], p[maxp], txor[maxp], dep[maxp];
inline void pushup(int x) {
p[x] = p[ls[x]] + p[rs[x]];
t0[x] = t0[ls[x]] | t0[rs[x]];
t1[x] = t1[ls[x]] | t1[rs[x]];
}
inline void XOR(int x, int t) {
if (!x) return;
txor[x] ^= t;
if (t >> dep[x] & 1) swap(ls[x], rs[x]);
int a = (t0[x] & (~t)) | (t1[x] & t), b = (t1[x] & (~t)) | (t0[x] & t);
t0[x] = a, t1[x] = b;
}
inline void pushdown(int x) {
if (txor[x]) {
XOR(ls[x], txor[x]), XOR(rs[x], txor[x]);
txor[x] = 0;
}
}
inline void insert(int &x, int s, int k) {
if (!x) x = ++tot;
dep[x] = k;
if (k == -1) {
t1[x] = s, t0[x] = s ^ (M - 1);
p[x] = 1;
return;
}
insert((s >> k & 1) ? rs[x] : ls[x], s, k - 1);
pushup(x);
}
inline void split(int &x, int &y, int l, int r, int le, int re) {
if (!x || re < l || le > r) {
y = 0;
return;
}
if (le <= l && r <= re) {
y = x;
x = 0;
return;
}
int mid = l + r >> 1; pushdown(x);
dep[y = ++tot] = dep[x];
split(ls[x], ls[y], l, mid, le, re);
split(rs[x], rs[y], mid + 1, r, le, re);
pushup(x), pushup(y);
}
inline void merge(int &x, int y) {
if (!x || !y) {
x = x | y;
return;
}
pushdown(x), pushdown(y);
merge(ls[x], ls[y]), merge(rs[x], rs[y]);
if (~dep[x]) pushup(x);
}
inline void OR(int x, int s) {
if (!x) return;
if (!(s & t0[x] & t1[x])) {
XOR(x, s & t0[x]);
return;
}
pushdown(x);
if (s >> dep[x] & 1) XOR(ls[x], 1 << dep[x]), merge(rs[x], ls[x]), ls[x] = 0;
OR(ls[x], s), OR(rs[x], s);
pushup(x);
}
int main() {
int n, q, x = 0;
R(n, q);
for (int i = 1; i <= n; ++i) {
int v; R(v); insert(x, v, K - 1);
}
for (; q; --q) {
int oth;
int t, l, r, v; R(t, l, r);
split(x, oth, 0, M - 1, l, r);
if (t == 1) R(v),XOR(oth, M - 1),OR(oth, v ^ (M - 1)),XOR(oth, M - 1);
else if (t == 2) R(v),OR(oth, v);
else if (t == 3) R(v), XOR(oth, v);
else W(p[oth]);
merge(x, oth);
}
return 0;
} | ### Prompt
Develop a solution in CPP to the problem described below:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int MOD;
struct modint {
private:
int v;
static int minv(int a, int m) {
a %= m;
assert(a);
return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a);
}
public:
modint() : v(0) {}
modint(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }
explicit operator int() const { return v; }
friend std::ostream& operator << (std::ostream& out, const modint& n) { return out << int(n); }
friend std::istream& operator >> (std::istream& in, modint& n) { ll v_; in >> v_; n = modint(v_); return in; }
friend bool operator == (const modint& a, const modint& b) { return a.v == b.v; }
friend bool operator != (const modint& a, const modint& b) { return a.v != b.v; }
modint inv() const {
modint res;
res.v = minv(v, MOD);
return res;
}
friend modint inv(const modint& m) { return m.inv(); }
modint neg() const {
modint res;
res.v = v ? MOD-v : 0;
return res;
}
friend modint neg(const modint& m) { return m.neg(); }
modint operator- () const {
return neg();
}
modint operator+ () const {
return modint(*this);
}
modint& operator ++ () {
v ++;
if (v == MOD) v = 0;
return *this;
}
modint& operator -- () {
if (v == 0) v = MOD;
v --;
return *this;
}
modint& operator += (const modint& o) {
v += o.v;
if (v >= MOD) v -= MOD;
return *this;
}
modint& operator -= (const modint& o) {
v -= o.v;
if (v < 0) v += MOD;
return *this;
}
modint& operator *= (const modint& o) {
v = int(ll(v) * ll(o.v) % MOD);
return *this;
}
modint& operator /= (const modint& o) {
return *this *= o.inv();
}
friend modint operator ++ (modint& a, int) { modint r = a; ++a; return r; }
friend modint operator -- (modint& a, int) { modint r = a; --a; return r; }
friend modint operator + (const modint& a, const modint& b) { return modint(a) += b; }
friend modint operator - (const modint& a, const modint& b) { return modint(a) -= b; }
friend modint operator * (const modint& a, const modint& b) { return modint(a) *= b; }
friend modint operator / (const modint& a, const modint& b) { return modint(a) /= b; }
};
namespace IO {
template<class T>
void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(ll &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { x = getchar(); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U>
void R(T &head, U &... tail) { _R(head), R(tail...); }
template<class T>
void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const ll &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T, class U>
void _W(const pair<T, U> &x) { _W(x.first), putchar(' '), _W(x.second); }
template<class T>
void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U>
void W(const T &head, const U &... tail) { _W(head), putchar(sizeof...(tail) ? ' ' : '\n'), W(tail...); }
}
using namespace IO;
template <typename T> T pow(T a, long long b) {
assert(b >= 0);
T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r;
}
const int maxn = 2e5+50;
const int maxp = maxn*32+5;
const int K = 20;
const int M = 1 << K;
int ls[maxp], rs[maxp], tot;
// 0, 1, cnt, XOR
int t0[maxp], t1[maxp], p[maxp], txor[maxp], dep[maxp];
inline void pushup(int x) {
p[x] = p[ls[x]] + p[rs[x]];
t0[x] = t0[ls[x]] | t0[rs[x]];
t1[x] = t1[ls[x]] | t1[rs[x]];
}
inline void XOR(int x, int t) {
if (!x) return;
txor[x] ^= t;
if (t >> dep[x] & 1) swap(ls[x], rs[x]);
int a = (t0[x] & (~t)) | (t1[x] & t), b = (t1[x] & (~t)) | (t0[x] & t);
t0[x] = a, t1[x] = b;
}
inline void pushdown(int x) {
if (txor[x]) {
XOR(ls[x], txor[x]), XOR(rs[x], txor[x]);
txor[x] = 0;
}
}
inline void insert(int &x, int s, int k) {
if (!x) x = ++tot;
dep[x] = k;
if (k == -1) {
t1[x] = s, t0[x] = s ^ (M - 1);
p[x] = 1;
return;
}
insert((s >> k & 1) ? rs[x] : ls[x], s, k - 1);
pushup(x);
}
inline void split(int &x, int &y, int l, int r, int le, int re) {
if (!x || re < l || le > r) {
y = 0;
return;
}
if (le <= l && r <= re) {
y = x;
x = 0;
return;
}
int mid = l + r >> 1; pushdown(x);
dep[y = ++tot] = dep[x];
split(ls[x], ls[y], l, mid, le, re);
split(rs[x], rs[y], mid + 1, r, le, re);
pushup(x), pushup(y);
}
inline void merge(int &x, int y) {
if (!x || !y) {
x = x | y;
return;
}
pushdown(x), pushdown(y);
merge(ls[x], ls[y]), merge(rs[x], rs[y]);
if (~dep[x]) pushup(x);
}
inline void OR(int x, int s) {
if (!x) return;
if (!(s & t0[x] & t1[x])) {
XOR(x, s & t0[x]);
return;
}
pushdown(x);
if (s >> dep[x] & 1) XOR(ls[x], 1 << dep[x]), merge(rs[x], ls[x]), ls[x] = 0;
OR(ls[x], s), OR(rs[x], s);
pushup(x);
}
int main() {
int n, q, x = 0;
R(n, q);
for (int i = 1; i <= n; ++i) {
int v; R(v); insert(x, v, K - 1);
}
for (; q; --q) {
int oth;
int t, l, r, v; R(t, l, r);
split(x, oth, 0, M - 1, l, r);
if (t == 1) R(v),XOR(oth, M - 1),OR(oth, v ^ (M - 1)),XOR(oth, M - 1);
else if (t == 2) R(v),OR(oth, v);
else if (t == 3) R(v), XOR(oth, v);
else W(p[oth]);
merge(x, oth);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define debug(...) //ignore
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long double ld;
const int LEV = 22;
const int M1 = (1<<LEV)-1;
int BIT(int x, int bit) { return (1<<bit) & x; }
struct node;
using pn = node*;
pn merge(pn a, pn b);
struct node {
pn l = nullptr, r = nullptr;
int cnt_ = 0, here = 0;
int level = -1;
int lazy_xor = 0;
int has_on = 0, has_off = 0;
friend int cnt(pn x) { return x ? x->cnt_ : 0; }
friend int ON(pn x) { return x ? x->has_on : 0; }
friend int OFF(pn x) { return x ? x->has_off : 0; }
friend pn create(pn l, pn r, int lev) {
pn t = new node();
t->level = lev;
t->l = l;
t->r = r;
t->pull();
return t;
}
void pull() {
cnt_ = here + cnt(l) + cnt(r);
has_on = ON(l) | ON(r);
has_off = OFF(l) | OFF(r);
if(r) has_on ^= (1<<level);
if(l) has_off ^= (1<<level);
}
void apply_xor(int x) {
rep(bit,0,LEV) if(x>>bit&1){
lazy_xor ^= (1<<bit);
if((has_on>>bit&1) != (has_off>>bit&1)){
has_on ^= (1<<bit);
has_off ^= (1<<bit);
}
}
}
void push() {
if(level == -1) return;
if(lazy_xor>>level&1) swap(l,r);
for(auto c : {l,r}) if(c) c->apply_xor(lazy_xor);
lazy_xor = 0;
pull();
}
};
pn merge(pn a, pn b) {
if(!a) return b;
if(!b) return a;
a->push();
b->push();
pn t = create(merge(a->l, b->l), merge(a->r, b->r), a->level);
t->here = (a->here || b->here);
t->pull();
delete a;
delete b;
return t;
}
pair<pn,pn> split(pn t, int x, int level = LEV) {
if(!t) return {t,t};
assert(level == t->level);
t->push();
if(level == -1) return {nullptr, t}; // x on RHS
if(x>>level&1) {
auto [a,b] = split(t->r,x,level-1);
auto p = make_pair(create(t->l,a,level), create(nullptr,b,level));
delete t;
return p;
}
else {
auto [a,b] = split(t->l,x,level-1);
auto p = make_pair(create(a,nullptr,level), create(b,t->r,level));
delete t;
return p;
}
}
void insert(pn& t, int x, int level = LEV) {
if(!t) t = create(t, t, level);
assert(level == t->level);
t->push();
if(level == -1) {
t->here = 1;
t->pull();
return;
}
if(x>>level&1) insert(t->r, x, level-1);
else insert(t->l, x, level-1);
t->pull();
}
tuple<pn,pn,pn> split3(pn t, int l, int r) {
auto [L,T] = split(t,l);
auto [M,R] = split(T,r+1);
return {L,M,R};
}
void XOR(pn& t, int x) {
if(!t) return;
t->apply_xor(x);
t->push();
}
void OR(pn& t, int x) {
if(!x) return;
if(!t) return;
t->push();
if(t->level == -1) return;
if(x>>t->level&1) {
t->r = merge(t->l,t->r);
t->l = nullptr;
t->pull();
}
rep(bit,0,t->level) if(x>>bit&1) {
if((t->has_off>>bit&1) == 0) x ^= (1<<bit);
else if((t->has_on>>bit&1) == 0) { x ^= (1<<bit); XOR(t, 1<<bit); }
}
OR(t->l, x);
OR(t->r, x);
t->pull();
}
void AND(pn& t, int x) { XOR(t, M1); OR(t, x^M1); XOR(t, M1); }
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int n, q;
cin>>n>>q;
pn root = nullptr;
rep(i,0,n) {
int a;
cin>>a;
insert(root, a);
}
rep(i,0,q) {
int typ,l,r,x;
cin>>typ>>l>>r;
if(typ <= 3) cin>>x;
auto [a,b,c] = split3(root,l,r);
if(typ == 1) AND(b,x);
if(typ == 2) OR(b,x);
if(typ == 3) XOR(b,x);
if(typ == 4) cout << cnt(b) << "\n";
root = merge(a,merge(b,c));
}
cout << flush;
_Exit(0);
}
| ### Prompt
Please create a solution in Cpp to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define debug(...) //ignore
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long double ld;
const int LEV = 22;
const int M1 = (1<<LEV)-1;
int BIT(int x, int bit) { return (1<<bit) & x; }
struct node;
using pn = node*;
pn merge(pn a, pn b);
struct node {
pn l = nullptr, r = nullptr;
int cnt_ = 0, here = 0;
int level = -1;
int lazy_xor = 0;
int has_on = 0, has_off = 0;
friend int cnt(pn x) { return x ? x->cnt_ : 0; }
friend int ON(pn x) { return x ? x->has_on : 0; }
friend int OFF(pn x) { return x ? x->has_off : 0; }
friend pn create(pn l, pn r, int lev) {
pn t = new node();
t->level = lev;
t->l = l;
t->r = r;
t->pull();
return t;
}
void pull() {
cnt_ = here + cnt(l) + cnt(r);
has_on = ON(l) | ON(r);
has_off = OFF(l) | OFF(r);
if(r) has_on ^= (1<<level);
if(l) has_off ^= (1<<level);
}
void apply_xor(int x) {
rep(bit,0,LEV) if(x>>bit&1){
lazy_xor ^= (1<<bit);
if((has_on>>bit&1) != (has_off>>bit&1)){
has_on ^= (1<<bit);
has_off ^= (1<<bit);
}
}
}
void push() {
if(level == -1) return;
if(lazy_xor>>level&1) swap(l,r);
for(auto c : {l,r}) if(c) c->apply_xor(lazy_xor);
lazy_xor = 0;
pull();
}
};
pn merge(pn a, pn b) {
if(!a) return b;
if(!b) return a;
a->push();
b->push();
pn t = create(merge(a->l, b->l), merge(a->r, b->r), a->level);
t->here = (a->here || b->here);
t->pull();
delete a;
delete b;
return t;
}
pair<pn,pn> split(pn t, int x, int level = LEV) {
if(!t) return {t,t};
assert(level == t->level);
t->push();
if(level == -1) return {nullptr, t}; // x on RHS
if(x>>level&1) {
auto [a,b] = split(t->r,x,level-1);
auto p = make_pair(create(t->l,a,level), create(nullptr,b,level));
delete t;
return p;
}
else {
auto [a,b] = split(t->l,x,level-1);
auto p = make_pair(create(a,nullptr,level), create(b,t->r,level));
delete t;
return p;
}
}
void insert(pn& t, int x, int level = LEV) {
if(!t) t = create(t, t, level);
assert(level == t->level);
t->push();
if(level == -1) {
t->here = 1;
t->pull();
return;
}
if(x>>level&1) insert(t->r, x, level-1);
else insert(t->l, x, level-1);
t->pull();
}
tuple<pn,pn,pn> split3(pn t, int l, int r) {
auto [L,T] = split(t,l);
auto [M,R] = split(T,r+1);
return {L,M,R};
}
void XOR(pn& t, int x) {
if(!t) return;
t->apply_xor(x);
t->push();
}
void OR(pn& t, int x) {
if(!x) return;
if(!t) return;
t->push();
if(t->level == -1) return;
if(x>>t->level&1) {
t->r = merge(t->l,t->r);
t->l = nullptr;
t->pull();
}
rep(bit,0,t->level) if(x>>bit&1) {
if((t->has_off>>bit&1) == 0) x ^= (1<<bit);
else if((t->has_on>>bit&1) == 0) { x ^= (1<<bit); XOR(t, 1<<bit); }
}
OR(t->l, x);
OR(t->r, x);
t->pull();
}
void AND(pn& t, int x) { XOR(t, M1); OR(t, x^M1); XOR(t, M1); }
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int n, q;
cin>>n>>q;
pn root = nullptr;
rep(i,0,n) {
int a;
cin>>a;
insert(root, a);
}
rep(i,0,q) {
int typ,l,r,x;
cin>>typ>>l>>r;
if(typ <= 3) cin>>x;
auto [a,b,c] = split3(root,l,r);
if(typ == 1) AND(b,x);
if(typ == 2) OR(b,x);
if(typ == 3) XOR(b,x);
if(typ == 4) cout << cnt(b) << "\n";
root = merge(a,merge(b,c));
}
cout << flush;
_Exit(0);
}
``` |
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr);
#else
#define eprintf(...) 42
#endif
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template<typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
return (ull)rng() % B;
}
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
clock_t startTime;
double getCurrentTime() {
return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
const int P = 8, Q = 12;
struct Block {
bitset<(1 << P)> a;
int A, X, cnt;
Block() : a(), A((1 << P) - 1), X(0), cnt(0) {}
void push() {
bitset<(1 << P)> nw;
nw.reset();
for (int x = 0; x < (1 << P); x++) {
if (a[x]) {
nw[(x & A) ^ X] = 1;
}
}
a = nw;
X ^= X & A;
cnt = a.count();
}
void recalc() {
A = (1 << P) - 1;
X = 0;
cnt = a.count();
}
void makeOp(int t, int x) {
if (t == 3) {
X ^= x;
return;
}
if (t == 1) x ^= (1 << P) - 1;
bool p = (A & x) != 0;
X ^= X & x;
A ^= A & x;
if (t == 2) X ^= x;
if (p) push();
}
};
Block blocks[(1 << 14) + 55];
bitset<(1 << 14) + 55> usedBlock;
int curBlock;
int p[(1 << 12) + 5];
int np[(1 << 12) + 5];
void recalcFree() {
usedBlock.reset();
for (int i = 0; i < (1 << Q); i++)
if (p[i] != -1)
usedBlock[p[i]] = 1;
curBlock = 0;
}
int getNewBlock() {
while (usedBlock[curBlock]) curBlock++;
usedBlock[curBlock] = 1;
blocks[curBlock] = Block();
return curBlock;
}
void mergeInto(int &v, int u) {
if (v == -1) {
v = u;
return;
}
blocks[v].push();
blocks[u].push();
blocks[v].a |= blocks[u].a;
blocks[v].recalc();
}
int getAns(int l, int r) {
int lId = l >> P, rId = r >> P;
l &= (1 << P) - 1;
r &= (1 << P) - 1;
int ans = 0;
if (lId == rId) {
if (p[lId] == -1) return ans;
blocks[p[lId]].push();
for (int x = l; x <= r; x++)
ans += blocks[p[lId]].a[x];
return ans;
}
if (p[lId] != -1) {
blocks[p[lId]].push();
for (int x = l; x < (1 << P); x++)
ans += blocks[p[lId]].a[x];
}
if (p[rId] != -1) {
blocks[p[rId]].push();
for (int x = 0; x <= r; x++)
ans += blocks[p[rId]].a[x];
}
for (int i = lId + 1; i < rId; i++)
if (p[i] != -1)
ans += blocks[p[i]].cnt;
return ans;
}
void splitBlock(int &v, int &u, int l, int r) {
// eprintf("splitting ");
blocks[v].push();
// for (int i = 0; i < (1 << P); i++)
// eprintf("%d", (int)blocks[v].a[i]);
u = getNewBlock();
blocks[u] = blocks[v];
/*
eprintf(" -- ");
for (int i = 0; i < (1 << P); i++)
eprintf("%d", (int)blocks[v].a[i]);
eprintf(" ");
for (int i = 0; i < (1 << P); i++)
eprintf("%d", (int)blocks[u].a[i]);
*/
for (int i = 0; i <= (1 << P) - 1; i++) {
if (l <= i && i <= r) {
blocks[u].a[i] = 0;
} else {
blocks[v].a[i] = 0;
}
}
/*
eprintf(" -- ");
for (int i = 0; i < (1 << P); i++)
eprintf("%d", (int)blocks[v].a[i]);
eprintf(" ");
for (int i = 0; i < (1 << P); i++)
eprintf("%d", (int)blocks[u].a[i]);
*/
blocks[v].recalc();
blocks[u].recalc();
/*
eprintf(" -- ");
for (int i = 0; i < (1 << P); i++)
eprintf("%d", (int)blocks[v].a[i]);
eprintf(" ");
for (int i = 0; i < (1 << P); i++)
eprintf("%d", (int)blocks[u].a[i]);
eprintf("\n");
*/
}
void makeOp(int t, int l, int r, int x) {
int lId = l >> P, rId = r >> P;
int y = x >> P;
l &= (1 << P) - 1;
r &= (1 << P) - 1;
x &= (1 << P) - 1;
for (int i = 0; i < (1 << Q); i++) {
if (p[i] == -1) continue;
int v = p[i];
if (i < lId || i > rId) {
mergeInto(np[i], v);
continue;
}
if (i == lId || i == rId) {
int L = 0, R = (1 << P) - 1;
if (i == lId) L = l;
if (i == rId) R = r;
int u;
splitBlock(v, u, L, R);
mergeInto(np[i], u);
blocks[v].makeOp(t, x);
int j = i;
if (t == 1) {
j &= y;
} else if (t == 2) {
j |= y;
} else {
j ^= y;
}
mergeInto(np[j], v);
} else {
blocks[v].makeOp(t, x);
int j = i;
if (t == 1) {
j &= y;
} else if (t == 2) {
j |= y;
} else {
j ^= y;
}
mergeInto(np[j], v);
}
}
}
void eprintAll() {
for (int i = 0; i < (1 << Q); i++) {
if (p[i] == -1) {
for (int j = 0; j < (1 << P); j++)
eprintf("0");
} else {
for (int j = 0; j < (1 << P); j++)
eprintf("%d", (int)blocks[p[i]].a[j]);
}
eprintf("|");
}
eprintf("\n");
}
int main()
{
startTime = clock();
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int n, q;
scanf("%d%d", &n, &q);
for (int i = 0; i < (1 << Q); i++)
p[i] = -1;
while(n--) {
int x;
scanf("%d", &x);
int v = x >> P;
if (p[v] == -1) p[v] = getNewBlock();
blocks[p[v]].a[x & ((1 << P) - 1)] = 1;
}
for (int i = 0; i < (1 << Q); i++)
if (p[i] != -1)
blocks[p[i]].recalc();
// eprintf("! %d\n", blocks[p[0]].cnt);
while(q--) {
//eprintAll();
int t, l, r;
scanf("%d%d%d", &t, &l, &r);
if (t == 4) {
printf("%d\n", getAns(l, r));
} else {
int x;
scanf("%d", &x);
for (int i = 0; i < (1 << Q); i++)
np[i] = -1;
recalcFree();
makeOp(t, l, r, x);
for (int i = 0; i < (1 << Q); i++)
p[i] = np[i];
}
/*
eprintf("! %d\n", blocks[p[0]].cnt);
for (int i = 0; i < 8; i++)
eprintf("%d", (int)blocks[p[0]].a[i]);
eprintf("\n");
*/
}
return 0;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr);
#else
#define eprintf(...) 42
#endif
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template<typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
return (ull)rng() % B;
}
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
clock_t startTime;
double getCurrentTime() {
return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
const int P = 8, Q = 12;
struct Block {
bitset<(1 << P)> a;
int A, X, cnt;
Block() : a(), A((1 << P) - 1), X(0), cnt(0) {}
void push() {
bitset<(1 << P)> nw;
nw.reset();
for (int x = 0; x < (1 << P); x++) {
if (a[x]) {
nw[(x & A) ^ X] = 1;
}
}
a = nw;
X ^= X & A;
cnt = a.count();
}
void recalc() {
A = (1 << P) - 1;
X = 0;
cnt = a.count();
}
void makeOp(int t, int x) {
if (t == 3) {
X ^= x;
return;
}
if (t == 1) x ^= (1 << P) - 1;
bool p = (A & x) != 0;
X ^= X & x;
A ^= A & x;
if (t == 2) X ^= x;
if (p) push();
}
};
Block blocks[(1 << 14) + 55];
bitset<(1 << 14) + 55> usedBlock;
int curBlock;
int p[(1 << 12) + 5];
int np[(1 << 12) + 5];
void recalcFree() {
usedBlock.reset();
for (int i = 0; i < (1 << Q); i++)
if (p[i] != -1)
usedBlock[p[i]] = 1;
curBlock = 0;
}
int getNewBlock() {
while (usedBlock[curBlock]) curBlock++;
usedBlock[curBlock] = 1;
blocks[curBlock] = Block();
return curBlock;
}
void mergeInto(int &v, int u) {
if (v == -1) {
v = u;
return;
}
blocks[v].push();
blocks[u].push();
blocks[v].a |= blocks[u].a;
blocks[v].recalc();
}
int getAns(int l, int r) {
int lId = l >> P, rId = r >> P;
l &= (1 << P) - 1;
r &= (1 << P) - 1;
int ans = 0;
if (lId == rId) {
if (p[lId] == -1) return ans;
blocks[p[lId]].push();
for (int x = l; x <= r; x++)
ans += blocks[p[lId]].a[x];
return ans;
}
if (p[lId] != -1) {
blocks[p[lId]].push();
for (int x = l; x < (1 << P); x++)
ans += blocks[p[lId]].a[x];
}
if (p[rId] != -1) {
blocks[p[rId]].push();
for (int x = 0; x <= r; x++)
ans += blocks[p[rId]].a[x];
}
for (int i = lId + 1; i < rId; i++)
if (p[i] != -1)
ans += blocks[p[i]].cnt;
return ans;
}
void splitBlock(int &v, int &u, int l, int r) {
// eprintf("splitting ");
blocks[v].push();
// for (int i = 0; i < (1 << P); i++)
// eprintf("%d", (int)blocks[v].a[i]);
u = getNewBlock();
blocks[u] = blocks[v];
/*
eprintf(" -- ");
for (int i = 0; i < (1 << P); i++)
eprintf("%d", (int)blocks[v].a[i]);
eprintf(" ");
for (int i = 0; i < (1 << P); i++)
eprintf("%d", (int)blocks[u].a[i]);
*/
for (int i = 0; i <= (1 << P) - 1; i++) {
if (l <= i && i <= r) {
blocks[u].a[i] = 0;
} else {
blocks[v].a[i] = 0;
}
}
/*
eprintf(" -- ");
for (int i = 0; i < (1 << P); i++)
eprintf("%d", (int)blocks[v].a[i]);
eprintf(" ");
for (int i = 0; i < (1 << P); i++)
eprintf("%d", (int)blocks[u].a[i]);
*/
blocks[v].recalc();
blocks[u].recalc();
/*
eprintf(" -- ");
for (int i = 0; i < (1 << P); i++)
eprintf("%d", (int)blocks[v].a[i]);
eprintf(" ");
for (int i = 0; i < (1 << P); i++)
eprintf("%d", (int)blocks[u].a[i]);
eprintf("\n");
*/
}
void makeOp(int t, int l, int r, int x) {
int lId = l >> P, rId = r >> P;
int y = x >> P;
l &= (1 << P) - 1;
r &= (1 << P) - 1;
x &= (1 << P) - 1;
for (int i = 0; i < (1 << Q); i++) {
if (p[i] == -1) continue;
int v = p[i];
if (i < lId || i > rId) {
mergeInto(np[i], v);
continue;
}
if (i == lId || i == rId) {
int L = 0, R = (1 << P) - 1;
if (i == lId) L = l;
if (i == rId) R = r;
int u;
splitBlock(v, u, L, R);
mergeInto(np[i], u);
blocks[v].makeOp(t, x);
int j = i;
if (t == 1) {
j &= y;
} else if (t == 2) {
j |= y;
} else {
j ^= y;
}
mergeInto(np[j], v);
} else {
blocks[v].makeOp(t, x);
int j = i;
if (t == 1) {
j &= y;
} else if (t == 2) {
j |= y;
} else {
j ^= y;
}
mergeInto(np[j], v);
}
}
}
void eprintAll() {
for (int i = 0; i < (1 << Q); i++) {
if (p[i] == -1) {
for (int j = 0; j < (1 << P); j++)
eprintf("0");
} else {
for (int j = 0; j < (1 << P); j++)
eprintf("%d", (int)blocks[p[i]].a[j]);
}
eprintf("|");
}
eprintf("\n");
}
int main()
{
startTime = clock();
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int n, q;
scanf("%d%d", &n, &q);
for (int i = 0; i < (1 << Q); i++)
p[i] = -1;
while(n--) {
int x;
scanf("%d", &x);
int v = x >> P;
if (p[v] == -1) p[v] = getNewBlock();
blocks[p[v]].a[x & ((1 << P) - 1)] = 1;
}
for (int i = 0; i < (1 << Q); i++)
if (p[i] != -1)
blocks[p[i]].recalc();
// eprintf("! %d\n", blocks[p[0]].cnt);
while(q--) {
//eprintAll();
int t, l, r;
scanf("%d%d%d", &t, &l, &r);
if (t == 4) {
printf("%d\n", getAns(l, r));
} else {
int x;
scanf("%d", &x);
for (int i = 0; i < (1 << Q); i++)
np[i] = -1;
recalcFree();
makeOp(t, l, r, x);
for (int i = 0; i < (1 << Q); i++)
p[i] = np[i];
}
/*
eprintf("! %d\n", blocks[p[0]].cnt);
for (int i = 0; i < 8; i++)
eprintf("%d", (int)blocks[p[0]].a[i]);
eprintf("\n");
*/
}
return 0;
}
``` |
#include <cstdio>
#include <cassert>
#include <utility>
#include <functional>
#define X first
#define Y second
const int mod = 998244353;
template<int k>
struct node{
node <k-1>* chd[2];
int cnt;
int lazy;
int has[2];
int get_cnt(){
assert(this!=NULL);
return cnt;
}
int get_has(int d){
assert(this!=NULL);
push();
assert(has[d]<(1<<(k+1)));
return has[d];
}
node():chd{NULL, NULL}, has{0, 0}, cnt(0),lazy(0){};
node(node<k-1>* l,node <k-1>* r):chd{l,r},cnt(0),lazy(0),has{0,0}{
if(l){
cnt+=l->get_cnt();
has[0]|=l->get_has(0)|(1<<k);
has[1]|=l->get_has(1);
}
if(r){
cnt+=r->get_cnt();
has[0]|=r->get_has(0);
has[1]|=r->get_has(1)|(1<<k);
}
assert(has[0]<(1<<(k+1)));
assert(has[1]<(1<<(k+1)));
}
void push(){
assert(lazy<(1<<(k+1)));
if(!lazy) return;
if(lazy&(1<<k)){
std::swap(chd[0],chd[1]);
if((has[0]^has[1])&(1<<k)){
has[0]^=(1<<k);
has[1]^=(1<<k);
}
lazy^=(1<<k);
}
int flip=(has[0]^has[1])&lazy;
has[0]^=flip;
has[1]^=flip;
if(chd[0]) chd[0]->lazy^=lazy;
if(chd[1]) chd[1]->lazy^=lazy;
lazy=0;
assert(has[0]<(1<<(k+1)));
assert(has[1]<(1<<(k+1)));
}
};
template<>
struct node<-1>{
int lazy;
node():lazy(0){
}
int get_cnt(){
assert(this!=NULL);
return 1;
}
int get_has(int d){
assert(this!=NULL);
return 0;
}
};
template<int k>
node<k>* create(int x){
if(x&(1<<k)){
return new node<k>(NULL,create<k-1>(x));
}else{
return new node<k>(create<k-1>(x),NULL);
}
}
template<> node<-1>*create(int x)
{
return new node<-1>();
}
template<int k> std::pair <node<k - 1>*, node< k - 1>*> destruct(node <k>*a)
{
assert(a!=NULL);
a -> push();
auto res = std::make_pair(a -> chd[0], a -> chd[1]);
delete a;
return res;
}
template <int k> node <k>* join(node<k -1>* l, node <k - 1>* r)
{
if(l == NULL && r == NULL)
{
return NULL;
}
return new node<k>(l, r);
}
template<int k> node<k>* merge(node<k> *a, node<k> *b)
{
if(!a)
{
return b;
}
if(!b)
{
return a;
}
auto aa = destruct(a);
auto bb = destruct(b);
node <k - 1> *l = merge<k - 1>(aa.X, bb.X);
node <k - 1> *r = merge<k - 1>(aa.Y, bb.Y);
return join<k>(l, r);
}
template<>node<-1>* merge(node<-1>*a, node<-1>* b)
{
if(!a)
{
return b;
}
if(!b)
{
return a;
}
delete b;
return a;
}
template<int k>
std::pair<node<k>*,node<k>*> split(node<k>* a,int thres){
if(a==NULL){
return {NULL,NULL};
}
if(thres<=0) return {NULL,a};
if(thres>=(1<<(k+1))) return {a,NULL};
assert(k>=0);
auto aa =destruct(a);
if(thres<(1<<k)){
node<k-1>* l,*r;
std::tie(l,r)=split<k-1>(aa.first,thres);
return std::make_pair(join<k>(l,NULL),join<k>(r,aa.second));
}else if(thres>(1<<k)){
node<k-1>* l,*r;
std::tie(l,r)=split<k-1>(aa.second, thres-(1<<k));
return std::make_pair(join<k>(aa.first,l),join<k>(NULL,r));
}else{
return std::make_pair(join<k>(aa.first,NULL),join<k>(NULL,aa.second));
}
}
template<>
std::pair<node<-1>*,node<-1>*> split<-1>(node<-1>* a,int thres){
assert(0);
}
template<int k> node<k>* update(node<k> *a, int val)
{
if(a == NULL) return NULL;
a -> push();
assert(val<(1<<(k+1)));
if((val & a -> has[0] & a -> has[1]) == 0)
{
a -> lazy ^= (val & a ->has[0]);
return a;
}
node<k - 1>*l, *r;
std::tie(l, r) = destruct(a);
l = update<k - 1>(l, val & ~(1 << k));
r = update<k - 1>(r, val & ~(1 << k));
if(val & (1 << k))
{
return join<k>(NULL, merge<k - 1>(l, r));
}
else
{
return join<k>(l, r);
}
}
template<>node<-1>*update(node<-1>*a, int val)
{
return a;
}
int main()
{
// ifstream cin("input1.txt.4c");
// ios_base::sync_with_stdio(0);
// cin.tie(0);
//cout.tie(0);
node<19>* root = NULL;
int n, q;
//cin >> n >> q;
scanf("%d %d", &n, &q);
// printf("A\n");
for(int i = 0; i< n; i++)
{
int a;
scanf("%d", &a);
// root = create<19> (a);
root = merge(root, create<19>(a));
}
while(q--)
{
int t, l, r;
scanf("%d%d%d", &t, &l, &r);
node<19>*left, *right;
std::tie(left, root) = split(root, l);
std::tie(root, right) = split(root, r + 1);
if(t == 4)
{
if(root)
{
printf("%d\n", root -> cnt);
// return 0;
}
else
{
printf("0\n");
//return 0;
}
}
else
{
int x;
scanf("%d", &x);
if(root != NULL)
{
if(t == 1)
{
root -> lazy ^= ((1 << 20) - 1);
root = update(root, x ^ ((1 << 20) - 1));
root -> lazy ^= ((1 << 20) - 1);
}
else if(t == 2)
{
root = update(root, x);
}
else
{
root -> lazy ^=x;
}
}
}
root = merge(root, left);
root = merge(root, right);
}
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
#include <cstdio>
#include <cassert>
#include <utility>
#include <functional>
#define X first
#define Y second
const int mod = 998244353;
template<int k>
struct node{
node <k-1>* chd[2];
int cnt;
int lazy;
int has[2];
int get_cnt(){
assert(this!=NULL);
return cnt;
}
int get_has(int d){
assert(this!=NULL);
push();
assert(has[d]<(1<<(k+1)));
return has[d];
}
node():chd{NULL, NULL}, has{0, 0}, cnt(0),lazy(0){};
node(node<k-1>* l,node <k-1>* r):chd{l,r},cnt(0),lazy(0),has{0,0}{
if(l){
cnt+=l->get_cnt();
has[0]|=l->get_has(0)|(1<<k);
has[1]|=l->get_has(1);
}
if(r){
cnt+=r->get_cnt();
has[0]|=r->get_has(0);
has[1]|=r->get_has(1)|(1<<k);
}
assert(has[0]<(1<<(k+1)));
assert(has[1]<(1<<(k+1)));
}
void push(){
assert(lazy<(1<<(k+1)));
if(!lazy) return;
if(lazy&(1<<k)){
std::swap(chd[0],chd[1]);
if((has[0]^has[1])&(1<<k)){
has[0]^=(1<<k);
has[1]^=(1<<k);
}
lazy^=(1<<k);
}
int flip=(has[0]^has[1])&lazy;
has[0]^=flip;
has[1]^=flip;
if(chd[0]) chd[0]->lazy^=lazy;
if(chd[1]) chd[1]->lazy^=lazy;
lazy=0;
assert(has[0]<(1<<(k+1)));
assert(has[1]<(1<<(k+1)));
}
};
template<>
struct node<-1>{
int lazy;
node():lazy(0){
}
int get_cnt(){
assert(this!=NULL);
return 1;
}
int get_has(int d){
assert(this!=NULL);
return 0;
}
};
template<int k>
node<k>* create(int x){
if(x&(1<<k)){
return new node<k>(NULL,create<k-1>(x));
}else{
return new node<k>(create<k-1>(x),NULL);
}
}
template<> node<-1>*create(int x)
{
return new node<-1>();
}
template<int k> std::pair <node<k - 1>*, node< k - 1>*> destruct(node <k>*a)
{
assert(a!=NULL);
a -> push();
auto res = std::make_pair(a -> chd[0], a -> chd[1]);
delete a;
return res;
}
template <int k> node <k>* join(node<k -1>* l, node <k - 1>* r)
{
if(l == NULL && r == NULL)
{
return NULL;
}
return new node<k>(l, r);
}
template<int k> node<k>* merge(node<k> *a, node<k> *b)
{
if(!a)
{
return b;
}
if(!b)
{
return a;
}
auto aa = destruct(a);
auto bb = destruct(b);
node <k - 1> *l = merge<k - 1>(aa.X, bb.X);
node <k - 1> *r = merge<k - 1>(aa.Y, bb.Y);
return join<k>(l, r);
}
template<>node<-1>* merge(node<-1>*a, node<-1>* b)
{
if(!a)
{
return b;
}
if(!b)
{
return a;
}
delete b;
return a;
}
template<int k>
std::pair<node<k>*,node<k>*> split(node<k>* a,int thres){
if(a==NULL){
return {NULL,NULL};
}
if(thres<=0) return {NULL,a};
if(thres>=(1<<(k+1))) return {a,NULL};
assert(k>=0);
auto aa =destruct(a);
if(thres<(1<<k)){
node<k-1>* l,*r;
std::tie(l,r)=split<k-1>(aa.first,thres);
return std::make_pair(join<k>(l,NULL),join<k>(r,aa.second));
}else if(thres>(1<<k)){
node<k-1>* l,*r;
std::tie(l,r)=split<k-1>(aa.second, thres-(1<<k));
return std::make_pair(join<k>(aa.first,l),join<k>(NULL,r));
}else{
return std::make_pair(join<k>(aa.first,NULL),join<k>(NULL,aa.second));
}
}
template<>
std::pair<node<-1>*,node<-1>*> split<-1>(node<-1>* a,int thres){
assert(0);
}
template<int k> node<k>* update(node<k> *a, int val)
{
if(a == NULL) return NULL;
a -> push();
assert(val<(1<<(k+1)));
if((val & a -> has[0] & a -> has[1]) == 0)
{
a -> lazy ^= (val & a ->has[0]);
return a;
}
node<k - 1>*l, *r;
std::tie(l, r) = destruct(a);
l = update<k - 1>(l, val & ~(1 << k));
r = update<k - 1>(r, val & ~(1 << k));
if(val & (1 << k))
{
return join<k>(NULL, merge<k - 1>(l, r));
}
else
{
return join<k>(l, r);
}
}
template<>node<-1>*update(node<-1>*a, int val)
{
return a;
}
int main()
{
// ifstream cin("input1.txt.4c");
// ios_base::sync_with_stdio(0);
// cin.tie(0);
//cout.tie(0);
node<19>* root = NULL;
int n, q;
//cin >> n >> q;
scanf("%d %d", &n, &q);
// printf("A\n");
for(int i = 0; i< n; i++)
{
int a;
scanf("%d", &a);
// root = create<19> (a);
root = merge(root, create<19>(a));
}
while(q--)
{
int t, l, r;
scanf("%d%d%d", &t, &l, &r);
node<19>*left, *right;
std::tie(left, root) = split(root, l);
std::tie(root, right) = split(root, r + 1);
if(t == 4)
{
if(root)
{
printf("%d\n", root -> cnt);
// return 0;
}
else
{
printf("0\n");
//return 0;
}
}
else
{
int x;
scanf("%d", &x);
if(root != NULL)
{
if(t == 1)
{
root -> lazy ^= ((1 << 20) - 1);
root = update(root, x ^ ((1 << 20) - 1));
root -> lazy ^= ((1 << 20) - 1);
}
else if(t == 2)
{
root = update(root, x);
}
else
{
root -> lazy ^=x;
}
}
}
root = merge(root, left);
root = merge(root, right);
}
return 0;
}
``` |
// Author: wlzhouzhuan
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pb push_back
#define fir first
#define sec second
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define mset(s, t) memset(s, t, sizeof(s))
#define mcpy(s, t) memcpy(s, t, sizeof(t))
template<typename T1, typename T2> void ckmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> void ckmax(T1 &a, T2 b) { if (a < b) a = b; }
int read() {
int x = 0, f = 0; char ch = getchar();
while (!isdigit(ch)) f |= ch == '-', ch = getchar();
while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
return f ? -x : x;
}
template<typename T> void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) print(x / 10);
putchar(x % 10 + '0');
}
template<typename T> void print(T x, char let) {
print(x), putchar(let);
}
const int N = 200005;
const int B = 20;
const int K = (1 << B) - 1;
int a[N], n, q;
int ls[60 * N], rs[60 * N], tot, rt;
int Vand[60 * N], Vor[60 * N], siz[60 * N], lazy[60 * N];
void pushup(int u) {
Vand[u] = Vand[ls[u]] | Vand[rs[u]];
Vor[u] = Vor[ls[u]] | Vor[rs[u]];
siz[u] = siz[ls[u]] + siz[rs[u]];
}
void pushxor(int dep, int u, int val) {
if (!u) return ;
// printf("pushxor %d %d %d\n", dep, u, val);
if (val >> (dep - 1) & 1) swap(ls[u], rs[u]);
int tmpand = Vand[u], tmpor = Vor[u];
Vand[u] = (tmpand & (val ^ K)) | (tmpor & val);
Vor[u] = (tmpor & (val ^ K)) | (tmpand & val);
lazy[u] ^= val;
}
void pushdown(int dep, int u) {
if (lazy[u]) {
pushxor(dep - 1, ls[u], lazy[u]), pushxor(dep - 1, rs[u], lazy[u]);
lazy[u] = 0;
}
}
void ins(int dep, int &u, int l, int r, int val) {
if (!u) u = ++tot;
if (dep == 0) {
Vand[u] = val ^ K;
Vor[u] = val;
siz[u] = 1;
return ;
}
int mid = l + r >> 1;
if (val >> (dep - 1) & 1) ins(dep - 1, rs[u], l, mid, val);
else ins(dep - 1, ls[u], mid + 1, r, val);
// printf("%d %d\n", u, val);
pushup(u);
}
void split(int dep, int &u, int &v, int l, int r, int ql, int qr) {
// printf("split %d %d %d %d %d %d %d\n", u, v, dep, l, r, ql, qr);
if (!u || qr < l || ql > r) {
v = 0;
return ;
}
if (ql <= l && r <= qr) {
v = u, u = 0;
return ;
}
pushdown(dep, u), v = ++tot;
int mid = l + r >> 1;
split(dep - 1, ls[u], ls[v], l, mid, ql, qr);
split(dep - 1, rs[u], rs[v], mid + 1, r, ql, qr);
pushup(u), pushup(v);
// printf("ls[%d]=%d,rs[%d]=%d,ls[%d]=%d,rs[%d]=%d\n",u,ls[u],u,rs[u],v,ls[v],v,rs[v]);
// printf(">>> %d %d %d %d\n",Vand[u],Vor[u],Vand[v],Vor[v]);
}
void merge(int dep, int &x, int &y) {
// printf("merge %d %d %d\n", x, y, dep);
if (!x) {
x = y;
return ;
}
if (!y || dep == 0) return ;
pushdown(dep, x), pushdown(dep, y);
merge(dep - 1, ls[x], ls[y]);
merge(dep - 1, rs[x], rs[y]);
pushup(x);
}
void update_or(int dep, int u, int l, int r, int x) {
if (!u) return ;
int add = Vand[u] & x;
if ((add & Vor[u]) == 0) {
pushxor(dep, u, add);
return ;
}
pushdown(dep, u);
int mid = l + r >> 1;
if (x >> (dep - 1) & 1) {
pushxor(dep - 1, ls[u], 1 << dep - 1);
merge(dep - 1, rs[u], ls[u]);
ls[u] = 0;
}
update_or(dep - 1, ls[u], l, mid, x);
update_or(dep - 1, rs[u], mid + 1, r, x);
pushup(u);
// printf("update_or %d %d %d %d %d\n",u,ls[u],rs[u],Vand[u],Vor[u]);
}
int query(int dep, int u, int l, int r, int ql, int qr) {
// printf("query %d %d %d %d %d %d\n",dep,u,l,r,ql,qr);
if (!u || qr < l || ql > r) return 0;
if (ql <= l && r <= qr) return siz[u];
pushdown(dep, u);
int mid = l + r >> 1, ans = 0;
ans += query(dep - 1, ls[u], l, mid, ql, qr);
ans += query(dep - 1, rs[u], mid + 1, r, ql, qr);
return ans;
}
int main() {
n = read(), q = read();
rep(i, 1, n) {
a[i] = read();
ins(20, rt, 0, K, a[i]);
}
// printf("tot = %d\n", tot);
// for(int i=1;i<=tot;i++){
// printf("info[%d]: %d %d %d\n",i,Vand[i],Vor[i],siz[i]);
// }
// return 0;
while (q--) {
int opt = read(), l = read(), r = read(), x, y = 0;
if (opt == 4) {
print(query(20, rt, 0, K, l, r), '\n');
} else {
x = read();
split(20, rt, y, 0, K, l, r);
// printf("modify rt = %d\n", y);
if (opt == 1) pushxor(20, y, K), update_or(20, y, 0, K, x ^ K), pushxor(20, y, K);
else if (opt == 2) update_or(20, y, 0, K, x);
else pushxor(20, y, x);
merge(20, rt, y);
// printf("tot = %d\n", tot);
// for(int i=1;i<=tot;i++){
// printf("info[%d]: %d %d %d\n",i,Vand[i],Vor[i],siz[i]);
// }
// system("pause");
}
}
return 0;
} | ### Prompt
Please formulate a Cpp solution to the following problem:
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries:
1. replace all numbers a_i where l ≤ a_i ≤ r with a_i AND x;
2. replace all numbers a_i where l ≤ a_i ≤ r with a_i OR x;
3. replace all numbers a_i where l ≤ a_i ≤ r with a_i XOR x;
4. output how many distinct integers a_i where l ≤ a_i ≤ r.
For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ q ≤ 10^5) — the number of integers and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^{20}) — the integers that Phoenix starts with.
The next q lines contain the queries. For each query, the first integer of each line is t (1 ≤ t ≤ 4) — the type of query.
If t ∈ \{1, 2, 3\}, then three integers l_i, r_i, and x_i will follow (0 ≤ l_i, r_i, x_i < 2^{20}; l_i ≤ r_i).
Otherwise, if t=4, two integers l_i and r_i will follow (0 ≤ l_i ≤ r_i < 2^{20}).
It is guaranteed that there is at least one query where t=4.
Output
Print the answer for each query where t=4.
Examples
Input
5 6
5 4 3 2 1
1 2 3 2
4 2 5
3 2 5 3
4 1 6
2 1 1 8
4 8 10
Output
3
2
1
Input
6 7
6 0 2 3 2 7
1 0 4 3
2 6 8 4
4 0 7
3 2 5 3
1 0 1 2
4 0 3
4 2 7
Output
5
1
2
Note
In the first example:
* For the first query, 2 is replaced by 2 AND 2 = 2 and 3 is replaced with 3 AND 2 = 2. The set of numbers is \{1, 2, 4, 5\}.
* For the second query, there are 3 distinct numbers between 2 and 5: 2, 4, and 5.
* For the third query, 2 is replaced by 2 XOR 3 = 1, 4 is replaced by 4 XOR 3 = 7, and 5 is replaced by 5 XOR 3 = 6. The set of numbers is \{1, 6, 7\}.
* For the fourth query, there are 2 distinct numbers between 1 and 6: 1 and 6.
* For the fifth query, 1 is replaced by 1 OR 8 = 9. The set of numbers is \{6, 7, 9\}.
* For the sixth query, there is one distinct number between 8 and 10: 9.
### Response
```cpp
// Author: wlzhouzhuan
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pb push_back
#define fir first
#define sec second
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define mset(s, t) memset(s, t, sizeof(s))
#define mcpy(s, t) memcpy(s, t, sizeof(t))
template<typename T1, typename T2> void ckmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> void ckmax(T1 &a, T2 b) { if (a < b) a = b; }
int read() {
int x = 0, f = 0; char ch = getchar();
while (!isdigit(ch)) f |= ch == '-', ch = getchar();
while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
return f ? -x : x;
}
template<typename T> void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) print(x / 10);
putchar(x % 10 + '0');
}
template<typename T> void print(T x, char let) {
print(x), putchar(let);
}
const int N = 200005;
const int B = 20;
const int K = (1 << B) - 1;
int a[N], n, q;
int ls[60 * N], rs[60 * N], tot, rt;
int Vand[60 * N], Vor[60 * N], siz[60 * N], lazy[60 * N];
void pushup(int u) {
Vand[u] = Vand[ls[u]] | Vand[rs[u]];
Vor[u] = Vor[ls[u]] | Vor[rs[u]];
siz[u] = siz[ls[u]] + siz[rs[u]];
}
void pushxor(int dep, int u, int val) {
if (!u) return ;
// printf("pushxor %d %d %d\n", dep, u, val);
if (val >> (dep - 1) & 1) swap(ls[u], rs[u]);
int tmpand = Vand[u], tmpor = Vor[u];
Vand[u] = (tmpand & (val ^ K)) | (tmpor & val);
Vor[u] = (tmpor & (val ^ K)) | (tmpand & val);
lazy[u] ^= val;
}
void pushdown(int dep, int u) {
if (lazy[u]) {
pushxor(dep - 1, ls[u], lazy[u]), pushxor(dep - 1, rs[u], lazy[u]);
lazy[u] = 0;
}
}
void ins(int dep, int &u, int l, int r, int val) {
if (!u) u = ++tot;
if (dep == 0) {
Vand[u] = val ^ K;
Vor[u] = val;
siz[u] = 1;
return ;
}
int mid = l + r >> 1;
if (val >> (dep - 1) & 1) ins(dep - 1, rs[u], l, mid, val);
else ins(dep - 1, ls[u], mid + 1, r, val);
// printf("%d %d\n", u, val);
pushup(u);
}
void split(int dep, int &u, int &v, int l, int r, int ql, int qr) {
// printf("split %d %d %d %d %d %d %d\n", u, v, dep, l, r, ql, qr);
if (!u || qr < l || ql > r) {
v = 0;
return ;
}
if (ql <= l && r <= qr) {
v = u, u = 0;
return ;
}
pushdown(dep, u), v = ++tot;
int mid = l + r >> 1;
split(dep - 1, ls[u], ls[v], l, mid, ql, qr);
split(dep - 1, rs[u], rs[v], mid + 1, r, ql, qr);
pushup(u), pushup(v);
// printf("ls[%d]=%d,rs[%d]=%d,ls[%d]=%d,rs[%d]=%d\n",u,ls[u],u,rs[u],v,ls[v],v,rs[v]);
// printf(">>> %d %d %d %d\n",Vand[u],Vor[u],Vand[v],Vor[v]);
}
void merge(int dep, int &x, int &y) {
// printf("merge %d %d %d\n", x, y, dep);
if (!x) {
x = y;
return ;
}
if (!y || dep == 0) return ;
pushdown(dep, x), pushdown(dep, y);
merge(dep - 1, ls[x], ls[y]);
merge(dep - 1, rs[x], rs[y]);
pushup(x);
}
void update_or(int dep, int u, int l, int r, int x) {
if (!u) return ;
int add = Vand[u] & x;
if ((add & Vor[u]) == 0) {
pushxor(dep, u, add);
return ;
}
pushdown(dep, u);
int mid = l + r >> 1;
if (x >> (dep - 1) & 1) {
pushxor(dep - 1, ls[u], 1 << dep - 1);
merge(dep - 1, rs[u], ls[u]);
ls[u] = 0;
}
update_or(dep - 1, ls[u], l, mid, x);
update_or(dep - 1, rs[u], mid + 1, r, x);
pushup(u);
// printf("update_or %d %d %d %d %d\n",u,ls[u],rs[u],Vand[u],Vor[u]);
}
int query(int dep, int u, int l, int r, int ql, int qr) {
// printf("query %d %d %d %d %d %d\n",dep,u,l,r,ql,qr);
if (!u || qr < l || ql > r) return 0;
if (ql <= l && r <= qr) return siz[u];
pushdown(dep, u);
int mid = l + r >> 1, ans = 0;
ans += query(dep - 1, ls[u], l, mid, ql, qr);
ans += query(dep - 1, rs[u], mid + 1, r, ql, qr);
return ans;
}
int main() {
n = read(), q = read();
rep(i, 1, n) {
a[i] = read();
ins(20, rt, 0, K, a[i]);
}
// printf("tot = %d\n", tot);
// for(int i=1;i<=tot;i++){
// printf("info[%d]: %d %d %d\n",i,Vand[i],Vor[i],siz[i]);
// }
// return 0;
while (q--) {
int opt = read(), l = read(), r = read(), x, y = 0;
if (opt == 4) {
print(query(20, rt, 0, K, l, r), '\n');
} else {
x = read();
split(20, rt, y, 0, K, l, r);
// printf("modify rt = %d\n", y);
if (opt == 1) pushxor(20, y, K), update_or(20, y, 0, K, x ^ K), pushxor(20, y, K);
else if (opt == 2) update_or(20, y, 0, K, x);
else pushxor(20, y, x);
merge(20, rt, y);
// printf("tot = %d\n", tot);
// for(int i=1;i<=tot;i++){
// printf("info[%d]: %d %d %d\n",i,Vand[i],Vor[i],siz[i]);
// }
// system("pause");
}
}
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define M 1000000007
#define read(arr) for(int i=0;i<n;i++) cin>>arr[i];
#define print(arr) for(int i=0;i<n;i++) cout<<arr[i]<<" "; cout<<endl;
#define vl vector<ll>
bool sortcoll(vector<ll> &v1, vector<ll> &v2)
{
return v1[1]>v2[1];
}
bool isPrime(ll n)
{
if(n==1)
return true;
for(int i=2;i<=sqrt(n);i++)
if(n%i==0)
return false;
return true;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t;
cin>>t;
while(t--)
{
ll a,b;
cin>>a>>b;
if(a==b)
cout<<0<<" "<<0<<endl;
else
{
ll x=abs(a-b);
ll l=(min(a,b)/x)*x;
ll u=(max(a,b)/x+1)*x;
ll res=LLONG_MAX;
for(ll i=l;i<=u;i+=x)
{
if(min(abs(i-a),abs(i-b))<res)
res=min(abs(i-a),abs(i-b));
}
cout<<x<<" "<<res<<endl;
}
}
}
| ### Prompt
Please create a solution in cpp to the following problem:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define M 1000000007
#define read(arr) for(int i=0;i<n;i++) cin>>arr[i];
#define print(arr) for(int i=0;i<n;i++) cout<<arr[i]<<" "; cout<<endl;
#define vl vector<ll>
bool sortcoll(vector<ll> &v1, vector<ll> &v2)
{
return v1[1]>v2[1];
}
bool isPrime(ll n)
{
if(n==1)
return true;
for(int i=2;i<=sqrt(n);i++)
if(n%i==0)
return false;
return true;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t;
cin>>t;
while(t--)
{
ll a,b;
cin>>a>>b;
if(a==b)
cout<<0<<" "<<0<<endl;
else
{
ll x=abs(a-b);
ll l=(min(a,b)/x)*x;
ll u=(max(a,b)/x+1)*x;
ll res=LLONG_MAX;
for(ll i=l;i<=u;i+=x)
{
if(min(abs(i-a),abs(i-b))<res)
res=min(abs(i-a),abs(i-b));
}
cout<<x<<" "<<res<<endl;
}
}
}
``` |
#include <bits/stdc++.h>
typedef long long int ll;
const unsigned int MOD = 1000000007;
using namespace std;
int main()
{
int t;
cin >> t;
for (int tt = 0; tt < t; tt++)
{
ll a,b;
cin>>a>>b;
if(a>b)
swap(a,b);
if(a==b)
cout<<"0 0\n";
else
{
ll x=abs(a-b);
ll y= a%x;
y = min(y, x-y);
cout<<x<<" "<<y<<"\n";
}
}
} | ### Prompt
Develop a solution in Cpp to the problem described below:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include <bits/stdc++.h>
typedef long long int ll;
const unsigned int MOD = 1000000007;
using namespace std;
int main()
{
int t;
cin >> t;
for (int tt = 0; tt < t; tt++)
{
ll a,b;
cin>>a>>b;
if(a>b)
swap(a,b);
if(a==b)
cout<<"0 0\n";
else
{
ll x=abs(a-b);
ll y= a%x;
y = min(y, x-y);
cout<<x<<" "<<y<<"\n";
}
}
}
``` |
//@ikung
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fast \
ios_base::sync_with_stdio(0); \
cin.tie(0);
#define f(i, k) for (int i = 0; i < k; i++)
#define F first
#define S second
#define endl "\n"
#define rep(i, n) for (int i = 1; i <= n; i++)
#define rew(i, a, b) for (int i = a; i <= b; i++)
#define dbg(...) logger(#__VA_ARGS__, __VA_ARGS__)
template <typename... Args>
void logger(string vars, Args &&...values)
{
cout << vars << " = ";
string delim = "";
(..., (cout << delim << values, delim = ","));
cout << endl;
}
#define mod 1000000007
const int inf = 1e18;
const int N = 200005;
int n;
void solve()
{
int i, j, k;
int a, b;
cin >> a >> b;
if (a < b)
swap(a, b);
int diff = a - b,move;
if(diff==0)
move = 0;
else
{
move = (a % diff);
if(diff-a%diff>0)
move = min(move, diff - a % diff);
}
cout << diff << " "<<move<< endl;
return;
}
signed main()
{
fast int t = 1, i, j, k;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
//#ifndef ONLINE_JUDGE
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
//#endif | ### Prompt
Your task is to create a CPP solution to the following problem:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
//@ikung
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fast \
ios_base::sync_with_stdio(0); \
cin.tie(0);
#define f(i, k) for (int i = 0; i < k; i++)
#define F first
#define S second
#define endl "\n"
#define rep(i, n) for (int i = 1; i <= n; i++)
#define rew(i, a, b) for (int i = a; i <= b; i++)
#define dbg(...) logger(#__VA_ARGS__, __VA_ARGS__)
template <typename... Args>
void logger(string vars, Args &&...values)
{
cout << vars << " = ";
string delim = "";
(..., (cout << delim << values, delim = ","));
cout << endl;
}
#define mod 1000000007
const int inf = 1e18;
const int N = 200005;
int n;
void solve()
{
int i, j, k;
int a, b;
cin >> a >> b;
if (a < b)
swap(a, b);
int diff = a - b,move;
if(diff==0)
move = 0;
else
{
move = (a % diff);
if(diff-a%diff>0)
move = min(move, diff - a % diff);
}
cout << diff << " "<<move<< endl;
return;
}
signed main()
{
fast int t = 1, i, j, k;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
//#ifndef ONLINE_JUDGE
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
//#endif
``` |
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define DBG(x) \
(void)(cout << "L" << __LINE__ \
<< ": " << #x << " = " \
<< (x) << "\n")
typedef long long ll;
const int maxn=110000;
const int INF=0x3f3f3f3f;
void run_case()
{
int T;
cin >> T;
while (T--)
{
ll a, b;
cin >> a >> b;
if (a == b)
cout << 0 << " " << 0 << "\n";
else
{
ll cur = abs(a-b);
cout << cur << " " << min(a % cur, cur - a % cur) << "\n";
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout << setiosflags(ios::fixed) << setprecision(12);
run_case();
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define DBG(x) \
(void)(cout << "L" << __LINE__ \
<< ": " << #x << " = " \
<< (x) << "\n")
typedef long long ll;
const int maxn=110000;
const int INF=0x3f3f3f3f;
void run_case()
{
int T;
cin >> T;
while (T--)
{
ll a, b;
cin >> a >> b;
if (a == b)
cout << 0 << " " << 0 << "\n";
else
{
ll cur = abs(a-b);
cout << cur << " " << min(a % cur, cur - a % cur) << "\n";
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout << setiosflags(ios::fixed) << setprecision(12);
run_case();
return 0;
}
``` |
#include<bits/stdc++.h>
#define ll long long
#define str string
#define ch char
#define sz 200005
using namespace std;
int main()
{
ll t;
cin>>t;
while(t--)
{
ll a,b,x;
cin>>a>>b;
if(a==b)
{
cout<<0<<' '<<0<<endl;
}
else
{
x=abs(a-b);
cout<<x<<' '<<min(a%x, x-(a%x))<<endl;
}
}
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include<bits/stdc++.h>
#define ll long long
#define str string
#define ch char
#define sz 200005
using namespace std;
int main()
{
ll t;
cin>>t;
while(t--)
{
ll a,b,x;
cin>>a>>b;
if(a==b)
{
cout<<0<<' '<<0<<endl;
}
else
{
x=abs(a-b);
cout<<x<<' '<<min(a%x, x-(a%x))<<endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sum(a) (accumulate((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) (min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) (max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) (lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) (upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define pv(v) \
for (auto u : v) \
cout << u << " ";
#define nl (cout << "\n")
typedef long long ll;
using namespace std;
int i, j, k;
ll mod = 1e9 + 7;
const int N = 1e5 + 7;
void solve()
{
ll a, b;
cin >> a >> b;
ll gd = abs(b - a);
ll ma, mb;
if (gd == 0)
{
cout << gd << " " << 0 << "\n";
return;
}
ma = min(gd - a % gd, a % gd);
mb = min(gd - b % gd, b % gd);
cout << gd << " " << min(ma, mb) << "\n";
}
int main()
{
int tcase;
cin >> tcase;
while (tcase--)
solve();
return 0;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sum(a) (accumulate((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) (min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) (max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) (lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) (upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define pv(v) \
for (auto u : v) \
cout << u << " ";
#define nl (cout << "\n")
typedef long long ll;
using namespace std;
int i, j, k;
ll mod = 1e9 + 7;
const int N = 1e5 + 7;
void solve()
{
ll a, b;
cin >> a >> b;
ll gd = abs(b - a);
ll ma, mb;
if (gd == 0)
{
cout << gd << " " << 0 << "\n";
return;
}
ma = min(gd - a % gd, a % gd);
mb = min(gd - b % gd, b % gd);
cout << gd << " " << min(ma, mb) << "\n";
}
int main()
{
int tcase;
cin >> tcase;
while (tcase--)
solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
int32_t mod = 1e9 + 7;
void solveCase()
{
int a = 0, b = 0;
cin >> a >> b;
int mx = abs(a - b);
if (mx == 0)
{
cout << "0 0\n";
return;
}
int moves = (a % mx ? min(a % mx, mx - a % mx) : 0);
cout << mx << ' ' << moves << '\n';
}
int32_t main()
{
ios::sync_with_stdio(false), cin.tie(NULL);
int t = 0;
cin >> t;
while (t--)
solveCase();
} | ### Prompt
Please provide a Cpp coded solution to the problem described below:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
int32_t mod = 1e9 + 7;
void solveCase()
{
int a = 0, b = 0;
cin >> a >> b;
int mx = abs(a - b);
if (mx == 0)
{
cout << "0 0\n";
return;
}
int moves = (a % mx ? min(a % mx, mx - a % mx) : 0);
cout << mx << ' ' << moves << '\n';
}
int32_t main()
{
ios::sync_with_stdio(false), cin.tie(NULL);
int t = 0;
cin >> t;
while (t--)
solveCase();
}
``` |
#include<iostream>
using namespace std;
#include<bits/stdc++.h>
#include<string.h>
#include <iomanip>
#include <ctype.h>
#define ll long long int
#define test cout<<"Nevil\n";
#define ios ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pi 3.14159265359
#define precise(x) fixed << setprecision(x)
ll md=1e9+7;
ll mod_pow(ll a,ll b,ll M = md)
{
if(a == 0) return 0;
b %= (M - 1);
ll res = 1;
while(b > 0)
{
if(b&1) res=(res*a)%M;
a=(a*a)%M;
b>>=1;
}
return res;
}
void solve()
{
}
int main()
{
ios;
ll cases=1;
cin>>cases;
for(ll zz=1;zz<=cases;zz++)
{
ll a,b;
cin>>a>>b;
if(a==b)
{
cout<<"0 0\n";
continue;
}
ll t=abs(a-b);
ll t2=min(a%t,t-(a%t));
cout<<t<<" "<<t2<<"\n";
}
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include<iostream>
using namespace std;
#include<bits/stdc++.h>
#include<string.h>
#include <iomanip>
#include <ctype.h>
#define ll long long int
#define test cout<<"Nevil\n";
#define ios ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pi 3.14159265359
#define precise(x) fixed << setprecision(x)
ll md=1e9+7;
ll mod_pow(ll a,ll b,ll M = md)
{
if(a == 0) return 0;
b %= (M - 1);
ll res = 1;
while(b > 0)
{
if(b&1) res=(res*a)%M;
a=(a*a)%M;
b>>=1;
}
return res;
}
void solve()
{
}
int main()
{
ios;
ll cases=1;
cin>>cases;
for(ll zz=1;zz<=cases;zz++)
{
ll a,b;
cin>>a>>b;
if(a==b)
{
cout<<"0 0\n";
continue;
}
ll t=abs(a-b);
ll t2=min(a%t,t-(a%t));
cout<<t<<" "<<t2<<"\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
#define all(x) x.begin(),x.end()
#define setval(arr,v) memset(arr,v,sizeof(arr));
#define int long long int
#define mod 1000000007
#define v vector
#define umap unordered_map
#define pb push_back
#define pf push_front
#define pii pair<int,int>
#define fi first
#define se second
#define INF 1000000000000000
using namespace std;
void solve()
{
int a,b;
cin>>a>>b;
if(b > a)
swap(a,b);
cout<<a-b<<" ";
if(a-b == 0)
cout<<0<<"\n";
else
cout<<min(((a-b)-(b)%(a-b))%(a-b),b%(a-b))<<"\n";
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
cin>>t;
while(t--)
{
solve();
}
return 0;
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include <bits/stdc++.h>
#define all(x) x.begin(),x.end()
#define setval(arr,v) memset(arr,v,sizeof(arr));
#define int long long int
#define mod 1000000007
#define v vector
#define umap unordered_map
#define pb push_back
#define pf push_front
#define pii pair<int,int>
#define fi first
#define se second
#define INF 1000000000000000
using namespace std;
void solve()
{
int a,b;
cin>>a>>b;
if(b > a)
swap(a,b);
cout<<a-b<<" ";
if(a-b == 0)
cout<<0<<"\n";
else
cout<<min(((a-b)-(b)%(a-b))%(a-b),b%(a-b))<<"\n";
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
cin>>t;
while(t--)
{
solve();
}
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
int t;
int main() {
cin >> t;
while(t--) {
ll a,b;
cin >> a >> b;
if(a==b) {
cout << "0 0" << endl;
continue;
}
ll num=abs(a-b);
cout << num << " " << min(a%num,num-a%num) << endl;
}
} | ### Prompt
Generate a CPP solution to the following problem:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
int t;
int main() {
cin >> t;
while(t--) {
ll a,b;
cin >> a >> b;
if(a==b) {
cout << "0 0" << endl;
continue;
}
ll num=abs(a-b);
cout << num << " " << min(a%num,num-a%num) << endl;
}
}
``` |
#include<bits/stdc++.h>
using namespace std;
///typedef
typedef long long ll;
typedef pair<int, int> pii;
///define constant
const ll mxn = 200005;
const int mod = 1000000007;//1e9+7;
///faster io
#define faster_io ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
///debug
#define watch(x) cout << __LINE__ << " says: " << #x << " = " << x << "\n"
#define watch2(x,y) cout<< __LINE__ << " says: " <<#x<<" = "<<x<<" "<<#y<<" = "<<y <<endl
#define watch3(x,y,z) cout<< __LINE__ << " says: " <<#x<<" = "<<x<<" "<<#y<<" = "<<y <<" "<<#z<<" = "<<z<<endl
///program starts...........
void solve_case(int tc)
{
ll a, b;
cin>>a>>b;
ll gcd = abs(a-b);
cout<<gcd;
if(gcd == 0)
{
cout<<" 0"<<endl;
return;
}
ll r = a % gcd;
cout<<" "<<min(r, gcd - r)<<endl;
return;
}
int main()
{
faster_io;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int test_case=1;
cin>>test_case;
for(int tc=1 ; tc<=test_case; tc++)
{
solve_case(tc);
}
/// KeyPoint
/// Corner Cases
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
///typedef
typedef long long ll;
typedef pair<int, int> pii;
///define constant
const ll mxn = 200005;
const int mod = 1000000007;//1e9+7;
///faster io
#define faster_io ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
///debug
#define watch(x) cout << __LINE__ << " says: " << #x << " = " << x << "\n"
#define watch2(x,y) cout<< __LINE__ << " says: " <<#x<<" = "<<x<<" "<<#y<<" = "<<y <<endl
#define watch3(x,y,z) cout<< __LINE__ << " says: " <<#x<<" = "<<x<<" "<<#y<<" = "<<y <<" "<<#z<<" = "<<z<<endl
///program starts...........
void solve_case(int tc)
{
ll a, b;
cin>>a>>b;
ll gcd = abs(a-b);
cout<<gcd;
if(gcd == 0)
{
cout<<" 0"<<endl;
return;
}
ll r = a % gcd;
cout<<" "<<min(r, gcd - r)<<endl;
return;
}
int main()
{
faster_io;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int test_case=1;
cin>>test_case;
for(int tc=1 ; tc<=test_case; tc++)
{
solve_case(tc);
}
/// KeyPoint
/// Corner Cases
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
#define read(x) freopen(x, "r", stdin);
#define write(x) freopen(x, "w", stdout);
#define ll long long
#define lli long long int
#define FOR(n) for(int i=0; i<(int)(n); ++i)
#define mod 1073741824
#define MAX 100001
#define gcd(a, b) __gcd(a, b)
int main()
{
ll t;
cin>>t;
while(t--)
{
ll n,m,a,b,flag;
cin>>a>>b;
flag =abs(a-b);
if(flag == 0)
{
cout<<"0 "<<"0"<<endl;
}
else
{
cout<<flag<<" "<<min(a%flag,flag-(a%flag))<<endl;
}
}
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
#define read(x) freopen(x, "r", stdin);
#define write(x) freopen(x, "w", stdout);
#define ll long long
#define lli long long int
#define FOR(n) for(int i=0; i<(int)(n); ++i)
#define mod 1073741824
#define MAX 100001
#define gcd(a, b) __gcd(a, b)
int main()
{
ll t;
cin>>t;
while(t--)
{
ll n,m,a,b,flag;
cin>>a>>b;
flag =abs(a-b);
if(flag == 0)
{
cout<<"0 "<<"0"<<endl;
}
else
{
cout<<flag<<" "<<min(a%flag,flag-(a%flag))<<endl;
}
}
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
long long a,b;
cin>>a>>b;
if (a==b)
cout<<0<<" "<<0<<endl;
else
{
long long d= abs(a-b);
long long s= 0;
if( a==0 || b==0)
s=0;
else
s=min(a%d, d-(a%d));
cout<<d<<" "<<s<<endl;
}
}
return 0;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
long long a,b;
cin>>a>>b;
if (a==b)
cout<<0<<" "<<0<<endl;
else
{
long long d= abs(a-b);
long long s= 0;
if( a==0 || b==0)
s=0;
else
s=min(a%d, d-(a%d));
cout<<d<<" "<<s<<endl;
}
}
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
long long a,b;
cin>>a>>b;
if(a==b)
cout<<0<<" "<<0<<"\n";
else
{
long long g = abs(a-b);
long long d = min(a%g,g-a%g);
cout<<g<<" "<<d<<"\n";
}
}
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
long long a,b;
cin>>a>>b;
if(a==b)
cout<<0<<" "<<0<<"\n";
else
{
long long g = abs(a-b);
long long d = min(a%g,g-a%g);
cout<<g<<" "<<d<<"\n";
}
}
return 0;
}
``` |
#include<bits/stdc++.h>
#define inf 100000000
#define int long long
#define ll long long
#define fr(a,b) for(int i = a; i < b; i++)
#define rep(i,a,b) for(int i = a; i < b; i++)
#define repn(i,a,b) for(int i = a-1; i >=0; i--)
#define prDouble(x) cout << fixed << setprecision(10) << x
#define endl "\n"
#define yes cout<<"YES"<<"\n"
#define no cout<<"NO"<<"\n"
#define mod 1000000007
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define vi vector<int>
#define read(x) int x; cin >> x
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)
#define pi pair < int,int >
using namespace std;
signed main()
{
fast_io;
int t;
cin>>t;
while(t--)
{
int a,b;
cin>>a>>b;
int x=abs(a-b);
if (x==0) cout<<0<<" "<<0<<endl;
else cout<<abs(a-b)<<" "<<min((b%x),x-(b%x))<<endl;
}
} | ### Prompt
Your challenge is to write a CPP solution to the following problem:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include<bits/stdc++.h>
#define inf 100000000
#define int long long
#define ll long long
#define fr(a,b) for(int i = a; i < b; i++)
#define rep(i,a,b) for(int i = a; i < b; i++)
#define repn(i,a,b) for(int i = a-1; i >=0; i--)
#define prDouble(x) cout << fixed << setprecision(10) << x
#define endl "\n"
#define yes cout<<"YES"<<"\n"
#define no cout<<"NO"<<"\n"
#define mod 1000000007
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define vi vector<int>
#define read(x) int x; cin >> x
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)
#define pi pair < int,int >
using namespace std;
signed main()
{
fast_io;
int t;
cin>>t;
while(t--)
{
int a,b;
cin>>a>>b;
int x=abs(a-b);
if (x==0) cout<<0<<" "<<0<<endl;
else cout<<abs(a-b)<<" "<<min((b%x),x-(b%x))<<endl;
}
}
``` |
#include <iostream>
#include<string>
#include<algorithm>
#include<climits>
#include<cmath>
#include<vector>
#include<map>
using namespace std;
#define ll long long
// void pg()
// {
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
// }
int main(){
ll t;
cin>>t;
while(t--){
ll a,b;
cin>>a>>b;
if(a==b){
cout<<0<<" "<<0<<endl;
continue;
}
ll c=abs(a-b);
ll d=c-(a%c);
d=min(d,c-d);
if(a%c==0) d=0;
cout<<c<<" "<<d<<endl;
}
return 0;
} | ### Prompt
Construct a Cpp code solution to the problem outlined:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include <iostream>
#include<string>
#include<algorithm>
#include<climits>
#include<cmath>
#include<vector>
#include<map>
using namespace std;
#define ll long long
// void pg()
// {
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
// }
int main(){
ll t;
cin>>t;
while(t--){
ll a,b;
cin>>a>>b;
if(a==b){
cout<<0<<" "<<0<<endl;
continue;
}
ll c=abs(a-b);
ll d=c-(a%c);
d=min(d,c-d);
if(a%c==0) d=0;
cout<<c<<" "<<d<<endl;
}
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
typedef long long ll;
typedef pair<int,int> pii;
const ll MOD = 1e9 + 7;
const int N = 2e5 + 5;
int n, t;
ll gcd(ll a, ll b){
if(a % b) return gcd(b, a % b);
return b;
}
int main(){
scanf("%d", &t);
while(t--){
ll a, b;
scanf("%lld %lld", &a, &b);
if(a == b) printf("0 0\n");
else if(!a || !b) printf("%lld 0\n", max(a, b));
else printf("%lld %lld\n", abs(a - b), min(a % abs(a - b), abs(a - b) - a % abs(a - b)));
}
return 0;
}
| ### Prompt
In CPP, your task is to solve the following problem:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
typedef long long ll;
typedef pair<int,int> pii;
const ll MOD = 1e9 + 7;
const int N = 2e5 + 5;
int n, t;
ll gcd(ll a, ll b){
if(a % b) return gcd(b, a % b);
return b;
}
int main(){
scanf("%d", &t);
while(t--){
ll a, b;
scanf("%lld %lld", &a, &b);
if(a == b) printf("0 0\n");
else if(!a || !b) printf("%lld 0\n", max(a, b));
else printf("%lld %lld\n", abs(a - b), min(a % abs(a - b), abs(a - b) - a % abs(a - b)));
}
return 0;
}
``` |
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
typedef long long ll;
/*int gcd(ll n1,ll n2)
{
return _gcd(n1,n2);
}
*/int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin>>t;
while(t--)
{
ll a,b,c,d;
cin>>a>>b;
d=abs(a-b);
if(d==0)
{
cout<<"0 0"<<endl;
}
else
{
c=min(a%d , d-(a%d));
cout<<d<<" "<<c<<endl;
}
}
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
typedef long long ll;
/*int gcd(ll n1,ll n2)
{
return _gcd(n1,n2);
}
*/int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin>>t;
while(t--)
{
ll a,b,c,d;
cin>>a>>b;
d=abs(a-b);
if(d==0)
{
cout<<"0 0"<<endl;
}
else
{
c=min(a%d , d-(a%d));
cout<<d<<" "<<c<<endl;
}
}
}
``` |
#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> //Policy Based Data Structure
// using namespace __gnu_pbds; //Policy Based Data Structure
using namespace std;
// typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> pbds; //Policy Based Data Structure
// #define gc getchar_unlocked
// #define pqb priority_queue<int>
// #define pqs priority_queue<int, vi, greater<int> >
// #define mk(arr,n,type) type *arr = new type[n]
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define int long long
#define endl '\n'
#define w(t) int t; cin>>t; while(t--)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x,y) cout << #x << "=" << x << "," << #y << "=" << y << endl
void print(bool n) {
if (n) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x,val) memset(x, val, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it,a) for(auto it = a.begin(); it != a.end(); it++)
#define ps(x,y) fixed<<setprecision(y)<<x
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define PI 3.1415926535897932384626
#define inf 1e18
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //Random Shuffler
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef map<int, int> mii;
int mpow(int base, int exp);
void ipgraph(int m);
void dfs(int u, int par);
const int mod = 1000000007;
// const int N = 3e5, M = N;
// vi g[N];
// no of prime numbers in range : (70,19) , (1000,168), (100000,1229) , (sqrt(10^9),3409)
//=======================
void sol()
{
int a, b;
cin >> a >> b;
if (a - b == 0)
{
cout << 0 << " " << 0 << endl;
return;
}
if (a < b)
{
swap(a, b);
}
int ans = a - b;
int ans2 = inf;
if (((a + ans - 1) / ans) * ans - a >= 0)
{
ans2 = min(ans2, ((a + ans - 1) / ans) * ans - a);
}
if ( a - (a / ans)*ans >= 0)
{
ans2 = min(ans2, a - (a / ans) * ans);
}
if (((b + ans - 1) / ans)*ans - b >= 0)
{
ans2 = min(ans2, ((b + ans - 1) / ans) * ans - b);
}
if (b - (b / ans)*ans >= 0)
{
ans2 = min(ans2, b - (b / ans) * ans);
}
cout << ans << " " << ans2 << endl;
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
w(t)
sol();
return 0;
}
int mpow(int base, int exp) {
base %= mod;
int result = 1;
while (exp > 0) {
if (exp & 1) result = (result * base) % mod;
base = (base * base) % mod;
exp >>= 1;
}
return result;
}
// void ipgraph(int n, int m){
// int i, u, v;
// while(m--){
// cin>>u>>v;
// g[u-1].pb(v-1);
// g[v-1].pb(u-1);
// }
// }
//
// void dfs(int u, int par){
// for(int v:g[u]){
// if (v == par) continue;
// dfs(v, u);
// }
// } | ### Prompt
Generate a cpp solution to the following problem:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> //Policy Based Data Structure
// using namespace __gnu_pbds; //Policy Based Data Structure
using namespace std;
// typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> pbds; //Policy Based Data Structure
// #define gc getchar_unlocked
// #define pqb priority_queue<int>
// #define pqs priority_queue<int, vi, greater<int> >
// #define mk(arr,n,type) type *arr = new type[n]
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define int long long
#define endl '\n'
#define w(t) int t; cin>>t; while(t--)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x,y) cout << #x << "=" << x << "," << #y << "=" << y << endl
void print(bool n) {
if (n) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x,val) memset(x, val, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it,a) for(auto it = a.begin(); it != a.end(); it++)
#define ps(x,y) fixed<<setprecision(y)<<x
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define PI 3.1415926535897932384626
#define inf 1e18
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //Random Shuffler
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef map<int, int> mii;
int mpow(int base, int exp);
void ipgraph(int m);
void dfs(int u, int par);
const int mod = 1000000007;
// const int N = 3e5, M = N;
// vi g[N];
// no of prime numbers in range : (70,19) , (1000,168), (100000,1229) , (sqrt(10^9),3409)
//=======================
void sol()
{
int a, b;
cin >> a >> b;
if (a - b == 0)
{
cout << 0 << " " << 0 << endl;
return;
}
if (a < b)
{
swap(a, b);
}
int ans = a - b;
int ans2 = inf;
if (((a + ans - 1) / ans) * ans - a >= 0)
{
ans2 = min(ans2, ((a + ans - 1) / ans) * ans - a);
}
if ( a - (a / ans)*ans >= 0)
{
ans2 = min(ans2, a - (a / ans) * ans);
}
if (((b + ans - 1) / ans)*ans - b >= 0)
{
ans2 = min(ans2, ((b + ans - 1) / ans) * ans - b);
}
if (b - (b / ans)*ans >= 0)
{
ans2 = min(ans2, b - (b / ans) * ans);
}
cout << ans << " " << ans2 << endl;
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
w(t)
sol();
return 0;
}
int mpow(int base, int exp) {
base %= mod;
int result = 1;
while (exp > 0) {
if (exp & 1) result = (result * base) % mod;
base = (base * base) % mod;
exp >>= 1;
}
return result;
}
// void ipgraph(int n, int m){
// int i, u, v;
// while(m--){
// cin>>u>>v;
// g[u-1].pb(v-1);
// g[v-1].pb(u-1);
// }
// }
//
// void dfs(int u, int par){
// for(int v:g[u]){
// if (v == par) continue;
// dfs(v, u);
// }
// }
``` |
///to serach
//n to go forwards N to bakcwards
//? to search backwards???? idr
//bitwise
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<long long>
#define vpi vector<pair<ll, ll>>
#define pb push_back
#define pi pair<ll, ll>
#define mp make_pair
#define mod 4294967295
#define p pair
#define int ll
signed main(){
// freopen("test.in","r",stdin);
int t = 1;
cin>>t;
while(t--){
ll a, b;
cin>>a>>b;
ll x = max(a, b)-min(a, b);
cout<<x<<" ";
if(x==0){
cout<<0<<endl;
}else{
cout<<min(a%x, x-(a%x))<<endl;
}
}
} | ### Prompt
Please create a solution in cpp to the following problem:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
///to serach
//n to go forwards N to bakcwards
//? to search backwards???? idr
//bitwise
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<long long>
#define vpi vector<pair<ll, ll>>
#define pb push_back
#define pi pair<ll, ll>
#define mp make_pair
#define mod 4294967295
#define p pair
#define int ll
signed main(){
// freopen("test.in","r",stdin);
int t = 1;
cin>>t;
while(t--){
ll a, b;
cin>>a>>b;
ll x = max(a, b)-min(a, b);
cout<<x<<" ";
if(x==0){
cout<<0<<endl;
}else{
cout<<min(a%x, x-(a%x))<<endl;
}
}
}
``` |
//Jai Shri Ram
//Jai Shri Krishna
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int> vi ;
typedef map<int,int> mii ;
typedef pair<int,int> pii ;
#define pb push_back
#define all(V) V.begin(),V.end()
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define distinct(v) distance(v.begin(), unique(all(v))) //first sort then use
#define ub upper_bound
#define lb lower_bound
#define ff first
#define ss second
#define fill(baxq,I) (memset(baxq,I,sizeof(baxq)))
#define sz(x) (int)((x).size())
#define endl "\n"
const int32_t mod=1e9+7;
const long long inf=1e18;
const int N = 1e5+5;
void testCases()
{
int a,b;
cin>>a>>b;
if(a==b)cout<<0<<" "<<0<<endl;
else
{
int x= abs(a-b);
int y=max(a,b);
int q=min(a,b);
int h=x*((y+x-1)/x) - y;
int k= q- x*((q)/x);
cout<<abs(a-b)<<" "<<min(h,k)<<endl;
}
}
int32_t main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tcs=1;
cin>>tcs;
while(tcs--)
{
testCases() ;
}
} | ### Prompt
Your task is to create a Cpp solution to the following problem:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
//Jai Shri Ram
//Jai Shri Krishna
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int> vi ;
typedef map<int,int> mii ;
typedef pair<int,int> pii ;
#define pb push_back
#define all(V) V.begin(),V.end()
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define distinct(v) distance(v.begin(), unique(all(v))) //first sort then use
#define ub upper_bound
#define lb lower_bound
#define ff first
#define ss second
#define fill(baxq,I) (memset(baxq,I,sizeof(baxq)))
#define sz(x) (int)((x).size())
#define endl "\n"
const int32_t mod=1e9+7;
const long long inf=1e18;
const int N = 1e5+5;
void testCases()
{
int a,b;
cin>>a>>b;
if(a==b)cout<<0<<" "<<0<<endl;
else
{
int x= abs(a-b);
int y=max(a,b);
int q=min(a,b);
int h=x*((y+x-1)/x) - y;
int k= q- x*((q)/x);
cout<<abs(a-b)<<" "<<min(h,k)<<endl;
}
}
int32_t main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tcs=1;
cin>>tcs;
while(tcs--)
{
testCases() ;
}
}
``` |
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
#include <cmath>
#include <map>
#include <algorithm>
#include <utility>
#include <string>
#include <cstdlib>
#include <queue>
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const ll N = 2e5 + 7;
ll t, ma, nm, cnt = 0, edx, mi = N, sum;
ll num[N];
ll mm[N];
int main()
{
cin >> t;
while (t --)
{
ll a, b;
cin >> a >> b;
if (a == b)
{
cout << "0 0" << endl;
continue;
}
int c = max(a, b) - min(a, b);
nm = 0;
ll g = abs(a-b);
ll m = min(a%g,g-a%g);
cout << g << " " << m << endl;
}
return 0;
} | ### Prompt
Develop a solution in CPP to the problem described below:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
#include <cmath>
#include <map>
#include <algorithm>
#include <utility>
#include <string>
#include <cstdlib>
#include <queue>
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const ll N = 2e5 + 7;
ll t, ma, nm, cnt = 0, edx, mi = N, sum;
ll num[N];
ll mm[N];
int main()
{
cin >> t;
while (t --)
{
ll a, b;
cin >> a >> b;
if (a == b)
{
cout << "0 0" << endl;
continue;
}
int c = max(a, b) - min(a, b);
nm = 0;
ll g = abs(a-b);
ll m = min(a%g,g-a%g);
cout << g << " " << m << endl;
}
return 0;
}
``` |
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Code written by Aditya ;) || Codechef/codeforces: @adityaraj5200
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include<bits/stdc++.h>
using namespace std;
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//template<class T> using ordered_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update >;
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define endl "\n"
#define mod 1000000007
#define mod2 998244353
#define PI 3.14159265358979323846
#define deb(u) cout<<'>'<<#u<<" = "<<u<<endl
#define deb2(u, v) cout<<'>'<<#u<<" = "<<u<<" , "<<#v<<" = "<<v<<endl
#define all(u) u.begin(), u.end()
#define rall(v) v.rbegin(), v.rend()
#define sortall(u) sort(all(u))
#define lcm(a,b) (a*b)/__gcd(a,b)
#define gcd(a,b) __gcd(a,b)
#define inclusive(a,b) (abs(b-a)+1)
#define gap(a,b) max(0,(abs(b-a)-1))
#define summation(n) (((n)*(n+1))/2)
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define mp make_pair
#define lb(u,val) lower_bound(all(u),val)
#define ub(u,val) upper_bound(all(u),val)
#define yes "YES"
#define no "NO"
#define chartoint(ch) ch-'0'
#define digits(n) (1+floor(log10(n)))
#define presum(p,a,n) int (p)[(n)];p[0]=a[0];for(int i=1;i<(n);i++)p[i]=a[i]+p[i-1];
#define cube(u) (u)*(u)*(u)
#define sq(u) (u)*(u)
#define fill0(a) memset(a,0,sizeof(a))
#define fillneg1(a) memset(a,-1,sizeof(a))
#define fillbig(a) memset(a,63,sizeof(a))
#define setbits(u) __builtin_popcount(u)
#define ctz(u) __builtin_ctz(u)
#define clz(u) __builtin_clz(u)
#define checkbit(num,i) (num&(1<<i)) //select the bit of position i of val
#define lowbit(u) ((u)&((u)^((u)-1))) //get the lowest bit of u
#define trav(u,it) for(auto it = u.begin(); it != u.end(); it++)
#define present(u,key) u.find(key) != u.end()
#define notpresent(u,key) u.find(key) == u.end()
#define in(u, a, b) (a <= u && u <= b)
#define print(u) for(auto it=u.begin();it!=u.end();it++)\
cout<<*it<<' '; cout<<endl
#define printii(u) for(auto it=u.begin();it!=u.end();it++)\
cout<<it->first<<' '<<it->second<<endl
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<long long> vll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
ll mod_add(ll a, ll b, ll m) {return (a%m + b%m)%m;}
ll mod_mul(ll a, ll b, ll m) {return (a%m * b%m)%m;}
ll mod_sub(ll a, ll b, ll m) {return (a%m - b%m)%m;}
// all 4 & 8- direction move
int dx4[4]={1,0,-1,0};
int dy4[4]={0,1,0,-1};
int dx8[8]={1,1,1,0,0,-1,-1,-1};
int dy8[8]={0,1,-1,1,-1,0,1,-1};
/*/------------------------------ CODE BEGINS ------------------------------/*/
void solve(){
ll a,b; cin>>a>>b;
if(a>b) swap(a,b);
ll maxgcd = b-a;
if(a==b) cout<<"0 0";
else cout<<maxgcd<<' '<<min(a%maxgcd, maxgcd-(a%maxgcd));
cout<<endl;
}
/*/------------------------------- CODE ENDS -------------------------------/*/
int main(){
fastio;
// cout << setprecision(12) << fixed;
int tc=1;
cin>>tc;
//precompute();
for(int t=1;t<=tc;t++){
// cout<<"Case #" << t << ": ";
solve();
}
return 0;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Code written by Aditya ;) || Codechef/codeforces: @adityaraj5200
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include<bits/stdc++.h>
using namespace std;
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//template<class T> using ordered_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update >;
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define endl "\n"
#define mod 1000000007
#define mod2 998244353
#define PI 3.14159265358979323846
#define deb(u) cout<<'>'<<#u<<" = "<<u<<endl
#define deb2(u, v) cout<<'>'<<#u<<" = "<<u<<" , "<<#v<<" = "<<v<<endl
#define all(u) u.begin(), u.end()
#define rall(v) v.rbegin(), v.rend()
#define sortall(u) sort(all(u))
#define lcm(a,b) (a*b)/__gcd(a,b)
#define gcd(a,b) __gcd(a,b)
#define inclusive(a,b) (abs(b-a)+1)
#define gap(a,b) max(0,(abs(b-a)-1))
#define summation(n) (((n)*(n+1))/2)
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define mp make_pair
#define lb(u,val) lower_bound(all(u),val)
#define ub(u,val) upper_bound(all(u),val)
#define yes "YES"
#define no "NO"
#define chartoint(ch) ch-'0'
#define digits(n) (1+floor(log10(n)))
#define presum(p,a,n) int (p)[(n)];p[0]=a[0];for(int i=1;i<(n);i++)p[i]=a[i]+p[i-1];
#define cube(u) (u)*(u)*(u)
#define sq(u) (u)*(u)
#define fill0(a) memset(a,0,sizeof(a))
#define fillneg1(a) memset(a,-1,sizeof(a))
#define fillbig(a) memset(a,63,sizeof(a))
#define setbits(u) __builtin_popcount(u)
#define ctz(u) __builtin_ctz(u)
#define clz(u) __builtin_clz(u)
#define checkbit(num,i) (num&(1<<i)) //select the bit of position i of val
#define lowbit(u) ((u)&((u)^((u)-1))) //get the lowest bit of u
#define trav(u,it) for(auto it = u.begin(); it != u.end(); it++)
#define present(u,key) u.find(key) != u.end()
#define notpresent(u,key) u.find(key) == u.end()
#define in(u, a, b) (a <= u && u <= b)
#define print(u) for(auto it=u.begin();it!=u.end();it++)\
cout<<*it<<' '; cout<<endl
#define printii(u) for(auto it=u.begin();it!=u.end();it++)\
cout<<it->first<<' '<<it->second<<endl
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<long long> vll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
ll mod_add(ll a, ll b, ll m) {return (a%m + b%m)%m;}
ll mod_mul(ll a, ll b, ll m) {return (a%m * b%m)%m;}
ll mod_sub(ll a, ll b, ll m) {return (a%m - b%m)%m;}
// all 4 & 8- direction move
int dx4[4]={1,0,-1,0};
int dy4[4]={0,1,0,-1};
int dx8[8]={1,1,1,0,0,-1,-1,-1};
int dy8[8]={0,1,-1,1,-1,0,1,-1};
/*/------------------------------ CODE BEGINS ------------------------------/*/
void solve(){
ll a,b; cin>>a>>b;
if(a>b) swap(a,b);
ll maxgcd = b-a;
if(a==b) cout<<"0 0";
else cout<<maxgcd<<' '<<min(a%maxgcd, maxgcd-(a%maxgcd));
cout<<endl;
}
/*/------------------------------- CODE ENDS -------------------------------/*/
int main(){
fastio;
// cout << setprecision(12) << fixed;
int tc=1;
cin>>tc;
//precompute();
for(int t=1;t<=tc;t++){
// cout<<"Case #" << t << ": ";
solve();
}
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long int
#define endl "\n"
#define pb push_back
#define mp make_pair
#define ut unsigned int
#define ss second
#define ff first
#define vi vector<int>
#define vl vector<ll>
#define lb lower_bound
#define up upper_bound
#define re return
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define sp(x) fixed << setprecision(x)
ll inline power(ll a, ll b, ll p){
a %= p;
ll ans = 1;
while(b>0){
if(b & 1)
ans = (ans*a)%p;
a = (a*a)%p;
b >>= 1;
}
return ans;
}
ll inv(ll n, ll p){
return power(n,p-2, p);
}
bool inline isprime(ll n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n%2 == 0 || n%3 == 0) return false;
for (ll i=5; i*i<=n; i=i+6)
if (n%i == 0 || n%(i+2) == 0)
return false;
return true;
}
/*ll dp[1000001];
//memset(dp,0,sizeof(dp));
void seive()
{
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
memset(dp,0,sizeof(dp));
ll n=1000000,i;
bool prime[n+1];
memset(prime, true, sizeof(prime));
for (ll p=2; p*p<=n; p++)
{
// If prime[p] is not changed, then it is a prime
if (prime[p] == true)
{
// Update all multiples of p greater than or
// equal to the square of it
// numbers which are multiple of p and are
// less than p^2 are already been marked.
for ( ll i=p*p; i<=n; i += p)
prime[i] = false;
}
}
dp[1]=1;
for (int p=2; p<=n; p++){
if (prime[p]==true)
dp[p]=1+dp[p-1];
else
dp[p]=dp[p-1];
}
}
bool issorted(int a[],int n){
int i=0,cnt=0;
for(i=1;i<n;i++){
if(a[i]>=a[i-1])
cnt++;
else
return false;
}
return true;
}
// ll dp[1000001],powe[1000001];
int binary-search(int a[],int n,int target){
int low,high,mid;
low=0;
high=n-1;
while(high-low>1){
mid=(low+high)/2;
if(a[mid]==target){
return mid;
}
if(a[mid]>target)
high=mid-1;
else
low=mid+1;
}
if(a[low]==target)
return low;
if(a[high]==target)
return high;
return 0;
}
*/
void solve(){
ll a,b,ans=0;
cin>>a>>b;
if(a==b){
cout<<"0"<<" "<<"0"<<endl;
re;
}
ans=abs(a-b);
if(ans==1){
cout<<"1"<<" "<<"0"<<endl;
re;
}
ll r=min(a,b),k=0,m;
m=min(r%ans,ans-r%ans);
cout<<ans<<" "<<m<<endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t=1 ;
cin >> t;
while(t--)
{
solve();
}
return 0;
}
| ### Prompt
Create a solution in CPP for the following problem:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long int
#define endl "\n"
#define pb push_back
#define mp make_pair
#define ut unsigned int
#define ss second
#define ff first
#define vi vector<int>
#define vl vector<ll>
#define lb lower_bound
#define up upper_bound
#define re return
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define sp(x) fixed << setprecision(x)
ll inline power(ll a, ll b, ll p){
a %= p;
ll ans = 1;
while(b>0){
if(b & 1)
ans = (ans*a)%p;
a = (a*a)%p;
b >>= 1;
}
return ans;
}
ll inv(ll n, ll p){
return power(n,p-2, p);
}
bool inline isprime(ll n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n%2 == 0 || n%3 == 0) return false;
for (ll i=5; i*i<=n; i=i+6)
if (n%i == 0 || n%(i+2) == 0)
return false;
return true;
}
/*ll dp[1000001];
//memset(dp,0,sizeof(dp));
void seive()
{
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
memset(dp,0,sizeof(dp));
ll n=1000000,i;
bool prime[n+1];
memset(prime, true, sizeof(prime));
for (ll p=2; p*p<=n; p++)
{
// If prime[p] is not changed, then it is a prime
if (prime[p] == true)
{
// Update all multiples of p greater than or
// equal to the square of it
// numbers which are multiple of p and are
// less than p^2 are already been marked.
for ( ll i=p*p; i<=n; i += p)
prime[i] = false;
}
}
dp[1]=1;
for (int p=2; p<=n; p++){
if (prime[p]==true)
dp[p]=1+dp[p-1];
else
dp[p]=dp[p-1];
}
}
bool issorted(int a[],int n){
int i=0,cnt=0;
for(i=1;i<n;i++){
if(a[i]>=a[i-1])
cnt++;
else
return false;
}
return true;
}
// ll dp[1000001],powe[1000001];
int binary-search(int a[],int n,int target){
int low,high,mid;
low=0;
high=n-1;
while(high-low>1){
mid=(low+high)/2;
if(a[mid]==target){
return mid;
}
if(a[mid]>target)
high=mid-1;
else
low=mid+1;
}
if(a[low]==target)
return low;
if(a[high]==target)
return high;
return 0;
}
*/
void solve(){
ll a,b,ans=0;
cin>>a>>b;
if(a==b){
cout<<"0"<<" "<<"0"<<endl;
re;
}
ans=abs(a-b);
if(ans==1){
cout<<"1"<<" "<<"0"<<endl;
re;
}
ll r=min(a,b),k=0,m;
m=min(r%ans,ans-r%ans);
cout<<ans<<" "<<m<<endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t=1 ;
cin >> t;
while(t--)
{
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
#define cs(n) scanf("%d", &n)
#define lcs(n) scanf("%lld", &n)
#define F(i, j, k) for(int i = j; i <= k; ++i)
using namespace std;
using ll = long long;
const int MAXN = 1000004;
const int INF = 0x3f3f3f3f;
int main() {
int T; cs(T);
while(T--) {
ll a; lcs(a);
ll b; lcs(b);
if(a == b) {
puts("0 0"); continue;
}
ll d = abs(a - b);
if(a > b) swap(a, b);
printf("%lld %lld\n", d, min(a % d, d - a % d));
}
return 0;
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include <bits/stdc++.h>
#define cs(n) scanf("%d", &n)
#define lcs(n) scanf("%lld", &n)
#define F(i, j, k) for(int i = j; i <= k; ++i)
using namespace std;
using ll = long long;
const int MAXN = 1000004;
const int INF = 0x3f3f3f3f;
int main() {
int T; cs(T);
while(T--) {
ll a; lcs(a);
ll b; lcs(b);
if(a == b) {
puts("0 0"); continue;
}
ll d = abs(a - b);
if(a > b) swap(a, b);
printf("%lld %lld\n", d, min(a % d, d - a % d));
}
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef pair<ll, ll> pii;
const int N = 2e5 + 10, MOD = 1e9 + 7;
int n, m, t, ans, tmp, k1, k2, ans1, ans2;
int a[N], b[N];
string s;
void read_input() {
cin >> n >> m;
tmp = max(n, m) - min(n, m);
cout << tmp << ' ';
if (tmp == 0) {
cout << 0 << '\n';
return;
}
k1 = n % tmp;
k2 = tmp - k1;
cout << min(k1, k2) << '\n';
}
int32_t main () {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while(t--) {
read_input();
}
return 0;
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef pair<ll, ll> pii;
const int N = 2e5 + 10, MOD = 1e9 + 7;
int n, m, t, ans, tmp, k1, k2, ans1, ans2;
int a[N], b[N];
string s;
void read_input() {
cin >> n >> m;
tmp = max(n, m) - min(n, m);
cout << tmp << ' ';
if (tmp == 0) {
cout << 0 << '\n';
return;
}
k1 = n % tmp;
k2 = tmp - k1;
cout << min(k1, k2) << '\n';
}
int32_t main () {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while(t--) {
read_input();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
ll a, b;
cin>>a>>b;
if(a == b){
cout<<0<<" "<<0<<"\n";
}
else {
ll k = max(a, b)-min(a, b);
cout<<k<<" "<<min(k-(b%k), b%k)<<"\n";
}
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
} | ### Prompt
Develop a solution in Cpp to the problem described below:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
ll a, b;
cin>>a>>b;
if(a == b){
cout<<0<<" "<<0<<"\n";
}
else {
ll k = max(a, b)-min(a, b);
cout<<k<<" "<<min(k-(b%k), b%k)<<"\n";
}
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
int t;
cin>>t;
while(t--)
{
int a,b;
cin>>a>>b;
if(a==b)
{
cout<<"0 0"<<endl;
}
else
{
if(a<b) swap(a,b);
int g=a-b;
cout<<g<<" ";
/*if(a%g==0)
cout<<0<<endl;
else*/
{
//cout<<(a/g+1)*g-a<<endl;
cout<<min(a-a/g*g,(a/g+1)*g-a)<<endl;
}
}
}
return 0;
} | ### Prompt
Your task is to create a cpp solution to the following problem:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
int t;
cin>>t;
while(t--)
{
int a,b;
cin>>a>>b;
if(a==b)
{
cout<<"0 0"<<endl;
}
else
{
if(a<b) swap(a,b);
int g=a-b;
cout<<g<<" ";
/*if(a%g==0)
cout<<0<<endl;
else*/
{
//cout<<(a/g+1)*g-a<<endl;
cout<<min(a-a/g*g,(a/g+1)*g-a)<<endl;
}
}
}
return 0;
}
``` |
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define mset(x,y) memset(x,y,sizeof(x))
using namespace std;
void DBG(){ cerr<<")\n";}
template<class H, class... T >
void DBG( H h, T... t){cerr << h;if( sizeof...(t))cerr<<", ";DBG(t...);}
#define dbg(...) cerr <<" values[ "<< #__VA_ARGS__ << " ] = ( ", DBG(__VA_ARGS__)
template <class T >
void dbgv(vector < T > &x){cerr<<"[ ";for( auto a: x) cerr<<a<<","[a==x.back()]<<" "; cerr<<"]\n"; }
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ul;
constexpr int MAXN = 1e6+2;
constexpr int inf = 2e9;
///aqui puede ir algo
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
/*freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);*/
int t;
cin>>t;
while(t--)
{
ul a,b;
cin>>a>>b;
if( a == b )
cout<<"0 0";
else
{
ul maximo = max(a,b) - min(a,b);
/// la diferencia es el max gcd
/// buscar que ambos sean multiplos de la diff
/// respuesta minima
if( a % maximo == 0 && b % maximo == 0 )
cout<<maximo<<" "<<0;
else
{
ul aux = min(a,b);
ul aux2 = max( a,b);
b = aux;
a = aux2;
// dbg( b % maximo, maximo);
if( b < maximo)
{
if( b < ( maximo - b))
cout<<maximo<<" "<<b;
else cout<<maximo<<" "<<maximo - b;
}
else
{
ul divi = ( b / maximo);
ul atras = (divi * maximo);
ul enfrente = ( (divi + 1) * maximo);
if( (enfrente - b ) < ( b - atras))
cout<<maximo<<" "<< (enfrente - b);
else cout<<maximo<<" "<< b - atras;
}
}
}
cout<<"\n";
}
cout<<"\n";
///uwu - vrm
}
/* [°-°] <- tss
[./../] <- este mensaje puede cambiar
by Benqi
COSAS QUE DEBERIAS BUSCAR
* desbordamientos de int, rango de los arreglos
* casos especiales ( n = 1? )
* haz algo en lugar de nada, mantente organizado
* ESCRIBE COSAS E IDEAS ABAJO
* NO TE CASES CON UNA IDEA O ENFOQUE
*/
| ### Prompt
Create a solution in CPP for the following problem:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define mset(x,y) memset(x,y,sizeof(x))
using namespace std;
void DBG(){ cerr<<")\n";}
template<class H, class... T >
void DBG( H h, T... t){cerr << h;if( sizeof...(t))cerr<<", ";DBG(t...);}
#define dbg(...) cerr <<" values[ "<< #__VA_ARGS__ << " ] = ( ", DBG(__VA_ARGS__)
template <class T >
void dbgv(vector < T > &x){cerr<<"[ ";for( auto a: x) cerr<<a<<","[a==x.back()]<<" "; cerr<<"]\n"; }
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ul;
constexpr int MAXN = 1e6+2;
constexpr int inf = 2e9;
///aqui puede ir algo
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
/*freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);*/
int t;
cin>>t;
while(t--)
{
ul a,b;
cin>>a>>b;
if( a == b )
cout<<"0 0";
else
{
ul maximo = max(a,b) - min(a,b);
/// la diferencia es el max gcd
/// buscar que ambos sean multiplos de la diff
/// respuesta minima
if( a % maximo == 0 && b % maximo == 0 )
cout<<maximo<<" "<<0;
else
{
ul aux = min(a,b);
ul aux2 = max( a,b);
b = aux;
a = aux2;
// dbg( b % maximo, maximo);
if( b < maximo)
{
if( b < ( maximo - b))
cout<<maximo<<" "<<b;
else cout<<maximo<<" "<<maximo - b;
}
else
{
ul divi = ( b / maximo);
ul atras = (divi * maximo);
ul enfrente = ( (divi + 1) * maximo);
if( (enfrente - b ) < ( b - atras))
cout<<maximo<<" "<< (enfrente - b);
else cout<<maximo<<" "<< b - atras;
}
}
}
cout<<"\n";
}
cout<<"\n";
///uwu - vrm
}
/* [°-°] <- tss
[./../] <- este mensaje puede cambiar
by Benqi
COSAS QUE DEBERIAS BUSCAR
* desbordamientos de int, rango de los arreglos
* casos especiales ( n = 1? )
* haz algo en lugar de nada, mantente organizado
* ESCRIBE COSAS E IDEAS ABAJO
* NO TE CASES CON UNA IDEA O ENFOQUE
*/
``` |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main () {
ll tt;
cin >> tt;
while (tt --) {
ll a, b, one, two;
cin >> a >> b;
if (a == b) {
cout << "0 0" << '\n';
continue;
}
one = abs (a - b);
two = min (a % one, one - a % one);
cout << one << ' ' << two << '\n';
}
}
| ### Prompt
Generate a Cpp solution to the following problem:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main () {
ll tt;
cin >> tt;
while (tt --) {
ll a, b, one, two;
cin >> a >> b;
if (a == b) {
cout << "0 0" << '\n';
continue;
}
one = abs (a - b);
two = min (a % one, one - a % one);
cout << one << ' ' << two << '\n';
}
}
``` |
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
void s()
{
ll a,b;
cin>>a>>b;
if(a==b)
{
cout<<"0"<<" "<<"0";
return;
}
ll dif = abs(a-b);
b=min(a,b);
if(b%dif==0)cout<<dif<<" "<<"0";
else
{
ll y = min(b%dif,(dif-b%dif));
cout<<dif<<" "<<y;
}
}
int main()
{
int t;
cin>>t;
while(t)
{
t-=1;
s();
cout<<endl;
}
return 0;
} | ### Prompt
Your task is to create a cpp solution to the following problem:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
void s()
{
ll a,b;
cin>>a>>b;
if(a==b)
{
cout<<"0"<<" "<<"0";
return;
}
ll dif = abs(a-b);
b=min(a,b);
if(b%dif==0)cout<<dif<<" "<<"0";
else
{
ll y = min(b%dif,(dif-b%dif));
cout<<dif<<" "<<y;
}
}
int main()
{
int t;
cin>>t;
while(t)
{
t-=1;
s();
cout<<endl;
}
return 0;
}
``` |
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
long long int t,x,y;
cin>>t;
for(int i=0;i<t;++i)
{
cin>>x>>y;
if(x==y)
cout<<0<<" "<<0<<endl;
else if(abs(x-y)==1)
cout<<1<<" "<<0<<endl;
else
{
if(x<y)
swap(x,y);
long long int dif=x-y;
long long int index1,index2;
index1=y%dif;
index2=dif-(y%dif);
if(index1<index2)
cout<<dif<<" "<<index1<<endl;
else
cout<<dif<<" "<<index2<<endl;
}
}
return 0;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
long long int t,x,y;
cin>>t;
for(int i=0;i<t;++i)
{
cin>>x>>y;
if(x==y)
cout<<0<<" "<<0<<endl;
else if(abs(x-y)==1)
cout<<1<<" "<<0<<endl;
else
{
if(x<y)
swap(x,y);
long long int dif=x-y;
long long int index1,index2;
index1=y%dif;
index2=dif-(y%dif);
if(index1<index2)
cout<<dif<<" "<<index1<<endl;
else
cout<<dif<<" "<<index2<<endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define en "\n";
#define all(x) (x).begin(), (x).end()
ll mod = 1000000007;
//ull n = 1e8;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin >> t;
//cout<<"as.size()"<<en;
while (t--)
{
ll n,m;
cin>>n>>m;
ll d = abs(n-m);
if (n==m)
{
cout<<"0"<<" 0"<<en;
continue;
}
ll k= n/d;
if (n%d!=0)
{
k++;
}
// if (n==1)
// { k--;
// ll p = abs(d*k-n);
// cout<<d<<" "<<"0"<<en;
// continue;
// }
ll p = abs(d*k-n);
if (p>d/2)
{
p = d-p;
}
//cout<<p<<"."<<en;
cout<<d<<" "<<p<<en;
}
} | ### Prompt
Develop a solution in CPP to the problem described below:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define en "\n";
#define all(x) (x).begin(), (x).end()
ll mod = 1000000007;
//ull n = 1e8;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin >> t;
//cout<<"as.size()"<<en;
while (t--)
{
ll n,m;
cin>>n>>m;
ll d = abs(n-m);
if (n==m)
{
cout<<"0"<<" 0"<<en;
continue;
}
ll k= n/d;
if (n%d!=0)
{
k++;
}
// if (n==1)
// { k--;
// ll p = abs(d*k-n);
// cout<<d<<" "<<"0"<<en;
// continue;
// }
ll p = abs(d*k-n);
if (p>d/2)
{
p = d-p;
}
//cout<<p<<"."<<en;
cout<<d<<" "<<p<<en;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int tc;
cin >> tc;
while (tc--) {
lint a, b;
cin >> a >> b;
if (a < b) swap(a, b);
if (a == b) cout << 0 << " " << 0 << '\n';
else {
cout << (a - b) << " " << min(a % (a - b), (a - b) - (a % (a - b))) << '\n';
}
}
return 0;
} | ### Prompt
Develop a solution in CPP to the problem described below:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int tc;
cin >> tc;
while (tc--) {
lint a, b;
cin >> a >> b;
if (a < b) swap(a, b);
if (a == b) cout << 0 << " " << 0 << '\n';
else {
cout << (a - b) << " " << min(a % (a - b), (a - b) - (a % (a - b))) << '\n';
}
}
return 0;
}
``` |
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
long long a,b,c,d,ans;
scanf("%lld%lld",&a,&b);
if(a==b) printf("0 0\n");
else
{
c=abs(a-b);
d=a%c;
if(d<=c-d) ans=d;
else ans=c-d;
printf("%lld %lld\n",c,ans);
}
}
return 0;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
long long a,b,c,d,ans;
scanf("%lld%lld",&a,&b);
if(a==b) printf("0 0\n");
else
{
c=abs(a-b);
d=a%c;
if(d<=c-d) ans=d;
else ans=c-d;
printf("%lld %lld\n",c,ans);
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 * 100 + 10;
int main(){
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int t;
cin >> t;
while (t--){
long long a, b;
cin >> a >> b;
long long r = abs(a - b);
if (!r){
cout << 0 << ' ' << 0 << '\n';
continue;
}
cout << r << ' ' << min((a % r), (r - (a % r))) << '\n';
}
return 0;
} | ### Prompt
Generate a Cpp solution to the following problem:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 * 100 + 10;
int main(){
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int t;
cin >> t;
while (t--){
long long a, b;
cin >> a >> b;
long long r = abs(a - b);
if (!r){
cout << 0 << ' ' << 0 << '\n';
continue;
}
cout << r << ' ' << min((a % r), (r - (a % r))) << '\n';
}
return 0;
}
``` |
#pragma GCC target ("avx2,fma")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int IOS = []() {ios::sync_with_stdio(0); std::cin.tie(nullptr); std::cout.tie(nullptr); return 0; }();
#define pb push_back #define po pop_back
#define YES cout<<"YES\n" #define Yes cout<<"Yes\n" #define NO cout<<"NO\n" #define No cout<<"No\n"
#define for00 for(int i=1;i<=n;i++) cin>>a[i];
#define for0 for(int i=0;i<n;i++)
#define for1 for(int i=1;i<=n;i++)
#define for2(i,a,b) for(int i=a; i<=b;i++)
#define for3(i,a,b) for(int i=a; i>=b;i--)
#define cn change_number_tostring #define cs change_string_tonumber #define pri init_prime_distance
#define all(a) begin(a), end(a)
#define SUM(a) accumulate(all(a), 0LL)
#define MIN(a) (*min_element(all(a)))
#define MAX(a) (*max_element(all(a)))
#define lb(a, x) distance(begin(a), lower_bound(all(a), (x)))
#define ub(a, x) distance(begin(a), upper_bound(all(a), (x)))
#define gcd __gcd
template<class T> void _W(const T &x) { cout << x; }
template<class T> void _W(T &x) { cout << x; }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.first); putchar(' '); _W(x.second);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) cout<<' '; }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); cout<<", "; W(tail...); }
#ifdef CF_DEBUG
#define debug(...) { cout << "debug : [ "; cout << #__VA_ARGS__; cout << " ] = [ "; W(__VA_ARGS__); cout<<"\b\b ]\n"; }
#else
#define debug(...) (0)
#endif
const int mod = 1e9+7;
const int sp = 1e9;
const int ma = 1e5+10;
//int f[ma];
//inline int find(int k) { return f[k]==k? k : f[k]=find(f[k]);} // f[find(j)] = find(k);
inline int digit(int i) { stringstream ss; ss<<i; return ss.str().size(); }
inline string change_number_tostring(int i) { stringstream ss; ss<<i; return ss.str(); }
inline int change_string_tonumber(string s) { int num; stringstream ss(s); ss>>num; return num; }
inline int quick(int a, int b) { a%=mod; int res = 1; while(b) { if(b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; }
inline int C(int n, int m) { int resdw1 = 1; for2(i, 1, n) resdw1 = resdw1 * i % mod; int resdw2 = 1; for2(i, 1, m - n) resdw2 = resdw2 * i % mod; int resup = 1; for2(i, 1, m) resup = resup * i % mod; return resup * quick(resdw1, mod - 2) % mod * quick(resdw2, mod - 2) % mod; }
inline void read(int &x) { int f = 1; x = 0; char op = getchar(); while(op < '0' || op > '9') { if(op == '-') f = -1; op = getchar(); } while(op >= '0' && op <= '9') { x = x * 10 + op - '0'; op = getchar(); } x *= f; }
inline void print(int x) { if(x < 0) putchar('-'), x = -x; if(x > 9) print(x / 10); putchar(x % 10 + '0'); }
const int man=1e7+5;
bool vis[man]={0};
int prime[man];
inline void init_prime_distance() {int cnt= 0; for (int i=2;i<=man;i++) { if (!vis[i]) prime[cnt++]=i; for(int j=0;j<cnt&&i*prime[j]<=man;j++) { vis[i*prime[j]] = 1; if (i%prime[j]== 0) break; } } }
// double time=clock(); printf("%lf\n",clock()-time);
const int N = 2e5+10;
void solve()
{
int a,b; cin>>a>>b;
if(a==b) cout<<0<<" "<<0<<endl;
else{
if(a-b==1||a-b==-1) cout<<1<<" "<<0<<endl;
else
{
int q,w;
if(max(a,b)%abs(a-b) <= abs(a-b)/2) q=max(a,b)%abs(a-b);
else q = abs(a-b)-max(a,b)%abs(a-b);
cout<<abs(a-b)<<" "<<q<<endl;
}
}
}
signed main()
{
int tt=1;
cin>>tt;
while(tt--)
{
solve();
}
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
Welcome to Rockport City!
It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations:
1. Increase both a and b by 1.
2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.
Note that gcd(x,0)=x for any x ≥ 0.
Input
The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases.
The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}).
Output
For each test case, print a single line containing two integers.
If the fans can get infinite excitement, print 0 0.
Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
Example
Input
4
8 5
1 2
4 4
3 9
Output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.
For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation.
For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.
For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
### Response
```cpp
#pragma GCC target ("avx2,fma")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int IOS = []() {ios::sync_with_stdio(0); std::cin.tie(nullptr); std::cout.tie(nullptr); return 0; }();
#define pb push_back #define po pop_back
#define YES cout<<"YES\n" #define Yes cout<<"Yes\n" #define NO cout<<"NO\n" #define No cout<<"No\n"
#define for00 for(int i=1;i<=n;i++) cin>>a[i];
#define for0 for(int i=0;i<n;i++)
#define for1 for(int i=1;i<=n;i++)
#define for2(i,a,b) for(int i=a; i<=b;i++)
#define for3(i,a,b) for(int i=a; i>=b;i--)
#define cn change_number_tostring #define cs change_string_tonumber #define pri init_prime_distance
#define all(a) begin(a), end(a)
#define SUM(a) accumulate(all(a), 0LL)
#define MIN(a) (*min_element(all(a)))
#define MAX(a) (*max_element(all(a)))
#define lb(a, x) distance(begin(a), lower_bound(all(a), (x)))
#define ub(a, x) distance(begin(a), upper_bound(all(a), (x)))
#define gcd __gcd
template<class T> void _W(const T &x) { cout << x; }
template<class T> void _W(T &x) { cout << x; }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.first); putchar(' '); _W(x.second);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) cout<<' '; }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); cout<<", "; W(tail...); }
#ifdef CF_DEBUG
#define debug(...) { cout << "debug : [ "; cout << #__VA_ARGS__; cout << " ] = [ "; W(__VA_ARGS__); cout<<"\b\b ]\n"; }
#else
#define debug(...) (0)
#endif
const int mod = 1e9+7;
const int sp = 1e9;
const int ma = 1e5+10;
//int f[ma];
//inline int find(int k) { return f[k]==k? k : f[k]=find(f[k]);} // f[find(j)] = find(k);
inline int digit(int i) { stringstream ss; ss<<i; return ss.str().size(); }
inline string change_number_tostring(int i) { stringstream ss; ss<<i; return ss.str(); }
inline int change_string_tonumber(string s) { int num; stringstream ss(s); ss>>num; return num; }
inline int quick(int a, int b) { a%=mod; int res = 1; while(b) { if(b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; }
inline int C(int n, int m) { int resdw1 = 1; for2(i, 1, n) resdw1 = resdw1 * i % mod; int resdw2 = 1; for2(i, 1, m - n) resdw2 = resdw2 * i % mod; int resup = 1; for2(i, 1, m) resup = resup * i % mod; return resup * quick(resdw1, mod - 2) % mod * quick(resdw2, mod - 2) % mod; }
inline void read(int &x) { int f = 1; x = 0; char op = getchar(); while(op < '0' || op > '9') { if(op == '-') f = -1; op = getchar(); } while(op >= '0' && op <= '9') { x = x * 10 + op - '0'; op = getchar(); } x *= f; }
inline void print(int x) { if(x < 0) putchar('-'), x = -x; if(x > 9) print(x / 10); putchar(x % 10 + '0'); }
const int man=1e7+5;
bool vis[man]={0};
int prime[man];
inline void init_prime_distance() {int cnt= 0; for (int i=2;i<=man;i++) { if (!vis[i]) prime[cnt++]=i; for(int j=0;j<cnt&&i*prime[j]<=man;j++) { vis[i*prime[j]] = 1; if (i%prime[j]== 0) break; } } }
// double time=clock(); printf("%lf\n",clock()-time);
const int N = 2e5+10;
void solve()
{
int a,b; cin>>a>>b;
if(a==b) cout<<0<<" "<<0<<endl;
else{
if(a-b==1||a-b==-1) cout<<1<<" "<<0<<endl;
else
{
int q,w;
if(max(a,b)%abs(a-b) <= abs(a-b)/2) q=max(a,b)%abs(a-b);
else q = abs(a-b)-max(a,b)%abs(a-b);
cout<<abs(a-b)<<" "<<q<<endl;
}
}
}
signed main()
{
int tt=1;
cin>>tt;
while(tt--)
{
solve();
}
}
``` |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.