D - Broken BST

思路:

  二叉搜索树;

  它时间很优是因为每次都能把区间缩减为原来的一半;

  所以,我们每次都缩减权值区间。

  然后判断dis[now]是否在区间中;

代码:

#include <map>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 100005
#define INF 0x7fffffff int n,ch[maxn][],dis[maxn],ans;
int l[maxn],r[maxn],root; bool if_[maxn]; map<int,int>ma;
map<int,bool>maa; inline void in(int &now)
{
int if_z=;now=;
char Cget=getchar();
while(Cget>''||Cget<'')
{
if(Cget=='-') if_z=-;
Cget=getchar();
}
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
now*=if_z;
} void dfs(int now,int l,int r)
{
if(l>r) return ;
if(dis[now]>=l&&dis[now]<=r)
{
if(!maa[dis[now]])
{
ans+=ma[dis[now]];
maa[dis[now]]=true;
}
}
if(ch[now][]!=-) dfs(ch[now][],l,min(dis[now]-,r));
if(ch[now][]!=-) dfs(ch[now][],max(l,dis[now]+),r);
} int main()
{
in(n);
for(int i=;i<=n;i++)
{
in(dis[i]),in(ch[i][]),in(ch[i][]),ma[dis[i]]++;
if(ch[i][]!=-) if_[ch[i][]]=true;
if(ch[i][]!=-) if_[ch[i][]]=true;
}
for(int i=;i<=n;i++)
{
if(!if_[i])
{
root=i;
break;
}
}
dfs(root,,INF);
cout<<n-ans;
return ;
}

最新文章

  1. Mac OSX:Powerline风格的zsh配置
  2. jQuery Mobile动态刷新页面样式
  3. Perfect Scrollbar – 完美的 jQuery 滚动条插件
  4. Android sdk 镜像服务器资源
  5. UWP入门一 7天酒店客户端源码及说明
  6. Ubuntu install mysql
  7. System.Web.HttpContext.Current.Session获取值出错
  8. JQ 无刷新评论
  9. c#进程、定时器初步学习
  10. JDK源码——单例模式
  11. BUG -Failed to compile.
  12. Vue diff 算法
  13. web缓存服务器varnish-4.1.6的部署及配置详解
  14. C#:进程
  15. 课程四(Convolutional Neural Networks),第一周(Foundations of Convolutional Neural Networks) —— 1.Practice questions:The basics of ConvNets
  16. day_11py学习
  17. ios 精简日历
  18. 猫猫学iOS之小知识之xcode6自己主动提示图片插件 KSImageNamed的安装
  19. ExtJS+SpringMVC文件上传与下载
  20. 使用Django来处理对于静态文件的请求

热门文章

  1. HA集群中namenode连接不上journalnode,导致namenode启动不了
  2. Java中数据类型转换&amp;基本类型变量和对象型变量
  3. 《Cracking the Coding Interview》——第18章:难题——题目9
  4. DOS程序员手册(五)
  5. dynamic基元类型与隐式类型的局部变量var
  6. Djano之写api使用django_rest_framework【海瑞博客】
  7. 孤荷凌寒自学python第四十二天python线程控制之Condition对象
  8. Visual C++ 图像处理类库CxImage源代码
  9. MVC学习笔记----@Helper标签(HelperMethod方法)和HtmlExtesion扩展
  10. nyoj 题目16 矩形嵌套