0

So I need some help again.I recently started doing medium level problems on codechef and hence I am getting TLE quite a lot.

So basically the question is to find the sum of of multiple maximum range queries given in the question.The initial range is given and the next values are calculated by a formula which is given in the problem.

I used segment trees to solve the problem,but I keep getting TLE for some sub-tasks.Please help me optimize this code.

Problem link- https://www.codechef.com/problems/FRMQ

//solved using segment tree
#include <stdio.h>
#define gc getchar_unlocked
inline int read_int()  //fast input function 
{
    char c = gc();
    while(c<'0' || c>'9') 
        c = gc();
    int ret = 0;
    while(c>='0' && c<='9') 
    {
        ret = 10 * ret + c - '0';
        c = gc();
    }
    return ret;
}
int min(int a,int b)
{
    return (a<b?a:b);
}
int max(int a,int b)
{
    return (a>b?a:b);
}
void construct(int a[],int tree[],int low,int high,int pos)  //constructs 
{                                          //the segment tree by recursion
    if(low==high)
    {
        tree[pos]=a[low];
        return;
    }
    int mid=(low+high)>>1;
    construct(a,tree,low,mid,(pos<<1)+1);
    construct(a,tree,mid+1,high,(pos<<1)+2);
    tree[pos]=max(tree[(pos<<1)+1],tree[(pos<<1)+2]);
}
int query(int tree[],int qlow,int qhigh,int low,int high,int pos)
{   //function finds the maximum value using the 3 cases
    if(qlow<=low && qhigh>=high)
        return tree[pos];            //total overlap
    if(qlow>high || qhigh<low)
        return -1;                   //no overlap
    int mid=(low+high)>>1;           //else partial overlap
    return max(query(tree,qlow,qhigh,low,mid,(pos<<1)+1),query(tree,qlow,qhigh,mid+1,high,(pos<<1)+2));
}
int main()
{
    int n,m,i,temp,x,y,ql,qh;
    long long int sum;
    n=read_int();
    int a[n];
    for(i=0;i<n;i++)
        a[i]=read_int();
    i=1;
    while(temp<n)       //find size of tree
    {
        temp=1<<i;
        i++;
    }
    int size=(temp<<1)-1;
    int tree[size];
    construct(a,tree,0,n-1,0);
    m=read_int();
    x=read_int();
    y=read_int();
    sum=0;
    for(i=0;i<m;i++)
    {
        ql=min(x,y);
        qh=max(x,y);
        sum+=query(tree,ql,qh,0,n-1,0);
        x=(x+7)%(n-1);     //formula to generate the range of query
        y=(y+11)%n;
    }
    printf("%lld",sum);
    return 0;
}
gospelslide
  • 179
  • 2
  • 2
  • 12

2 Answers2

0

Several notes:

  1. It's great you are using fast IO routines.
  2. Make sure you do NOT use modulo operation, because it is VERY slow. To calculate remainder, simply subtract N from the number until it becomes less that N. This would work much faster.
  3. Your algorithm works in O((M+N) * log N) time, which is not optimal. For static RMQ problem, it is better and much simpler to use sparse table. It needs O(N log N) space and O(M + N log N) time.
stgatilov
  • 5,333
  • 31
  • 54
0

Well , I think to get 100 points you need to use sparse table.

I tried to optimize your code https://www.codechef.com/viewsolution/7535957 (run time decreased from 0.11 sec to 0.06 sec) but still not enough to pass subtask 3..

V K
  • 33
  • 6