AC日记——Broken BST codeforces 797d
思路:
二叉搜索树;
它时间很优是因为每次都能把区间缩减为原来的一半;
所以,我们每次都缩减权值区间。
然后判断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的更多相关文章
- Broken BST CodeForces - 797D
Broken BST CodeForces - 797D 题意:给定一棵任意的树,对树上所有结点的权值运行给定的算法(二叉查找树的查找算法)(treenode指根结点),问对于多少个权值这个算法会返回 ...
- AC日记——Cards Sorting codeforces 830B
Cards Sorting 思路: 线段树: 代码: #include <cstdio> #include <cstring> #include <iostream> ...
- AC日记——Card Game codeforces 808f
F - Card Game 思路: 题意: 有n张卡片,每张卡片三个值,pi,ci,li: 要求选出几张卡片使得pi之和大于等于给定值: 同时,任意两两ci之和不得为素数: 求选出的li的最小值,如果 ...
- AC日记——Success Rate codeforces 807c
Success Rate 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> ...
- AC日记——T-Shirt Hunt codeforces 807b
T-Shirt Hunt 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> ...
- AC日记——Magazine Ad codeforces 803d
803D - Magazine Ad 思路: 二分答案+贪心: 代码: #include <cstdio> #include <cstring> #include <io ...
- AC日记——Array Queries codeforces 797e
797E - Array Queries 思路: 分段处理: 当k小于根号n时记忆化搜索: 否则暴力: 来,上代码: #include <cmath> #include <cstdi ...
- AC日记——Maximal GCD codeforces 803c
803C - Maximal GCD 思路: 最大的公约数是n的因数: 然后看范围k<=10^10; 单是答案都会超时: 但是,仔细读题会发现,n必须不小于k*(k+1)/2: 所以,当k不小于 ...
- AC日记——Vicious Keyboard codeforces 801a
801A - Vicious Keyboard 思路: 水题: 来,上代码: #include <cstdio> #include <cstring> #include < ...
随机推荐
- pg数据库数据表异常挂起
pg数据库即是PostgreSQL数据库. 前几天在一个Java项目中,出现运行Java程序后,pg数据库的数据表异常挂起.而且是在某台电脑上出现的,重装数据库也没用,其它电脑未能复现,是个很奇怪的现 ...
- Spring 各种注解备注
Spring 各种注解备注 felix_feng 关注 2016.12.28 10:34* 字数 2092 阅读 790评论 0喜欢 6 转载 (http://blog.csdn.net/sudilu ...
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目9
2014-04-23 23:57 题目:如何设计一个内存文件系统,如果可以的话,附上一些代码示例. 解法:很遗憾,对我来说不可以.完全没有相关经验,所以实在无从入手.这题目应该和工作经验相关吧? 代码 ...
- cookie不能删除
cookie不仅仅包含一个键值对,还包含域 domain 路径path, 一般domain是请求的地址 www.baidu.com/news.html 那domain就是www.baidu.com ...
- Python 字符串换行的几种方式
第一种: x0 = '<?xml version="1.0"?>' \ '<ol>' \ ' <li><a href="/pyt ...
- 【Python】Django学习一:第一个Django程序
项目开发环境 Python 3.6 Django 1.11.5 Django安装 在开始安装Django之前,Django更新比较频繁,所以要选择合适的版本,这里选择Django1.11.5. pip ...
- win10 64位 C# 连接oracle 32位, 遇到的问题及解决
首次 本机电脑是win10系统 64位的:安装的oracle数据库也是64位的: 服务器端的oracle 是32位的: 第一次安装的pl/sql 也是64位的, 配置完 F:\app\ln_qi\p ...
- RAID介绍和实现
RAID的全称是廉价磁盘冗余阵列(Redundant Array of Inexpensive Disks),于1987年由美国Berkeley 大学的两名工程师提出的. RAID出现的,最初目的是将 ...
- Java 循环队列的实现
队列概念 队列(Queue)是限定只能在一端插入.另一端删除的线性表.允许删除的一端叫做队头(front),允许插入的一端叫做队尾(rear),没有元素的队列称为“空队列”. 队列具有先进先出(FIF ...
- A - 最长上升子序列
A - 最长上升子序列 Time Limit: 1000/1000MS (C++/Others) Memory Limit: 65536/65536KB (C++/Others) Problem De ...