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. 支付宝C# RSA签名 报系统找不到指定的文件问题解决

    做支付宝在线支付模块,安卓和苹果端都没为问题,服务器也能顺利的收到付款异步通知. 在做WEB端支付的时候遇到个奇怪的问题:本地localhost调试支付没问题,代码更新到服务器就出现了未将对象引用设置 ...

  2. java基础学习总结——基础语法2

    一.语句

  3. [Virtualization][qemu][kvm][virtio] 使用 QEMU/KVM 模拟网卡多队列

    序: 做DPDK例子的时候,发现一些例子需要多队列,而我当前所使用的虚拟机并不是多队列的.关于我当前虚拟机的状态,可以见前文. 所以,我的需求就是,让虚拟机里的网卡,有多队列! 参考: http:// ...

  4. 《Linux内核分析》第七周 可执行程序的装载

    [刘蔚然 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000] WEEK SEVEN ...

  5. Eclipse新增Web项目

    [前置条件] 1. 电脑已安装JDK1.6,并成功配置环境变量 2. 电脑已存在tomcat6.0包,无需安装 [操作步骤] 1. 为eclipse配置tomcat6.0 (1)eclipse菜单栏, ...

  6. 微软良心之作——Visual Studio Code 开源免费跨平台代码编辑器

    微软良心之作——Visual Studio Code 开源免费跨平台代码编辑器 在 Build 2015 大会上,微软除了发布了 Microsoft Edge 浏览器和新的 Windows 10 预览 ...

  7. C++之路进阶——codevs2492(上帝造题的七分钟 2)

    2492 上帝造题的七分钟 2  时间限制: 1 s  空间限制: 64000 KB  题目等级 : 大师 Master    题目描述 Description XLk觉得<上帝造题的七分钟&g ...

  8. 使用jquery的imagecropper插件做用户头像上传 兼容移动端

    在移动端开发的过程中,或许会遇到对图片裁剪的问题.当然遇到问题问题,不管你想什么方法都是要进行解决的,哪怕是丑点,难看点,都得去解决掉. 图片裁剪的jquery插件有很多,我也测试过很多,不过大多数都 ...

  9. java实现求数组中元素第二大的元素

    /** * 找出数组中数第二大的值 * @param array * @date 2016-9-25 * @author shaobn */ public static void getMethod_ ...

  10. C#中把Datatable转换为Json的5个代码实例

    一. /// <summary> /// Datatable转换为Json /// </summary> /// <param name="table" ...