zoukankan      html  css  js  c++  java
  • [CF707D]Persistent Bookcase(离线,DFS)

    题目链接:http://codeforces.com/contest/707/problem/D

    题意:维护一个图,一共四个操作。

    1.第i行第j列变1

    2.第i行第j列变0

    3.第i行颠倒,1变0,0变1

    4.将整张图变为k次操作时的状态如果k是0的话,则清空图

    乍一看就知道可以用bitset来保存图,但是更新图的话复杂度太高,纯模拟一定会TLE。所以可以离线做,存下所有状态。并且根据操作建树,假如j操作是i次操作的下一步,则将j操作作为i的儿子,1~3操作都是i-1作为i的父亲,只有4操作特殊,设i是k的儿子。这里树根设0,0存一个空的图就解决操作4的操作数是0的问题了。之后回溯每次查询,查询完一条后回溯到前一状态走另外的儿子就行了。

      1 /*
      2 ━━━━━┒ギリギリ♂ eye!
      3 ┓┏┓┏┓┃キリキリ♂ mind!
      4 ┛┗┛┗┛┃\○/
      5 ┓┏┓┏┓┃ /
      6 ┛┗┛┗┛┃ノ)
      7 ┓┏┓┏┓┃
      8 ┛┗┛┗┛┃
      9 ┓┏┓┏┓┃
     10 ┛┗┛┗┛┃
     11 ┓┏┓┏┓┃
     12 ┛┗┛┗┛┃
     13 ┓┏┓┏┓┃
     14 ┃┃┃┃┃┃
     15 ┻┻┻┻┻┻
     16 */
     17 #include <algorithm>
     18 #include <iostream>
     19 #include <iomanip>
     20 #include <cstring>
     21 #include <climits>
     22 #include <complex>
     23 #include <fstream>
     24 #include <cassert>
     25 #include <cstdio>
     26 #include <bitset>
     27 #include <vector>
     28 #include <deque>
     29 #include <queue>
     30 #include <stack>
     31 #include <ctime>
     32 #include <set>
     33 #include <map>
     34 #include <cmath>
     35 using namespace std;
     36 #define fr first
     37 #define sc second
     38 #define cl clear
     39 #define BUG puts("here!!!")
     40 #define W(a) while(a--)
     41 #define pb(a) push_back(a)
     42 #define Rint(a) scanf("%d", &a)
     43 #define Rll(a) scanf("%I64d", &a)
     44 #define Rs(a) scanf("%s", a)
     45 #define Cin(a) cin >> a
     46 #define FRead() freopen("in", "r", stdin)
     47 #define FWrite() freopen("out", "w", stdout)
     48 #define Rep(i, len) for(int i = 0; i < (len); i++)
     49 #define For(i, a, len) for(int i = (a); i < (len); i++)
     50 #define Cls(a) memset((a), 0, sizeof(a))
     51 #define Clr(a, x) memset((a), (x), sizeof(a))
     52 #define Full(a) memset((a), 0x7f7f7f, sizeof(a))
     53 #define lrt rt << 1
     54 #define rrt rt << 1 | 1
     55 #define pi 3.14159265359
     56 #define RT return
     57 #define lowbit(x) x & (-x)
     58 #define onecnt(x) __builtin_popcount(x)
     59 typedef long long LL;
     60 typedef long double LD;
     61 typedef unsigned long long ULL;
     62 typedef pair<int, int> pii;
     63 typedef pair<string, int> psi;
     64 typedef pair<LL, LL> pll;
     65 typedef map<string, int> msi;
     66 typedef vector<int> vi;
     67 typedef vector<LL> vl;
     68 typedef vector<vl> vvl;
     69 typedef vector<bool> vb;
     70 
     71 typedef struct Node {
     72     int k, x, y;
     73 }Node;
     74 const int maxn = 1010;
     75 const int maxm = 100100;
     76 int ret;
     77 int q, n, m;
     78 int cmd, a, b, c;
     79 bitset<maxn> st[maxn];
     80 vi mp[maxm];
     81 Node qq[maxm];
     82 int ans[maxm];
     83 
     84 void go(int x, int& ok) {
     85     int k = qq[x].k;
     86     int i = qq[x].x;
     87     int j = qq[x].y;
     88     if(k == 1) {
     89         if(!st[i][j]) {
     90             st[i][j] = 1;
     91             ret++;
     92             ok = 1;
     93         }
     94         else ok = 0;
     95     }
     96     if(k == 2) {
     97         if(st[i][j]) {
     98             st[i][j] = 0;
     99             ret--;
    100             ok = 1;
    101         }
    102         else ok = 0;
    103     }
    104     if(k == 3) {
    105         int cur = st[i].count();
    106         if(st[i][maxn-1] == 1) cur -= (maxn - m);
    107         ret = ret - cur + (m - cur);
    108         st[i] = ~st[i];
    109     }
    110 }
    111 
    112 void re(int x, int& ok) {
    113     int k = qq[x].k;
    114     int i = qq[x].x;
    115     int j = qq[x].y;
    116     if(k == 1) {
    117         if(ok) {
    118             st[i][j] = 0;
    119             ret--;
    120         }
    121     }
    122     if(k == 2) {
    123         if(ok) {
    124             st[i][j] = 1;
    125             ret++;
    126         }
    127     }
    128     if(k == 3) {
    129         int cur = st[i].count();
    130         if(st[i][maxn-1] == 1) cur -= (maxn - m);
    131             ret = ret - cur + (m - cur);
    132             st[i] = ~st[i];
    133     }
    134 }
    135 
    136 void dfs(int x) {
    137     int ok;
    138     if(x) {
    139         go(x, ok);
    140         ans[x] = ret;
    141     }
    142     Rep(i, mp[x].size()) dfs(mp[x][i]);
    143     re(x, ok);
    144 }
    145 
    146 int main() {
    147     // FRead();
    148     while(~scanf("%d%d%d",&n,&m,&q)) {
    149         For(i, 1, q+1) {
    150             Rint(qq[i].k);
    151             if(qq[i].k <= 2) {
    152                 Rint(qq[i].x);
    153                 Rint(qq[i].y);
    154             }
    155             else Rint(qq[i].x);
    156         }
    157         For(i, 2, q+1) {
    158             if(qq[i].k == 4) mp[qq[i].x].push_back(i);
    159             else mp[i-1].push_back(i);
    160         }
    161         mp[0].push_back(1);
    162         dfs(0);
    163         For(i, 1, q+1) printf("%d
    ", ans[i]);
    164     }
    165     RT 0;
    166 }
  • 相关阅读:
    1130
    Oracle 数据库常用操作语句大全
    Oracle用sys登陆报:ORA-28009:connection as sys should be as sysdba
    导出数据报ORA-39002: 操作无效 ORA-39070: 无法打开日志文件。 ORA-39087: 目录名 DUMP_DIR 无效
    SGI STL源码stl_bvector.h分析
    SGI STL源码stl_vector.h分析
    CGI 萃取技术 __type_traits
    迭代器iterator和traits编程技法
    智能指针分析及auto_ptr源码
    C++深拷贝和浅拷贝细节理解
  • 原文地址:https://www.cnblogs.com/kirai/p/5793978.html
Copyright © 2011-2022 走看看