【Codeforces-707D】Persistent Bookcase DFS + 线段树

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 31 1 13 24 0

output

140

input

4 2 63 22 2 23 33 22 2 23 2

output

213324

input

2 2 23 22 2 1

output

21

Note

This image illustrates the second sample case.

Solution

 题目大意:

给出一个矩阵,要支持如下操作

1.(x,y)位置变成1

2.(x,y)位置变成0

3.整行取反,0变成1,1变成0

4.退回到第k次操作后的状态

一共Q次询问,每次询问后输出矩阵中1的个数

首先,把矩阵展成序列,对其建线段树,这样,1,2,3操作就是简单的单点修改,区间修改

操作4的难处在于空间不允许保存历史状态,

考虑离线。

首先假设我们得到$i$之前的所有操作的答案,$i+1$次操作是退回操作,显然$i+1$次操作的答案,可以通过以前的答案得到,但问题涉及状态的变化

很显然,一次退回操作就相当于将这个操作之后的,到下一次退回操作之前的所有操作,从其退回到的状态开始修改

这显然是个树形的结构,于是我们的方法就非常直观了

对于所有的操作,我们假定$i$操作是向$i+1$操作连一条单向边的,那么对于一个退回操作$k$,它所退回到的操作是$x$,就相当于从$x$也向$k+1$连一条单向边,然后我们用$x$把$k$的答案更新,去掉$k$既可

那么从一号操作为根的树上DFS,每次修改,记录答案,修改完后回溯,直到遍历整棵树

而这样,状态不能记录的问题就被解决了,只需要一棵线段树,不过是修改2Q次

一个操作,可能不合法,这时候需要记录一下,回溯的时候特判

Code

code from yveh

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
struct edgetype{
    int s,t,next;
}e[200010];
int head[100010],cnt=0;
void addedge(int s,int t)
{
    e[cnt].s=s;e[cnt].t=t;e[cnt].next=head[s];head[s]=cnt++;
}
struct Node{
    int data,size;
    bool rev;
    Node()
    {
        data=rev=0;
    }
};
bool flag;
namespace Segtree
{
    Node tree[4000010];
    void pushup(int node)
    {
        tree[node].data=tree[node<<1].data+tree[node<<1|1].data;
    }
    void build(int l,int r,int node)
    {
        tree[node].size=r-l+1;
        if (l==r)
            return;
        int mid=(l+r)>>1;
        build(l,mid,node<<1);
        build(mid+1,r,node<<1|1);
    }
    void pushdown(int node)
    {
        if (tree[node].rev)
        {
            tree[node<<1].data=tree[node<<1].size-tree[node<<1].data;
            tree[node<<1].rev^=1;
            tree[node<<1|1].data=tree[node<<1|1].size-tree[node<<1|1].data;
            tree[node<<1|1].rev^=1;
            tree[node].rev=0;
        }
    }
    void modify_pos(int pos,int l,int r,int node,int val)
    {
        if (l==r)
        {
            flag=tree[node].data==val;
            tree[node].data=val;
            return;
        }
        pushdown(node);
        int mid=(l+r)>>1;
        if (pos<=mid)
            modify_pos(pos,l,mid,node<<1,val);
        else
            modify_pos(pos,mid+1,r,node<<1|1,val);
        pushup(node);
    }
    void modify_rev(int L,int R,int l,int r,int node)
    {
        if (L<=l&&r<=R)
        {
            tree[node].data=tree[node].size-tree[node].data;
            tree[node].rev^=1;
            return;
        }
        pushdown(node);
        int mid=(l+r)>>1;
        if (L<=mid)
            modify_rev(L,R,l,mid,node<<1);
        if (R>mid)
            modify_rev(L,R,mid+1,r,node<<1|1);
        pushup(node);
    }
    int query()
    {
        return tree[1].data;
    }
}
int n,m,q,opt,u,v,k;
int a[100010][5],ans[100010];
void init()
{
    scanf("%d%d%d",&n,&m,&q);
    Segtree::build(1,n*m,1);
}
void dfs(int node)
{
    if (a[node][0]==1)
    {
        Segtree::modify_pos((a[node][1]-1)*m+a[node][2],1,n*m,1,a[node][3]);
        if (!flag)
            a[node][3]=0;
        else
            a[node][0]=4;
    }
    if (a[node][0]==2)
    {
        Segtree::modify_pos((a[node][1]-1)*m+a[node][2],1,n*m,1,a[node][3]);
        if (!flag)
            a[node][3]=1;
        else
            a[node][0]=4;
    }
    if (a[node][0]==3)
        Segtree::modify_rev((a[node][1]-1)*m+1,a[node][1]*m,1,n*m,1);
    ans[node]=Segtree::query();
    for (int i=head[node];i!=-1;i=e[i].next)
        dfs(e[i].t);
    if (a[node][0]==1)
        Segtree::modify_pos((a[node][1]-1)*m+a[node][2],1,n*m,1,a[node][3]);
    if (a[node][0]==2)
        Segtree::modify_pos((a[node][1]-1)*m+a[node][2],1,n*m,1,a[node][3]);
    if (a[node][0]==3)
        Segtree::modify_rev((a[node][1]-1)*m+1,a[node][1]*m,1,n*m,1);
}
void work()
{
    memset(head,0xff,sizeof(head));
    cnt=0;
    for (int i=1;i<=q;i++)
    {
        scanf("%d",&a[i][0]);
        if (a[i][0]==1)
        {
            scanf("%d%d",&a[i][1],&a[i][2]);
            a[i][3]=1;
        }
        if (a[i][0]==2)
        {
            scanf("%d%d",&a[i][1],&a[i][2]);
            a[i][3]=0;
        }

        if (a[i][0]==3)
            scanf("%d",&a[i][1]);
        if (a[i][0]==4)
        {
            scanf("%d",&k);
            addedge(k,i);
        }
        else
            addedge(i-1,i);
    }
    dfs(0);
    for (int i=1;i<=q;i++)
        printf("%d\n",ans[i]);
}
int main()
{
    init();
    work();
    return 0;
}

YveH打CF时问我的题...当时蹦出这个想法,但是他没能来得及当场A掉

觉得思路挺有意义的一道题,所以留下了想法...

实际上我还不知道题解是什么.....

时间: 2024-10-05 10:29:12

【Codeforces-707D】Persistent Bookcase DFS + 线段树的相关文章

CodeForces 707D Persistent Bookcase ——(巧妙的dfs)

一个n*m的矩阵,有四种操作: 1.(i,j)处变1: 2.(i,j)处变0: 3.第i行的所有位置1,0反转: 4.回到第k次操作以后的状态: 问每次操作以后整个矩阵里面有多少个1. 其实不好处理的操作只有第四个,但是这题的思路很巧妙,123三种操作全部建立顺边,第四种操作将k和这次操作的序号建边,然后dfs进行操作即可,遇到尽头,则退回到前一个分岔点,并且回溯的过程中将操作反转. 具体见代码: 1 #include <stdio.h> 2 #include <algorithm>

【深搜】【数】Codeforces 707D Persistent Bookcase

题目链接: http://codeforces.com/problemset/problem/707/D 题目大意: 一个N*M的书架,支持4种操作 1.把(x,y)变为有书. 2.把(x,y)变为没书. 3.把x行上的所有书状态改变,有变没,没变有. 4.回到第K个操作时的状态. 求每一次操作后书架上总共多少书. 题目思路: [深搜][树] 现场有一点思路不过没敢写哈.还是太弱了. 总共只用保存一张图,把操作看成一棵树,一开始I操作连接在I-1操作后,如果遇到操作4的话,把I操作与I-1操作的

CodeForces 707D Persistent Bookcase

$dfs$,优化. $return$操作说明该操作完成之后的状态和经过操作$k$之后的状态是一样的.因此我们可以建树,然后从根节点开始$dfs$一次(回溯的时候复原一下状态)就可以算出所有状态的答案. 对于$1$和$2$操作,可以开一个数组$a[i][j]$记录每一格子被操作$1$和$2$操作了几次. 然后开一个数组$r[i]$记录每一行被操作$3$操作了几次. 每一个真正的状态为$\left( {a\left[ i \right]\left[ j \right] + r\left[ i \ri

CodeForces - 383C Propagating tree(dfs + 线段树)

题目大意: 给出一棵树,树上每个节点都有权值,然后有两个操作. 1 x val 在结点x上加上一个值val,x的儿子加上 -val,x的儿子的儿子加上 - (-val),以此类推. 2 x 问x节点的值. 思路分析: 每个节点上加值都是给自己的儿子节点加,而且这个是颗树. 比如样例上的,如果你给node 1加一个值,那么五个节点都加. 再给node 2加个值,2的儿子节点也加了,之前给1加的值也要加到2号节点的儿子. 所以你会发现节点的儿子会存在一个从属的关系. 这样的话,我们可以把所有节点从新

Codeforces 444C DZY Loves Colors(线段树)

题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是修改区间上l到r上面德值为x,2是询问l到r区间总的修改值. 解题思路:线段树模板题. #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using namespace std; const int maxn = 5*1e5; typedef long lo

2014 Super Training #9 F A Simple Tree Problem --DFS+线段树

原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个mark坑了我好几个小时..哎.太弱. 先DFS这棵树,树形结构转换为线性结构,每个节点有一个第一次遍历的时间和最后一次遍历的时间,之间的时间戳都为子树的时间戳,用线段树更新这段区间即可实现更新子树的效果,用到懒操作节省时间. 坑我的地方: update时,不能写成:tree[rt].mark = 1,

HDU 5877 dfs+ 线段树(或+树状树组)

1.HDU 5877  Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au,查询它有多少个祖先av满足av<=k/au. (1)dfs+线段树 #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm>

POJ 3321 Apple Tree (dfs+线段树)

题目大意: 修改树上的节点,然后求子树的和. 思路分析: dfs 重新编号,烂大街了... #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #define maxn 100005 #define lson num<<1,s,mid #define rson num<<1|1,mid+1,e using namespace std

Codeforces 57B Martian Architecture 暴力||线段树

题目链接:点击打开链接 题意:n长的序列(初始全为0) m个操作 k个查询 下面m个操作[l,r] h 代表 a[l] +=h; a[l+1] += h+i; a[l+i] += h+i;  l<=i<=r 然后问k个位置的和 因为k<=100 所以直接暴力也可以 ----------------------- 如果k<=100000 也是可以做的 只需要给区间记录一个标记lazy,表示从左端点开始 l, l+1, l+i ··· l+r 而向下更新时, 左区间则直接更新, 右区间