HDU 4602 Magic Ball Game(离线处理,树状数组,dfs)
Magic Ball Game
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 640 Accepted Submission(s): 169
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≤105), 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] ≤ 109, 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≤105). The following Q lines, each with two integers v and X(1≤v≤N,1≤X≤109), describe all the queries.
3
2 3 1
1
1 2 3
3
3 2
1 1
3 4
0 0
1 3
离线之后,对树进行一次dfs.
用两个树状数组,分别代表往左和往右的节点的值。
递归dfs的,会爆栈,手动加栈后,只有C++可以过。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std; const int MAXN = ;
int next[MAXN][];
int n;
int root;
int w[MAXN];
struct QQ
{
int v;
int X;
int ans1,ans2;
}Query[MAXN];
vector<int>vec[MAXN];
bool used[MAXN];
int a[MAXN];
map<int,int>mp; int c1[MAXN];
int c2[MAXN];
int t;
int lowbit(int x)
{
return x&(-x);
}
void add1(int i,int val)
{
while(i <= t)
{
c1[i] += val;
i += lowbit(i);
}
}
int sum1(int i)
{
int s = ;
while(i > )
{
s += c1[i];
i -= lowbit(i);
}
return s;
}
void add2(int i,int val)
{
while(i <= t)
{
c2[i] += val;
i += lowbit(i);
}
}
int sum2(int i)
{
int s = ;
while(i > )
{
s += c2[i];
i -= lowbit(i);
}
return s;
} void dfs(int u)
{
int sz = vec[u].size();
for(int i = ;i < sz;i++)
{
int id = vec[u][i];
int X = mp[Query[id].X];
if(sum1(X)-sum1(X-)!= || sum2(X)-sum2(X-)!=)
{
Query[id].ans1 = Query[id].ans2 = -;
}
else
{
Query[id].ans1 = Query[id].ans2 = ;
Query[id].ans2 += *sum1(X-)+sum1(t)-sum1(X);
Query[id].ans1 += sum2(X-);
Query[id].ans2 += *sum2(X-)+sum2(t)-sum2(X);
}
}
if(next[u][]== && next[u][]==)return;
add1(mp[w[u]],);
dfs(next[u][]);
add1(mp[w[u]],-);
add2(mp[w[u]],);
dfs(next[u][]);
add2(mp[w[u]],-);
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
int m;
int u,x,y;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i = ;i <= n;i++)
{
used[i] = false;
next[i][] = next[i][] = ;
vec[i].clear();
}
t = ;
for(int i = ;i <= n;i++)
{
scanf("%d",&w[i]);
a[t++] = w[i];
}
scanf("%d",&m);
while(m--)
{
scanf("%d%d%d",&u,&x,&y);
used[x] = true;
used[y] = true;
next[u][] = x;
next[u][] = y;
}
scanf("%d",&m);
for(int i = ;i < m;i++)
{
scanf("%d%d",&u,&x);
Query[i].v = u;
Query[i].X = x;
a[t++] = x;
vec[u].push_back(i);
}
for(int i = ;i <= n;i++)
if(!used[i])
{
root = i;
break;
}
sort(a,a+t);
t = unique(a,a+t)-a;
mp.clear();
for(int i = ;i < t;i++)
mp[a[i]]=i+;
memset(c1,,sizeof(c1));
memset(c2,,sizeof(c2));
dfs(root);
for(int i = ;i < m;i++)
{
if(Query[i].ans1 == -)
printf("0\n");
else printf("%d %d\n",Query[i].ans1,Query[i].ans2);
}
}
return ;
}
HDU 4602 Magic Ball Game(离线处理,树状数组,dfs)的更多相关文章
- 【loj6041】「雅礼集训 2017 Day7」事情的相似度 后缀自动机+STL-set+启发式合并+离线+扫描线+树状数组
题目描述 给你一个长度为 $n$ 的01串,$m$ 次询问,每次询问给出 $l$ .$r$ ,求从 $[l,r]$ 中选出两个不同的前缀的最长公共后缀长度的最大值. $n,m\le 10^5$ 题解 ...
- 【bzoj4540】[Hnoi2016]序列 单调栈+离线+扫描线+树状数组区间修改区间查询
题目描述 给出一个序列,多次询问一个区间的所有子区间最小值之和. 输入 输入文件的第一行包含两个整数n和q,分别代表序列长度和询问数.接下来一行,包含n个整数,以空格隔开,第i个整数为ai,即序列第i ...
- hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)
hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...
- 【BZOJ】2434: [Noi2011]阿狸的打字机 AC自动机+树状数组+DFS序
[题意]阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的: l 输入小写 ...
- hdu 4605 Magic Ball Game (在线主席树/离线树状数组)
版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4605 题意: 有一颗树,根节点为1,每一个节点要么有两个子节点,要么没有,每个节点都有一个权值wi .然后,有一个球,附带值x . 球 ...
- hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 4746 莫比乌斯反演+离线查询+树状数组
题目大意: 一个数字组成一堆素因子的乘积,如果一个数字的素因子个数(同样的素因子也要多次计数)小于等于P,那么就称这个数是P的幸运数 多次询问1<=x<=n,1<=y<=m,P ...
- HDU 4638 Group (2013多校4 1007 离线处理+树状数组)
Group Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- hdu-3333 Turing Tree 离线区间+树状数组(区间不同数的和)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3333 题目大意: 给出一数组,以及m个查询区间,每次查询该区间不同数字的和.相同数字只加一次. 解题 ...
随机推荐
- D:/apache2/conf/httpd.conf:Cannot load D:/apache2/modules/mod_actions.so
报错如下: errors reported here must be corrected before service can be started.httpd:Syntax error on lin ...
- HDU 4324 (拓扑排序) Triangle LOVE
因为题目说了,两个人之间总有一个人喜欢另一个人,而且不会有两个人互相喜欢.所以只要所给的图中有一个环,那么一定存在一个三元环. 所以用拓扑排序判断一下图中是否有环就行了. #include <c ...
- 51nod1431 快乐排队
神???.我们可以发现无论怎么交换ai+i都是不变的.那么这样就可以了 #include<cstdio> #include<cstring> #include<cctyp ...
- asp.net限时发送手机验证码
html代码 <p> <strong>手机验证码:</strong> <asp:TextBox ID="code" runat=" ...
- Jquery源码中的Javascript基础知识(二)
接上一篇,jquery源码的这种写法叫做匿名函数自执行 (function( window, undefined ) { // code })( window ); 函数定义了两个参数window和u ...
- swift2.0 Cannot assign a value of type '[CFString]' to a value of type '[String]'
Cannot assign a value of type '[CFString]' to a value of type '[String]' 代码示例如下: picker.mediaTypes = ...
- 8个必备的PHP功能开发 (转)
做过PHP开发的程序员应该清楚,PHP中有很多内置的功能,掌握了它们,可以帮助你在做PHP开发时更加得心应手,本文将分享8个开发必备的PHP功能,个个都非常实用,希望各位PHP开发者能够掌握. 1.传 ...
- xp关不了机
自己测试:按第2点的设置既可成功1.点“开始→设置→控制面板→电源选项→高级电源管理”,如果你的机器支持高级电源管理功能,则选中“启用高级电源管理支持”:2.单击“开始”,并运行“regedit”,然 ...
- 剖析OkHttp缓存机制
现在应用市场上的 App 无一不需要网络操作,这些应用的开发者大多数都选择结合使用 OkHttp 和 Retrofit 来完成网络操作.okHttp 最为人称道的一个特性就是它的缓存机制,而我将在本篇 ...
- Node.js的循环依赖
我们知道在实际编程过程中,要尽可能的减少或者规避循环依赖情况的发生.但在现实环境中,有时却不得不产生循环依赖.Node.js不提倡使用循环依赖,但真有如此情况发生时Node.js也有办法解决.这篇博文 ...