hdu 5735 Born Slippy 折半枚举


题解上说的很清楚


折半枚举,mx[le][ri]代表往根结点方向的

某一节点j的权值前8位为le、当结点权值后8为为ri情况下的,

后8位操作和的最大值。


权值的大小有限,折半枚举时间正好。


#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
const int maxn=65536    ;
string s;
int n,kind;
int a[maxn+5],nedge,to[2*maxn+5],nex[2*maxn+5],fir[maxn+3];
const int mod=1e9+7.5;
const int maxS=256;
int cnt[maxS+5];
ll  tmp[maxn+3][maxS],mx[maxS][maxS];
int ans;

inline void add_edge(int x,int y)
{
    to[nedge]=y;
    nex[nedge]=fir[x];
    fir[ x ]=nedge++;
}

int cal(int le,int ri)
{
    if(kind==0)  return le&ri;
    if(kind==1) return  (le^ri) ;
    return le|ri;
}

void update(ll &x,ll v)
{
    if(x<v)  x=v;
}
void dfs(int x)
{

    ll dp=0;
    int  ri= (a[x]&(maxS-1)),le=(a[x]>>8);
    for(int i=0;i<maxS;i++) if(cnt[i]){
        update(dp,mx[i][ri]+ (cal( le,i  )<<8) );
    }

    ans=( ans+ ((ll)x*(dp+a[x] )  ) )%mod;


    cnt[le]++;
    for(int i=0;i<maxS;i++)
    {
       tmp[x][i]=mx[le][i];
    }
    for0(i,maxS){
       update(mx[le][i],dp+  cal(ri,i)   );
    }


    for(int i=fir[x];~i;i=nex[i])
    {
        int y=to[i];
        dfs(y);
    }
    cnt[le]--;
    for0(i,maxS){
        mx[le][i]=tmp[x][i];
    }


}
int main()
{
   std::ios::sync_with_stdio(false);
   int T;
   cin>>T;
   while(T--)
   {
       cin>>n;
       cin>>s;
       if(s=="AND")  kind=0;
       else if(s=="XOR")  kind=1;
       else kind=2;
       for1(i,n)
       {
          cin>>a[i];
       }
       int x;
       memset(fir,-1,sizeof fir);
       memset(cnt,0,sizeof cnt);
       memset(mx,0,sizeof mx);
       nedge=0;ans=0;
       for(int i=2;i<=n;i++)
       {
           cin>>x;
           add_edge(x,i);
       }
       dfs(1);
       cout<<ans<<endl;

   }
   return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值