B 树形dp

组合的思想。

Z队长的思路。

dp[i][1]表示以i为跟结点的子树向上贡献1个的方案,dp[i][0]表示以i为跟结点的子树向上贡献0个的方案.

如果当前为叶子节点,dp[i][0] = 1,(颜色为1,可以断开与父节点的连接,颜色为0,不断开,方案恒为1),dp[i][1] = co[i](i节点的颜色)。

非叶子节点:将所有孩子节点的dp[child][0]乘起来为sum,孩子贡献为0的总方案。

当前颜色为0时, dp[i][1] += sum/dp[child][0]*dp[child][1],(选当前孩子贡献的1) ,

dp[i][0] = sum+dp[i][1](将i与其父亲断开)。

当颜色为1时,  dp[i][1] (需儿子们贡献为0)= dp[i][0](需与父亲断开) = sum.

中间除法取模需要用到逆元。 (s/y)%mod = (s*y^mod-2)%mod;

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 100010
#define LL long long
#define INF 0xfffffff
#define mod 1000000007
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
vector<int>ch[N];
int dp[N][];
int co[N];
LL q_mod(LL a,LL b)
{
LL d,t;
d = ,t=a;
while(b)
{
if(b&) d = (d*t)%mod;
b/=;
t = (t*t)%mod;
}
return d;
}
LL cal(LL s,LL y,LL x)
{
s = (s*x)%mod;
return (s*q_mod(y,mod-))%mod;
}
void dfs(int u,int pre)
{
int i;
LL s0=;
int flag = ;
for(i = ; i < ch[u].size() ;i++)
{
int v = ch[u][i];
if(v==pre) continue;
flag = ;
dfs(v,u);
s0 = (s0*dp[v][])%mod;
}
if(!flag)
{
dp[u][] = ;
dp[u][] = co[u];
return ;
}
if(co[u]==)
{
dp[u][] = s0;
dp[u][] = ;
for(i = ;i < ch[u].size() ; i++)
{
int v = ch[u][i];
if(v==pre) continue;
if(!dp[v][]) continue;
dp[u][] = (dp[u][]+cal(s0,dp[v][],dp[v][]))%mod;
}
dp[u][] = (dp[u][]+dp[u][])%mod;
}
else
{
dp[u][] = s0;
dp[u][] = s0;
}
}
int main()
{
int n,i;
cin>>n;
for(i = ; i < n-; i++)
{
int u;
scanf("%d",&u);
ch[u].push_back(i+);
ch[i+].push_back(u);
}
for(i = ; i < n; i++)
scanf("%d",&co[i]);
dfs(,-);
cout<<dp[][]<<endl;
return ;
}

C 标记数组开始和尾部 线段树维护。

因为有翻转操作,当前数组的开始与结尾不确定,用两个变量标记。

线段树节点表示以它为右端点的线段   比如 0-1-2-3-4-5-6  分别用1 2 3 4 5 6表示它左边那条线段。

如果旋转位置大于当前线段长度的一半,先把此线段翻转,再翻转相对的较短一段。

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 100000
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
int s[N<<];
int a[N];
void up(int w)
{
s[w] = s[w<<]+s[w<<|];
}
void build(int l,int r,int w)
{
if(l==r)
{
s[w] = a[l] = ;
return ;
}
int m = (l+r)>>;
build(l,m,w<<);
build(m+,r,w<<|);
up(w);
}
void update(int p,int d,int l,int r,int w)
{
if(l==r)
{
s[w]+=d;
a[l] = s[w];
return ;
}
int m = (l+r)>>;
if(p<=m) update(p,d,l,m,w<<);
else update(p,d,m+,r,w<<|);
up(w);
}
int query(int a,int b,int l,int r,int w)
{
if(a<=l&&b>=r)
{
return s[w];
}
int m = (l+r)>>;
int res = ;
if(a<=m) res+=query(a,b,l,m,w<<);
if(b>m) res+=query(a,b,m+,r,w<<|);
return res;
}
int main()
{
int n,m,i,j;
cin>>n>>m;
build(,n,);
int lef=,rig=n;
while(m--)
{
int x,y,z;
scanf("%d%d",&x,&y);
if(x==)
{
int mid = (abs(rig-lef)+)>>;
//cout<<mid<<" .."<<rig<<" "<<lef<<endl;
if(y>mid)
{
swap(lef,rig);
y = abs(rig-lef)+-y;
}
//cout<<y<<".."<<endl;
int st,en;
if(lef<rig)
{
st = lef+y-;
j = st+;
for(i = st ; i >= lef ; i--)
{
update(j,a[i],,n,);
j++;
}
lef = st+;
}
else
{
st = lef-y+;
j = st-;
for(i = st ;i <= lef ;i++)
{
update(j,a[i],,n,);
j--;
}
lef = st-;
}
}
else
{
scanf("%d",&z);
y++;
int ll,rr;
if(lef>rig)
{
ll = lef-z+;
rr = lef-y+;
}
else
{
ll = lef+y-;
rr = lef+z-;
}
cout<<query(ll,rr,,n,)<<endl;
}
}
return ;
}

Codeforces Round #263 (Div. 1)的更多相关文章

  1. 贪心 Codeforces Round #263 (Div. 2) C. Appleman and Toastman

    题目传送门 /* 贪心:每次把一个丢掉,选择最小的.累加求和,重复n-1次 */ /************************************************ Author :R ...

  2. Codeforces Round #263 (Div. 2)

    吐槽:一辈子要在DIV 2混了. A,B,C都是简单题,看AC人数就知道了. A:如果我们定义数组为N*N的话就不用考虑边界了 #include<iostream> #include &l ...

  3. Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】

    题意:给了一棵树以及每个节点的颜色,1代表黑,0代表白,求将这棵树拆成k棵树,使得每棵树恰好有一个黑色节点的方法数 解法:树形DP问题.定义: dp[u][0]表示以u为根的子树对父亲的贡献为0 dp ...

  4. Codeforces Round #263 (Div. 2) D. Appleman and Tree(树形DP)

    题目链接 D. Appleman and Tree time limit per test :2 seconds memory limit per test: 256 megabytes input ...

  5. Codeforces Round #263 (Div. 2) A B C

    题目链接 A. Appleman and Easy Task time limit per test:2 secondsmemory limit per test:256 megabytesinput ...

  6. Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper 树状数组暴力更新

    C. Appleman and a Sheet of Paper   Appleman has a very big sheet of paper. This sheet has a form of ...

  7. Codeforces Round #263 (Div. 2) proC

    题目: C. Appleman and Toastman time limit per test 2 seconds memory limit per test 256 megabytes input ...

  8. Codeforces Round #263 (Div. 2)C(贪心,联想到huffman算法)

    数学家伯利亚在<怎样解题>里说过的解题步骤第二步就是迅速想到与该题有关的原型题.(积累的重要性!) 对于这道题,可以发现其实和huffman算法的思想很相似(可能出题人就是照着改编的).当 ...

  9. Codeforces Round #263 (Div. 2) proB

    题目: B. Appleman and Card Game time limit per test 1 second memory limit per test 256 megabytes input ...

随机推荐

  1. 20145209&20145309信息安全系统设计基础实验报告 (3)

    实验内容.步骤与体会: 实验过程的理解,实验指导书中知识点的理解. (1)为什么在双击了GIVEIO和JTAG驱动安装文件后还需要手动进行配置? 因为安装文件只是将驱动文件释放了出来,并没有在系统中将 ...

  2. 【五子棋AI循序渐进】关于VCT,VCF的思考和核心代码

    前面几篇发布了一些有关五子棋的基本算法,其中有一些BUG也有很多值得再次思考的问题,在框架和效果上基本达到了一个简单的AI的水平,当然,我也是初学并没有掌握太多的高级技术.对于这个程序现在还在优化当中 ...

  3. vertical-align属性

    准备阶段 vertical-align取值及含义: 值 含义 baseline 默认.元素放置在父元素的基线上. top 把元素的顶端与行中最高元素的顶端对齐 text-top 把元素的顶端与父元素字 ...

  4. Go 模板

    原文链接 很多语言都有很多方式将字符串从一只形式转换成另一种形式.Go 使用模板的方法通过提供一个对象作为参数来转换字符串.这个一般来讲是用来将对象插入到HTML中的,不过它同样可以用在其他的情况下. ...

  5. discuz 二次开发

    discuz 框架也算是比较流行的社区论坛框架,discuz 的基础架构采用世界上最流行的 web 编程组合 PHP + MySQL 实现,是一个经过完善设计,适用于各种服务器环境的高效论坛系统解决方 ...

  6. Hibernate Validator验证标签说明

    Hibernate Validator是JSR-303的一个实现. 在FormBean里添加Hibernate Validator的注解,与定义一个校验类的做法相比.注解更加简洁.灵活. Bean V ...

  7. csuoj 1114: 平方根大搜索

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1114 1114: 平方根大搜索 Time Limit: 5 Sec  Memory Limit:  ...

  8. .NET WebForm简介

    WebForm简介 微软开发的一款产品,它将用户的请求和响应都封装为控件.让开发者认为自己是在操作一个windows界面.极大地提高了开发效率. C/S(客户端) 主要是在本机执行(每一个客户端是独立 ...

  9. MVC返回JSON数据格式书写方式

    返回json数据格式,多个返回值加,隔开 [Route("api/users/web")] //如果不加这个路由请这样调用:/api/users/web?schoolname=十五 ...

  10. -XX:+TraceClassLoading 监控类的加载

    -XX:+TraceClassLoading –监控类的加载 •[Loaded java.lang.Object from shared objects file] •[Loaded java.io. ...