1

https://cses.fi/problemset/task/1649

I'm solving this problem using Segment Trees and the solution I've written is

#include <bits/stdc++.h>
#define MAX 1000000001

using namespace std; 

int n;  
vector<int> tree;

int sum(int a, int b)
{
    a += n; 
    b += n; 

    int s = INT_MAX; 
    while(a <= b) {
        if (a % 2 == 1) s = min(s, tree[a++]); 
        if (b % 2 == 0) s = min(s, tree[b--]); 

        a>>=1; 
        b>>=1; 
    }

    return s; 
}

void update(int k, int change)
{
    k += n; 
    tree[k] = change; 

    for(int i = k>>1; i >= 1; i>>=1) {
        tree[i] = min(tree[2*i], tree[2*i+1]); 
    }

    return; 
}

int main()
{
    int q; 
    cin >> n >> q; 

    n = pow(2, ceil(log2(n)));  
    tree.resize(2*n, INT_MAX); 
    for(int i = 0; i < n; i++) {
        cin >> tree[i+n]; 
    } 

    for(int i = n-1; i >= 1; i--) {
        tree[i] = min(tree[2*i], tree[2*i+1]); 
    }

    int type, a, b; 
    for(int i = 0; i < q; i++) {
        cin >> type >> a >> b; 
        if (type == 1) {
            update(a-1, b); 
        } else {
            cout << sum(a-1, b-1) << endl; 
        }
    }

    return 0; 
}

It works with first test case, but not with the second one. I've looked at other solutions online and they all look similar to mine. Please, help me spot the mistake.

Benny
  • 41
  • 4

0 Answers0