×

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

 0 //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;i0) { 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 && 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 1 accept rate: 0%

 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. answered 13 Apr '15, 18:06 100●9 accept rate: 5%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• 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