Problem Statement`` ================= : Given a list which initially contains 0, the following queries can be performed:
0 X : Add X to the List
1 X : Replace each element in List With its XOR with X (i.e. Replace A with A^X where is ^ is an XOR operation)
Return a list in increasing order after all queries.
-------- Sample :
-------- Q=5
0 4
0 2
1 4
0 5
1 8
Answer : 8 12 13 14
Let me know the concept in this problem :
Thanks.
Update : Got The Solution : Thanks
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define MOD 1000000007
signed main()
{
vector<int>v;
int effect=0;
int q;
cin >> q;
v.push_back(0);
while(q--)
{
int type,x;
cin >> type >> x;
if(type==0)
{
v.push_back(x);
v.back()^=effect;
}
else
{
effect^=x;
}
}
for(int i=0;i<v.size();i++)
{
v[i]^=effect;
}
sort(v.begin(),v.end());
for(int i=0;i<v.size();i++)
cout<<v[i]<<' ';
}
Thanks CodeForces Community ... This Was my First Attempt to CF Blogs..
Think about prefix sums
How to solve this with prefix sum?
Can you explain a little more...
It's not necessarily using prefix sums, but I meant that this concept is helpful in figuring out your solution :P
Process all the queries of type 0 and store the type 1 query in a vector of arrays (say End). Now after getting all the elements, You have some ranges(starting from 1st element). You just have to take Xor all the elements of a particular range with the calculated value.
Lets say we have a variable Xor which is the current value with which we have to take the xor with the i-th element of the given array.
Initially Xor = xor(all X of type 1 query).
Now as you procees forward and leave a range You again take xor of the value proviede for the given range(which ended right before) which will compensate the original Xor taken of all the values provided...
Not clear...?
Have a look at the code and read above strategy again. Hope it is clear now.
Thanks CodeForces For This Great Community
lmao, this solution is correct. why the downvotes
Hey just ignore, basically situation is like there are lot of beginner on codeforces almost 99 percentage you could say, so most probably people will upvote you if you already have some upvotes already, or downvote you if downvoted already.
Else just reach purple+ color and type anything you want, in 95% cases you will get upvotes.