You are not logged in. Please login at www.codechef.com to post your questions!

×

Segment tree.Not getting accepted for few test cases.contraints are large

//x,y are the range in which queries are to be calculated //n is the length of array //m is the no. of queries

import java.io.; import java.util.;

class Segmenttree {

public static void main(String[] args) throws IOException{
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    int n=Integer.parseInt(br.readLine());
    String s[]=br.readLine().split(" ");           
    int arr[]=new int[n];
    for(int i=0;i<n;i++)
        arr[i]=Integer.parseInt(s[i]);

    String s1[]=br.readLine().split(" ");
    int m=Integer.parseInt(s1[0]);
    int x=Integer.parseInt(s1[1]);
    int y=Integer.parseInt(s1[2]);

    //int l=(int)(Math.ceil(Math.log(n)))+1;
    //int size=2*(int)(Math.pow(2, l));

    //System.out.println(l+"  "+size);
    int tree[]=new int[100000000];

    Segmenttree obj=new Segmenttree();
    obj.buildtree(1,0,n-1,arr,tree);

    int max1,min1,sum=0;
    //for(int i=0;i<=size;i++)
      //  System.out.println(tree[i]);
   // int z=1;
     while(m-->0)
    {                               
        max1=Math.max(x,y);
        min1=Math.min(x,y);    
        int k=obj.query(1,0,n-1,min1,max1,tree);
        sum=sum+k;

       // z++;
        x=(x+7)%(n-1);
        y=(y+11)%n;

    }
     System.out.println(sum);
}
public void buildtree(int node,int a,int b,int arr[],int tree[])
{         
    if(a>b)
        return;
    if(a==b){
        tree[node]=arr[a];

        return;
    }
    buildtree(node*2,a,(a+b)/2,arr,tree);
    buildtree(node*2+1,(a+b)/2 +1,b,arr,tree);

    tree[node]=Math.max(tree[node*2], tree[(node*2) +1]);

}
public int query(int node,int a,int b,int i,int j,int tree[])
{

    if(a>b || a>j || b<i){                    
        return Integer.MIN_VALUE;
    }
    if(a>=i && b<=j)
        return tree[node];

    int q1=query(node*2,a,(a+b)/2,i,j,tree);
    int q2=query(1+node*2,1+(a+b)/2,b,i,j,tree);

    int res=Math.max(q1, q2);

    return res;
}

}

asked 12 Apr '15, 03:10

abhaytibrewal7's gravatar image

2★abhaytibrewal7
1
accept rate: 0%


Segment tree will cost you O(logn) time per query and the solution will not pass. So, you have to use sparse table. In sparse table, preprocessing will cost you O(nlogn) and constant time per query.

link

answered 13 Apr '15, 18:06

rohanagarwal's gravatar image

5★rohanagarwal
1009
accept rate: 5%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,755
×1,404
×1,302

question asked: 12 Apr '15, 03:10

question was seen: 993 times

last updated: 13 Apr '15, 18:06