mercredi 24 août 2016

Segment tree difficulty on hackerearth tutorial for same giving wrong answer

This is regarding the following problem given at the end of tutorial to check understanding -Problem statement at end of page of this link

For ease the problem is stated as follows ( you can use that link to check if accepted )-

Problem statement:

Given an array A of size N, there are two types of queries on this array.

  1. q l r: In this query you need to print the minimum in the sub-array A[l:r].

  2. u x y: In this query you need to update A[x]=y.

Input:

First line of the test case contains two integers, N and Q, size of array A and number of queries. Second line contains N space separated integers, elements of A. Next Q lines contain one of the two queries.

Output:

For each type 1 query, print the minimum element in the sub-array A[l:r].

Contraints:

1 ≤ N, Q, y ≤ 105

1 ≤ l, r, x ≤ N

Code:

#include<bits/stdc++.h>

using namespace std;

#define MAXK 100000


int segtree[2*MAXK];
int input[MAXK];

void init()
{
    for(int i=0;i<2*MAXK;i++)
    {
        segtree[i] = MAXK+1;
    }
}

void build(int low, int high, int pos)
{
    if(low == high)
    {
        segtree[pos] = input[low]; return;
    }
    else
    {
        int mid = (low+high)/2;
        build(low,mid,2*pos+1);
        build(mid+1,high,2*pos+2);
        segtree[pos] = min(segtree[2*pos+1],segtree[2*pos+2]);
    }
}


int query(int pos, int nodel,int noder, int l, int r)
{
    //no overlap
    if(nodel > r || noder < l)
    {
        return MAXK+1;
    }
    //complete overlap
    if(nodel >= l && noder <= r)
    {
        return segtree[pos];
    }
    //partial overlap
    int mid = (nodel + noder)/2;
    int p1  = query(2*pos+1,nodel,mid,l,r);
    int p2  = query(2*pos+2,mid+1,noder,l,r);
    return min(p1,p2);
}

void update(int pos, int nodel, int noder, int index, int value)
{
    if(nodel == noder)
    {
        input[index] = value;
        segtree[pos] = value;
    }
    else
    {
        int mid = (nodel + noder)/2;
        if(index >= nodel && index <= mid)
        {
            update(2*pos + 1, nodel, mid, index, value);
        }
        else
        {
            update(2*pos + 2, mid+1, noder, index, value);
        }
        segtree[pos] = min(segtree[2*pos+1], segtree[2*pos+2]);
    }
}

int main(){
    init();
    int n,q;cin>>n>>q;
    for(int i=0;i<n;i++)
    {
        int x;cin>>x;input[i] = x;
    }
    build(0,n-1,0);
    while(q--)
    {
        char ch;
        int l,r;
        cin>>ch>>l>>r;
        if(ch == 'q')
        {
            cout<<query(0,0,n-1,l-1,r-1)<<endl;
        }
        else
        {
            update(0,0,n-1,l-1,r-1);
        }
    }

    return 0;
}

When I submit this code on the hackerearth tutorial page (link given above), its getting partially accepted. It is giving wrong answer for many cases. You can submit the code and check the difference between my output and expected output.

And thank you for going through all this stuff to solve my problem. I really appreciate.

Aucun commentaire:

Enregistrer un commentaire