Codeforces707D. Persistent Bookcase

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 1to 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

 

思路:每一行建线段树,用于区间更新,离线处理查询次序。离线+dfs+线段树。

import java.util.*;
import java.io.*;

public class Main {
	public static void main(String args[]) {new Main().run();}

	FastReader in = new FastReader();
	PrintWriter out = new PrintWriter(System.out);
	void run(){
		work();
		out.flush();
	}
	long mod=1000000007;
	long gcd(long a,long b) {
		return a==0?b:b>=a?gcd(b%a,a):gcd(b,a);
	}
	ArrayList<Integer>[] graph;
	int[][] A;
	Node[] roots;
	int[][] Q;
	int[] ret;
	int sum;
	int n,m,q;
	void work() {
		n=in.nextInt();
		m=in.nextInt();
		q=in.nextInt();
		roots=new Node[n];
		graph=(ArrayList<Integer>[])new ArrayList[q+1];
		for(int i=0;i<=q;i++) {
			graph[i]=new ArrayList<>();
		}
		Q=new int[q+1][];
		Q[0]=new int[] {0};
		ret=new int[q+1];
		A=new int[n][m];
		int pre=0;
		for(int i=1;i<=q;i++) {
			int t=in.nextInt();
			if(t==1||t==2) {
				graph[pre].add(i);
				Q[i]=new int[] {t,in.nextInt()-1,in.nextInt()-1};
				pre++;
			}else if(t==3) {
				graph[pre].add(i);
				Q[i]=new int[] {t,in.nextInt()-1};
				pre++;
			}else {
				pre=in.nextInt();
				graph[pre].add(i);
				Q[i]=new int[] {t,pre};
				pre=i;
			}
		}
		for(int i=0;i<n;i++) {
			roots[i]=new Node();
		}
		dfs(0);
		for(int i=1;i<=q;i++) {
			out.println(ret[i]);
		}
	}
	private void dfs(int node) {
		int t=Q[node][0];
		boolean f=false;
		if(t==1) {
			int i=Q[node][1];
			int j=Q[node][2];
			if(query(roots[i],0,m-1,j,j)==0) {
				f=true;
				update(roots[i],0,m-1,j,1);
				sum++;
			}
		}else if(t==2) {
			int i=Q[node][1];
			int j=Q[node][2];
			if(query(roots[i],0,m-1,j,j)==1) {
				f=true;
				update(roots[i],0,m-1,j,-1);
				sum--;
			}
		}else if(t==3) {
			int i=Q[node][1];
			int pre=query(roots[i],0,m-1,0,m-1);
			update2(roots[i]);
			int cur=query(roots[i],0,m-1,0,m-1);
			sum+=cur-pre;
		}
		for(int nn:graph[node]) {
			dfs(nn);
		}
		ret[node]=sum;
		if(t==1) {
			int i=Q[node][1];
			int j=Q[node][2];
			if(f) {
				update(roots[i],0,m-1,j,-1);
				sum--;
			}
		}else if(t==2) {
			int i=Q[node][1];
			int j=Q[node][2];
			if(f) {
				update(roots[i],0,m-1,j,1);
				sum++;
			}
		}else if(t==3) {
			int i=Q[node][1];
			int pre=query(roots[i],0,m-1,0,m-1);
			update2(roots[i]);
			int cur=query(roots[i],0,m-1,0,m-1);
			sum+=cur-pre;
		}
	}
	private void update(Node node, int l, int r, int idx,int v) {
		updatelazy(node,l,r);
		if(l==r) {
			node.sum+=v;
			return;
		}
		int m=l+(r-l)/2;
		if(idx<=m) {
			update(getLnode(node),l,m,idx,v);
		}else {
			update(getRnode(node),m+1,r,idx,v);
		}
		updatelazy(getLnode(node),l,m);
		updatelazy(getRnode(node),m+1,r);
		node.sum=getLnode(node).sum+getRnode(node).sum;//左右节点可能有lazy标记
	}
	
	private void update2(Node node) {
		node.lazy^=1;
	}
	void updatelazy(Node node,int l,int r) {
		if(node.lazy==1) {
			node.sum=(r-l+1)-node.sum;
			node.lazy=0;
			if(l!=r) {
				getLnode(node).lazy^=1;
				getRnode(node).lazy^=1;
			}
		}
	}
	private int query(Node node, int l, int r, int s, int e) {
		updatelazy(node,l,r);
		if(s<=l&&r<=e) {
			return node.sum;
		}
		int m=l+(r-l)/2;
		int ret=0;
		if(m>=s) {
			ret+=query(getLnode(node),l,m,s,e);
		}
		if(m+1<=e) {
			ret+=query(getRnode(node),m+1,r,s,e);
		}
		return ret;
	}
	private Node getLnode(Node node) {
		if(node.lnode==null)node.lnode=new Node();
		return node.lnode;
	}
	private Node getRnode(Node node) {
		if(node.rnode==null)node.rnode=new Node();
		return node.rnode;
	}
	
	class Node{
		Node lnode,rnode;
		int sum,lazy;
	}
}	



class FastReader
{
	BufferedReader br;
	StringTokenizer st;

	public FastReader()
	{
		br=new BufferedReader(new InputStreamReader(System.in));
	}


	public String next() 
	{
		while(st==null || !st.hasMoreElements())//回车,空行情况
		{
			try {
				st = new StringTokenizer(br.readLine());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return st.nextToken();
	}

	public int nextInt() 
	{
		return Integer.parseInt(next());
	}

	public long nextLong()
	{
		return Long.parseLong(next());
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值