【题目链接】:http://codeforces.com/contest/797/problem/D

【题意】



给你一个二叉树;

然后问你,对于二叉树中每个节点的权值;

如果尝试用BST的方法去找;

能不能找到这样一个权值的节点;

(只要找到这个权值的节点就好,没有说特定找哪一个)

【题解】



对于二叉树中的每一个节点,我们都能确定;

可以到达这个节点的权值的范围;

对于节点不在这个权值范围内的点;

答案递增;

但有可能会有

3

2 -1 -1

1 1 3

2 -1 -1

这样的数据

这里的答案应该是0而不是1;

因为2是能够通过bst的方法找到的;

虽然找左儿子的时候返回的是右儿子的;但是找到了!

所以最后如果有某些权值在某一些地方找不到,但是在另外一些地方找到了则也是可以的.



【Number Of WA】



2



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100;
const int INF = 1e9+1000; int n,a[N],l[N],r[N],bo[N],root,ans;
int siz[N];
map <int,int> dic1,dic2; void dfs2(int x,int fa,int mi,int ma)
{
if (mi<=a[x]&&a[x]<=ma)
{
dic1[a[x]] = 1;
}
else
{
ans++;
dic2[a[x]]++;
}
if (l[x]!=-1) dfs2(l[x],x,mi,min(ma,a[x]));
if (r[x]!=-1) dfs2(r[x],x,max(mi,a[x]),ma);
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false),cin.tie(0);
cin >> n;
rep1(i,1,n)
{
cin >> a[i] >> l[i] >> r[i];
if (l[i]!=-1) bo[l[i]] = true;
if (r[i]!=-1) bo[r[i]] = true;
}
rep1(i,1,n)
if (!bo[i])
root = i;
dfs2(root,0,-INF,INF);
rep1(i,1,n)
if (dic1[a[i]] && dic2[a[i]]>0)
{
dic1[a[i]]=0;
ans-=dic2[a[i]];
}
cout << ans << endl;
return 0;
}

【codeforces 797D】Broken BST的更多相关文章

  1. 【26.34%】【codeforces 722A】Broken Clock

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  2. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  3. Codeforces 797 D. Broken BST

    D. Broken BST http://codeforces.com/problemset/problem/797/D time limit per test 1 second memory lim ...

  4. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  5. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  6. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  7. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  8. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  9. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

随机推荐

  1. 怎样使用OpenShare部署和运营企业门户

    怎样使用OpenShare部署和运营企业门户 这篇Blog是偏向企业内总体门户部署和运营的指南,是偏向总体管理和规划的.并非针对终端用户的OpenShare软件操作手冊,详细的操作能够上优酷看相关视频 ...

  2. 大规模的I/O流中有效识别大数据并增强时间局部性

    一篇热数据识别存储外文翻译,本文主要在讲思想 原文题目:  HDCat: Effectively Identifying Hot Data in    Large-scale I/O Streams ...

  3. 10g异机恢复后EM无法启动故障处理一例

    之前在自己的測试环境上做了个异机恢复,原来的库上是配置过EM的,可是在恢复的库上去启动EM就报错了.以下看详细解决过程: PS:原主机名为zlm,恢复出来的主机名为bak [root@bak ~]# ...

  4. P1121 环状最大两段子段和

    P1121 环状最大两段子段和 题目描述 给出一段环状序列,即认为A[1]和A[N]是相邻的,选出其中连续不重叠且非空的两段使得这两段和最大. 输入输出格式 输入格式: 输入文件maxsum2.in的 ...

  5. mac os lscpu 【转】

    CPU Information on Linux and OS X This is small blog post detailing how to obtain information on you ...

  6. hdfs du命令是算的一份数据

    As you can see, hadoop fsck and hadoop fs -dus report the effective HDFS storage space used, i.e. th ...

  7. svn for vs

    现在官方下载需要注册一堆的东西,为方便群众使用在这里提供一个新版的下载. http://files.cnblogs.com/wfcfan/AnkhSvn-2.5.12266.rar

  8. [NOI 2003] 逃学的小孩

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1509 [算法] 树的直径 [代码] #include<bits/stdc++. ...

  9. IJ-Error:常见错误

    ylbtech-IJ-Error:常见错误 1.返回顶部 1. This application has no explicit mapping for /error, so you are seei ...

  10. JFreeChart之堆叠柱形图(StackedBar)

    JFreeChart之堆叠柱形图(StackedBar) JAVA JFreeChart 最近的项目使用有个功能需要使用到堆叠柱形图,看了项目以前的代码实现没有想要的结果.所以自己就先到官网下载了 D ...