I Like Matrix Forever! Tree [search]

Description–

Zero matrix A a n * m to q-operations:
•. 1 ij of: the Ai, j negated;
• 2 i: all the elements of the matrix A i-th row all negated;
•. 3 J: The matrix A of All elements of all columns j negated;
• k. 4: reduction of matrix a k th state after the operation.
After each operation, asked the current matrix of all elements.


Input–

The first line contains three integers n, m and q.
After q lines each comprising two or three integers, it represents one operation all parameters.

Output–

Co q lines contains an integer ans, it represents the current matrix and all elements.


Sample Input–

2 2 4
2 1
1 1 2
4 1
3 2

Sample Output–

2
1
2
2


Notes -

For 30% of the data: n, m ≤ 300, q ≤ 1000
for 60% of data: n, m ≤ 1000, q ≤ 5000
100% data: n, m ≤ 1000, q ≤ 100000


Problem-solving ideas -

If normal operation is connected to 1 to 3, then the operation of even 4 to k.


Code -

#include<iostream>
#include<cstdio>
using namespace std;
int n,m,o,t,s,a[100005],b[100005],tt[100005],ls[100005],ans[100005];
bool x[1005][1005];
struct hh
{
	int x,next;
}f[100005];
int read()
{
	int k=0;
	char c=getchar();
	while (c<'0' || c>'9') c=getchar();
	while (c>='0' && c<='9')
	  k=k*10+(c-'0'),c=getchar();
	return k; 
}
void op(int e)
{
	if (e>9) op(e/10);
	putchar(e%10+'0');
}
void fy(int ff,int y)
{
	f[++t].x=y,f[t].next=ls[ff],ls[ff]=t;
}
void hhd(int l)
{
	if (tt[l]==1)
	{
		x[a[l]][b[l]]=!x[a[l]][b[l]];
		if (x[a[l]][b[l]]) s++;
		else s--;
	}
	if (tt[l]==2)
	  for (int j=1;j<=m;++j)
      {
	      x[a[l]][j]=!x[a[l]][j];
		  if (x[a[l]][j]) s++;
		  else s--;
	  }
	if (tt[l]==3)
	  for (int j=1;j<=n;++j)
	  {
	  	  x[j][a[l]]=!x[j][a[l]];
	  	  if (x[j][a[l]]) s++;
	  	  else s--;
	  }
	ans[l]=s;
	for (int i=ls[l];i;i=f[i].next)
	  hhd(f[i].x);
	if (tt[l]==1)
	{
		x[a[l]][b[l]]=!x[a[l]][b[l]];
		if (x[a[l]][b[l]]) s++;
		else s--;
	}
	if (tt[l]==2)
	  for (int j=1;j<=m;++j)
      {
	      x[a[l]][j]=!x[a[l]][j];
		  if (x[a[l]][j]) s++;
		  else s--;
	  }
	if (tt[l]==3)
	  for (int j=1;j<=n;++j)
	  {
	  	  x[j][a[l]]=!x[j][a[l]];
	  	  if (x[j][a[l]]) s++;
	  	  else s--;
	  }//回溯
}
int main()
{
    n=read(),m=read(),o=read();
    for (int i=1;i<=o;++i)
    {
    	tt[i]=read();
    	if (tt[i]==1) a[i]=read(),b[i]=read();
    	else a[i]=read();
    	if (tt[i]==4) f[++t].x=i,f[t].next=ls[a[i]],ls[a[i]]=t;//连到a[i]
    	else f[++t].x=i,f[t].next=ls[i-1],ls[i-1]=t;//正常连
    }
    hhd(0);
    for (int i=1;i<=o;++i)
      op(ans[i]),putchar(10);
    
    return 0; 
} 

Guess you like

Origin blog.csdn.net/qq_43654542/article/details/90701225