//先排序,然后两次二分搜索找到喜好值==k的区间,统计即可 import java.util.*; class ac { int value=0; int num=0; public ac(int num,int value) { this.value=value; this.num=num; } } public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n= sc.nextInt(); ac[] a= new ac[n]; for(int i=0;i<n;i++){ int k=sc.nextInt(); a[i]=new ac(i+1, k);} Arrays.sort(a,new Comparator<ac>() { @Override public int compare(ac o1, ac o2) { // TODO Auto-generated method stub return Integer.compare(o1.value, o2.value); } }); int q=sc.nextInt(); for(int i=0;i<q;i++){ int l=sc.nextInt(); int r=sc.nextInt(); int k=sc.nextInt(); int left=0; int right=a.length-1; while(left<=right){ int mid=(right-left)/2+left; if(a[mid].value<=k)left=mid+1; else right=mid-1; } int rindex=right;left=0; while(left<=right){ int mid=(right-left)/2+left; if(a[mid].value>=k)right=mid-1; else left=mid+1; } right=rindex; int res=0; while(left<=right){ if(a[left].num>=l && a[left].num<=r)res++;left++; } System.out.println(res); } } } }//最后沉痛悼念我0%的第一题,做懵逼了,以后想不到就暴力先试试
点赞 评论

相关推荐

牛客771574427号:恭喜你,华杰
点赞 评论 收藏
分享
牛客网
牛客企业服务