hdu 4605 线段树与二叉树遍历
思路:
首先将所有的查询有一个vector保存起来。我们从1号点开始dfs这颗二叉树,用线段树记录到当前节点时,走左节点的有多少比要查询该节点的X值小的,有多少大的,
同样要记录走右节点的有多少比X小的,多少比X大的。小和大的x,y值题目给了。当要进行左儿子时,建该节点值插入走左的线段树,回退的时候将其删除,进入右儿子时将其插入走右的线段树,同样回退时删除。遍历完一个树,整个查询就做完了,最后输出。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#define Maxn 200100
#define lson(x) (x<<1)
#define rson(x) ((x<<1)+1)
#define mid ((tree[po].l+tree[po].r)>>1)
#define inf 100000000
using namespace std;
struct Query{
int x,i;
};
struct AA{
int x,y;
}ans[Maxn];
struct Tree{
int l,r,lnum,rnum;
}tree[Maxn*];
int head[Maxn],e,w[Maxn],Index[Maxn],flag=,L,R,Lsum,Rsum;
vector<Query> query[Maxn];
struct Edge{
int u,v,next;
}edge[Maxn];
void buildTree(int l,int r,int po)
{
tree[po].l=l,tree[po].r=r;
tree[po].lnum=,tree[po].rnum=;
if(l==r)
return ;
buildTree(l,mid,lson(po));
buildTree(mid+,r,rson(po));
}
void Insert(int val,int po,int type)
{
if(tree[po].l==tree[po].r)
{
if(type)
tree[po].rnum++;
else
tree[po].lnum++;
return ;
}
if(type)
tree[po].rnum++;
else
tree[po].lnum++;
if(Index[mid]>=val)
Insert(val,lson(po),type);
else
Insert(val,rson(po),type);
}
void del(int val,int po,int type)
{
if(tree[po].l==tree[po].r)
{
if(type)
tree[po].rnum--;
else
tree[po].lnum--;
return ;
}
if(type)
tree[po].rnum--;
else
tree[po].lnum--;
if(Index[mid]>=val)
del(val,lson(po),type);
else
del(val,rson(po),type);
}
void get_Num(int val,int po)
{
if(Index[tree[po].l]>val)
return ;
if(Index[tree[po].r]<val)
{
R+=tree[po].rnum;
L+=tree[po].lnum;
return ;
}
if(tree[po].l==tree[po].r)
{
if(tree[po].rnum||tree[po].lnum)//如果图中有相同数值,则不能到达
flag=;
return ;
}
get_Num(val,lson(po));
get_Num(val,rson(po));
}
void init()
{
e=;
Lsum=Rsum=;
memset(head,-,sizeof(head));
for(int i=;i<=;i++)
{
query[i].clear();
ans[i].x=ans[i].y=;
}
}
void add(int u,int v)
{
edge[e].u=u;edge[e].v=v;edge[e].next=head[u];head[u]=e++;
}
void dfs(int u)//进行二叉树遍历
{
int i,j,temp,Size,pos;
Size=query[u].size();
for(i=;i<Size;i++)
{
L=R=flag=;
get_Num(query[u][i].x,);
if(flag)
{
ans[query[u][i].i].y=-;
continue;
}
ans[query[u][i].i].y+=L*;
ans[query[u][i].i].y+=Lsum-L;
ans[query[u][i].i].y+=R*;
ans[query[u][i].i].y+=Rsum-R;
ans[query[u][i].i].x+=R;
}
int f=;
for(i=head[u];i!=-;i=edge[i].next)
{
if(f)
{
Insert(w[u],,);
Rsum++;
}
else
{
Insert(w[u],,);
Lsum++;
}
f++;
dfs(edge[i].v);
if(f==)
{
del(w[u],,);
Rsum--;
}
else
{
del(w[u],,);
Lsum--;
}
}
}
int main()
{
int n,m,q,t,i,j,a,b,u,num;
scanf("%d",&t);
while(t--)
{
init();
num=;
scanf("%d",&n);
for(i=;i<=n;i++)
{
scanf("%d",w+i);
Index[i]=w[i];
}
sort(Index+,Index+n+);
num=;
for(i=;i<=n;i++)//将坐标离散化
if(Index[i]>Index[num])
Index[++num]=Index[i];
buildTree(,num,);//建立线段树
scanf("%d",&m);
for(i=;i<=m;i++)
{
scanf("%d%d%d",&u,&a,&b);
add(u,b);
add(u,a);
}
scanf("%d",&q);
Query temp;
int v;
for(i=;i<=q;i++)//将所有查询保存起来
{
scanf("%d%d",&v,&temp.x);
temp.i=i;
query[v].push_back(temp);
}
int f=;//标记走左还是右
for(i=head[];i!=-;i=edge[i].next)//从1号节点开始遍历
{
if(f)//若走右
{
Insert(w[],,);//将该值插入右线段树,并且总数加1
Rsum++;
}
else
{
Insert(w[],,);//插入左线段树,总数加1
Lsum++;
}
f++;
dfs(edge[i].v);
if(f==)
{
del(w[],,);//从右儿子退出,将其删除,总数减1
Rsum--;
}
else
{
del(w[],,);//从左儿子退出
Lsum--;
}
}
for(i=;i<=q;i++)
{
if(ans[i].y!=-)
printf("%d %d\n",ans[i].x,ans[i].y);
else
printf("0\n");
}
}
return ;
}
hdu 4605 线段树与二叉树遍历的更多相关文章
- hdu 5877 线段树(2016 ACM/ICPC Asia Regional Dalian Online)
Weak Pair Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- hdu 3974 线段树 将树弄到区间上
Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3436 线段树 一顿操作
Queue-jumpers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- hdu 3397 线段树双标记
Sequence operation Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- hdu 4578 线段树(标记处理)
Transformation Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 65535/65536 K (Java/Others) ...
- hdu 4533 线段树(问题转化+)
威威猫系列故事——晒被子 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Tot ...
- hdu 2871 线段树(各种操作)
Memory Control Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 4052 线段树扫描线、奇特处理
Adding New Machine Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- hdu 1542 线段树扫描(面积)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
随机推荐
- 大道至简—SQLite3 使用教学
OS X自从10.4后把SQLite这套相当出名的数据库软件,放进了作业系统工具集里.OS X包装的是第三版的SQLite,又称SQLite3.这套软件有几个特色: 软件属于公共财(pu ...
- I - Control - HDU 4289 (最大流)
题意:有N个城市,现在城市S出现了一伙歹徒,他们想运送一些炸弹到D城市,不过警方已经得到了线报知道他们的事情,不过警察不知道他们所在的具体位置,所以只能采取封锁城市的办法来阻断暴徒,不过封锁城市是需要 ...
- Umbraco中的Examine Search功能讲解
转载原地址: http://24days.in/umbraco/2013/getting-started-with-examine/ Everytime I read the word Examine ...
- HttpMessageConverter用法
HttpMessageConverter接口定义 * Strategy interface that specifies a converter that can convert from and t ...
- c# 如何使用DLL的config文件中的信息
我知道用c#编写的exe程序可以读取config文件中的配置信息,比如Test.exe,可以在与Test.exe相同目录下放置一个config文件:Test.exe.config,用System.Co ...
- Epplus 使用的简单介绍
操作Excel的主要有以下类库: MyXls(http://sourceforge.net/projects/myxls/) Koogra(http://sourceforge.net/project ...
- [Ruby01]Class, Module, Object,Kernel的关系
puts Class.ancestorsputs '11111111111111111111'puts Module.ancestorsputs '2222222222222222222'puts O ...
- Android优化系列之ListView优化老生常谈
本文内容:adapter,listview的优化,RecycleBi,google大会推荐优化, 实现ListView的过程,Adapter起到了至关重要的作用,不仅仅因为getview()方法.那么 ...
- 【转】移动端App测试实用指南
转自:互联网那点事 英文原文: http://mobile.smashingmagazine.com/2012/10/22/a-guide-to-mobile-app-testing/ 测试人员常被看 ...
- Android学习笔记(2)
今天我继续看Mars老师的Android开发视频教程,看到一个“深入LinearLayout”的时候,发现一个比较好玩的技巧. 控件的layout_weight属性,他是父控件剩余空间的比例. 如果把 ...