zoukankan      html  css  js  c++  java
  • codeforces 707D Persistent Bookcase

    D. Persistent Bookcase

     

    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
    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.

    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<algorithm>
    #define pb push_back
    using namespace std;
    const int N=1e5+5;
    struct Ope
    {
        int com;
        int x,y;
    }Q[N];
    vector<int>ch[N];
    bool sh[1005][1005];
    int ans[N],cnt,n,m,q;
    void dfs(int u)
    {
        bool flag=0;
        if(Q[u].com==1)
        {
            if(sh[Q[u].x][Q[u].y]==0)
                sh[Q[u].x][Q[u].y]=1,cnt++,flag=1;
        }
        else if(Q[u].com==2)
        {
            if(sh[Q[u].x][Q[u].y]==1)
                sh[Q[u].x][Q[u].y]=0,cnt--,flag=1;
        }
        else if(Q[u].com==3)
        {
            flag=1;
            for(int i=1;i<=m;i++)
                if(sh[Q[u].x][i]==0)cnt++,sh[Q[u].x][i]=1;
                else cnt--,sh[Q[u].x][i]=0;
        }
        ans[u]=cnt;
        for(int i=0,len=ch[u].size();i<len;i++)dfs(ch[u][i]);
        if(!flag)return;
        if(Q[u].com==1)sh[Q[u].x][Q[u].y]=0,cnt--;
        else if(Q[u].com==2)sh[Q[u].x][Q[u].y]=1,cnt++;
        else if(Q[u].com==3)
            for(int i=1;i<=m;i++)
                if(sh[Q[u].x][i]==0)cnt++,sh[Q[u].x][i]=1;
                else cnt--,sh[Q[u].x][i]=0;
    }
    int main()
    {
        scanf("%d%d%d",&n,&m,&q);
        for(int i=1;i<=q;i++)
        {
            scanf("%d",&Q[i].com);
            if(Q[i].com<=2)
                scanf("%d%d",&Q[i].x,&Q[i].y),ch[i-1].pb(i);
            else
            {
                scanf("%d",&Q[i].x);
                if(Q[i].com==4)
                    ch[Q[i].x].pb(i);
                else
                    ch[i-1].pb(i);
            }
        }
        dfs(0);
        for(int i=1;i<=q;i++)
            printf("%d
    ",ans[i]);
        return 0;
    }

     

  • 相关阅读:
    页面显示时间
    如何用JavaScript实现获取验证码的效果
    CSS中style用法详解
    html 样式之style属性的使用
    使用JavaScript实现弹出层效果的简单实例
    [转]创建一个JavaScript弹出DIV窗口层的效果
    eclipse环境下如何配置tomcat,并且把项目部署到Tomcat服务器上
    关于eclipse部署项目后,在tomcat中的webapps文件夹下没有项目
    jsp页面提示“Multiple annotations found at this line:
    Maven3路程(三)用Maven创建第一个web项目(1)
  • 原文地址:https://www.cnblogs.com/homura/p/5846217.html
Copyright © 2011-2022 走看看