离线dfs CF div2 707 D

http://codeforces.com/contest/707/problem/D

先说一下离线和在线:在线的意思就是每一个询问单独处理复杂度O(多少多少),离线是指将所有的可能的询问先一次都处理出来,最后对于每个询问O(1)回答。

然后说一下cf的这题:

D. Persistent Bookcase
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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.

 

 题目大意:给你一个n*m的书架,刚开始书架上面都是空的。然后有如下四种操作

第一种:放一本书在[i][j]这个位置,如果这里有书的话,那就不用放了。

第二种:拿走[i][j]这个位置的书,如果没有书的话,那就不用拿了。

第三种:将第i行取亦或。

第四种:回到第i步操作的状态

有q个询问,输出该询问下目前书架上还有几本书?

 

思路:一次性全部取出来,然后建图进行dfs。所以这里的复杂度是O(q)

 

 1 //看看会不会爆int!数组会不会少了一维!
 2 //取物问题一定要小心先手胜利的条件
 3 #include <bits/stdc++.h>
 4 using namespace std;
 5 #define LL long long
 6 #define ALL(a) a.begin(), a.end()
 7 #define pb push_back
 8 #define mk make_pair
 9 #define fi first
10 #define se second
11 const int maxn = 1000 + 5;
12 const int maxq = 100000 + 5;
13 bitset<maxn> bite[maxn];
14 bitset<maxn> tmp;
15 int n, m, q;
16 int a[maxq], b[maxq], op[maxq], ans[maxq];
17 vector<int> E[maxq];
18 
19 void dfs(int u){
20     //printf("u = %d\n", u);
21     int len = E[u].size();
22     if (u == 0){
23         ans[u] = 0;
24         for (int i = 0; i < len; i++){
25             ans[E[u][i]] = ans[u];
26             dfs(E[u][i]);
27         }
28     }
29     else if (op[u] == 1){
30         bool flag = false;
31         if (!bite[a[u]][b[u]]) flag = true, bite[a[u]][b[u]] = 1, ans[u] += 1;
32         for (int i = 0; i < len; i++){
33             ans[E[u][i]] = ans[u];
34             dfs(E[u][i]);
35         }
36         if (flag) bite[a[u]][b[u]] = 0;
37     }
38     else if (op[u] == 2){
39         bool flag = false;
40         if (bite[a[u]][b[u]]) flag = true, bite[a[u]][b[u]] = 0, ans[u] -= 1;
41         for (int i = 0; i < len; i++){
42             ans[E[u][i]] = ans[u];
43             dfs(E[u][i]);
44         }
45         if (flag) bite[a[u]][b[u]] = 1;
46     }
47     else if (op[u] == 3){
48         ans[u] = ans[u] + m - bite[a[u]].count() - bite[a[u]].count();
49         bite[a[u]] ^= tmp;
50         for (int i = 0; i < len; i++){
51             ans[E[u][i]] = ans[u];
52             dfs(E[u][i]);
53         }
54         bite[a[u]] ^= tmp;
55     }
56     else if (op[u] == 4){
57         for (int i = 0; i < len; i++){
58             ans[E[u][i]] = ans[u];
59             dfs(E[u][i]);
60         }
61     }
62 }
63 
64 int main(){
65     cin >> n >> m >> q;
66     for (int i = 1; i <= m; i++)
67         tmp[i] = 1;
68     for (int i = 1; i <= q; i++){
69         scanf("%d", op + i);
70         if (op[i] == 1 || op[i] == 2){
71             scanf("%d%d", a + i, b + i);//表示摆放的位置
72         }
73         if (op[i] == 3){
74             scanf("%d", a + i);//表示第i行取反
75         }
76         if (op[i] == 4){
77             scanf("%d", a + i);
78             E[a[i]].pb(i);
79         }
80         else
81             E[i - 1].pb(i);///让每个询问之间相连
82     }
83     dfs(0);
84     for (int i = 1; i <= q; i++){
85         printf("%d\n", ans[i]);
86     }
87     return 0;
88 }
View Code

 

转载于:https://www.cnblogs.com/heimao5027/p/5793241.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目标检测(Object Detection)是计算机视觉领域的一个核心问题,其主要任务是找出图像中所有感兴趣的目标(物体),并确定它们的类别和位置。以下是对目标检测的详细阐述: 一、基本概念 目标检测的任务是解决“在哪里?是什么?”的问题,即定位出图像中目标的位置并识别出目标的类别。由于各类物体具有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具挑战性的任务之一。 二、核心问题 目标检测涉及以下几个核心问题: 分类问题:判断图像中的目标属于哪个类别。 定位问题:确定目标在图像中的具体位置。 大小问题:目标可能具有不同的大小。 形状问题:目标可能具有不同的形状。 三、算法分类 基于深度学习的目标检测算法主要分为两大类: Two-stage算法:先进行区域生成(Region Proposal),生成有可能包含待检物体的预选框(Region Proposal),再通过卷积神经网络进行样本分类。常见的Two-stage算法包括R-CNN、Fast R-CNN、Faster R-CNN等。 One-stage算法:不用生成区域提议,直接在网络中提取特征来预测物体分类和位置。常见的One-stage算法包括YOLO系列(YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5等)、SSD和RetinaNet等。 四、算法原理 以YOLO系列为例,YOLO将目标检测视为回归问题,将输入图像一次性划分为多个区域,直接在输出层预测边界框和类别概率。YOLO采用卷积网络来提取特征,使用全连接层来得到预测值。其网络结构通常包含多个卷积层和全连接层,通过卷积层提取图像特征,通过全连接层输出预测结果。 五、应用领域 目标检测技术已经广泛应用于各个领域,为人们的生活带来了极大的便利。以下是一些主要的应用领域: 安全监控:在商场、银行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值