the question of the program i have posted...

There are N numbers a[0],a[1]..a[N - 1]. Initally all are 0. You have to perform two types of operations :
1) Increase the numbers between indices A and B by 1. This is represented by the command "0 A B"
2) Answer how many numbers between indices A and B are divisible by 3. This is represented by the command "1 A B".
Input :
The first line contains two integers, N and Q. Each of the next Q lines are either of the form "0 A B" or "1 A B" as mentioned above.
Output :
Output 1 line for each of the queries of the form "1 A B" containing the required answer for the corresponding query.
Sample Input :
4 7
1 0 3
0 1 2
0 1 3
1 0 0
0 0 3
1 3 3
1 0 3

Sample Output :
4
1
0
2

Constraints :
1 <= N <= 100000
1 <= Q <= 100000
0 <= A <= B <= N - 1

it is giving run time error..earlier there is time limit exceeded error.
i want to know how should one approach with such a high range of input. time limit exceeded and run time error are most common errors i am facing with such hight input questions.
please explain...!!!

Edited 5 Years Ago by peter_budo: Please do not use CODE SNIPPETS to post your question. Use FORUM THREAD instead

Please post the specific runtime error you're getting and also your code; we can't help if we don't know what you're doing.

this is my code....

#include<iostream>
 
using namespace std;
/*#define  N1 1000000
#define a[N1]
#define C[N1][3]*/
int a[1000000]={0};
int C[1000000][3];
int main()
{
long N,Q,A,B,count,A1,B1;
int i,j,k,i1,j1;
cin>>N>>Q;
/*for(i=0;i<N;i++)
 {a[i]=0;}*/
 
for(j=0;j<Q;j++)
{ for(k=0;k<3;k++)
   {cin>>C[j][k];
   }
  if(C[j][0]==0)
   { A=C[j][1];
     B=C[j][2];
    for(i1=A;i1<=B;i1++)
      {a[i1]+=1;}
   }
  if(C[j][0]==1)
  {count=0;
   A1=C[j][1];
   B1=C[j][2];
   for(j1=A1;j1<=B1;j1++)
    {if(a[j1]%3==0)
     {count++;}
     }cout<<"\n"<<count;
   }
 }
/* for(o=0;o<=o1;o++)
     { cout<<"\n"<<output[o];}
*/
 
return 0;
}

actually run time error was coming when i was declaring array 'a' inside main().
but now its showing time limit exceeded. i mean the site on which i am checking my solution showing "time limtit exceeded".
i know the array size is very big and using this big size in the loop basically the main reason of time limit exceeded.
there are many questions of such big range and i have same time problem.
how do i overcome this???

this code is giving me run time error now

#include<iostream>

int a[10000]={0};
using namespace std;
int main()
{ 
    int i,c,n,q,x,y,z;
    cin>>n>>q;
    while(q>0)
    { q--;c=0;
      cin>>x>>y>>z;
      if(x==0)
        {  for(i=0;i<n;i++)
            { if(i>=y&&i<=z)
                a[i]++;
            }   
        }
        else
        { for(i=0;i<n;i++)
          if(i>=y&&i<=z)
            if(a[i]%3==0)
               c++;
          cout<<c<<endl;  
        }
    
   }
}

and the code i have posted earlier is giving time limit exceeded error.

above codes are my two logics to solve the problem.

this code is giving me run time error now

and the code i have posted earlier is giving time limit exceeded error.

above codes are my two logics to solve the problem.

I've finally had a chance to test your code--sorry for the delay!

Both versions of your code build, run, and work as expected for me... I don't see any runtime errors. Have you tried running them yourself, i.e., not through the site you're using to check your solution?

Again, posting the specific text of this mysterious runtime error would be a big help.

int a[10000]={0};

Constraints :
1 <= N <= 100000

Most likely the fault is when N exceeds 10000 you've allocated.

yes..i m ..

In that case, I'm not sure what is wrong here... Looking at the site you're submitting to, I see from the comments that other people have been having trouble with apparently working code as well. There are quite a few "runtime error(SIGSEGV)" results--this sounds like something you should take up with whoever manages the site.

As for the "time limit exceeded" error, the limit is only one second... your original code had an array size of 1000000 (one million); that might have had something to do with it.

then how should i approach it ...i mean there are successful submissions of this problem too but i didnot understand their approach.
what will u do to make it successful.???

This article has been dead for over six months. Start a new discussion instead.