AcWing 1270. 数列区间最大值

AcWing 1270. 数列区间最大值,第1张

AcWing 1270. 数列区间最大值 代码
#include
#include
#include
using namespace std;

const int N = 100010;

int w[N];

struct node
{
    int l, r;
    int maxx;
}tree[4 * N];

int n, m;

void build_tree(int node, int l, int r)
{
    if(l == r){
        tree[node] = {l ,r, w[r]} ; 
        return ;
}

   // tree[node].l=l, tree[node].r = r;
   
    tree[node] = {l, r};
    int mid = l + r >> 1;
    int left_node = node << 1;
    int right_node = node << 1 | 1;
    build_tree(left_node, l, mid);
    build_tree(right_node, mid + 1, r);

    tree[node].maxx = max(tree[left_node].maxx, tree[right_node].maxx);

}

int query_tree(int node, int l, int r)
{
   // if(tree[node].l >=x && tree[node].r <= y) return tree[node].maxx;
   if(tree[node].l >=l && tree[node].r <= r) return tree[node].maxx;
   // else if(tree[node].l < x || tree[node].r > y) return -0x3f;
    
  //  int mid = l + r >> 1;
    int mid = tree[node].l + tree[node].r >> 1;

    int left_node = node << 1;
    int right_node = node << 1 | 1;

    int maxx = INT_MIN;
   // if(l <= mid) maxx = query_tree(left_node, l, mid);
    //if(r > mid) maxx = (maxx, query_tree(right_node, mid + 1, r));
    if(l <= mid) maxx = query_tree(left_node, l, r);
    if(r > mid) maxx = max(maxx, query_tree(right_node, l, r));
    return maxx;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i = 1; i <= n; i++) scanf("%d", &w[i]);

    build_tree(1,1,n);
    int x, y;
    while(m--)
    {
        scanf("%d%d",&x,&y);
        int ans = query_tree(1, x, y);
        printf("%dn",ans);
    }
    return 0;
}

注释部分为个人错误或者不简便写法的思路

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5718537.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存