Bzoj 4552 [tjoi2016&heoi2016] sort | Two-part answer line tree

Source: Internet
Author: User

Topic link problem surface
Title 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: give a 1 to n full arrangement, now the full permutation sequence of M-local ordering, sorted into two kinds:
1: (0,l,r) indicates the ascending order of the number of the interval [l,r]
2: (1,l,r) indicates that the number of the interval [l,r] is sorted in descending order and the number at position Q is finally queried.

Input output Format input format:

The first behavior of the input data is 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^5

Output format:

The output data has only one row, and an integer that represents the number at the Q position after the whole part is sorted in order.

Exercises

This question is very interesting = =
Positive solution is actually a two-point answer!

Because there is only one position to ask for the final question, the number of this position is directly two points.
Then modify the original array-the original array of the answer of the larger than the number of two to 1, less than equal to 0.
The entire sorting process is then simulated with a segment tree. It is simple to sort the interval of a 01 sequence-how many 1 are in the number of intervals, and then (if ordered) to change the corresponding number of the right to 1, and the remainder to 0.
After the simulation is executed once you can know that the actual queue after the query location should be more than your two answers big or small, continue to two points.

#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <iostream>#include <vector>#define Space Putchar (")#define ENTER Putchar ('\n ')typedef Long Longllusing namespaceStdTemplate<classT>voidRead (T &x) {CharCBOOLOP =0; while(c = GetChar (), C <' 0 '|| C >' 9 ')if(c = ='-') op =1; x = C-' 0 '; while(c = GetChar (), C >=' 0 '&& C <=' 9 ') x = x *Ten+ C-' 0 ';if(OP) x =-X.;}Template<classT>voidWrite (T x) {if(X <0) Putchar ('-'), x =-X;if(x >=Ten) Write (X/Ten); Putchar (' 0 '+ x%Ten);}Const intN =100005;intN, M, A[n], Val[n], POS, op[n], ql[n], qr[n];intdata[4*n];BOOLlazy[4*n], tag[4*n];voidBuildintKintLintR) {Tag[k] =0;if(L = = r)return(void) (Data[k] = val[l]);intMid = (L + r) >>1; Build (k <<1, L, mid); Build (k <<1|1, Mid +1, R); Data[k] = data[k <<1] + data[k <<1|1];}voidPushdown (intKintLintR) {if(!tag[k])return;intMid = (L + r) >>1; Data[k <<1] = lazy[k] * (Mid-l +1); Data[k <<1|1] = lazy[k] * (R-MID); Lazy[k <<1] = Lazy[k <<1|1] = Lazy[k]; Tag[k <<1] = Tag[k <<1|1] =1; TAG[K] =0;}voidChangeintKintLintRintQlintQrintx) {if(QL <= l && qr >= R)return(void) (Data[k] = x * (r-l +1), lazy[k] = x, tag[k] =1); Pushdown (k, L, R);intMid = (L + r) >>1;if(QL <= mid) change (k <<1, L, Mid, QL, QR, x);if(QR > Mid) change (k <<1|1, Mid +1, R, QL, QR, x); Data[k] = data[k <<1] + data[k <<1|1];}intQueryintKintLintRintQlintQR) {if(QL <= l && qr >= R)returnDATA[K]; Pushdown (k, L, R);intMid = (L + r) >>1, ret =0;if(QL <= mid) ret + = query (k <<1, L, Mid, QL, QR);if(QR > Mid) ret + = query (k <<1|1, Mid +1, R, QL, QR);returnRET;}intMain () {read (n), read (m); for(inti =1; I <= N; i++) read (A[i]); for(inti =1; I <= m;    i++) read (Op[i]), read (Ql[i]), read (Qr[i]); Read (POS);intL =1, R = N, mid; while(L < R) {mid = (L + r) >>1; for(inti =1; I <= N;    i++) Val[i] = A[i] > mid; Build1,1, n); for(inti =1; I <= m; i++) {intCNT = Query (1,1, N, Ql[i], qr[i]);if(Op[i]) {if(CNT) Change (1,1, N, Ql[i], Ql[i] + CNT-1,1);if(Ql[i] + cnt <= qr[i]) Change (1,1, N, Ql[i] + cnt, qr[i],0); }Else{if(CNT) Change (1,1, N, Qr[i]-cnt +1, Qr[i],1);if(Qr[i]-cnt >= ql[i]) Change (1,1, N, Ql[i], Qr[i]-CNT,0); }    }if(Query (1,1, N, POS, pos)) L = mid +1;ElseR = Mid; } write (L), enter;return 0;}

Bzoj 4552 [tjoi2016&heoi2016] sort | Two-part answer line tree

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.