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 ;
}

AC日记——Broken BST codeforces 797d的更多相关文章

  1. Broken BST CodeForces - 797D

    Broken BST CodeForces - 797D 题意:给定一棵任意的树,对树上所有结点的权值运行给定的算法(二叉查找树的查找算法)(treenode指根结点),问对于多少个权值这个算法会返回 ...

  2. AC日记——Cards Sorting codeforces 830B

    Cards Sorting 思路: 线段树: 代码: #include <cstdio> #include <cstring> #include <iostream> ...

  3. AC日记——Card Game codeforces 808f

    F - Card Game 思路: 题意: 有n张卡片,每张卡片三个值,pi,ci,li: 要求选出几张卡片使得pi之和大于等于给定值: 同时,任意两两ci之和不得为素数: 求选出的li的最小值,如果 ...

  4. AC日记——Success Rate codeforces 807c

    Success Rate 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> ...

  5. AC日记——T-Shirt Hunt codeforces 807b

    T-Shirt Hunt 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> ...

  6. AC日记——Magazine Ad codeforces 803d

    803D - Magazine Ad 思路: 二分答案+贪心: 代码: #include <cstdio> #include <cstring> #include <io ...

  7. AC日记——Array Queries codeforces 797e

    797E - Array Queries 思路: 分段处理: 当k小于根号n时记忆化搜索: 否则暴力: 来,上代码: #include <cmath> #include <cstdi ...

  8. AC日记——Maximal GCD codeforces 803c

    803C - Maximal GCD 思路: 最大的公约数是n的因数: 然后看范围k<=10^10; 单是答案都会超时: 但是,仔细读题会发现,n必须不小于k*(k+1)/2: 所以,当k不小于 ...

  9. AC日记——Vicious Keyboard codeforces 801a

    801A - Vicious Keyboard 思路: 水题: 来,上代码: #include <cstdio> #include <cstring> #include < ...

随机推荐

  1. java前台传参json,后台用map或者实体对象接收

    (一)前台传js对象,里面包含数组,后台用map接收 (1)第一种情况:数组里不包含js对象 var param ={}: param.id=id; param.name=name; var scor ...

  2. SVN迁移到Git原因说明

    1.Git分布式的源码管理 每位开发人员计算机本地会有一份代码库,开发人员可在不受其他人代码提交影响的前提下对源码进行提交/回滚/撤销等操作. 在独立的开发任务中即可实现对源码管理又不受其他开发人员提 ...

  3. Vim常用指令总结(持续更新中)

    1 模式变更 命令 说明 a(append)/i(insert) 普通模式→插入模式 : 普通模式→命令行模式 ESC或者Ctrl 插入模式→普通模式 R(Replace)/Insert两次 普通模式 ...

  4. 孤荷凌寒自学python第五十五天初识MongoDb数据库

    孤荷凌寒自学python第五十五天第一天初识MongoDb数据库 (完整学习过程屏幕记录视频地址在文末) 大家好,2019年新年快乐! 本来我想的是借新年第一天开始,正式尝试学习爬虫,结果今天偶然发现 ...

  5. (原)Unreal 渲染模块引言Temp

            @author:白袍小道     引言 本文只在对Unreal渲染模块做一些详细的理解,务求能分析出个大概. 其中框架的思想和实现的过程,是非常值得学习和推敲一二的. 涉及资源系统,材 ...

  6. PHP面向对象练习

    练习内容:随机生成一个字符串 代码: <?phpclass randstring{ private $length; private $type; private $one = array(0, ...

  7. 安装bcc

    sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys D4284CDD echo "deb https://repo.i ...

  8. easyUI layout

    layout是一个容器,它有5个区域:north(北丐),south(南帝),east(东邪),west(西毒),center(中神通),像不像金庸的天龙八部,中间区域的panel是必须的, 周边区域 ...

  9. 如何修改root密码

    默认情况下,每次登录ubuntu都会生成一个随机的root密码,如果想要修改, sudo passwd 然后输入密码,这个密码就作为root用户的密码

  10. [poj] 1389 Area of Simple Polygons

    原题 线段树+扫描线 对于这样一个不规则图形,我们要求他的面积有两种方法,割和补. 补显然不行,因为补完你需要求补上去的内部分不规则图形面积-- 那么怎么割呢? 像这样: 我们就转化成了无数个矩形的和 ...