CF: 368 (Div2 D) [ BITSET+DFS ]

CF: 368 (Div. 2) [ BITSET+DFS ]
///==================================================///
///                HELLO WORLD !!                    ///
///                  IT'S ME                         ///
///               BISHAL GAUTAM                      ///
///         [ bsal.gautam16@gmail.com ]              ///
///==================================================///
#include<bits/stdc++.h>
#define X first
#define Y second
#define mpp make_pair
#define nl printf("\n")
#define SZ(x) (int)(x.size())
#define pb(x) push_back(x)
#define pii pair<int,int>
#define pll pair<ll,ll>
///---------------------
#define S(a) scanf("%d",&a)
#define P(a) printf("%d",a)
#define SL(a) scanf("%lld",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define SL2(a,b) scanf("%lld%lld",&a,&b)
///------------------------------------
#define all(v) v.begin(),v.end()
#define CLR(a) memset(a,0,sizeof(a))
#define SET(a) memset(a,-1,sizeof(a))
#define fr(i,a,n) for(int i=a;i<=n;i++)
using namespace std;
typedef long long ll;

///  Digit     0123456789012345678 ///
#define MX     100005
#define inf    2000000010
#define MD     1000000007
#define eps    1e-9
///===============================///

struct data {
    int op,x,y;
    data() {};
    data(int o,int a,int b) {
        op=o,x=a,y=b;
    }
} Q[MX+2];

vector<int>G[MX+2];
bitset<1001>dp[1001],lim;
int n,m,ans[MX+2];

void Dfs(int u,int sm) {
    ans[u]=sm;
    for(int i=0; i<SZ(G[u]); i++) {
        int v=G[ u ][ i ];
        int op=Q[v].op;
        int x=Q[v].x;
        int y=Q[v].y;
        if( op==1 ) {
            if( !dp[x].test(y) ) {
                dp[x][ y ]=1;
                Dfs(v,sm+1);
                dp[x][ y ]=0;
            } else Dfs(v,sm);
        }
        else if( op==2 ) {
            if( dp[x].test(y) ) {
                dp[x][ y ]=0;
                Dfs(v,sm-1);
                dp[x][ y ]=1;
            } else Dfs(v,sm);
        }
        else if( op==3 ){
            int pc=dp[x].count();
            dp[ x ]^=lim;
            int nc=m-pc;
            Dfs( v,sm-pc+nc );
            dp[ x ]^=lim;
        }else Dfs(v,sm);
    }
}

int main() {
    int q,i,j,k,x,y,z,op;
    S2(n,m);
    S(q);
    fr(i,1,m)lim.set(i); ///Making Full SetBit
    fr(i,1,q) {
        S(op);
        y=-1;
        ///Time Machine DAG
        if(op<=2)S2(x,y),G[i-1].pb( i );
        else S(x);
        Q[i]=data(op,x,y);
        if(op==3)G[ i-1 ].pb( i );
        if(op==4)G[ x ].pb( i );
    }
    Dfs(0,0); ///Answering Queries On the basis of DFS tree
    fr(i,1,q){
        printf("%d\n",ans[i]);
    }
    return 0;
}


Comments

Popular posts from this blog

UVA 11019 - Matrix Matcher( 2D String matching, Rolling Hash )

UVA-12062 - Reverse Assignment

HackerRank: Poisonous Plants ( Stacks, DS )