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


Problem Description
When the magic ball game turns up, Kimi immediately falls in it. The interesting game is made up of N balls, each with a weight of w[i]. These N balls form a rooted tree, with the 1st ball as the root. Any ball in the game has either 0 or 2 children ball. If a node has 2 children balls, we may define one as the left child and the other as the right child.
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.
 

 

Input
The input contains several test cases. An integer T(T≤15) will exist in the first line of input, indicating the number of test cases.
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.
 

 

Output
If the magic ball is impossible to arrive at node v, output a single 0. Otherwise, you may easily find that the answer will be in the format of 7 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.
 

 

Sample Input
1 3 2 3 1 1 1 2 3 3 3 2 1 1 3 4
 

 

Sample Output
0 0 0 1 3
 

 

Source
 

 

Recommend
liuyiding
 

 

 

 

 

 

离线之后,对树进行一次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 = 400010;

int next[MAXN][2];

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 = 0;

    while(i > 0)

    {

        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 = 0;

    while(i > 0)

    {

        s += c2[i];

        i -= lowbit(i);

    }

    return s;

}



void dfs(int u)

{

    int sz = vec[u].size();

    for(int i = 0;i < sz;i++)

    {

        int id = vec[u][i];

        int X = mp[Query[id].X];

        if(sum1(X)-sum1(X-1)!=0 || sum2(X)-sum2(X-1)!=0)

        {

            Query[id].ans1 = Query[id].ans2 = -1;

        }

        else

        {

            Query[id].ans1 = Query[id].ans2 = 0;

            Query[id].ans2 += 3*sum1(X-1)+sum1(t)-sum1(X);

            Query[id].ans1 += sum2(X-1);

            Query[id].ans2 += 3*sum2(X-1)+sum2(t)-sum2(X);

        }

    }

    if(next[u][0]==0 && next[u][1]==0)return;

    add1(mp[w[u]],1);

    dfs(next[u][0]);

    add1(mp[w[u]],-1);

    add2(mp[w[u]],1);

    dfs(next[u][1]);

    add2(mp[w[u]],-1);

}



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 = 1;i <= n;i++)

        {

            used[i] = false;

            next[i][0] = next[i][1] = 0;

            vec[i].clear();

        }

        t = 0;

        for(int i = 1;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][0] = x;

            next[u][1] = y;

        }

        scanf("%d",&m);

        for(int i = 0;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 = 1;i <= n;i++)

            if(!used[i])

            {

                root = i;

                break;

            }

        sort(a,a+t);

        t = unique(a,a+t)-a;

        mp.clear();

        for(int i = 0;i < t;i++)

            mp[a[i]]=i+1;

        memset(c1,0,sizeof(c1));

        memset(c2,0,sizeof(c2));

        dfs(root);

        for(int i = 0;i < m;i++)

        {

            if(Query[i].ans1 == -1)

                printf("0\n");

            else printf("%d %d\n",Query[i].ans1,Query[i].ans2);

        }

    }

    return 0;

}

 

 

你可能感兴趣的:(game)