Codeforces Round #368 (Div. 2) D. Persistent Bookcase(离线)

185 篇文章 0 订阅
10 篇文章 0 订阅
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:

  • 1 i j — Place a book at position j at shelf i if there is no book at it.
  • 2 i j — Remove the book from position j at shelf i if there is a book at it.
  • 3 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.
  • 4 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 n, m 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
2 3 3
1 1 1
3 2
4 0
Output
1
4
0
Input
4 2 6
3 2
2 2 2
3 3
3 2
2 2 2
3 2
Output
2
1
3
3
2
4
Input
2 2 2
3 2
2 2 1
Output
2
1
Note

This image illustrates the second sample case.


题意:给你一个n*m的书架,q个操作,每次可以在i,j位置放一本书,取走i,j位置一本书,反转第i个书架,回溯到第k个操作,问你每次操作后书的总数。



分析:所有的操作可以连成一棵树,然后我们可以每次DFS,到叶子节点后开始回溯,O(q)。


#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MAX 30*200002  
using namespace std;
struct Ask
{
	int p,i,j,k;
}ask[100005];
vector <int> G[100005];
int ans,n,m,q,re[1005],pos[1005][1005],Ans[1005],ANS[100005];
void dfs(int u,int fa)
{
	bool did = false;
	if(u != 0)
	{
		if(ask[u].p == 1) 
		{
			if(pos[ask[u].i][ask[u].j]^re[ask[u].i] == 0) 
			{
				did = true;
				Ans[ask[u].i]++;
				ans++;
			}
			pos[ask[u].i][ask[u].j] = re[ask[u].i]^1;
		}
		if(ask[u].p == 2)
		{
			if(pos[ask[u].i][ask[u].j]^re[ask[u].i]) 
			{
				did = true;
				Ans[ask[u].i]--;
				ans--;
			}
			pos[ask[u].i][ask[u].j] = re[ask[u].i];
		}
		if(ask[u].p == 3)
		{
			ans -= Ans[ask[u].k];
			Ans[ask[u].k] = m - Ans[ask[u].k];
			ans += Ans[ask[u].k];
			re[ask[u].k] ^= 1;
		}
		ANS[u] = ans;
	}	
	for(int v : G[u])
	 if(v != fa) dfs(v,u);
	if(u != 0)
	{
		if(ask[u].p == 2 && did) 
		{
			Ans[ask[u].i]++;
			ans++;
			pos[ask[u].i][ask[u].j] = re[ask[u].i]^1;
		}
		if(ask[u].p == 1 && did)
		{
			Ans[ask[u].i]--;
			ans--;
			pos[ask[u].i][ask[u].j] = re[ask[u].i];
		}
		if(ask[u].p == 3)
		{
			ans -= Ans[ask[u].k];
			Ans[ask[u].k] = m - Ans[ask[u].k];
			ans += Ans[ask[u].k];
			re[ask[u].k] ^= 1;
		}
	}	 
} 
int main()
{
	scanf("%d%d%d",&n,&m,&q);
	int now = 0;
	for(int i = 1;i <= q;i++)
	{
		scanf("%d",&ask[i].p);
		if(ask[i].p == 1 || ask[i].p == 2)
		{
			G[now].push_back(i);
			scanf("%d%d",&ask[i].i,&ask[i].j);
		}
		else 
		{
			scanf("%d",&ask[i].k);
			if(ask[i].p == 4)
			{
				G[ask[i].k].push_back(i);
			}
			else G[now].push_back(i);
		}
		now = i;
	}
	dfs(0,-1);
	for(int i = 1;i <= q;i++) printf("%d\n",ANS[i]);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值