zoukankan      html  css  js  c++  java
  • hdu 3584 Cube

    三维树状数组,思维转化很有意思,好像是什么容斥原理。
    主要原因是纯暴力肯定会超时,所以要优化,将某一点的和与该点状态想关联,用这样的方法来求解问题。

    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    using namespace std;
    
    const int N=100+24;
    int p[N][N][N],n;
    
    int lowbit(int x)
    {
        return x&-x;
    }
    
    int sum(int x,int y,int z)
    {
        int i,j,k,ret=0;
    
        for(i=x;i>0;i-=lowbit(i))
            for(j=y;j>0;j-=lowbit(j))
                for(k=z;k>0;k-=lowbit(k))
                    ret+=p[i][j][k];
    
        return ret%2;
    }
    
    void add(int x,int y,int z)
    {
        int i,j,k;
        for(i=x;i<105;i+=lowbit(i))
            for(j=y;j<105;j+=lowbit(j))
                for(k=z;k<105;k+=lowbit(k))
                    p[i][j][k]+=1;
    }
    int main()
    {
        int m,i,j,k,a1,a2,b1,b2,c1,c2,t;
        while(~scanf("%d%d",&n,&m))
        {
            memset(p,0,sizeof(p));
            while(m--)
            {
                scanf("%d",&t);
                if(t==0)
                {
                    scanf("%d%d%d",&a1,&b1,&c1);
                    printf("%d
    ",sum(a1,b1,c1));
                }
                else
                {
                    scanf("%d%d%d",&a1,&b1,&c1);
    
                    scanf("%d%d%d",&a2,&b2,&c2);
                    //可以画图来理解一下
                    add(a1,b1,c1);
    
                    add(a1,b1,c2+1);
                    add(a1,b2+1,c1);
                    add(a1,b2+1,c2+1);
    
                    add(a2+1,b2+1,c2+1);
    
                    add(a2+1,b1,c2+1);
                    add(a2+1,b2+1,c1);
                    add(a2+1,b1,c1);
                }
            }
        }
        return 0;
    }
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/

  • 相关阅读:
    Javascript FP-ramdajs
    微信小程序开发
    SPA for HTML5
    One Liners to Impress Your Friends
    Sass (Syntactically Awesome StyleSheets)
    iOS App Icon Template 5.0
    React Native Life Cycle and Communication
    Meteor framework
    RESTful Mongodb
    Server-sent Events
  • 原文地址:https://www.cnblogs.com/xryz/p/4848023.html
Copyright © 2011-2022 走看看