Acdreamoj1108 (the kth number)

Source: Internet
Author: User

Question link: http://acdream.info/problem? PID = 1, 1108

Question: N number series. The number of occurrences of the number k in a certain interval in M queries. N, m <= 100000


Solution: because it is offline, it can be processed in a unified manner before being output. You can maintain a left and right pointers. Pre, pre [I] indicates the number of occurrences in the interval greater than or equal to I. To reduce the complexity, the key is the moving method of left and right, that is, how to sort the query interval. If the range is sorted by the Left endpoint, the maximum return of the right endpoint is N at a time, if you sort by the right endpoint, the left endpoint must be n at most each time. There is a good solution here, that is, fuzzy sorting. The left endpoint is not strictly ordered, that is, divided by SQRT (N) and then sorted. In this way, the maximum complexity is N * SQRT (n)


Code:

/******************************************************* @author:xiefubao*******************************************************/#pragma comment(linker, "/STACK:102400000,102400000")#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <queue>#include <vector>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <string.h>//freopen ("in.txt" , "r" , stdin);using namespace std;#define eps 1e-8#define zero(_) (abs(_)<=eps)const double pi=acos(-1.0);typedef long long LL;const int Max=100010;const int INF=1e9+7;int num[Max];int help[Max];int r[Max];int l[Max];int k[Max];int pre[Max];int cnt[Max];int tool;bool cmp(int i,int j){    if(l[i]/tool==l[j]/tool&&r[i]!=r[j])        return r[i]<r[j];    return l[i]<l[j];}int ans[Max];int n,m;int findans(int t){    int l=1,r=n;    while(l<=r)    {        int middle=(l+r)/2;        if(pre[middle]>=t)        l=middle+1;        else            r=middle-1;    }    return l-1;}int main(){    int t;    cin>>t;    while(t--)    {        scanf("%d%d",&n,&m);        tool=sqrt(n);        for(int i=0; i<n; i++)            scanf("%d",num+i),help[i]=i;        for(int i=0; i<m; i++)            scanf("%d%d%d",l+i,r+i,k+i),l[i]--,r[i]--;        sort(help,help+m,cmp);        memset(cnt,0,sizeof cnt);        memset(pre,0,sizeof pre);        int left=0,right=-1;        for(int i=0;i<m;i++)        {           int L=l[help[i]],R=r[help[i]];           while(left<L){ pre[cnt[num[left++]]--]--;}           while(L<left){ pre[++cnt[num[--left]]]++;}           while(right<R){ pre[++cnt[num[++right]]]++;}           while(R<right){ pre[cnt[num[right--]]--]--;}           ans[help[i]]=findans(k[help[i]]);        }        for(int i=0;i<m;i++)            printf("%d\n",ans[i]);    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.