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 ...
随机推荐
- python3中urllib2的问题
import urllib from urllib import request a = urllib.request.Request(url) b = urllib.request.urlopen( ...
- zepto源码--核心方法5(文本操作)--学习笔记
涉及到文本内容的主要有三个函数:html, text, val. 我们已经见过多次,一个函数多种用途的情况,今天这三个函数也不例外,既可以获取内容,也可以设置内容.判断条件就是有没有传入参数,如果没有 ...
- 在Altium_Designer_PCB_中插入图片的方法
详细请看PDF: http://files.cnblogs.com/files/BinB-W/在Altium_Designer_PCB_中插入图片的方法.pdf 配套文件: http://files. ...
- iOS:一些常用的框架
一.更多框架请看链接: https://github.com/xiayuanquan/TimLiu-iOS http://www.cnblogs.com/XYQ-208910/p/5901012.ht ...
- Android应用程序窗口(Activity)与WindowManagerService服务的连接过程分析
在前两文中,我们分析了Activity组件的窗口对象和视图对象的创建过程.Activity组件在其窗口对象和视图对象创建完成之后,就会请求与WindowManagerService建立一个连接,即请求 ...
- (转载)throw和throws的区别
1.throw:(针对对象的做法)抛出一个异常,可以是系统定义的,也可以是自己定义的.下面举两个例子:抛出Java中的一个系统异常:public class One {public void yich ...
- 字节流和字符流(BufferedReader类和BufferedWriter类)
一般情况下,为了提高字符文件读/写效率,通常需要为文件读/写器添加一个缓冲读/写器,分别为BufferedReader类和BufferedWriter类. 1:BufferedReader类 假如上面 ...
- delphi TIdHTTP Post乱码问题
这里主要说的是中文乱码的问题 1. 发过去的是乱码如下处理, 服务器采用的是UFT-8编码的情况下 uses HTTPApp; sPost := HTTPEncode(UTF8Encode ...
- List,Set,Map用法以及区别(转)
Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素.一些Collection允许相同的元素而另一些不行.一些能排序而另一些不行.Java ...
- js处理异常try{}catch(e){}
MXS&Vincene ─╄OvЁ &0000021─╄OvЁ MXS&Vincene MXS&Vincene ─╄OvЁ:今天很残酷,明天更残酷,后天很美好, ...