"Offline practices tree-like array" luoguP1972 [sdoi2009]hh Necklace

Source: Internet
Author: User

Similar to Bzoj3585:mex's line-tree approach

Title Description

HH has a chain of beautiful shells. HH believes that different shells will bring good luck, so after each walk, he will take out a shell and think about what they mean. HH keeps collecting new shells, so his necklace is getting longer. One day, he suddenly raised a question: how many different shells are included in a certain piece of shell? The question is difficult to answer ... Because the necklace is too long. So he had to ask the wise you to solve the problem.

Input/output format

Input format:

First line: An integer n that represents the length of the necklace.

Second line: N integers representing the number of shells in the necklace (integers numbered 0 to 1000000, in turn).

Line three: An integer m that indicates the number of HH queries.

Next m line: two integers per line, L and R (1≤l≤r≤n), indicating the interval of the inquiry.

Output format:

M lines, one integer per line, in turn, to ask the corresponding answer.

Input and Output Sample input example # #:
61 2 3 4 3 531 23 52 6
Sample # # of output:
224
Description

Data range:

For 100% of data,N <= 500000,m <= 200000.

Problem analysis

Early learned that this "Mo Team Board Problem" has a tree-like array solution but slow to learn ...

Obviously the answer is reduced, and no matter whether the answer is outside the range or not, it will not affect the answer within the interval.

Here is a kind of routine or technique: use $nxt[i]$ to represent the next element with the same nature of the $i$ position, then delete the $i$ after the location of the $nxt[i]$ will be in the position of the answer +1 means there is a new element ($nxt[i]$ is indeed a new element for the interval of inquiry).

An instant thought of a Bzoj3585mex line-tree approach that had been written in a muddle, and a deeper understanding of the routine.

1#include <bits/stdc++.h>2 Const intMAXN =500035;3 Const intMAXM =200035;4 Const intMAXC =1000035;5 6 structQRs7 {8     intL,r,id;9     BOOL operator< (QRs a)ConstTen     { One         returnL <A.L; A     } - }Q[MAXM]; - intCOL[MAXC],LST[MAXC],NXT[MAXN]; the intANS[MAXM]; - intF[MAXN]; - intn,m,mx; -  + intLowbit (intx) {returnx&-x;} - voidAddintx) { for(; x<=n; x+=lowbit (x)) f[x]++;} + intQueryintx) A { at     intRET =0; -      for(; x; x-=lowbit (x)) ret + =F[x]; -     returnret; - } - intRead () - { in     CharCH =GetChar (); -     intnum =0; to     BOOLFL =0; +      for(;!isdigit (ch); ch =GetChar ()) -         if(ch=='-') FL =1; the      for(; isdigit (ch); ch =GetChar ()) *num = (num<<1) + (num<<3) +ch- -; $     if(fl) num =-num;Panax Notoginseng     returnnum; - } the intMain () + { An =read (); the      for(intI=1; i<=n; i++) Col[i] =read (); +      for(intI=n; i>=1; i--) -     { $         if(lst[col[i]]==0) Lst[col[i]] = n+1; $Nxt[i] = Lst[col[i]], lst[col[i]] =i; -     } -      for(intI=1; i<=n; i++) the         if(Lst[col[i]]) Add (i), lst[col[i]] =0; -m =read ();Wuyi      for(intI=1; i<=m; i++) Q[I].L = Read (), Q[I].R = Read (), q[i].id =i; theStd::sort (q+1, q+m+1); -     intnow =0; Wu      for(intI=1; i<=m; i++) -     { About          while(Now <q[i].l) $         { -             if(Nxt[now]) Add (Nxt[now]); -now++; -         } AAns[q[i].id] = query (Q[I].R)-query (q[i].l-1); +     } the      for(intI=1; i<=m; i++) -printf"%d\n", Ans[i]); $     return 0; the}

END

"Offline practices tree Array" luoguP1972 [sdoi2009]hh necklace

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.