HDU 4605 Magic Ball Game(可持续化线段树,树状数组,离散化)
Magic Ball Game
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2489 Accepted Submission(s): 759
a node has 2 children balls, 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 balls, 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.
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.
answer should be put down in one line.
1
3
2 3 1
1
1 2 3
3
3 2
1 1
3 4
0
0 0
1 3这道题目和HDU 5877 差不多。两道题目都是在一棵树上进行操作,从树的根节点到某个节点的路径是唯一的,这个路径的所有节点形成的序列。如果要在这个序列里进行各种操作,那么就要用树状数组或者线段树来解决。用树状数组要注意,在dfs回朔的时候要删除点。如果用线段树来解决,那么可以有两种方式,一个是把线段树当做树状数组来用,毕竟树状数组能实现的东西线段树都能实现。另外一种方式可以用可持续化线段树,就是在树的每一个点上面建立一个线段树,那么每个点上的线段树就表示从根节点到这个节点的路径上所有点插到线段树里。可持续化线段树:#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <map>
#include <vector> using namespace std;
typedef long long int LL;
const int maxn=1e5;
vector<int> e[maxn+5];
map<LL,int> mm;
LL rt[maxn+5];
LL ls[maxn*20+5];
LL rs[maxn*20+5];;
LL p2;
LL p;
int n,m,q;
LL num[maxn*20+5][2];
LL a[maxn+5];
LL w[maxn+5];
int Hash(LL x)
{
return upper_bound(a+1,a+n+1,x)-a-1;
}
int newnode()
{
ls[p]=rs[p]=num[p][0]=num[p][1]=0;
return p++;
}
void update(LL &node,int l,int r,LL tag,int flag)
{ if(node==0)
node=newnode();
else
{
ls[p]=ls[node];
rs[p]=rs[node];
num[p][0]=num[node][0];
num[p][1]=num[node][1];
node=p++;
}
if(l==r)
{
num[node][flag]++;
return;
}
num[node][flag]++;
int mid=(l+r)>>1;
if(tag<=mid) update(ls[node],l,mid,tag,flag);
else update(rs[node],mid+1,r,tag,flag); }
LL query(LL node,int l,int r,LL L,LL R,int flag)
{
if(L>R) return 0;
if(!node) return 0;
if(L<=l&&r<=R)
return num[node][flag];
int mid=(l+r)>>1;
LL ret=0;
if(L<=mid) ret+=query(ls[node],l,mid,L,R,flag);
if(R>mid) ret+=query(rs[node],mid+1,r,L,R,flag);
return ret;
}
void dfs(int root)
{
int len=e[root].size();
for(int i=0;i<len;i++)
{
int v=e[root][i];
update(rt[v]=rt[root],1,n,Hash(w[root]),i);
dfs(v);
}
}
int main()
{
int t;
scanf("%d",&t);
LL x,y,z;
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
e[i].clear();
scanf("%lld",&w[i]);
a[i]=w[i];
}
sort(a+1,a+n+1);
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
scanf("%lld%lld%lld",&x,&y,&z);
e[x].push_back(y);
e[x].push_back(z); }
memset(rt,0,sizeof(rt));
memset(num,0,sizeof(num));
p=1;
dfs(1);
scanf("%d",&q);
for(int i=1;i<=q;i++)
{
scanf("%lld%lld",&x,&y);
if(x==1) {printf("0 0\n");continue;} int xx=Hash(y);
if(a[xx]==y)
{
int num2=query(rt[x],1,n,xx,xx,0);
int num5=query(rt[x],1,n,xx,xx,1); if(num2||num5)
{
printf("0\n");
continue;
} } LL num1=query(rt[x],1,n,1,xx,0); LL num3=query(rt[x],1,n,xx+1,n,0); LL num4=query(rt[x],1,n,1,xx,1); LL num6=query(rt[x],1,n,xx+1,n,1); LL ans1=3*(num1+num4)+num3+num6;
LL ans2=num4; printf("%lld %lld\n",ans2,ans1); }
}
return 0;
}树状数组:#include <iostream>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <vector> using namespace std;
const int maxn=1e5;
typedef long long int LL;
int e[maxn+5][2];
int c0[maxn+5];
int c1[maxn+5];
int n,m,q;
int lowbit(int x) {return x&(-x);}
int w[maxn+5];
int vis[maxn+5];
int a[maxn+5];
vector<pair<int,int> > b[maxn+5];
int ans1[maxn+5];
int ans2[maxn+5];
int Hash(int x)
{
return upper_bound(a+1,a+n+1,x)-a-1;
}
void update0(int x,int tag)
{
while(x<=n)
{
c0[x]+=tag;
x+=lowbit(x);
}
}
int sum0(int x)
{
int num=0;
for(int i=x;i>=1;i-=lowbit(i))
{
num+=c0[i];
}
return num;
}
void update1(int x,int tag)
{
while(x<=n)
{
c1[x]+=tag;
x+=lowbit(x);
}
}
int sum1(int x)
{
int num=0;
for(int i=x;i>=1;i-=lowbit(i))
{
num+=c1[i];
}
return num;
}
void init()
{
memset(c0,0,sizeof(c0));
memset(c1,0,sizeof(c1));
memset(vis,0,sizeof(vis));
memset(e,-1,sizeof(e));
memset(ans1,0,sizeof(ans1));
memset(ans2,0,sizeof(ans2));
for(int i=1;i<=n;i++)
{
b[i].clear();
}
}
void dfs(int root)
{
for(int i=0;i<=1;i++)
{
if(e[root][i]==-1) continue;
int v=e[root][i];
if(!i) update0(Hash(w[root]),1);
else update1(Hash(w[root]),1);
if(vis[v])
{
int len=b[v].size();
for(int j=0;j<len;j++)
{
int y=b[v][j].first;
int x=b[v][j].second;
int xx=Hash(y);
if(a[xx]==y)
{
if((sum0(xx)-sum0(xx-1))!=0||(sum1(xx)-sum1(xx-1))!=0)
{
ans1[x]=-1;
continue;
}
}
int left_low=sum0(xx);
int left_up=sum0(n)-sum0(xx);
int right_low=sum1(xx);
int right_up=sum1(n)-sum1(xx);
ans1[x]=right_low;
ans2[x]=3*(left_low+right_low)+left_up+right_up; }
}
dfs(v);
if(!i) update0(Hash(w[root]),-1);
else update1(Hash(w[root]),-1);
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int x,y,z;
scanf("%d",&n);
init();
for(int i=1;i<=n;i++)
{
scanf("%d",&w[i]);
a[i]=w[i];
}
sort(a+1,a+n+1);
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
e[x][0]=y;
e[x][1]=z;
}
scanf("%d",&q);
for(int i=1;i<=q;i++)
{
scanf("%d%d",&x,&y);
vis[x]=1;
b[x].push_back(make_pair(y,i));
}
dfs(1);
for(int i=1;i<=q;i++)
{
if(ans1[i]==-1) {printf("0\n");continue;}
else
{
printf("%d %d\n",ans1[i],ans2[i]);
}
} }
return 0;
}
HDU 4605 Magic Ball Game(可持续化线段树,树状数组,离散化)的更多相关文章
- HDU 4605 Magic Ball Game (dfs+离线树状数组)
题意:给你一颗有根树,它的孩子要么只有两个,要么没有,且每个点都有一个权值w. 接着给你一个权值为x的球,它从更节点开始向下掉,有三种情况 x=w[now]:停在此点 x<w[now]:当有孩子 ...
- hdu 4605 Magic Ball Game (在线主席树/离线树状数组)
版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4605 题意: 有一颗树,根节点为1,每一个节点要么有两个子节点,要么没有,每个节点都有一个权值wi .然后,有一个球,附带值x . 球 ...
- HDU 4605 Magic Ball Game 树状数组
题目大意很简单. 有一颗树(10^5结点),所有结点要么没有子结点,要么有两个子结点.然后每个结点都有一个重量值,根结点是1 然后有一个球,从结点1开始往子孙结点走. 每碰到一个结点,有三种情况 如果 ...
- hdu 4605 Magic Ball Game
http://acm.hdu.edu.cn/showproblem.php?pid=4605 可以离线求解 把所以可能出现的 magic ball 放在一个数组里(去重),从小到大排列 先不考虑特殊 ...
- HDU 4605 Magic Ball Game (在线主席树|| 离线 线段树)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意:给出一棵二叉树,每个结点孩子数目为0或者2. ...
- HDU 4605 Magic Ball Game 主席树
题意: 给一棵\(n(1 \leq n \leq 10^5)\)个节点的二叉树,除叶子节点外,每个点都有左儿子和右儿子. 每个点上都有一个权值. 游戏规则是这样的:在根节点放一个权值为\(X\)的小球 ...
- HDU 4605 Magic Ball Game(离线算法)
题目链接 思路就很难想+代码实现也很麻烦,知道算法后,已经写的很繁琐而且花了很长时间,200+,好久没写过这么长的代码了. #pragma comment(linker, "/STACK:1 ...
- hdu4605 树状数组+离散化+dfs
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- POJ 2299 【树状数组 离散化】
题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...
随机推荐
- java中成员变量、代码块、构造函数运行顺序
1.java虚拟机执行程序,首先须要装载类,安装现装载父类,初始化父类的静态代码块和静态成员变量 再load子类. 初始化子类静态代码块和成员变量 2.load完成父类与子类后,从main函数入口运行 ...
- github提交一个空目录
github默认不上传空目录,有的时候需要空目录来保持程序的结构. 二个小问题. 1.始终保持空目录,即时里面有文件,也全部忽略掉. 建立一个.gitignore文件放到空目录内. mkdir emp ...
- cadence orcad查找技巧
本文讲述了如何在OrCAD原理图中根据元件位号或者元件值快速查找元件的两种方法. 方法一:在.obj页面的“File”标签下查找元件. 方法二:在.obj页面的“Hierarchy”标签下查找元件. ...
- linux中使用lftp上传下载文件
lftp是linux中一款ftp服务器相比windows中的ftp显得要复杂不少了,下面我来总结一下lftp文件上传,文件下载,及文件查找等等相关命令吧. lftp连接的几种方法,最常用的是lftp ...
- php的ord函数——解决中文字符截断问题
php的ord函数——解决中文字符截断问题 分类: PHP2014-11-26 12:11 1033人阅读 评论(0) 收藏 举报 utf8字符截取 函数是这样定义的: int ord ( strin ...
- python (18)在linux中如何实现定时发送邮件到指定邮箱,监测任务
最近要用到,定时发送邮件功能: 如何定时,当然要用到linux中crontab了 如下的代码能够定时发送邮件 #!/usr/bin/env python # -*- coding=utf-8 -*- ...
- pyqt加载图片
使用QPixmap可以加载图片,但是图片只能是标准二进制文件格式: bmp,gif,ico,jpeg,jpg,mng,pbm,pgm,png,ppm,svg,svgz,tga,tif,tiff,xbm ...
- excel鼠标拖选慢shift选择快的问题
今天遇到个惊天大坑,关于excel的,最近,一直在调查这个东西,刚开始真的是毫无头绪,反正现在就是excel的值的copy会偶尔慢,慢的情况也是不明白,就是稀里糊涂的调查. 刚开始连100%再现这个b ...
- mysql索引学习
索引用于快速找出在某列中有一特定值的行. 如果不使用索引,MySQL必须从第一条记录开始读完整个表,直到找出相关的行. 表越大,查询数据所花费的时间越多. 如果表中查询的列有一个索引,MySQL能快速 ...
- Android——excise(用线性布局、表格布局、相对布局做发送邮件界面)
LinearLayout <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns ...