1. 程式人生 > >Codeforces-707D:Persistent Bookcase (離線處理特殊的可持久化問題&&Bitset)

Codeforces-707D:Persistent Bookcase (離線處理特殊的可持久化問題&&Bitset)

cif books nsis LG AR num eight which bit

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.

Example

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個操作,對於每個操作,輸入opt:

如果opt==1,那麽輸入x,y,如果第x行第y列無書,則放一本書。

如果opt==2,那麽輸入x,y,如果第x行第y列有書,則取走那本書。

如果opt==3,那麽輸入x,將第x行有書的取走,無書的位置放一本。

如果opt==4,那麽輸入k,表示把書架的情況恢復為第k次操作後的樣貌,k在當前操作之前。

思路:初看可能是可持久化數據結構,但是註意到整體操作順序為有根樹,可以DFS回溯,對於書架上的情況,可以直接積累或者Bitset假設。

#include<bitset>
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1010;
const int maxm=100010;
bitset<maxn>s[maxn],P;
int N,M,Q;
int Laxt[maxm],Next[maxm],To[maxm],cnt;
int opt[maxm],x[maxm],y[maxm],ans[maxm];
void read(int &res)
{
    char c=getchar(); res=0;
    for(;c>9||c<0;c=getchar());
    for(;c<=9&&c>=0;c=getchar()) res=(res<<3)+(res<<1)+c-0;
}
void add(int u,int v)
{
    Next[++cnt]=Laxt[u];
    Laxt[u]=cnt;
    To[cnt]=v;
}
void dfs(int u,int Now)
{
    for(int i=Laxt[u];i;i=Next[i]){
        int v=To[i];
        if(opt[v]==1&&s[x[v]][y[v]]==0) {
                s[x[v]][y[v]]=1;
                ans[v]=Now+1;
                dfs(v,ans[v]);
                s[x[v]][y[v]]=0;
        }
        else if(opt[v]==2&&s[x[v]][y[v]]==1) {
                s[x[v]][y[v]]=0;
                ans[v]=Now-1;
                dfs(v,ans[v]);
                s[x[v]][y[v]]=1;
        }
        else if(opt[v]==3){
                ans[v]=Now-s[x[v]].count();  
                s[x[v]]^=P;  
                ans[v]+=s[x[v]].count(); 
                dfs(v,ans[v]);
                s[x[v]]^=P;
        }
        else {
                ans[v]=Now;
                dfs(v,ans[v]);
        }
     }
}
int main()
{
    read(N); read(M); read(Q);
    for(int i=1;i<=M;i++) P.set(i);
    for(int i=1;i<=Q;i++){
        scanf("%d",&opt[i]);
        if(opt[i]==1||opt[i]==2) read(x[i]),read(y[i]);
        else read(x[i]);
        if(opt[i]==4) add(x[i],i);
        else add(i-1,i);
    }
    dfs(0,0);
    for(int i=1;i<=Q;i++) printf("%d\n",ans[i]);
    return 0;
}

Codeforces-707D:Persistent Bookcase (離線處理特殊的可持久化問題&&Bitset)