用线段树写的,为什么有3个RE,大佬求帮助
查看原帖
用线段树写的,为什么有3个RE,大佬求帮助
389268
LeM0NAdE楼主2021/8/9 23:48

这里是我的代码:

#include <stdlib.h>
#include <stdio.h>
typedef struct a
{
    int val,rank;
}T;
T tree[5500050];
T arr[100010];
void push_up(int trp)
{
    tree[trp].val=(tree[trp<<1].val)>(tree[trp<<1|1].val)?(tree[trp<<1].val):(tree[trp<<1|1].val);
    tree[trp].rank=(tree[trp<<1].val)>(tree[trp<<1|1].val)?(tree[trp<<1].rank):(tree[trp<<1|1].rank);
    return;
}
void build_tree(int p,int l,int r)
{
    if(l==r)
    {
        tree[p]=arr[l];
        return;
    }
    int mid=(l+r)>>1;
    build_tree(p<<1,1,mid);
    build_tree(p<<1|1,mid+1,r);
    push_up(p);
}
int main()
{
    int n,i;
    scanf("%d",&n);
    n=1<<n;
    for(i=1;i<=n;i++)
    {
        scanf("%d",&arr[i].val);
        arr[i].rank=i;
    }
    build_tree(1,1,n);
    if(tree[2].val<tree[3].val)
    {
        printf("%d",tree[2].rank);
    }
    else{
        printf("%d",tree[3].rank);
    }
    return 0;
}

2021/8/9 23:48
加载中...