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.

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[];
int head[],cnt=;
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=;
}
};
bool flag;
namespace Segtree
{
Node tree[];
void pushup(int node)
{
tree[node].data=tree[node<<].data+tree[node<<|].data;
}
void build(int l,int r,int node)
{
tree[node].size=r-l+;
if (l==r)
return;
int mid=(l+r)>>;
build(l,mid,node<<);
build(mid+,r,node<<|);
}
void pushdown(int node)
{
if (tree[node].rev)
{
tree[node<<].data=tree[node<<].size-tree[node<<].data;
tree[node<<].rev^=;
tree[node<<|].data=tree[node<<|].size-tree[node<<|].data;
tree[node<<|].rev^=;
tree[node].rev=;
}
}
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)>>;
if (pos<=mid)
modify_pos(pos,l,mid,node<<,val);
else
modify_pos(pos,mid+,r,node<<|,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^=;
return;
}
pushdown(node);
int mid=(l+r)>>;
if (L<=mid)
modify_rev(L,R,l,mid,node<<);
if (R>mid)
modify_rev(L,R,mid+,r,node<<|);
pushup(node);
}
int query()
{
return tree[].data;
}
}
int n,m,q,opt,u,v,k;
int a[][],ans[];
void init()
{
scanf("%d%d%d",&n,&m,&q);
Segtree::build(,n*m,);
}
void dfs(int node)
{
if (a[node][]==)
{
Segtree::modify_pos((a[node][]-)*m+a[node][],,n*m,,a[node][]);
if (!flag)
a[node][]=;
else
a[node][]=;
}
if (a[node][]==)
{
Segtree::modify_pos((a[node][]-)*m+a[node][],,n*m,,a[node][]);
if (!flag)
a[node][]=;
else
a[node][]=;
}
if (a[node][]==)
Segtree::modify_rev((a[node][]-)*m+,a[node][]*m,,n*m,);
ans[node]=Segtree::query();
for (int i=head[node];i!=-;i=e[i].next)
dfs(e[i].t);
if (a[node][]==)
Segtree::modify_pos((a[node][]-)*m+a[node][],,n*m,,a[node][]);
if (a[node][]==)
Segtree::modify_pos((a[node][]-)*m+a[node][],,n*m,,a[node][]);
if (a[node][]==)
Segtree::modify_rev((a[node][]-)*m+,a[node][]*m,,n*m,);
}
void work()
{
memset(head,0xff,sizeof(head));
cnt=;
for (int i=;i<=q;i++)
{
scanf("%d",&a[i][]);
if (a[i][]==)
{
scanf("%d%d",&a[i][],&a[i][]);
a[i][]=;
}
if (a[i][]==)
{
scanf("%d%d",&a[i][],&a[i][]);
a[i][]=;
} if (a[i][]==)
scanf("%d",&a[i][]);
if (a[i][]==)
{
scanf("%d",&k);
addedge(k,i);
}
else
addedge(i-,i);
}
dfs();
for (int i=;i<=q;i++)
printf("%d\n",ans[i]);
}
int main()
{
init();
work();
return ;
}

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

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

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

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

  1. Codeforces 707D Persistent Bookcase(时间树)

    [题目链接] http://codeforces.com/problemset/problem/707/D [题目大意] 给出一个矩阵,要求满足如下操作,单个位置x|=1或者x&=0,一行的数 ...

  2. 【离线】【深搜】【树】Codeforces 707D Persistent Bookcase

    题目链接: http://codeforces.com/problemset/problem/707/D 题目大意: 一个N*M的书架,支持4种操作 1.把(x,y)变为有书. 2.把(x,y)变为没 ...

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

    一个n*m的矩阵,有四种操作: 1.(i,j)处变1: 2.(i,j)处变0: 3.第i行的所有位置1,0反转: 4.回到第k次操作以后的状态: 问每次操作以后整个矩阵里面有多少个1. 其实不好处理的 ...

  4. CodeForces 707D Persistent Bookcase

    $dfs$,优化. $return$操作说明该操作完成之后的状态和经过操作$k$之后的状态是一样的.因此我们可以建树,然后从根节点开始$dfs$一次(回溯的时候复原一下状态)就可以算出所有状态的答案. ...

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

    1.HDU 5877  Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...

  6. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  7. Codeforces1110F Nearest Leaf dfs + 线段树 + 询问离线

    Codeforces1110F dfs + 线段树 + 询问离线 F. Nearest Leaf Description: Let's define the Eulerian traversal of ...

  8. dfs+线段树 zhrt的数据结构课

    zhrt的数据结构课 这个题目我觉得是一个有一点点思维的dfs+线段树 虽然说看起来可以用树链剖分写,但是这个题目时间卡了树剖 因为之前用树剖一直在写这个,所以一直想的是区间更新,想dfs+线段树,有 ...

  9. codeforces 707D D. Persistent Bookcase(dfs)

    题目链接: D. Persistent Bookcase time limit per test 2 seconds memory limit per test 512 megabytes input ...

随机推荐

  1. python问题:IndentationError:expected an indented block错误解决《转》

    python问题:IndentationError:expected an indented block错误解决 标签: python语言 2012-07-07 17:59 125145人阅读 评论( ...

  2. SQL Server数据库代码指令简介

    这些是比较常用的命令操作,事先声明,这些命令是不区分大小写的,我按照我的课本来总结用法和知识点,无用的章节自动省略. 没有一点数据库知识基础的可以等我录制视频,不然可能看不懂,视频链接:http:// ...

  3. YII框架概念与安装

    Yii概念: YII安装:      下载最版本http://www.framework.com      解压至访问目录下 直接打开advanced/init.bat文件输入0之后输入yes 打不开 ...

  4. opencv6.3-imgproc图像处理模块之边缘检测

    接opencv6.2-improc图像处理模块之图像尺寸上的操作 本文大部分都是来自于转http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutori ...

  5. 移动端页面(css)调试之“weinre大法”

    移动端页面调试一般分两步.第一步我们需要把本地(pc端)写的页面效果展现在移动端,一个很方便的办法是用 fiddler 作为代理,具体可以参考 如何用 fiddler 代理调试本地手机页面,这样我们就 ...

  6. 浅析WPhone、Android的Back与Home键

    浅析WPhone.Android的Back与Home键 背景 本人一直在用诺基亚手机(目前是Nokia 925,Windows Phonre 8.1),在界面设计.应用多样性等方面没少受身边Andro ...

  7. 史密斯(smith)圆图讲解

    不管多么经典的射频教程,为什么都做成黑白的呢?让想理解史密斯原图的同学一脸懵逼. 这是什么东东? 今天解答三个问题: 1.是什么? 2.为什么? 3.干什么? 1.是什么? 该图表是由菲利普·史密斯( ...

  8. VBPR: Visual Bayesian Personalized Ranking from Implicit Feedback-AAAI2016 -20160422

    1.Information publication:AAAI2016 2.What 基于BPR模型的改进:在商品喜好偏序对的学习中,将商品图片的视觉信息加入进去,冷启动问题. 3.Dataset Am ...

  9. vijos-1447 开关灯泡-大整数开方算法

    描述 一个房间里有n盏灯泡,一开始都是熄着的,有1到n个时刻,每个时刻i,我们会将i的倍数的灯泡改变状态(即原本开着的现将它熄灭,原本熄灭的现将它点亮),问最后有多少盏灯泡是亮着的. 提示 范围:40 ...

  10. Linux下sysstat工具学习

    Linux下,我们多用ssh链接服务器远程操控.对于系统的监控必不可少,sysstat很不错的监控工具包. sysstat官网:http://sebastien.godard.pagesperso-o ...