HDU-4605 Magic Ball Game 树状数组+离散+dfs

时间:2023-03-08 22:53:30
HDU-4605 Magic Ball Game 树状数组+离散+dfs

  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4605

  题意:给一颗树,每个节点有个权值w[u],每个节点只有两个儿子或者没有儿子,从根节点放下一个小球,小球有一个权值X:

    1.如果X=w[u],小球停止下落。

    2.如果X<w[u],小球往左儿子和右儿子的概率都是1/2。

    3.如果X>w[u],小球往左儿子的概率为1/7,往右儿子的概率为7/8。

  现在有m个询问<v,x>,表示重量为x的小球到达v节点的概率。

  首先离散化节点的权值。考虑从根节点到达v节点是一条路径,那么我们可以深度遍历树的每个节点,对于很多节点的询问都会经过相同的路径,因此我们可以保存前面的经过的点的重量的数目,因为左边和右边的情况不一样,所以分别存储左边和右边的数目和,这里用树状数组来优化。因为这里是对树来搜索,因此对询问离线化操作。

 //STATUS:C++_AC_1125MS_8572KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=,M=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End struct Node{
int v,x,id;
}nod[N];
int hs[N<<],w[N],ans[N][],g[N][],vis[N<<];
int suml[N<<],sumr[N<<];
int T,n,m,cnt;
vector<int> q[N]; int binary(int l,int r,int tar)
{
int mid;
while(l<r){
mid=(l+r)>>;
if(hs[mid]<tar)l=mid+;
else if(hs[mid]>tar)r=mid;
else return mid;
}
return -;
} inline int lowbit(int x)
{
return x&(-x);
} void update(int *sum,int x,int val)
{
while(x<=cnt){
sum[x]+=val;
x+=lowbit(x);
}
} int getsum(int *sum,int x)
{
int ret=;
while(x){
ret+=sum[x];
x-=lowbit(x);
}
return ret;
} void dfs(int u)
{
int i,j,v,k,key;
for(i=;i<q[u].size();i++){
k=q[u][i];
key=binary(,cnt+,nod[k].x);
if(vis[key]){
ans[nod[k].id][]=-;
continue;
}
ans[nod[k].id][]=getsum(sumr,key-);
ans[nod[k].id][]=getsum(suml,cnt)-getsum(suml,key)
+getsum(sumr,cnt)-getsum(sumr,key)
+(getsum(suml,key-)+getsum(sumr,key-))*;
}
key=binary(,cnt+,w[u]);
if(g[u][]){
for(i=;i<;i++){
update(i?sumr:suml,key,);
vis[key]++;
dfs(g[u][i]);
update(i?sumr:suml,key,-);
vis[key]--;
}
}
} int main()
{
// freopen("in.txt","r",stdin);
int i,j,t,u,a,b;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
j=;
for(i=;i<=n;i++){
scanf("%d",&w[i]);
hs[j++]=w[i];
}
scanf("%d",&t);
mem(g,);
while(t--){
scanf("%d%d%d",&u,&a,&b);
g[u][]=a;g[u][]=b;
}
scanf("%d",&m);
for(i=;i<=n;i++)q[i].clear();
for(i=;i<=m;i++){
scanf("%d%d",&nod[i].v,&nod[i].x);
nod[i].id=i;
hs[j++]=nod[i].x;
q[nod[i].v].push_back(i);
}
sort(hs+,hs+j);
for(i=,cnt=;i<j;i++){
if(hs[i]!=hs[cnt])hs[++cnt]=hs[i];
} mem(suml,),mem(sumr,);
mem(vis,);
dfs(); for(i=;i<=m;i++){
if(ans[i][]==-)
printf("%d\n",);
else printf("%d %d\n",ans[i][],ans[i][]);
}
}
return ;
}