• Evanyou Blog 彩带


      题目传送门

    Mars Rover

    格式难调,题面就不放了。


      分析:

      今天考试的时候考了这道题目的加强版,所以来做。

      其实也并不难,我们建立好树形结构以后先把初始权值全部求出,然后就得到了根节点的初始值。因为一次只修改一个点的值,所以我们只要自上而下根据位运算的种类得出每一个节点的值修改后是否会改变根节点的值就行了。

      Code:

      

    //It is made by HolseLee on 2nd Nov 2018
    //CF1010D
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    const int N=1e6+7;
    int n,dp[N];
    struct Node { int ls,rs,val,type; }t[N];
    
    inline int read()
    {
        char ch=getchar(); int x=0; bool flag=false;
        while( ch<'0' || ch>'9' ) {
            if( ch=='-' ) flag=true; ch=getchar(); }
        while( ch>='0' && ch<='9' ) {
            x=x*10+ch-'0'; ch=getchar(); }
        return flag ? -x : x;
    }
    
    void dfs(int x)
    {
        if( t[x].type==5 ) return;
        switch (t[x].type) {
            case 1:
                dfs(t[x].ls), dfs(t[x].rs);
                t[x].val=t[t[x].ls].val&t[t[x].rs].val;
                break;
            case 2:
                dfs(t[x].ls), dfs(t[x].rs);
                t[x].val=t[t[x].ls].val^t[t[x].rs].val;
                break;
            case 3:
                dfs(t[x].ls), dfs(t[x].rs);
                t[x].val=t[t[x].ls].val|t[t[x].rs].val;
                break;
            case 4:
                dfs(t[x].ls); t[x].val=(t[t[x].ls].val^1);
                break;
        }
    }
    
    void DP(int x)
    {
        if( t[x].type==5 ) return;
        switch (t[x].type) {
            case 1:
                if( t[t[x].ls].val==1 && t[t[x].rs].val==1 ) {
                    dp[t[x].ls]=dp[t[x].rs]=1;
                    DP(t[x].ls), DP(t[x].rs);
                } else if( t[t[x].ls].val==1 && t[t[x].rs].val==0 ) {
                    dp[t[x].rs]=1; DP(t[x].rs);
                } else if( t[t[x].ls].val==0 && t[t[x].rs].val==1 ) {
                    dp[t[x].ls]=1; DP(t[x].ls);
                }
                break;
            case 2:
                dp[t[x].ls]=dp[t[x].rs]=1;
                DP(t[x].ls), DP(t[x].rs);
                break;
            case 3:
                if( t[t[x].ls].val==1 && t[t[x].rs].val==0 ) {
                    dp[t[x].ls]=1, DP(t[x].ls);
                } else if( t[t[x].ls].val==0 && t[t[x].rs].val==1 ) {
                    dp[t[x].rs]=1, DP(t[x].rs);
                } else if( t[t[x].ls].val==0 && t[t[x].rs].val==0 ) {
                    dp[t[x].ls]=dp[t[x].rs]=1;
                    DP(t[x].ls), DP(t[x].rs);
                }
                break;
            case 4:
                dp[t[x].ls]=1, DP(t[x].ls);
                break;
        }
    }
    
    int main()
    {
        n=read(); char s[10];
        for(int i=1; i<=n; ++i) {
            scanf("%s",s);
            switch (s[0]) {
                case 'A': t[i].type=1, t[i].ls=read(), t[i].rs=read(); break;
                case 'X': t[i].type=2, t[i].ls=read(), t[i].rs=read(); break;
                case 'O': t[i].type=3, t[i].ls=read(), t[i].rs=read(); break;
                case 'N': t[i].type=4, t[i].ls=read(); break;
                case 'I': t[i].type=5, t[i].val=read(); break;
            }
        }
        dfs(1); DP(1);
        for(int i=1; i<=n; ++i) {
            if( t[i].type!=5 ) continue;
            if( dp[i] ) putchar((t[1].val^1)+'0');
            else putchar(t[1].val+'0');
        }
        puts("");
        return 0;
    }
  • 相关阅读:
    CF280C Game on Tree 概率与期望
    bzoj 3420: Poi2013 Triumphal arch 树形dp+二分
    bzoj 2111: [ZJOI2010]Perm 排列计数 Lucas
    bzoj 3709: [PA2014]Bohater 贪心
    bzoj 1396/2865: 识别子串 后缀自动机+线段树
    【教程】如何搭建一个自己的网站
    C#单例设计模式
    C#双缓冲代码
    Hibernate的查询功能
    hibernate事务规范写法
  • 原文地址:https://www.cnblogs.com/cytus/p/9898942.html
Copyright © 2020-2023  润新知