Codeforces 484E sign on Fence (is a persistent segment tree + dichotomy)

Source: Internet
Author: User

Title Link: Codeforces 484E sign on Fence

Topic: given to a sequence, each position has a value, indicating the height, now there are a number of queries, each query l,r,w, expressed in the interval l,r,

What is the maximum height of the longest continuous length greater than w?

Problem-solving ideas: Can persist segment tree maintenance interval Merge, the front-end time encountered a problem can be persisted dictionary tree, went to check the related papers, probably know is

What is it.

The height is sorted in order from large to small, and then each time a position is inserted, the segment tree maintains the longest continuous interval, as the insertion is in accordance with the large-to-small

Successive maximum lengths in each segment tree are satisfied with height greater than or equal to the current newly inserted height value. For each query, the two-point height,

The height is definitely in the existing height, so just two points below the table can be.

#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace STD;Const intMAXN =1e6+5;typedefpair<int,int> PII;structNode {intLC, RC, LP, RP, L, R, S;intLength () {returnRp-lp +1; }}ND[MAXN <<2];intN, SZ, ROOT[MAXN];p II BLO[MAXN];inline intNewNode () {returnsz++;}inline voidPushup (intu) {intLCID = nd[u].lc, rcid = nd[u].rc; Nd[u]. L = Nd[lcid]. L + (Nd[lcid]. L = = Nd[lcid].length ()? ND[RCID]. L0); Nd[u]. R = Nd[rcid]. R + (Nd[rcid]. R = = Nd[rcid].length ()? ND[LCID]. R:0); Nd[u]. S = Max (Nd[lcid]. R + nd[rcid]. L, Max (Nd[lcid]. S, Nd[rcid]. S));}inlineNode merge (Node A, node B) {node u; U.LP = A.LP;    U.RP = B.RP; U.L = A.l + (A.L = = A.length ()? B.L:0); U.R = B.R + (B.R = = B.length ()? A.R:0); U.S. = max (A.R + B.L, max (A.S, B.S));returnU;}voidBuildint& U,intLintR) {if(U = =0) U = NewNode (); Nd[u] = (Node) {0,0, L, R,0,0,0};if(L = = r)return;intMid = (L + r) >>1;    Build (ND[U].LC, L, mid); Build (nd[u].rc, Mid +1, R); Pushup (u);}intInsertintUintx) {intK = NewNode (); ND[K] = Nd[u];if(ND[K].LP = = x && x = = ND[K].RP) {Nd[k]. S = Nd[k]. L = Nd[k]. R =1;returnK }intMid = (ND[K].LP + nd[k].rp) >>1;if(x <= mid) nd[k].lc = insert (ND[K].LC, x);Elsend[k].rc = insert (nd[k].rc, x); Pushup (k);returnK;} Node Query (intUintLintR) {if(l <= nd[u].lp && nd[u].rp <= R)returnNd[u];intMid = (ND[U].LP + nd[u].rp) >>1;if(R <= Mid)returnQuery (ND[U].LC, L, R);Else if(L > Mid)returnQuery (Nd[u].rc, L, R);Else{Node ll = query (ND[U].LC, L, R); Node rr = query (Nd[u].rc, L, R);returnMerge (ll, RR); }}inline BOOLCMP (Constpii& A,Constpii& b) {returnA.first > B.first;}voidInit () {sz =1;scanf("%d", &n); for(inti =1; I <= N; i++) {scanf("%d", &blo[i].first);    Blo[i].second = i; } sort (Blo +1, Blo +1+ N, CMP); Build (root[0],1, N); for(inti =1; I <= N; i++) Root[i] = insert (root[i-1], blo[i].second);}intMain () {init ();intQ, L, R, W;scanf("%d", &q); while(q--) {scanf("%d%d%d", &l, &r, &w);intL =1, R = N; while(L < R) {intMid = (L + R) >>1;if(Query (Root[mid], L, R). S >= W) R = mid;ElseL = mid +1; }printf("%d\n", Blo[l].first); }return 0;}

Codeforces 484E sign on Fence (is a persistent segment tree + dichotomy)

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.