[BZOJ4552] [tjoi2016&&heoi2016] Sort

Source: Internet
Author: User

Tags: partial can pre full array BBS submit two-point End column sort

4552: [tjoi2016&heoi2016] sort time limit:60 sec  memory limit:256 MB
submit:1697  solved:883
[ Submit][status][discuss] description in 2016, her sister liked the number sequence. So he often studies some weird questions about sequences, and now he's studying a problem that needs you to help him. This puzzle is like this: given a 1 to n full arrangement, now the full permutation sequence of M-order, sorted into two kinds: 1: (0,l,r) for the interval [l,r] of the number ascending sort 2: (1,l,r) means the interval [l,r] The number in descending order finally asks the number at position Q. Input inputs the first behavior of the data two integers n and M. n indicates the length of the sequence, and M indicates the number of local sorts. 1 <= N, M <= 10^5 the second behavior n integers, representing a full array of 1 to N. Next enter M line, each line has three integer op, L, R, op 0 for ascending sort, op 1 for descending sort, L, R for sorting interval. Finally, enter an integer q,q to indicate where to ask after sorting, 1 <= q <= N. 1 <= n <= 10^5,1 <= m <= 10^5output

output data has only one row, an integer that represents the number in the order in which the whole part is sorted after the end of the Q position.

Sample Input 6 3
1 6 2 5 3 4
0 1 4
1 3 6
0 2 4
3 Sample Output 5 hintsource

Overall difficult, is a more natural thinking, but easy to go offline sorting and other wrong direction of thinking, once you know the general idea is good to do.

First we know that for a 01 sequence ordering, the single order complexity can be done only with the log level if the line tree is maintained.

This problem has only one query, so offline meaningless, and a query let us naturally think of the two-point answer. First two to distinguish the number of this position, and then all the number of less than equals is all assigned to 0, the rest is assigned to 1, so that each sort is 01 sequence sorted. If the number at the last p position is 0, the final answer is less than or equal to the current two-point answer, and vice versa.

This solves the problem within the complexity of \$o (n \log^2 N) \$.

`1#include <cstdio>2#include <algorithm>3 #defineLS (x<<1)4 #defineRS ((x<<1) |5 #defineLson Ls,l,mid6 #defineRson Rs,mid+1,r7 #defineRep (i,l,r) for (int i=l; i<=r; i++)8 using namespacestd;9 Ten Const intn=100100; One intn,m,qry,a[n],c[n],sm[n<<2],tag[n<<2]; A structp{intOp,l,r;} B[n]; -  - voidPushintXintLintR) { the     if(tag[x]==-1)return; -     intMid= (l+r) >>1; -Sm[ls]= (mid-l+1) *tag[x]; tag[ls]=Tag[x]; -sm[rs]= (R-mid) *tag[x]; tag[rs]=Tag[x]; +tag[x]=-1; - } +  A voidBuildintXintLintR) { attag[x]=-1; -     if(L==R) {Sm[x]=c[l];return; } -     intMid= (l+r) >>1; - build (Lson); build (Rson); -sm[x]=sm[ls]+Sm[rs]; - } in  - voidMdfintXintLintRintLintRintk) { to     if(L==l && r==r) {sm[x]=k* (r-l+1); Tag[x]=k;return; } +     intMid= (l+r) >>1; push (x,l,r); -     if(r<=mid) MDF (lson,l,r,k); the     Else if(l>mid) MDF (rson,l,r,k); *         ElseMDF (lson,l,mid,k), MDF (rson,mid+1, r,k); \$sm[x]=sm[ls]+Sm[rs];Panax Notoginseng } -  the intQueintXintLintRintLintR) { +     if(L==l && r==r)returnSm[x]; A     intMid= (l+r) >>1; push (x,l,r); the     if(R<=mid)returnque (lson,l,r); +     Else if(L>mid)returnque (rson,l,r); -         Else returnQue (lson,l,mid) +que (rson,mid+1, R); \$ } \$  - intMain () { -scanf"%d%d",&n,&m); theRep (I,1, N) scanf ("%d",&a[i]); -Rep (I,1, m) scanf ("%d%d%d",&b[i].op,&b[i].l,&B[I].R);Wuyiscanf"%d",&qry); the     intL=1, r=N; -      while(l<R) { Wu         intMid= (l+r) >>1; -Rep (I,1-nhif(A[i]<=mid) c[i]=0;Elsec[i]=1; AboutBuild1,1, n); \$Rep (I,1, M) { -             intL=b[i].l,r=b[i].r,s=que (1,1, n,l,r); -             if(b[i].op==0){ -                 if(l<=r-s) MDF (1,1, N,l,r-s,0); A                 if(r-s+1&LT;=R) MDF (1,1, n,r-s+1R1); +}Else{ the                 if(l<=l+s-1) MDF (1,1, n,l,l+s-1,1); -                 if(L+S&LT;=R) MDF (1,1, N,l+s,r,0); \$             } the         } the         if(Que (1,1, n,qry,qry) = =0) R=mid;ElseL=mid+1; the     } theprintf"%d\n", L); -     return 0; in}`

[BZOJ4552] [tjoi2016&&heoi2016] Sort

Related Keywords:
Related Article