$dfs$,线段树。

通过观察可以发现,某位置要能被找到,和他到根这条路上的每个节点的权值存在密切的联系,且是父节点的左儿子还是右儿子也有联系。

可以从根开始$dfs$,边走边更新线段树,如果遍历左儿子,那么将$[1,val-1]$全部加$1$,否则将$[val+1,n]$全部加$1$,回溯的时候减$1$,判断某位置能否到达可以比较单点值与深度的关系。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std; int f[400010];
int s[400010];
int res; void pushDown(int rt)
{
if(f[rt]==0) return ;
s[2*rt] += f[rt];
s[2*rt+1] += f[rt];
f[2*rt] += f[rt];
f[2*rt+1] += f[rt];
f[rt] = 0;
return ;
} void pushUp(int rt)
{
s[rt] = s[2*rt] + s[2*rt+1];
} void update(int L,int R,int val,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
s[rt] += val;
f[rt] += val;
return ;
} int m = (l+r)/2;
pushDown(rt);
if(L<=m) update(L,R,val,l,m,2*rt);
if(R>m) update(L,R,val,m+1,r,2*rt+1);
pushUp(rt);
} void query(int pos,int l,int r,int rt)
{
if(l==r)
{
res = s[rt];
return;
} int m = (l+r)/2;
pushDown(rt);
if(pos<=m) query(pos,l,m,2*rt);
else query(pos,m+1,r,2*rt+1);
pushUp(rt); } int n;
struct X
{
int val;
int left,right;
}node[100010];
int root;
int b[100010],sz;
int ans; int get(int x)
{
int L = 0,R = sz-1; while(L<=R)
{
int mid = (L+R)/2;
if(b[mid]>x) R = mid-1;
else if(b[mid] == x) return mid+1;
else L = mid+1;
}
} int u[100010]; void dfs(int x,int y)
{
query(node[x].val,1,n,1);
if(res != y) {}
else u[node[x].val]=1; if(node[x].left!=-1)
{
if(node[x].val>1) update(1,node[x].val-1,1,1,n,1);
dfs(node[x].left,y+1);
if(node[x].val>1) update(1,node[x].val-1,-1,1,n,1);
} if(node[x].right!=-1)
{
if(node[x].val<n) update(node[x].val+1,n,1,1,n,1);
dfs(node[x].right,y+1);
if(node[x].val<n) update(node[x].val+1,n,-1,1,n,1);
} } int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d%d%d",&node[i].val,&node[i].left,&node[i].right); for(int i=1;i<=n;i++) b[sz++] = node[i].val;
sort(b,b+sz);
for(int i=1;i<=n;i++)
{
node[i].val = get(node[i].val);
u[node[i].val]=1;
} int sum=0;
for(int i=1;i<=n;i++) sum=sum+1; for(int i=1;i<=n;i++)
{
if(node[i].left!=-1) f[node[i].left] = 1;
if(node[i].right!=-1) f[node[i].right] = 1;
} for(int i=1;i<=n;i++)
{
if(f[i]) continue;
root = i; break;
} memset(f,0,sizeof f);
memset(u,0,sizeof u);
dfs(root,0);
for(int i=1;i<=n;i++) sum=sum-u[node[i].val]; printf("%d\n",sum); return 0;
}

CodeForces 797D Broken BST的更多相关文章

  1. Broken BST CodeForces - 797D

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

  2. AC日记——Broken BST codeforces 797d

    D - Broken BST 思路: 二叉搜索树: 它时间很优是因为每次都能把区间缩减为原来的一半: 所以,我们每次都缩减权值区间. 然后判断dis[now]是否在区间中: 代码: #include ...

  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 24D Broken robot(期望+高斯消元)

    CodeForces 24D Broken robot 大致题意:你有一个n行m列的矩形板,有一个机器人在开始在第i行第j列,它每一步会随机从可以选择的方案里任选一个(向下走一格,向左走一格,向右走一 ...

  5. CodeForces 1251A --- Broken Keyboard

    [CodeForces 1251A --- Broken Keyboard ] Description Recently Polycarp noticed that some of the butto ...

  6. 【codeforces 797D】Broken BST

    [题目链接]:http://codeforces.com/contest/797/problem/D [题意] 给你一个二叉树; 然后问你,对于二叉树中每个节点的权值; 如果尝试用BST的方法去找; ...

  7. CodeForces 24D Broken robot (概率DP)

    D. Broken robot time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  8. CodeForces 24D Broken Robot

    题意:n*m的棋盘,一个机器人在(i,j)处,每次等概率地停在原地,向左移动一格,向右移动一格,向下移动一格(不能移出棋盘).求走到最后一行所需期望步数.n<=1000,m<=1000 一 ...

  9. Codeforces.24D.Broken robot(期望DP 高斯消元)

    题目链接 可能这儿的会更易懂一些(表示不想再多写了). 令\(f[i][j]\)表示从\((i,j)\)到达最后一行的期望步数.那么有\(f[n][j]=0\). 若\(m=1\),答案是\(2(n- ...

随机推荐

  1. Tensorflow BatchNormalization详解:4_使用tf.nn.batch_normalization函数实现Batch Normalization操作

    使用tf.nn.batch_normalization函数实现Batch Normalization操作 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 吴恩达deeplearnin ...

  2. vue-router路由原理

    Vue-router路由原理 目前实现路由的方式有两中,vue通过参数mode来设置,默认是hash模式. 利用URL中的hash(‘#’)来实现 利用History interface在HTML5中 ...

  3. Jquery中find与each方法使用详解

    本文实例讲述了jQuery中find与each方法用法.分享给大家供大家参考.具体如下: 一.find()方法 jquery选择器非常强大,利用css的命名规约,可以更快更方便的找出想要的元素. 图解 ...

  4. 2017北京国庆刷题Day6 morning

    期望得分:100+100+20=220 实际得分:100+100+20=220 模拟栈 #include<cstdio> #include<cstring> using nam ...

  5. C11构造函数的改善

    1.委托构造函数 委托构造函数就是允许在同一个类中一个构造函数可以调用另一个构造函数,从而在初始化时简化变量的初始化. class CTest { public: int x; int y; int ...

  6. 51nod1110 距离之和最小 V3

    基准时间限制:1 秒 空间限制:131072 KB 分值: 40  X轴上有N个点,每个点除了包括一个位置数据X[i],还包括一个权值W[i].该点到其他点的带权距离 = 实际距离 * 权值.求X轴上 ...

  7. 【BZOJ】1036 [ZJOI2008]树的统计Count

    [算法]树链剖分+线段树 [题解]模板题,见http://www.cnblogs.com/onioncyc/p/6207462.html 调用线段数时要用新编号pos[i] !!! #include& ...

  8. MySql 快速去重方法

    1.复制需要去重的表 CREATE TABLE 新表 LIKE 旧表 ; 2.将需要去重的字段 设置为唯一union 索引 ALTER TABLE 表名 ADD UNIQUE(`字段`); 3.复制旧 ...

  9. HDU 1081 To The Max (dp)

    题目链接 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rect ...

  10. [整理] magento搬家

    将原来网站文件中的var文件中的cache和session文件删除,将media中的缓存文件删除.然后将所有文件制作成一个压缩包,以减少文件体积,方便转移. 将压缩包转移到新的服务器域名指向的文件夹, ...