AC日记——Broken BST codeforces 797d

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][2],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=1;now=0;
    char Cget=getchar();
    while(Cget>‘9‘||Cget<‘0‘)
    {
        if(Cget==‘-‘) if_z=-1;
        Cget=getchar();
    }
    while(Cget>=‘0‘&&Cget<=‘9‘)
    {
        now=now*10+Cget-‘0‘;
        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][0]!=-1) dfs(ch[now][0],l,min(dis[now]-1,r));
    if(ch[now][1]!=-1) dfs(ch[now][1],max(l,dis[now]+1),r);
}

int main()
{
    in(n);
    for(int i=1;i<=n;i++)
    {
        in(dis[i]),in(ch[i][0]),in(ch[i][1]),ma[dis[i]]++;
        if(ch[i][0]!=-1) if_[ch[i][0]]=true;
        if(ch[i][1]!=-1) if_[ch[i][1]]=true;
    }
    for(int i=1;i<=n;i++)
    {
        if(!if_[i])
        {
            root=i;
            break;
        }
    }
    dfs(root,0,INF);
    cout<<n-ans;
    return 0;
}
时间: 2024-12-29 00:50:56

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

Broken BST CodeForces - 797D

Broken BST CodeForces - 797D 题意:给定一棵任意的树,对树上所有结点的权值运行给定的算法(二叉查找树的查找算法)(treenode指根结点),问对于多少个权值这个算法会返回false. 方法:如果要求对于值x运行算法能访问到结点k,根据给定算法还有树,可以推出对于每个结点k的x的范围(即最小值,最大值)(某结点p左子树的结点的x全部小于p的权值,右子树的结点的x全部大于p的权值)(由于全部权值均为整数,即使只知道小于和大于也可以推出最小值.最大值). 然而,对于某个结

AC日记——Success Rate codeforces 807c

Success Rate 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define ll long long inline void in(ll &now) { char Cget=getchar();now=0; while(Cget>'9'||Cget<'0

AC日记——Cards Sorting codeforces 830B

Cards Sorting 思路: 线段树: 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 100005 #define INF 0x3f3f3f3f #define maxtree maxn<<2 int n,ai[maxn],val[maxtree],L[ma

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

T-Shirt Hunt 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; int p,x,y,ai[27],ans; bool check(int xx) { if(xx<y) return false; int i=(xx/50)%475; for(int j=1;j<=2

AC日记——Aragorn&#39;s Story HDU 3966

Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10510    Accepted Submission(s): 2766 Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lor

CodeForces 797D Broken BST

$dfs$,线段树. 通过观察可以发现,某位置要能被找到,和他到根这条路上的每个节点的权值存在密切的联系,且是父节点的做儿子还是有儿子也有联系. 可以从根开始$dfs$,边走边更新线段树,如果遍历左儿子,那么将$[1,val-1]$全部加$1$,否则将$[val+1,n]$全部加$1$,回溯的时候减$1$,判断某位置能否到达可以比较单点值与深度的关系. #include <iostream> #include <cstdio> #include <cmath> #inc

AC日记——Dynamic Problem Scoring codeforces 807d

Dynamic Problem Scoring 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 130 int n,ai[maxn][6],ac[6],cnt,all,last1,last2; double map[3][6]; inline void in(i

AC日记——[USACO1.1]坏掉的项链Broken Necklace 洛谷 P1203

题目描述 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里是 n=29 的二个例子: 第一和第二个珠子在图片中已经被作记号. 图片 A 中的项链可以用下面的字符串表示: brbrrrbbbrrrrrbrrbbrbbbbrrrrb 假如你要在一些点打破项链,展开成一条直线,然后从一端开始收集同颜色的珠子直到你遇到一个不同的颜色珠子,在另一端做同样的事(颜色可能与在这之前收集的不同). 确定应该在哪里打破项链来收集到最大数目的珠子. 例如,在图片

AC日记——Sagheer and Crossroads codeforces 812a

812A - Sagheer and Crossroads 思路: 模拟: 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define Yes {puts("YES\n");return 0;} int ai[5][5]; int main() { for(int i=1;i&l