Codeforces Round #263 (Div. 1)
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)的更多相关文章
- 贪心 Codeforces Round #263 (Div. 2) C. Appleman and Toastman
题目传送门 /* 贪心:每次把一个丢掉,选择最小的.累加求和,重复n-1次 */ /************************************************ Author :R ...
- Codeforces Round #263 (Div. 2)
吐槽:一辈子要在DIV 2混了. A,B,C都是简单题,看AC人数就知道了. A:如果我们定义数组为N*N的话就不用考虑边界了 #include<iostream> #include &l ...
- Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】
题意:给了一棵树以及每个节点的颜色,1代表黑,0代表白,求将这棵树拆成k棵树,使得每棵树恰好有一个黑色节点的方法数 解法:树形DP问题.定义: dp[u][0]表示以u为根的子树对父亲的贡献为0 dp ...
- 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 ...
- Codeforces Round #263 (Div. 2) A B C
题目链接 A. Appleman and Easy Task time limit per test:2 secondsmemory limit per test:256 megabytesinput ...
- 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 ...
- Codeforces Round #263 (Div. 2) proC
题目: C. Appleman and Toastman time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces Round #263 (Div. 2)C(贪心,联想到huffman算法)
数学家伯利亚在<怎样解题>里说过的解题步骤第二步就是迅速想到与该题有关的原型题.(积累的重要性!) 对于这道题,可以发现其实和huffman算法的思想很相似(可能出题人就是照着改编的).当 ...
- Codeforces Round #263 (Div. 2) proB
题目: B. Appleman and Card Game time limit per test 1 second memory limit per test 256 megabytes input ...
随机推荐
- wpf 属性变更通知接口 INotifyPropertyChanged
在wpf中将控件绑定到对象的属性时, 当对象的属性发生改变时必须通知控件作出相应的改变, 所以此对象需要实现 INotifyPropertyChanged 接口 例: //实现属性变更通知接口 INo ...
- 记录工作中用到的linux命令
日常工作中会对centos进行操作,总是会有一些常用的命令记不住,特开一贴,记录那些命令,学而时习之. RPM操作类命令: 查看RPM安装路径: 1.rpm -qa|grep Memcache ...
- html5 canvas标签
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 简单实用的双向电平转换电路(3.3v-5v)
当你使用3.3V的单片机的时候,电平转换就在所难免了,经常会遇到3.3转5V或者5V转3.3V的情况,这里介绍一个简单的电路,他可以实现两个电平的相互转换(注意是相互哦,双向的,不是单向的!).电路十 ...
- Simple JavaScript Inheritance--一个极简JS面向对象-类库
面向对象 面向对象思想的几个重要特征(针对类的要求): 抽象-封装.信息隐藏(将内部实现的方法和数据隐藏, 定义开放的接口) 继承-子类可以使用父类的资源,并可以定制自己的资源, 资源包括方法和数据 ...
- js 给样式添加随机颜色
下面提供了三种获取随机颜色值的方法 方法一: 创建一个颜色 HEX 值数组,然后随机抽取这个数组里6个值,组合生成颜色. function color1(){ var color = "&q ...
- [原创]java WEB学习笔记102:Spring学习---Spring Bean配置:bean配置方式(工厂方法(静态工厂方法 & 实例工厂方法)、FactoryBean) 全类名
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- js数组去重的4个方法
面试前端必须准备的一个问题:怎样去掉Javascript的Array的重复项, 这个问题看起来简单,但是其实暗藏杀机. 考的不仅仅是实现这个功能,更能看出你对计算机程序执行的深入理解. 我总共总结4种 ...
- -XX:+TraceClassLoading 监控类的加载
-XX:+TraceClassLoading –监控类的加载 •[Loaded java.lang.Object from shared objects file] •[Loaded java.io. ...
- 十九、Java基础--------IO流之字节流
在上一篇文章中介绍了IO体系以及一些基本概念和字符流的相关应用,本文着重介绍字节流的相关操作. 字节流 它的操作与字符流类似,可以参与字符流的定义.读取.写入.处理异常的格式,只不过是处理的数据不同, ...