Topic Link: POINT here!!!
Test instructions
Given a tree of N (n<=1e5) nodes, each node has either two sons (about two sons) or no sons, each node has a weight of w[i].
A weight of X-ball begins to fall from the root node, each time it falls to a node,
1. If x=w[i], or no son node, the ball stops falling.
2. If X<w[i], the ball each has a 1/2 probability of falling to the left and right son node.
3. If X>w[i], the ball has a 1/8 probability of falling to the left son, 7/8 of the probability of falling to the right son.
I'll give you a Q (Q<=1E5) inquiry which contains v,x. Ask you what the probability of the ball falling from the root node (the root node is 1) to the V node is the value of X. (No output 0, otherwise the probability is expressed in 7^x/2^y, that is, output x and Y can be.) )
Exercises
1, this problem we can be offline for all inquiries, the equivalent of the current node to the root node of the path on the number is greater than it, how much less than it, how many numbers equal to it, we can directly use tree-like tree-like implementation.
2, starting from the root node Dfs, when reaching the X node, take out the query, the tree array is stored in the root node to father[x] weight, I can quickly find out where the greater than, equal to, less than its number.
3, this problem is OK, specific see Code implementation.
Code:
#include <cstdio> #include <cstring> #include <iostream> #include <sstream> #include < algorithm> #include <vector> #include <bitset> #include <set> #include <queue> #include < stack> #include <map> #include <cstdlib> #include <cmath> #define PI 2*asin (1.0) #define LL Long long# Define PB push_back#define pa pair<int,int> #define CLR (A, B) memset (A,b,sizeof (a)) #define Lson lr<<1,l,mid# Define Rson Lr<<1|1,mid+1,r#define Bug (x) printf ("%d++++++++++++++++++++%d\n", x,x) #define Key_value Ch[ch[root ][1]][0]c:\program files\git\binconst int MOD = 1e9+7;const LL N = 2e5+15;const int MAXN = 5e5+15;const int letter = 130; const int INF = 1e17;const double Pi=acos ( -1.0); const double eps=1e-10;using namespace Std;inline int read () {int x=0,f =1;char Ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} int w[n],n,m,q,ps[n],cnt;int c[2][n],ans[n][2];vector<int>g[n];vector<pa>que[n];void init () {cnt=0; CLR (c,0), CLR (ans,0); for (int i=0;i<n;i++) g[i].clear (), Que[i].clear ();} int lowbit (int x) {return x& (-X);} void update (int i,int val,int f) {for (int x=i;x<=cnt;x+=lowbit (x)) C[f][x]+=val;} int getsum (int i,int f) {int ans=0; for (int x=i;x>0;x-=lowbit (x)) ans+=c[f][x]; return ans;} void Dfs (int x) {for (int i=0;i<que[x].size (); i++) {int id=que[x][i].first,pos=que[x][i].second; Pos=lower_bound (Ps+1,ps+cnt+1,pos)-ps; 0 Zuo 1you int lqb=getsum (cnt,0); int rqb=getsum (cnt,1); int lx=getsum (pos-1,0); Xiaoyu int rx=getsum (pos-1,1); int ld=lqb-getsum (pos,0); int rd=rqb-getsum (pos,1); if (LX+LD+RX+RD!=LQB+RQB) {ans[id][0]=-1; Continue } Ans[id][0]=rx; Ans[id][1]=lx*3+rx*3+ld+rd; } for (int i=0;i<g[x].size (); i++) {int Vs=lower_bound (ps+1,ps+cNT+1,W[X])-ps; Update (VS,1,I); DFS (G[x][i]); Update (VS,-1,I); }}int Main () {int T; scanf ("%d", &t); while (t--) {init (); scanf ("%d", &n); for (int i=1;i<=n;i++) scanf ("%d", w+i), ps[++cnt]=w[i]; scanf ("%d", &m); for (int i=0;i<m;i++) {int u,a,b; scanf ("%d%d%d", &u,&a,&b); G[U].PB (a), G[U].PB (b); } scanf ("%d", &q); for (int i=1;i<=q;i++) {int id,x; scanf ("%d%d", &id,&x); QUE[ID].PB (Make_pair (i,x)); Ps[++cnt]=x; } sort (ps+1,ps+cnt+1); Cnt=unique (ps+1,ps+cnt+1)-(ps+1); DFS (1); for (int i=1;i<=q;i++) {if (Ans[i][0]==-1) puts ("0"); else printf ("%d%d\n", ans[i][0],ans[i][1]); }} return 0;} /**/
hdu4605 Magic Ball Game (tree-like array)