CF #368 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 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 ≤ 10^3, 1 ≤ q ≤ 10^5) — 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.

一个n*m书架,
4种操作,1,2单点修改;3整行修改;4 撤回操作;
每次操作求剩余书数量。

这是新学的(不知道叫啥);
下面是题解连接,讲的真心好

http://blog.csdn.net/queuelovestack/article/details/52269321

粘代码

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
void read(int &a)
{
    a=0;
    char c=getchar();
    while(c<'0'||c>'9')
        c=getchar();
    while(c>='0'&&c<='9')
    {
        a*=10;
        a+=c-'0';
        c=getchar();
    }
}
struct quiry{
    int op,x,y,ans;
}q[100005];
struct bian{
    int to;
}b[100005];
int fst[100005],nxt[100005];
int tot=0;
void build(int f,int t)
{
    b[++tot].to=t;
    nxt[tot]=fst[f];
    fst[f]=tot;
}
int n,m,Q;
int a[1005][1005];
int ans=0;
void dfs(int x)
{
    int i=x;
    int oq=-1,X,Y;
    if(q[i].op==1)
    {
        if(!a[q[i].x][q[i].y])
        {
            a[q[i].x][q[i].y]++;
            ans++;oq=0;
            X=q[i].x;Y=q[i].y;
        }
    }
    if(q[i].op==2)
    {
        if(a[q[i].x][q[i].y])
        {
            a[q[i].x][q[i].y]--;
            ans--;oq=0;
            X=q[i].x;Y=q[i].y;
        }
    }
    if(q[i].op==3)
    {
        for(int j=1;j<=m;j++)
        {
            if(a[q[i].x][j])
            {
                a[q[i].x][j]--;
                ans--;
            }
            else
            {
                a[q[i].x][j]++;
                ans++;
            }
        }
        oq=q[i].x;
    }
    q[i].ans=ans;
    int v;
    for(int j=fst[x];j;j=nxt[j])
    {
        v=b[j].to;
        dfs(v);
    }
    if(oq>=0)
    {
        if(oq>0)
        {
            for(int j=1;j<=m;j++)
            {
                if(a[oq][j])
                {
                    a[oq][j]--;
                    ans--;
                }
                else
                {
                    a[oq][j]++;
                    ans++;
                }
            }
        }
        else
        {
            if(a[X][Y])
            {
                a[X][Y]--;
                ans--;
            }
            else
            {
                a[X][Y]++;
                ans++;
            }
        }
    }
}
int main()
{
    scanf("%d%d%d",&n,&m,&Q);
    for(int i=1;i<=Q;i++)
    {
        scanf("%d",&q[i].op);
        if(q[i].op<=2)
        {
            read(q[i].x);read(q[i].y);
            build(i-1,i);
        }
        else
        {
            read(q[i].x);
            if(q[i].op==3)build(i-1,i);
            else    build(q[i].x,i);
        }
    }
    dfs(0);
    for(int i=1;i<=Q;i++)
        printf("%d\n",q[i].ans);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值