CodeForces707D.Persistent Bookcase(DFS)

                                                   D. Persistent Bookcase

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Recently in school Alina has learned what are the persistent data structures: they are data structures that always preserves the previous version of itself and access to it when it is modified.

After reaching home Alina decided to invent her own persistent data structure. Inventing didn't take long: there is a bookcase right behind her bed. Alina thinks that the bookcase is a good choice for a persistent data structure. Initially the bookcase is empty, thus there is no book at any position at any shelf.

The bookcase consists of n shelves, and each shelf has exactly m positions for books at it. Alina enumerates shelves by integers from 1 to n and positions at shelves — from 1 to m. Initially the bookcase is empty, thus there is no book at any position at any shelf in it.

Alina wrote down q operations, which will be consecutively applied to the bookcase. Each of the operations has one of four types:

  • i j — Place a book at position j at shelf i if there is no book at it.
  • i j — Remove the book from position j at shelf i if there is a book at it.
  • i — Invert book placing at shelf i. This means that from every position at shelf i which has a book at it, the book should be removed, and at every position at shelf i which has not book at it, a book should be placed.
  • k — Return the books in the bookcase in a state they were after applying k-th operation. In particular, k = 0 means that the bookcase should be in initial state, thus every book in the bookcase should be removed from its position.

After applying each of operation Alina is interested in the number of books in the bookcase. Alina got 'A' in the school and had no problem finding this values. Will you do so?

Input

The first line of the input contains three integers nm and q (1 ≤ n, m ≤ 103, 1 ≤ q ≤ 105) — the bookcase dimensions and the number of operations respectively.

The next q lines describes operations in chronological order — i-th of them describes i-th operation in one of the four formats described in the statement.

It is guaranteed that shelf indices and position indices are correct, and in each of fourth-type operation the number k corresponds to some operation before it or equals to 0.

Output

For each operation, print the number of books in the bookcase after applying it in a separate line. The answers should be printed in chronological order.

Examples

input

Copy

2 3 3
1 1 1
3 2
4 0

output

Copy

1
4
0

input

Copy

4 2 6
3 2
2 2 2
3 3
3 2
2 2 2
3 2

output

Copy

2
1
3
3
2
4

input

Copy

2 2 2
3 2
2 2 1

output

Copy

2
1

Note

This image illustrates the second sample case.

 

一、原题地址

点我传送

 

二、大致题意

现在有一个N*M的书架,有Q个询问。存在四种操作

如果 1,那么输入x,y,如果第x行第y列无书,则放一本书。

如果2,那么输入x,y,如果第x行第y列有书,则取走那本书。

如果3,那么输入x,将第x行有书的取走,无书的位置放一本。

如果4,那么输入k,表示把书架的情况恢复到第K个版本。

 

三、大致思路

预处理图,

对于操作1、2、3来说,实际上操作 i 继承的情况就是上一次的操作 i-1 ,所以建立的边是(i-1 , i)。

对于操作4来说,还原到版本 k 实际上就是,从版本 k 直接到达当前版本的意思,所以建立 ( k , i )的边。

以上的图跑DFS,这样就只需要一个[2000][2000]大小的矩阵就可以了。

 

四、代码

#include<iostream>
#include<cstring>
#include<vector>
#include<deque>
#include<algorithm>
#include<set>
#include<map>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;


int n,m,Q;
const int N=1e5+5;
int op[N];
int posx[N],posy[N];
vector<int>e[N];
int book[1005][1005];
int ans[N];


void DFS(int nx,int num)
{
    ans[nx]=num;
    for(int i=0;i<e[nx].size();i++)
    {
        int v=e[nx][i];
        if(op[v]==1)
        {
            if(book[posx[v] ][posy[v] ])
            {
                DFS(v,num);
            }
            else
            {
                book[posx[v] ][posy[v] ]=1;
                DFS(v,num+1);
                book[posx[v] ][posy[v] ]=0;
            }
        }
        else if(op[v]==2)
        {
            if(book[posx[v] ][posy[v] ])
            {
                book[posx[v] ][posy[v] ]=0;
                DFS(v,num-1);
                book[posx[v] ][posy[v] ]=1;
            }
            else
            {
                DFS(v,num);
            }
        }
        else if(op[v]==3)
        {
            int add=0;
            for(int i=1;i<=m;i++)
            {
                if(book[posx[v] ][i ]==0)
                {
                    book[posx[v] ][i ]=1;add++;
                }
                else book[posx[v] ][i ]=0,add--;
            }
            DFS(v,num+add);
            for(int i=1;i<=m;i++)
            {
                if(book[posx[v] ][i ]==0)
                {
                    book[posx[v] ][i ]=1;
                }
                else book[posx[v] ][i ]=0;
            }
        }
        else if(op[v]==4)
        {
            DFS(v,num);
        }

    }



}

int main()
{
    scanf("%d %d %d",&n,&m,&Q);
    for(int i=1;i<=Q;i++)
    {
        scanf("%d",&op[i]);
        if(op[i]==1||op[i]==2)
        {
            scanf("%d %d",&posx[i],&posy[i]);
        }
        else
        {
            scanf("%d",&posx[i]);
        }
        if(op[i]<=3)e[i-1].push_back(i);
        else e[posx[i] ].push_back(i);
    }
    DFS(0,0);
    for(int i=1;i<=Q;i++)
    {
        printf("%d\n",ans[i]);
    }


}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值