转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove

题意:给出一棵二叉树,每个结点孩子数目为0或者2。每个节点都有一个权值,初始在根,扔一个筛子,筛子的值与结点的权值大小关系影响往左往右的概率。

问给出筛子权值,问到达某个结点的概率。

http://acm.hdu.edu.cn/showproblem.php?pid=4605

做法:肯定需要统计每个点到根的路径中,有哪些结点是需要往左孩子走,哪些需要往右孩子走。然后 根据筛子权值,分别二分,就知道有多少个结点是什么概率。

对于每个结点维护一个set或者线段树是不可达的。

因此有了离线做法:离线处理之后,先处理父亲节点,再处理孩子节点,维护两个线段树或者set,保存到达这个节点,哪些是需要往左遍历,哪些需要往右。之后区间查询或者二分,就能统计个数。

在线做法:利用可持久性,对于每个结点维护一个线段树,就需要利用主席树。做法大致相似。

code : 主席树

#include <iostream>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 200005;
const int M = 10000005;
struct Edge{
int v,next,k;
}e[N];
int start[N], totaledge;
int T[M], lson[M], rson[M], lcnt[M], rcnt[M];
int w[N], n, tot, m, x[N], cnt;
queue<int> que;
void add(int u,int v,int k){
e[totaledge].v = v;
e[totaledge].k = k;
e[totaledge].next = start[u];
start[u] = totaledge ++;
}
int bulid (int l ,int r){
int root = tot ++;
lcnt[root] = rcnt[root] = 0;
if(l == r){
lcnt[root] = rcnt[root] = 0;
return root;
}
int m = (l + r) >> 1;
lson[root] = bulid(l , m);
rson[root] = bulid(m + 1 , r);
lcnt[root] = lcnt[lson[root]] + lcnt[rson[root]];
rcnt[root] = rcnt[lson[root]] + rcnt[rson[root]];
return root;
}
int update(int root,int l,int r, int pos, int lval, int rval) {
int newroot = tot ++;
lcnt[newroot] = rcnt[newroot] = 0;
if(pos == l && pos == r){
lcnt[newroot] = lcnt[root] + lval;
rcnt[newroot] += rcnt[root] + rval;
return newroot;
}
int m = (l + r) >> 1;
if(pos <= m) {
lson[newroot] = update(lson[root], l, m, pos, lval, rval);
rson[newroot] = rson[root];
}
else {
rson[newroot] = update(rson[root], m + 1, r, pos, lval, rval);
lson[newroot] = lson[root];
}
lcnt[newroot] = lcnt[lson[newroot]] + lcnt[rson[newroot]];
rcnt[newroot] = rcnt[lson[newroot]] + rcnt[rson[newroot]];
return newroot;
}
int query(int root,int L,int R, int l,int r, int k){
if (l > r ) return 0;
if (l >= cnt) return 0;
if (r < 0 ) return 0;
if(L == l && R == r) {
if(k == 0) return lcnt[root];
return rcnt[root];
}
int m = (L + R) >> 1;
if(r <= m) return query(lson[root], L, m, l, r, k);
else if(l > m) return query(rson[root], m + 1, R, l, r, k);
else return query(lson[root], L ,m , l , m, k) + query(rson[root], m + 1, R, m + 1 ,r, k);
}
int main() {
int t;
scanf ("%d", &t);
while (t--) {
tot = 0;
totaledge = 0;
scanf ("%d", &n);
for (int i = 1 ; i <= n ; i ++) {
scanf ("%d", &w[i]);
x[i - 1] = w[i];
}
sort (x, x+n);
cnt = unique(x, x + n) - x;
x[cnt] = -1;
T[1] = bulid(0, cnt - 1);
scanf ("%d", &m);
for (int i = 0 ; i < m ; i++){
int u , a , b;
scanf("%d%d%d", &u, &a, &b);
add(u , a, 0);
add(u , b, 1);
}
que.push(1);
while(!que.empty()) {
int u = que.front();
que.pop();
for (int i = start[u] ; i != -1 ; i = e[i].next) {
int v = e[i].v, k = e[i].k;
T[v] = update(T[u], 0, cnt - 1, lower_bound(x, x + cnt , w[u]) - x , k == 0 , k == 1);
que.push(v);
}
}
int q;
scanf ("%d", &q);
while(q --){
int num, v;
scanf ("%d%d", &v, &num);
if(v == 1){
printf("0 0\n");
continue;
}
int p = lower_bound(x, x + cnt , num) - x;
int l = p - 1 , r = p ;
if(x[p] == num){
int ret = query(T[v], 0 , cnt - 1, p, p, 0) + query(T[v], 0 , cnt - 1 , p, p, 1);
if(ret){
puts("0");
continue;
}
r ++ ;
}
int left_small = query(T[v],0 , cnt - 1, 0, l, 0);
int left_large = query(T[v],0 , cnt - 1, r , cnt - 1, 0);
int right_small = query(T[v],0 , cnt - 1, 0, l, 1);
int right_large = query(T[v],0 , cnt - 1, r , cnt - 1, 1);
int down = 0, up = 0;
// cout<<left_small<<" "<<left_large<<" "<<right_small<<" "<<right_large<<endl;
down += left_small * 3;
down += left_large;
down += right_small * 3;
up += right_small;
down += right_large;
printf("%d %d\n",up,down);
}
}
return 0;
}

HDU 4605 Magic Ball Game (在线主席树|| 离线 线段树)的更多相关文章

  1. hdu 4605 Magic Ball Game (在线主席树/离线树状数组)

    版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4605 题意: 有一颗树,根节点为1,每一个节点要么有两个子节点,要么没有,每个节点都有一个权值wi .然后,有一个球,附带值x . 球 ...

  2. HDU 4605 Magic Ball Game(可持续化线段树,树状数组,离散化)

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  3. HDU 4605 Magic Ball Game 树状数组

    题目大意很简单. 有一颗树(10^5结点),所有结点要么没有子结点,要么有两个子结点.然后每个结点都有一个重量值,根结点是1 然后有一个球,从结点1开始往子孙结点走. 每碰到一个结点,有三种情况 如果 ...

  4. hdu 4605 Magic Ball Game

    http://acm.hdu.edu.cn/showproblem.php?pid=4605 可以离线求解 把所以可能出现的 magic ball  放在一个数组里(去重),从小到大排列 先不考虑特殊 ...

  5. hdu4348 To the moon (主席树 || 离线线段树)

    Problem Description Background To The Moon is a independent game released in November 2011, it is a ...

  6. HDU 4605 Magic Ball Game 主席树

    题意: 给一棵\(n(1 \leq n \leq 10^5)\)个节点的二叉树,除叶子节点外,每个点都有左儿子和右儿子. 每个点上都有一个权值. 游戏规则是这样的:在根节点放一个权值为\(X\)的小球 ...

  7. HDU 4605 Magic Ball Game (dfs+离线树状数组)

    题意:给你一颗有根树,它的孩子要么只有两个,要么没有,且每个点都有一个权值w. 接着给你一个权值为x的球,它从更节点开始向下掉,有三种情况 x=w[now]:停在此点 x<w[now]:当有孩子 ...

  8. HDU 4605 Magic Ball Game(离线算法)

    题目链接 思路就很难想+代码实现也很麻烦,知道算法后,已经写的很繁琐而且花了很长时间,200+,好久没写过这么长的代码了. #pragma comment(linker, "/STACK:1 ...

  9. Codeforces 1000F One Occurrence 主席树|| 离线+线段树

    One Occurrence 为什么我半年前这么菜呀, 这种场只A三题... 我们在主席树 || 线段树上维护每个数的右边和它一样的数在哪里, 然后就变成了区间求最大值. 注意加进去的时候要把它右边一 ...

随机推荐

  1. JNA入门实例

    JNA(Java Native Access):建立在JNI之上的Java开源框架,SUN主导开发,用来调用C.C++代码,尤其是底层库文件(windows中叫dll文件,linux下是so[shar ...

  2. COJ 0343 WZJ的公司(二)

    传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=313 试题描述: WZJ的公司放假了!为了保证假期期间公司的安全,WZJ决定 ...

  3. oracle连接总结(内连接、外连接、自然连接,交叉连接,自连接)

    1.简述  1) 两个表的连接,是通过将一个表中的一列或者多列同另一个表中的列链接而建立起来的.用来连接两张表的表达式组成了连接条件.当连接成功后,第二张表中的数据就同第一张表连接起来了,并形成了复合 ...

  4. web references是在.NET下的一个东东?它有什么用呢?和“引用”有什么区别!

    WEB引用的意思啊 在.net中有类库和WEB SERVICE这两种类型的项目, 前者编译出来的DLL就是我们普通使用的引用中的类库, 后都编译出来的,在服务器IIS上为其提供服务,我们调用时就要用到 ...

  5. NET设计模式(2):单件模式(Singleton Pattern)[转载]

    单件模式(Singleton Pattern) ——.NET设计模式系列之二 Terrylee,2005年12月07日 概述 Singleton模式要求一个类有且仅有一个实例,并且提供了一个全局的访问 ...

  6. Do's and Don'ts for Android development

    Do's and Don'ts for Android development, by Futurice developers Use Gradle and its recommended proje ...

  7. libvirt API管理hypervisors

    发布一段C代码,用于连接指定的KVM宿主机器,获得该宿主机器的配置信息,以及该主机上所有的虚拟主机列表.状态及配置信息: #include <stdio.h>#include <st ...

  8. 大规模Hadoop集群在腾讯数据仓库TDW的实践

    随着业务的快速增长,TDW的节点数也在增加,对单个大规模Hadoop集群的需求也越来越强烈.TDW需要做单个大规模集群,主要是从数据共享.计算资源共享.减轻运营负担和成本等三个方面考虑. 数据共享.T ...

  9. Struts2初学习记录

    以下笔记内容来自尚硅谷_Struts2_佟刚老师的视频教程+自己一点点整理 来源免责声明 一. 1. VS 自实现: 1). 搭建 Struts2 的开发环境 2). 不需要显式的定义 Filter, ...

  10. SQL SERVER 执行远端数据库的SQL命令

    --------------------------------------------------------------这段先执行exec sp_configure 'show advanced ...