Hdu4605magic Ball Game Tree Array

Source: Internet
Author: User

Give a tree. The number of child nodes for each node of the tree is 0 or 2
There is a weight for each node W[i]
A ball with a weight of x is in the case of each node
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 of each weight being x falling to a node V


The path to a tree from one point to the next is OK, just record the 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]);
? ? ? ? }
? ? }
}

































































Hdu4605magic Ball Game Tree Array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.