Multi-school combination of F Magic Ball Game (hdu 4605)

Source: Internet
Author: User

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


Problem Description
When the magic ball game turns up, Kimi immediately falls in it. the interesting game is made up of N bils, each with a weight of w [I]. these N ballform 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 bils, 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 bils, 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 ≤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.
 

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 7x/2y. you're only required to output the x and y for each query, separated by a blank. each answer shocould be put down in one line.
 

 

Idea: first consider a query (V, X). If a node with the weight of X already exists in the path from the root node to the V node (excluding V, the ball cannot reach the V node. Otherwise, the path to the left is defined as "Left path", and the path to the right is called "right path ". Set lma, rmi, and rma to indicate the number of nodes smaller than X in the left path, the number of nodes larger than X in the left path, and the number of nodes smaller than X in the right path, respectively, if the right path is larger than the number of X nodes, the final answer is (1/2) ^ (lma + rma) * (7/8) ^ (rmi) * (1/8) ^ (LMS ). That is, output rmi and 3 * (lmi + rmi) + (lma + rma.

For this question, we can process the query on each node offline and perform a DFS from the root node. In the process, each query passing through a node will process the query corresponding to the node, we can use data structures such as line tree and tree array to record all the weights (excluding V) in the path from the root node to V, and then we can get the lma, rmi, rma, the rest is a better answer. In addition, X is very large and requires discretization.

The Code is as follows:

 

# 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) // If x {ans [num] [0] =-1;} else {int lma, lma, rma, rmi; // The value on the left is greater than that on the left, the right side is greater than, and the right side is less than lsum = 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,); dfs (node [now]. left); addnum (w,-);} if (node [now]. right! =-1) {addnum (w, 1, 1); dfs (node [now]. right); addnum (w,-);} 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", & a, & B, & cc); add (a, B, cc ); vis [B] = vis [cc] = 1;} scanf ("% D", & q); for (I = 1; I <= q; I ++) {scanf ("% 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 \ n ", ans [I] [0], ans [I] [1]) ;}} return 0 ;}# 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) ;}} in T 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) // If x {ans [num] [0] =-1;} else {int lma, lma, rma, rmi; // The value on the left is greater than that on the left, right side greater than, right side less than LR = 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 + lm) + rma + lma;} if (node [now]. left! =-1) {addnum (w,); dfs (node [now]. left); addnum (w,-);} if (node [now]. right! =-1) {addnum (w, 1, 1); dfs (node [now]. right); addnum (w,-);} 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", & a, & B, & cc); add (a, B, cc ); vis [B] = vis [cc] = 1;} scanf ("% D ", & q); for (I = 1; I <= q; I ++) {scanf (" % 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 \ n ", ans [I] [0], ans [I] [1]) ;}} return 0 ;}

 

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.