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. win7下用mklink命令解决delphiXE系列占用C盘空间的问题

    DelphiXE从2010以后, 安装程序安装完成后都会在ProgramData目录里复制一份安装程序的备份, 随着版本升级安装包越来越大, 占用C盘的空间也就越来越大 虽然可以通过删除的方式删掉, ...

  2. js鲸鱼

    <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  3. asp.net mvc 控制器中操作方法重载问题 解决

    Controllers: public ActionResult Index() { return View(db.GuestBooks.ToList()); } // // GET: /Guest2 ...

  4. reporting service & wpf

    WPF做Windows程序界面很好很强大,RDLC做报表免费又好用,如何将两者强强联合呢?   方法1:    可以直接在WPF项目中加入一个WinForm窗体,在这个窗体上进行报表操作,与一般的Wi ...

  5. zepto源码--核心方法9(管理包装集)--学习笔记

    今天介绍的是与子元素相关的函数,children, find, contents children 从源码来看,主要是调用过滤函数filtered对遍历整个包装集返回的children进行过滤. 仔细 ...

  6. iOS下Html页面中input获取焦点弹出键盘时挡住input解决方案—scrollIntoView()

    问题描述 iOS系统下,移动web页面,inpu获取焦点弹出系统虚拟键盘时,偶尔会出现挡住input的情况,尽管概率不大,但是十分影响用户体验. 问题重现 原始页面:页面中有header.main.f ...

  7. 20145320 《Java程序设计》第5周学习总结

    20145320 <Java程序设计>第5周学习总结 教材学习内容总结 8.1 语法与继承架构 try.catch Java中的错误会被包装为对象,而使用try与catch,JVM会执行t ...

  8. NFC基础

    本文档描述了在Android执行的基本的NFC技术,它说明了如何发送和接收NDEF消息的形式的NFC数据,并介绍Android框架中支持这些功能的API,对于更高级的主题,包括讨论非NDEF数据相关的 ...

  9. 我去,徒弟半夜来电让写一个PHP短信验证(和群发)

    感觉很纳闷啊,,..好几天几乎通宵了,今晚本来以为有个早觉睡,居然2点多才打电话来让帮忙... 记得前段时间还有博友问过同类的问题.... 名字我就隐藏掉了,呵呵,, 我在网上随便找了一个提供相应接口 ...

  10. android消息处理机制之2handler与looper,MessageQueue:的关系

    // Looper: 在UI主线程里面有默认有一个Looper对象来管理UI线程的各条消息,但是在自定义的实现Thread的消息循环和消息派发,缺省情况下Thread是没有这个消息循环的既没有Loop ...