HDU 4605 Magic Ball Game 樹狀數組

 

HDU 4605 Magic Ball Game 樹狀數組



題目大意很簡單。

有一顆樹(10^5結點),所有結點要麼沒有子結點,要麼有兩個子結點。然後每個結點都有一個重量值,根結點是1

然後有一個球,從結點1開始往子孫結點走。

每碰到一個結點,有三種情況

如果此球重量等於該結點重量,球就停下了

如果此球重量小於該結點重量,則分別往左右兒子走的可能都是1/2

如果此球重量大於該結點重量,則走向左兒子的概率是1/8,右兒子的概率是7/8

然後若干個詢問(10^5次),問一個重量爲x的球經過結點v的概率


仔細想一下,一個球走到某個結點,路徑已經是固定的了,但是暴力肯定會超時,那麼觀察路徑,可以發現路徑可以分成兩種,向左走的路徑和向右走的路徑,分成這兩種的原因也是因爲各自的計算公式,在向左走的路徑中,設大於x的點權有a個,小於x的點權有b個,那麼向左走的路徑概率就是p1=(1/2)^a * (1/8) ^b, 同理向右的路徑中概率

p2 = (1/2)^c * (7/8) ^d,最後二者相乘即是答案。

需要注意的是,如果從1到該點的路徑中有一個點的重量等於x,那麼這個點是永遠被達不到的。


最後就是實現了。

看到要求大於某數的值有多少,一般就可以想到使用數據結構,如樹狀數組,線段樹來統計。 而樹狀數組又是最好寫的。

所以對於左右路徑,分別開一個樹狀數組,用來維護大於某數的點有幾個。

然後詢問需要先存下來。在我們DFS遍歷樹的時候再處理。

然後維護樹狀數組的時候,用的是回溯的一種方法,保證遍歷到某個點時,所用到的樹狀數組一定是隻記錄了1到該點的路徑上的所有重量值


  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <algorithm>  
  4. #include <cstring>  
  5. #include <cmath>  
  6. #include <map>  
  7. #include <queue>  
  8. #include <set>  
  9. #include <vector>  
  10. #define MAXM 111111  
  11. #define MAXN 111111  
  12. #define INF 1000000007  
  13. #define eps 1e-8  
  14. using namespace std;  
  15. vector<int>g[MAXN];  
  16. vector<pair<intint> >query[MAXN];  
  17. int ta[2][MAXN];  
  18. int n, m, q, cnt;  
  19. int w[MAXN];  
  20. int a[MAXN * 2];  
  21. int ans[MAXN][2];  
  22. int lowbit(int x)  
  23. {  
  24.     return x & -x;  
  25. }  
  26. void add(int x, int v, int j)  
  27. {  
  28.     for(int i = x; i <= cnt; i += lowbit(i))  
  29.         ta[j][i] += v;  
  30. }  
  31. int getsum(int x, int j)  
  32. {  
  33.     int sum = 0;  
  34.     for(int i = x; i > 0; i -= lowbit(i))  
  35.         sum += ta[j][i];  
  36.     return sum;  
  37. }  
  38. void dfs(int u)  
  39. {  
  40.     int sz = query[u].size();  
  41.     for(int i = 0; i < sz; i++)  
  42.     {  
  43.   
  44.         int weight = query[u][i].second;  
  45.         int id = query[u][i].first;  
  46.         int pos = lower_bound(a, a + cnt, weight) - a + 1;  
  47.         int ls = getsum(pos - 1, 0);  
  48.         int rs = getsum(pos - 1, 1);  
  49.         int lall = getsum(cnt, 0);  
  50.         int rall = getsum(cnt, 1);  
  51.         int lb = lall - getsum(pos, 0);  
  52.         int rb = rall - getsum(pos, 1);  
  53.         if(ls + lb + rs + rb - lall - rall != 0)  
  54.         {  
  55.             ans[id][0] = -1;  
  56.             continue;  
  57.         }  
  58.         ans[id][0] = ls * 3 + rs * 3 + lb + rb;  
  59.         ans[id][1] = rs;  
  60.     }  
  61.     sz = g[u].size();  
  62.     for(int i = 0; i < sz; i++)  
  63.     {  
  64.         int v = g[u][i];  
  65.         int weight = w[u];  
  66.         int pos = lower_bound(a, a + cnt, weight) - a + 1;  
  67.         add(pos, 1, i);  
  68.         dfs(v);  
  69.         add(pos, -1, i);  
  70.     }  
  71. }  
  72. int main()  
  73. {  
  74.     int T, u, v, fa, x;  
  75.     scanf("%d", &T);  
  76.     while(T--)  
  77.     {  
  78.         scanf("%d", &n);  
  79.         for(int i = 0; i <= n; i++) g[i].clear();  
  80.         cnt = 0;  
  81.         for(int i = 1; i <= n; i++)  
  82.         {  
  83.             scanf("%d", &w[i]);  
  84.             a[cnt++] = w[i];  
  85.         }  
  86.         scanf("%d", &m);  
  87.         while(m--)  
  88.         {  
  89.             scanf("%d%d%d", &fa, &u, &v);  
  90.             g[fa].push_back(u);  
  91.             g[fa].push_back(v);  
  92.         }  
  93.         scanf("%d", &q);  
  94.         for(int i = 0; i <= q; i++) query[i].clear();  
  95.         for(int i = 0; i < q; i++)  
  96.         {  
  97.             scanf("%d%d", &v, &x);  
  98.             query[v].push_back(make_pair(i, x));  
  99.             a[cnt++] = x;  
  100.         }  
  101.         sort(a, a + cnt);  
  102.         cnt = unique(a, a + cnt) - a;  
  103.         memset(ta, 0, sizeof(ta));  
  104.         dfs(1);  
  105.         for(int i = 0; i < q; i++)  
  106.             if(ans[i][0] == -1)  
  107.                 puts("0");  
  108.             else printf("%d %d\n", ans[i][1], ans[i][0]);  
  109.     }  
  110.     return 0;  
  111. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章