To a tree, the number of child nodes in each node of a tree is 0 or 2
For each node there is a weight of w[i]
A weight of x ball in each node case has
X=w[i] This ball doesn't fall down at that point.
X<w[i] The probability of the ball dropping to the left and right nodes is 1/2.
X>w[i] The probability that the ball is dropped to the left node is 1/8, and the probability to the right is 7/8.
Q The probability that each weight is X is dropped to a node V
For a tree from one point to the other the path is determined, just to record this path
The number of nodes that are greater than x in the path to the left L_GRT, the number of nodes less than x l_less
To the right, the number of nodes greater than x R_GRT, the number of nodes less than x r_less
#pragma COMMENT (linker, "/stack:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace Std;
const int MAXN = 1E6+10;
int map[maxn][2];
Vector<int> VEC[MAXN];
int X[MAXN], Y[MAXN];
int ROOT[MAXN];
int W_X[MAXN];
int W[MAXN];
int A[MAXN]; int len;
int TREE[2][MAXN];
void update (int x, int flag, int dx)
{
while (x < MAXN)
{
TREE[FLAG][X] + = DX;
x + = x& (-X);
}
}
int getsum (int x, int flag)
{
int sum = 0;
while (x)
{
Sum + = tree[flag][x];
X-= x& (-X);
}
return sum;
}
void Dfs (int u)
{
cout<<u<<endl;
for (int i = 0;i < Vec[u].size (); i++)
{
int id = vec[u][i];
int pos = Lower_bound (A, A + len-1, W_x[id])-A + 1;
int l_less = getsum (pos-1, 0);
int r_less = Getsum (pos-1, 1);
int l_all = getsum (len, 0);
int r_all = Getsum (len, 1);
int l_grt = l_all-getsum (pos, 0);
int r_grt = R_all-getsum (pos, 1);
if (l_less + r_less + l_grt + r_grt! = L_all + r_all)
{
X[id] = Y[id] =-1;
Continue;
}
X[id] = r_less;
Y[id] = l_grt + R_grt + (l_less + r_less);
}
for (int i = 0;i < 2;i++)
{
if (!map[u][i]) continue;
int v = map[u][i];
int pos = Lower_bound (A, A + len-1, W[u])-A + 1;
Update (POS, I, 1);
DFS (v);
Update (POS, I,-1);
}
}
int main ()
{
Freopen ("In.txt", "R", stdin);
int T;
int N; int M, Q;
scanf ("%d", &t);
while (t--)
{
scanf ("%d", &n);
len = 0;
for (int i = 1;i <= n;i++)
{
scanf ("%d", &w[i]);
a[len++] = W[i];
}
Sort (A, a + N);
Len = Unique (A, A + len)-A;
scanf ("%d", &m);
memset (map, 0, sizeof (map));
memset (root, 0, sizeof (root));
while (m--)
{
int U, l, R;
scanf ("%d%d%d", &u, &l, &r);
Map[u][0] = l; MAP[U][1] = r;
ROOT[L] = root[r] = 1;
}
scanf ("%d", &q);
for (int i = 1;i <= Q; i++)
{
int V;
scanf ("%d%d", &v, &w_x[i]);
Vec[v].push_back (i);
}
int POS;
for (int i = 1;i <= n;i++)
if (!root[i]) {pos = i; break;}
DFS (POS);
for (int i = 1;i <= q;i++)
{
if (X[i]==-1) puts ("0");
else printf ("%d%d\n", X[i], y[i]);
}
}
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdu4605magic Ball Game Tree Array