2013 多校联合 F Magic Ball Game (hdu 4605)
http://acm.hdu.edu.cn/showproblem.php?pid=4605
Magic Ball Game
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 309 Accepted Submission(s): 73
The rules are simple: when Kimi decides to drop a magic ball with a weight of X, the ball goes down through the tree from the root. When the magic ball arrives at a node in the tree, there's a possibility to be catched and stop rolling, or continue to roll down left or right. The game ends when the ball stops, and the final score of the game depends on the node at which it stops.
After a long-time playing, Kimi now find out the key of the game. When the magic ball arrives at node u weighting w[u], it follows the laws below:
1 If X=w[u] or node u has no children balls, the magic ball stops.
2 If X<w[u], there's a possibility of 1/2 for the magic ball to roll down either left or right.
3 If X>w[u], the magic ball will roll down to its left child in a possibility of 1/8, while the possibility of rolling down right is 7/8.
In order to choose the right magic ball and achieve the goal, Kimi wonders what's the possibility for a magic ball with a weight of X to go past node v. No matter how the magic ball rolls down, it counts if node v exists on the path that the magic ball goes along.
Manual calculating is fun, but programmers have their ways to reach the answer. Now given the tree in the game and all Kimi's queries, you're required to answer the possibility he wonders.
Each test case begins with an integer N(1≤N≤10
5), indicating the number of nodes in the tree. The following line contains N integers w[i], indicating the weight of each node in the tree. (1 ≤ i ≤ N, 1 ≤ w[i] ≤ 10
9, N is odd)
The following line contains the number of relationships M. The next M lines, each with three integers u,a and b(1≤u,a,b≤N), denotes that node a and b are respectively the left child and right child of node u. You may assume the tree contains exactly N nodes and (N-1) edges.
The next line gives the number of queries Q(1≤Q≤10
5). The following Q lines, each with two integers v and X(1≤v≤N,1≤X≤10
9), describe all the queries.
x/2
y . You're only required to output the x and y for each query, separated by a blank. Each answer should be put down in one line.
思路: 首先考虑一个询问(V,X),若从根节点到V节点的路径上(不包括V)已存在权值为X的节点,则小球不可能到达V节点,否则,不妨定义往左走的路径为“左路径”,往右走的路径称为“右路径”。设lmi,lma,rmi,rma分别表示左路径上小于X的节点数,左路径上大于X的节点数,右路径上小于X的节点数,右路径上大于X的节点数,则最终的答案即为: (1/2)^(lma+rma)*(7/8)^(rmi)*(1/8)^(lmi)。即输出 rmi 和 3*(lmi+rmi)+(lma+rma)即可。
对于本题,我们可以离线处理每个节点上的询问,从根节点做一次DFS,在过程中每经过一个节点就处理该节点所对应的询问,我们可以用线段树,树状数组等数据结构记录从根节点到V的路径上所有的权值(不包括V),然后就可以得到lmi,lma,rmi,rma,剩下的就是更行答案了,另外X很大,需要离散化处理。
代码如下:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
#define maxn 100010
#define mid ((t[p].l+t[p].r)>>1)
#define ls (p<<1)
#define rs (ls|1)
using namespace std;
struct Tree
{
int w;
int left,right;
}node[maxn];
vector<int> vec[maxn];
int c[maxn<<1][2];
void init(int n)
{
for(int i=0;i<=n;i++)
{
vec[i].clear();
node[i].left=node[i].right=node[i].w=-1;
}
}
void add(int a,int b,int c)
{
node[a].left=b;
node[a].right=c;
}
int ans[maxn][2],vis[maxn],po[maxn<<1],len; int lowbit(int x)
{
return x&(-x);
}
void addnum(int x,int val,int tt)
{
while(x<=len)
{
c[x][tt]+=val;
x+=lowbit(x);
}
}
int getsum(int x,int tt)
{
int sum=0;
while(x>0)
{
sum+=c[x][tt];
x-=lowbit(x);
}
return sum;
}
int search(int len,int x)
{
int mi=1,ma=len,Mid;
while(mi<=ma)
{
Mid=(mi+ma)>>1;
if(po[Mid]==x)
return Mid;
if(po[Mid]<x)
mi=Mid+1;
else
ma=Mid-1;
}
}
void dfs(int now)
{
int i,w=search(len,node[now].w);
for(i=0;i<vec[now].size();i+=2)
{
int x=search(len,vec[now][i]),num=vec[now][i+1];
if(getsum(x,0)-getsum(x-1,0)>0||getsum(x,1)-getsum(x-1,1)>0)//如果已经存在x
{
ans[num][0]=-1;
}
else
{
int lma,lmi,rma,rmi;// 左边大于,左边小于,右边大于,右边小于
lmi=getsum(x-1,0);
lma=getsum(len,0)-getsum(x,0);
rmi=getsum(x-1,1);
rma=getsum(len,1)-getsum(x,1);
ans[num][0]=rmi;
ans[num][1]=3*(rmi+lmi)+rma+lma;
}
}
if(node[now].left!=-1)
{
addnum(w,1,0);
dfs(node[now].left);
addnum(w,-1,0);
}
if(node[now].right!=-1)
{
addnum(w,1,1);
dfs(node[now].right);
addnum(w,-1,1);
}
}
int main()
{
//freopen("dd.txt","r",stdin);
int ncase;
scanf("%d",&ncase);
while(ncase--)
{
int n,i,m,q,x,v;
scanf("%d",&n);
init(n);
for(i=1;i<=n;i++)
{
scanf("%d",&node[i].w);
po[i]=node[i].w;
}
scanf("%d",&m);
int a,b,cc;
memset(vis,0,sizeof(vis));
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&cc);
add(a,b,cc);
vis[b]=vis[cc]=1;
}
scanf("%d",&q);
for(i=1;i<=q;i++)
{
scanf("%d%d",&v,&x);
vec[v].push_back(x);
vec[v].push_back(i);
po[i+n]=x;
}
sort(po+1,po+q+n+1);
len=unique(po+1,po+q+n+1)-(po+1);
int root;
for(i=1;i<=n;i++)
{
if(!vis[i])
{
root=i;
break;
}
}
for(i=0;i<=len;i++)
c[i][0]=c[i][1]=0;
dfs(root);
for(i=1;i<=q;i++)
{
if(ans[i][0]==-1)
printf("0\n");
else
printf("%d %d\n",ans[i][0],ans[i][1]);
}
}
return 0;
}
2013 多校联合 F Magic Ball Game (hdu 4605)的更多相关文章
- 2013 多校联合 2 A Balls Rearrangement (hdu 4611)
Balls Rearrangement Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- 2013 多校联合2 D Vases and Flowers (hdu 4614)
Vases and Flowers Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others ...
- 2013多校联合2 I Warm up 2(hdu 4619)
Warm up 2 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total ...
- 2013多校联合3 G The Unsolvable Problem(hdu 4627)
2013-07-30 20:35 388人阅读 评论(0) 收藏 举报 http://acm.hdu.edu.cn/showproblem.php?pid=4627 The Unsolvable Pr ...
- 2017ACM暑期多校联合训练 - Team 8 1002 HDU 6134 Battlestation Operational (数论 莫比乌斯反演)
题目链接 Problem Description The Death Star, known officially as the DS-1 Orbital Battle Station, also k ...
- 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)
题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...
- 2017ACM暑期多校联合训练 - Team 7 1002 HDU 6121 Build a tree (深搜+思维)
题目链接 Problem Description HazelFan wants to build a rooted tree. The tree has n nodes labeled 0 to n− ...
- 2017ACM暑期多校联合训练 - Team 6 1001 HDU 6096 String (字符串处理 字典树)
题目链接 Problem Description Bob has a dictionary with N words in it. Now there is a list of words in wh ...
- 2017ACM暑期多校联合训练 - Team 6 1010 HDU 6105 Gameia (博弈)
题目链接 Problem Description Alice and Bob are playing a game called 'Gameia ? Gameia !'. The game goes ...
随机推荐
- eclipse中调出android sdk manager和android virtual device manager图标
有时候在安装ADT插件后,eclipse菜单栏上不会显示android sdk manager和android virtual device manager两个图标, 这个时候,如果安装ADT插件的步 ...
- javascript函数值的重写
原文:javascript函数值的重写 javascript函数值的重写 定义了一个函数,需要重写这个函数并使用原先的函数值.做法是: 1.定义一个变量让原先函数的值指向它,把原先函数的指向一个新的函 ...
- ar解压deb包
解压文件 ar -x libstdc++6_4.7.2-5_i386.deb tar -zxvf data.tar.gz
- Android 它们的定义View (一)
转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/24252901 非常Android入门程序员AndroidView.可能都是比較恐 ...
- 联想A208T ROOT
前几天,一直陪伴我的海信T89电池挂掉...胀胀的肚子...真可怜 一时之间找不到配套的电池,就借了个手机先用着,就是这 联想A208T ROOT... 话说的配置一般般,勉强够我这样的不怎么玩手机的 ...
- Qt on Android:QTableView不显示选中虚框
在使用 QTableView 或 QTableWidget 时.有时我们不想要选中虚框,能够实现一个 ItemDelegate ,重写 drawFocus() 和 drawCheck() 两个虚函数 ...
- .NET程序员生活开始
不知不觉,开始踏入程序员生活了!加油吧.有关Session的好文章: 最近这两天被一个Web Farm环境下的Session处理问题虐得很痛苦,网上到处找解决方案,在无意中翻看到这篇文章,感觉很不错, ...
- 第9课_2_dbsoft安装
三 安装Oracle Database 数据库软件 1.上传数据库安装包到虚拟机上,unzip命令解压database软件,赋予正确的权限和属主,在以oracle身份登录进行图形界面安装 unzip ...
- log4j日志输出配置
# Configure logging for testing: optionally with log filelog4j.rootLogger=WARN, stdoutlog4j.rootLogg ...
- 加密传输SSL协议3_非对称加密
困死了,这里对非对称加密体系开个头,具体的实验明天写 非对称加密体系 为了解决对称加密中密钥的传输的问题,一些天才的数学家就提出了非对称式的加密体系,也称为公钥加密体系. 加密和解密的密钥是不同的.一 ...