I have got some impossible queries for you! (or are they? ;) )
You have n
binary numbers of length m
. The i
th binary number is Bi. Also, you have to perform q
queries on them. The indexing is zero-based and the indexing of bits starts from left.
The queries are of type : a
, i
, j
.
If a
is:
0
: perform Logical AND operation between Bi and Bj and output the number of1
s in the result.1
: perform Logical OR operation between Bi and Bj and output the number of1
s in the result.2
: perform Logical XOR operation between Bi and Bj and output the number of1
s in the result.3
: flip the value of thej
th bit of Bi (i.e. set the bit to0
if it equals1
and vice-versa).
Note: For queries of type 0
, 1
, and 2
, the binary numbers remain unchanged.
It is also recommended to use Fast I/O for C++ and JAVA programmers.
Input Format:
First line contains Integers n
and m
.
The next n
lines contain binary numbers of length m
.
The i
th line contains binary number Bi.
The next line contains an integer q
The next q
lines contain queries of type : a
, i
, j
.
Output Format:
Output number of 1
s in the result of type 0
, 1
and 2
queries.
Constraints:
1<=n
, m<=2500
1<=q<=10^6
I have tried changing the array size, but still the error remains the same!
#include <iostream>
#include <math.h>
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m;
cin>>n>>m;
char arr[3000][3000];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
cin>>arr[i][j];
}
long int q;
cin>>q;
char query[3000][3000];
for(long int k=0;k<q;k++)
for(long int l=0;l<3;l++)
{
cin>>query[k][l];
}
for(long int i=0;i<q;i++)
{
if(int(query[i][0]-48)==3)
{
if(arr[int(query[i][1])-48][int(query[i][2])-48]=='1')
{
arr[int(query[i][1])-48][int(query[i][2])-48]='0';
}
else
{
arr[int(query[i][1])-48][int(query[i][2])-48]='1';
}
}
else if(int(query[i][0]-48)==2)
{
int cntr=0;
int bi=int(query[i][1])-48;
int bj=int(query[i][2])-48;
for(int i=0;i<m;i++)
{
int xorres=arr[bi][i]^arr[bj][i];
if(xorres==1)
cntr++;
}
cout<<cntr<<endl;
}
else if(int(query[i][0]-48)==1)
{
int cntr=0;
int bi=int(query[i][1])-48;
int bj=int(query[i][2])-48;
for(int i=0;i<m;i++)
{
int andres=arr[bi][i]|arr[bj][i];
if(andres-48==1)
cntr++;
}
cout<<cntr<<endl;
}
else if(int(query[i][0]-48)==0)
{
int cntr=0;
int bi=int(query[i][1])-48;
int bj=int(query[i][2])-48;
for(int i=0;i<m;i++)
{
int andres=arr[bi][i]&arr[bj][i];
if(andres-48==1)
cntr++;
}
cout<<cntr<<endl;
}
}
return 0;
}