51nod 1471 small S interest sqrt

Source: Internet
Author: User

Little s likes interesting things. However, everyone's interests are unique. Little S is keen to ask himself a self-answer. One day, little s came up with a problem.

There is an array a with n positive integers and a few questions for this array. There are two types of problems:

1. On the array subscript l to R section, move a cell to the right side of the loop. The elements on the array are reassigned in the following manner.

A[l], a[l+1], ..., a[r-1], a[r]→a[r], a[l], a[l+1], ..., a[r-1].

2. On the part of the array subscript l to R, calculate how many elements have the same value as K.

Little s liked the problem and soon solved it, can you solve it?

blocks, each block maintains a linked list, can also be simulated with an array, does not contain the entire range of violent changes.

O (NSQRT (n))

The right shift is the equivalent of deleting an element at the end and adding an element at the header.

Debug for a long time, reason: In decryption l,r,k, after, it is possible L > r, this time to swap (L,R)

Code:

                                              //File Name:nod1471.cpp//Author:long//Mail: [email protected]//Created time:2016 September 15 Thursday 21:17 30 seconds#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<map>#defineLL Long Longusing namespacestd;Const intMAXN =100000+Ten;Const intNUM = -;intl[maxn/num+5],r[maxn/num+5],BELONG[MAXN];inta[maxn],tmp[num],cnt;structblock{intB[num],s[maxn],head,tot; voidBuildConst intLConst intR) {Tot= Head =0; memset (s),0,sizeofs);  for(inti=l;i<=r;i++) {B[tot++] =A[i]; S[a[i]]++; }    }    voidRightintSt) {Head--; if(Head <0) head = tot-1; CNT=B[head]; S[CNT]--; B[head]=St; S[ST]++; }    voidReset () {if(Head = =0)return ;  for(intI=0; i<tot;i++) Tmp[i] =B[i]; intU =0;  for(inti=head;i<tot;i++) b[u++] =Tmp[i];  for(intI=0; iTmp[i]; Head=0; }    voidBruteintStintLintR)        {Reset (); CNT=B[r]; S[CNT]--;  for(inti=r;i>l;i--) B[i] = b[i-1]; B[L]=St; S[ST]++; }    int Get(intk) {k= (head + k)%tot; returnB[k]; }    intQueryintk) {        returnS[k]; }    intCalintLintRintk)        {Reset (); intres =0;  for(inti=l;i<=r;i++)            if(B[i] = = k) res++; returnRes; }}BLOCK[MAXN/NUM +5];voidSolveintN) {     for(intI=1; i<=n;i++) Belong[i]= (I-1) /NUM; inttot = Belong[n] +1, u=1;  for(intI=0; i<tot;i++,u+=NUM) {L[i]=u; R[i]= min (U + NUM-1, N); }     for(intI=0; i<tot;i++) Block[i].build (L[i],r[i]); intAns =0, Q,op,x,y,k; scanf ("%d",&q);  while(q--) {scanf (" %d%d%d",&op,&x,&y); if(OP = =1) {x= (x + ans-1)% n +1; Y= (y + ans-1)% n +1; if(X >y) Swap (x, y); intBX = Belong[x],by =Belong[y]; if(BX = =by ) {CNT= Block[bx].Get(YL[BX]); Block[bx].brute (Cnt,x-l[bx],y-L[BX]); }            Else{cnt= Block[by].Get(YL[by]); Block[bx].brute (Cnt,x-l[bx],r[bx]-L[BX]);  for(inti=bx+1; i<by;i++) Block[i].right (CNT); Block[by].brute (CNT,0, YL[by]); }        }        Else{scanf ("%d",&k); X= (x + ans-1)% n +1; Y= (y + ans-1)% n +1; K= (k + ans-1)% n +1; if(X >y) Swap (x, y); //cout << x << "<< y <<" "<< k <<" "<< Endl;            intBX = Belong[x],by =Belong[y]; Ans=0; if(BX = =by ) ans= Block[bx].cal (x-l[bx],y-l[bx],k); Else{ans+ = Block[bx].cal (x-l[bx],r[bx]-l[bx],k);  for(inti=bx+1; i<by;i++) ans+=Block[i].query (k); Ans+ = Block[by].cal (0, Yl[by],k); } printf ("%d\n", ans); }    }}intMain () {intN;  while(~SCANF ("%d",&N)) {         for(intI=1; i<=n;i++) scanf ("%d", A +i);    Solve (n); }    return 0;}

51nod 1471 small S interest sqrt

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.