Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8357397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:27:50+00:00 2026-06-09T10:27:50+00:00

I am trying to solve SPOJ problem GSS1 (Can you answer these queries I)

  • 0

I am trying to solve SPOJ problem GSS1 (Can you answer these queries I) using segment tree. I am using ‘init’ method to initialise a tree and ‘query’ method to get maximum in a range [i,j].

Limits |A[i]| <= 15707 and 1<=N (Number of elements)<=50000.

int A[50500], tree[100500];

void init(int n, int b, int e) // n=1, b=lower, e=end
{
    if(b == e)
    {
        tree[n] = A[b];
        return;
    }
    init(2 * n, b, (b + e) / 2);
    init(2 * n + 1, ((b + e) / 2) + 1, e);

    tree[n] = (tree[2 * n] > tree[2 * n + 1]) ? tree[2 * n] : tree[2 * n + 1];
}

int query(int n, int b, int e, int i, int j) // n=1, b=lower, e=end, [i,j]=range
{
    if(i>e || j<b) 
        return -20000;
    if(b>=i && e<=j)  
        return tree[n];

    int p1 = query(2 * n, b, (b + e) / 2, i, j);
    int p2 = query(2 * n + 1, ((b + e) / 2) + 1, e, i, j);

    return (p1 > p2) ? p1 : p2;
}

Program is giving Wrong Answer.I debbuged code for most of the cases (negative numbers, odd/even N) but I am unable to figure out what is wrong with algorithm.

If anyone can point me in right direction, I would be grateful.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T10:27:52+00:00Added an answer on June 9, 2026 at 10:27 am

    Edit: it seems your implementation is also correct, I’m just having another. And we both misread the problem statement.


    I guess you call your query function with params

    query( 1, 0, n-1, x-1, y-1 );
    

    I believe that it’s wrong to handle with segment tree in such way when your n is not a pow of 2.
    I’m offering you to

    1. enlarge tree array to 131072 elements (2^17) and A to 65536 (2^16);
    2. found the smallest k such is not smaller than n and is a pow of 2;
    3. initialize elements from n (0-based) to k-1 with -20000;
    4. make n equal to k;
    5. make sure you call init(1,0,n-1);

    Hope that’ll help you to beat WA.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to solve this SPOJ problem . The question asks to find
edit: I was trying to solve a spoj problem. Here is the link to
I was trying to solve this problem on SPOJ (http://www.spoj.pl/problems/REC/) F(n) = a*F(n-1) +
I am trying to solve this problem http://www.spoj.pl/problems/PEBBMOV/ . I think I have the
I was trying to solve this problem - http://www.spoj.pl/problems/LISA/ I thought of Greedy initially,
I am trying to solve the problem Secret Code on SPOJ , and it's
I am trying to solve this problem : https://www.spoj.pl/problems/CERC07S/ I have identified that i
Trying to solve a very simple problem using mvvm-light, but after days of sifting
I am trying to solve a specific problem using prologs constraint solvers, and I'm
I am currently trying to solve a problem, that I have using Hibernate 4

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.