I cant find the error here... its giving 0 0 as output for all input cases. plz help.

#include<stdio.h>
#define max(a,b) a>b?a:b
void build(int ai,int l,int r);
int query(int ai,int l,int r,int i,int j,int count);
void update(int ai,int l,int r,int i,int j);
int aa,b,m;
int inp[6];

struct arr
{
int val;
int ct;
}a[15];
int main()
{
int i,nn;
int ai;
inp[0]=-1;
for(i=1;i<6;i++)
scanf("%d",&inp[i]);

for(ai=0;ai<15;ai++)
{

a[ai].val=0;
a[ai].ct=0;
}
build(1,1,5);
printf("biuld done   ");
nn=query(1,1,5,1,2,0);
printf("query done");
printf("%d   ",nn);
update(1,1,5,1,3);
printf("%d",nn);
return 0;
}
void build(int ai,int l,int r){
if(l==r){
a[ai].val=inp[l];
return;
}printf("in b  ");
build(2*ai,l,(l+r)/2);
build(2*ai+1,(l+r)/2+1,r);
a[ai].val=max(a[2*ai].val,a[2*ai+1].val);
}
int query(int ai,int l,int r,int i,int j,int count){
if(i<=l&&r<=j){
if(l==r){
return a[ai].val+count;
}
else
return m+count;
}
if(r<i||j<l)
return -1;
aa=query(2*ai,l,(l+r)/2,i,j,count+a[ai].ct);
b=query(2*ai+1,(l+r)/2+1,r,i,j,count+a[ai].ct);
m=max(aa,b);
}
void update(int ai,int l,int r,int i,int j){
if(i<=l&&r<=j){
a[ai].ct++;
a[ai].val++;
return;
}
if(r<i||j<l)
return;
update(2*ai,l,(l+r)/2,i,j);
update(2*ai+1,(l+r)/2+1,r,i,j);
a[ai].val=max(a[2*ai].val,a[2*ai+1].val)+a[ai].ct;
}

Dani AI

Generated

Brief diagnosis: several deterministic bugs explain the all-zero outputs. The segment-tree storage is too small (writing past a[15] causes undefined behavior), the query routine has execution paths that do not return a value and relies on globals (aa, b, m) which invite UB, the full-cover branch returns m+count instead of the node value, and the max macro lacks parentheses. Also main prints the old nn after update without calling query again. 's point about formatting is valid — clearer layout makes these issues easier to spot.

Concrete fixes (in order of impact)

  • Allocate the tree with capacity >= 4n (e.g., `arr tree[4MAXN]`) so recursive writes cannot overflow the array.
  • Replace global temporaries with local return values. Every branch of query must return an int.
  • When a node is completely inside the query range, return the node’s stored value plus any carried count (not an uninitialized global).
  • Use a safe max definition or an inline helper so operator precedence cannot break comparisons.
  • After calling update, call query again to obtain the new value (printing the old nn will naturally show no change).

Minimal corrected pattern to follow:

#define MAXN 5
#define MAXT (4*MAXN)
#define max(a,b) ((a) > (b) ? (a) : (b))

int query(int ai,int l,int r,int i,int j,int carry){
    if (r < i || j < l) return -1;
    if (i <= l && r <= j) return a[ai].val + carry;
    int mid = (l + r) / 2;
    int left = query(2*ai, l, mid, i, j, carry + a[ai].ct);
    int right = query(2*ai+1, mid+1, r, i, j, carry + a[ai].ct);
    return (left > right) ? left : right;
}

Final notes: initialize buffers, check scanf results, and run under a memory checker (valgrind) or add temporary printf traces to catch out-of-bounds writes. Addressing these fixes will remove undefined behavior and make the segment-tree logic produce correct results.

What compiler and operating system are you using? The code's formatting is the most horrible that I've seen posted here in the past 10 years! Format it properly and someone might actually try to help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.