[Chairman Tree] HDOJ4417 Super Mario

Source: Internet
Author: User

Test instructions: N number of M queries ($n, M \le 10^5$)

Each query has L, R, K ask is [L, R] The number of the interval is less than or equal to K

If you do it with the Chairman tree, you can compare the small number of the first I with the K.

1 #defineLson L, M2 #defineRson M+1, R3 Const intn=1e5+5;4 intl[n<<5], r[n<<5], sum[n<<5];5 inttot;6 intA[n], t[n], hash[n];7 intBuildintLintR)8 {9     intRt= (+ +)tot);Tensum[rt]=0; One     if(l<R) A     { -         intM= (l+r) >>1; -l[rt]=build (Lson); ther[rt]=build (Rson); -     } -     returnRT; - } +  - intUpdateintPreintLintRintx) + { A     intRt= (+ +)tot); atL[rt]=l[pre], R[rt]=r[pre], sum[rt]=sum[pre]+1; -     if(l<R) -     { -         intM= (l+r) >>1; -         if(x<=m) -l[rt]=Update (L[pre], Lson, x); in         Else -r[rt]=Update (R[pre], Rson, x); to     } +     returnRT; - } the  * intQueryintUintVintLintRintk) $ {Panax Notoginseng     if(l>=R) -         returnl; the     intM= (l+r) >>1; +     intnum=sum[l[v]]-Sum[l[u]]; A     if(num>=k) the         returnquery (L[u], l[v], Lson, k); +     Else -         returnQuery (R[u], r[v], Rson, K-num); $ } $  - intMain () - { the     intT, ca=1; -scanf"%d", &t);Wuyi      while(t--) the     { -tot=0; Wu         intN, M; -scanf"%d%d", &n, &m); About          for(intI=1; i<=n; i++) $         { -scanf"%d", &a[i]); -hash[i]=A[i]; -         } ASort (hash+1, hash+n+1); +         intD=unique (hash+1, hash+n+1)-hash-1; thet[0]=build (1, d); -          for(intI=1; i<=n; i++) $         { the             intX=lower_bound (hash+1, hash+d+1, A[i])-Hash; theT[i]=update (t[i-1],1, D, x); the         } theprintf"Case %d:\n", ca++); -          while(m--) in         { the             intL, R, K; thescanf"%d%d%d", &l, &r, &k); Aboutl++, r++; the             intLl=1, rr=r-l+1; the             intans=0; the              while(ll<=RR) +             { -                 intMm= (LL+RR) >>1; the                 intTmp=hash[query (t[l-1], T[r],1, D, mm)];Bayi                 if(tmp<=k) the                 { the                     if(mm==r-l+1|| Hash[query (t[l-1], T[r],1, D, mm+1)]>k) -                     { -cn1=mm; the                          Break; the                     } thell=mm+1; the                 } -                 Else the                 { the                     if(mm==1|| Hash[query (t[l-1], T[r],1, D, mm-1)]<=k) the                     {94ans=mm-1; the                          Break; the                     } therr=mm-1;98                 } About             } -printf"%d\n", ans);101         }102     }103     return 0;104}
hdoj 4417

[Chairman Tree] HDOJ4417 Super Mario

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.