Similar Pair _ HackerRank

Hacker rank真的比leetcode 难了不止一个等级。。


这题有点巧妙。。深度搜索每条路径,然后枚举,基本很多人都想的出来,但是关键在于这样肯定超时。巧妙之处在于要给每条路径建立一个线段树来加速查询,每次similar查询复杂度从O(h)变成O(lgh)。。


犯了两个错误

(1)要用long来存储线段树,以及可能的similar pairs。

 (2)值减去T可能小于0,值加上T也有可能大于n。


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;

public class Solution {
	
	public static LinkedList[] nodes = new LinkedList[100002];
	static int n , t, root;

	

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    	
    	Scanner scan = new Scanner(System.in);
    	
    	n = scan.nextInt();
    	t = scan.nextInt();
    	long[] stree = new long[4*n+1];
    	
    	for(int i=1;i<=n;i++)
    		nodes[i] = new LinkedList();
    	
    	int[] idegree = new int[n+1];
    	
    	for(int i=1;i n) ? n : nodeval + t;
    	
    	pairs[0] += query(stree,1,1,n,min, max);
    	
    	updateTree(stree,1,1,n,nodeval,1);
    	
    	for(int chd : nodes[nodeval]){
    		depthSearch(chd, stree, pairs);
    	}
    	
    	updateTree(stree,1,1,n,nodeval,-1);
    }
    
    
    public static void updateTree(long[] tree, int node,int tl, int tr, int val, long opt){
    		if(val < tl || val > tr || tl > tr)
    			return;
    		
    		tree[node] += opt;
    		
    		int m = (tl + tr) >> 1;
    		
    		if(tl == tr)
    			return;
    		else if(val <= m)
    			updateTree(tree,node<<1,tl,m,val,opt);
    		else
    			updateTree(tree,node<<1|1,m+1,tr,val,opt);
    }
   
    
    public static long query(long[] tree, int node, int tl, int tr, int min, int max){
    	
    	if(max < tl || min > tr)
    		return 0;
    	
    	else if(max == tr && min == tl)
    		return tree[node];
    	
    	else{
    		int mid = (tl + tr) >> 1;
    		int lmax = (mid < max) ? mid : max;
    		int rmin = (min > mid) ? min : mid + 1;
    		return query(tree,node<<1, tl, mid, min, lmax) + query(tree,node<<1|1, mid+1, tr, rmin, max);
    	}
    	
    }
}


你可能感兴趣的:(Algorithm)