# Spoj templeq-temple Queues (binary lookup + tree-like array)

Source: Internet
Author: User

Test instructions

There are n teams (1 <= N <= 100,000), each team begins to have an AI individual [0 <= ai<= 100,000,000], with Q operations [0<=q<= 500,000]

The operation is divided into three types, 1 A: Indicates the addition of a person in the first queue. 2 x: Indicates the length is greater than or equal to the number of X queues. 3 Y: Represents all queues with a length greater than or equal to Y minus one person.

Exercises

Sort each queue by length

This array is maintained with a differential sequence so that the length of each queue is the prefix and. The complexity of each length is LGN, because the queue is sorted by length, so you can find the position of a length in the queue by two points, with a complexity of LGN*LGN.

Two array sa[i] records the original position of the first queue of each sorted by length. Rk[i] records the position of the queue at position I, sorted by length.

For the I queue plus a person, find the length of the I queue Len, the last plus one for all the queues of Len, so the operation is to not change the order of the queues, then it is necessary to swap the position of the next I and the last position in the queue length len.

AC Code (950MS):

`#include <bits/stdc++.h>using namespacestd;Const intN =100005;structNode {intPOS; intLen; BOOL operator< (Node x)Const {        returnLen <X.len; }} A[n];intSa[n], rk[n], bit[n];intN, Q;intLowbit (intx) {returnX &-x;}voidAddintPosintval) {     while(Pos <=N) {Bit[pos]+=Val; POS+=Lowbit (POS); }}intSumintPOS) {    intres =0;  while(POS) {res+=Bit[pos]; POS-=Lowbit (POS); }    returnRes;}intlbintx) {                                           //find the first number greater than or equal to x    intL =1, R = n +1, M;  while(L <r) {m= (L + r) >>1; if(SUM (m) < x) L = m +1; ElseR =m; }    returnR;}intMain () { while(~SCANF ("%d%d", &n, &q)) { for(inti =1; I <= N; ++i) {scanf ("%d", &A[i].len); A[i].pos=i; } sort (A+1, A +1+N);  for(inti =1; I <= N; ++i) {Sa[i]=A[i].pos; rk[A[i].pos]=i; Add (i, A[i].len-A[i-1].len); }        intch, x;  while(q--) {scanf ("%d%d", &ch, &x); if(ch = =1) {                intbp = rk[x];//the original position                intLen =sum (BP); intSP = lb (len +1) -1;//the position after the addition of oneswap (rk[SA[BP]], rk[SA[SP]);                Swap (SA[BP], sa[sp]); Add (SP,1); Add (sp +1, -1); } Else if(ch = =2) {                intAns =lb (x); printf ("%d\n", N-ans +1); } Else {                intSP =lb (x); Add (SP,-1); }        }    }    return 0;}`

Spoj templeq-temple Queues (binary lookup + tree-like array)

Related Keywords:

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.