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 < ...
随机推荐
- 直接选择排序&堆排序
1.什么是直接选择排序? 直接选择排序(Straight Select Sort)是一种简单的排序方法,它的基本思想是:通过n-i次关键字之间的比较,从n-i+1个记录中选出关键字最小的记录,并和第i ...
- jQuery的.on方法
jQuery on()方法是官方推荐的绑定事件的一个方法. $(selector).on(event,childSelector,data,function,map)由此扩展开来的几个以前常见的方法有 ...
- 怎么用 copy 关键字?
NSString.NSArray.NSDictionary等等经常使用copy关键字,是因为他们有对应的可变类型:NSMutableString.NSMutableArray.NSMutableDic ...
- Rbac_权限管理
click!!! https://github.com/ugfly1210/rbac_100 有关于 rbac 的所有代码,包括 README. 用户和角色 : 多对多字段放在哪张表更好点? 用户找角 ...
- 【转载】Unity3D研究院之静态自动检查代码缺陷与隐患
代码缺陷和代码错误的最大区别是,代码缺陷不影响游戏编译,而代码错误编译都不通过.但是代码缺陷会影响游戏发布后产生的一系列BUG..我今天无意间逛外国论坛发现的一个方法,使用了一下感觉挺给力的第一时间分 ...
- 通过命令行安装或卸载Tomcat服务
一.安装Tomcat服务 1.打开命令提示符 方法1: 按住win+R,打开运行,输入cmd,打开命令提示符 方法2:在开始菜单>所有程序>附件>命令提示符 2. 通过命令进入到to ...
- 整合S2SH框架
S2SH框架(Struts2,Spring,Hibernate)整合 Struts2.Hibernate和Spring.其中在Struts2部分主要为MVC设计思想,Struts2的处理流程及配置,S ...
- Linq 聚合函数
var WReserve = (from m in _db.W_RESERVE select m).ToList().LastOrDefault(); 必须ToList() 之后才能 last fir ...
- [洛谷P4588][TJOI2018]数学计算
题目大意:有一个数$x$和取模的数$mod$,初始为$1$,有两个操作: $m:x=x\times m$并输出$x\% mod$ $pos:x=x/第pos次操作乘的数$(保证合法),并输出$x\%m ...
- 块元素包裹img空隙问题
解决方法 1.img设为block2.img对齐方式vertical-align:bottom(middle/top),不是baseline就行3.父容器font-size:0或line-height ...