100% JAVA  solution with segment tree, Moore vote , and binary search
This java solution using segment tree to find the query field. \nUsing Moore vote algorith to merge query reault in O(1) time. \nAnd using binary search to find
This java solution using segment tree to find the query field. \nUsing Moore vote algorith to merge query reault in O(1) time. \nAnd using binary search to find the actual frequency of the candidate num, in each specified query. \n\nfeel free to leave any questions!\n```\nclass MajorityChecker {\n\n    class STNode{\n      int majority,count,L,R;\n      STNode left,right;\n\n      public STNode(int L,int R){\n        this.L = L;\n        this.R = R;\n        left = right = null;\n        majority = count=0;\n      }\n\n      public void pushDown(){\n        if (L==R)\n          return;\n        if (left==null){\n          int mid = (L+R)>>1;\n          left = new STNode(L,mid);\n          right = new STNode(mid+1,R);\n        }\n      }\n\n      public void updateByVal(int M,int C){\n        majority = M;\n        count = C;\n      }\n\n      public void updateFromSon(){\n        if (left.majority==right.majority){\n          majority = left.majority;\n          count = left.count+right.count;\n        }\n        else{\n          majority = left.count>=right.count?left.majority:right.majority;\n          count = Math.abs(left.count-right.count);\n        }\n      }\n    }\n\n    private STNode root;\n    private Map<Integer,List<Integer>> idx;\n\n    public MajorityChecker(int[] arr) {\n      root = new STNode(0,arr.length-1);\n      idx = new HashMap<>();\n      for (int i=0;i<arr.length;i++){\n        idx.computeIfAbsent(arr[i],a->new ArrayList<>()).add(i);\n        updateSingle(root,i,arr[i]);\n      }\n    }\n\n    private void updateSingle(STNode root,int pos,int val){\n      if (root.L == root.R){\n        root.updateByVal(val,1);\n        return;\n      }\n      int mid = (root.L+root.R)>>1;\n      root.pushDown();\n      if (pos<=mid)\n        updateSingle(root.left,pos,val);\n      else\n        updateSingle(root.right,pos,val);\n      root.updateFromSon();\n    }\n\n    public int query(int left, int right, int threshold) {\n      int[] M =new int[1], C = new int[]{-1};\n      query(root,left,right,M,C);\n      int freq = getFreq(M[0],left,right);\n      return freq>=threshold?M[0]:-1;\n    }\n\n    private void query(STNode root,int left,int right,int[] M,int[] count){\n      if (root.R < left || root.L > right)\n        return;\n      if (left <= root.L && root.R <= right){\n        mergeQuery(M,count,root.majority,root.count);\n        return;\n      }\n      query(root.left,left,right,M,count);\n      query(root.right,left,right,M,count);\n    }\n\n    private void mergeQuery(int[] majority,int[] count,int curM,int curC){\n      if (count[0]==-1){\n        majority[0]=curM;\n        count[0] = curC;\n      }\n      else if (majority[0]==curM)\n        count[0]+= curC;\n      else{\n        majority[0] = count[0]>=curC?majority[0]:curM;\n        count[0] = Math.abs(count[0]-curC);\n      }\n    }\n\n    public int getFreq(int target,int left,int right){\n      List<Integer> index = idx.get(target);\n      int rightIdx = findIdx(index,right),leftIdx = findIdx(index,left);\n      if (rightIdx<0)\n        return 0;\n      else if (leftIdx<0)\n        return rightIdx-leftIdx;\n      else if (index.get(leftIdx)==left)\n        return rightIdx-leftIdx+1;\n      else\n        return rightIdx-leftIdx;\n    }\n\n    private int findIdx(List<Integer> idx,int pos){\n      int start = 0,end = idx.size()-1,mid;\n      while (start<=end){\n        mid = (start+end)>>1;\n        if (idx.get(mid)<pos)\n          start = mid +1;\n        else if (idx.get(mid)>pos)\n          end = mid-1;\n        else\n          return mid;\n      }\n      return end;\n    }\n  }
[]