Bzoj 1878: [Sdoi2009]hh's Necklace "tree-shaped Array"

Source: Internet
Author: User

For one LR, each color contributes to the right-most position that appears in the (1,r) interval, so recording a B array indicates where the color of the current point appears.
Then take the inquiry offline and sort by R in ascending order
Each time you move the right endpoint to the right, put this point in the tree array +1, and at the current point of the B position-1 (indicating no use), and then the tree array prefix and minus a bit can be
I wrote the mo team will t

#include <iostream> #include <cstdio> #include <algorithm>using namespace Std;const int N=1000005;int n,m,a[n],b[n],la[n],t[n],ans[n];struct qwe{int l,r,id;} Q[n];bool CMP (const qwe &a,const qwe &b) {return A.R&LT;B.R;}    int read () {int r=0,f=1;    Char P=getchar (); while (p> ' 9 ' | |        p< ' 0 ') {if (p== '-') f=-1;    P=getchar ();        } while (p>= ' 0 ' &&p<= ' 9 ') {r=r*10+p-48;    P=getchar (); } return r*f;}    void Update (int x,int W) {if (x==0) return; for (int i=x;i<=n;i+= (i& (-i))) t[i]+=w;}    int ques (int x) {int r=0;    for (int i=x;i>=1;i-= (i& (-i))) r+=t[i]; return r;}    int main () {n=read ();    for (int i=1;i<=n;i++) A[i]=read (), b[i]=la[a[i]],la[a[i]]=i;    M=read ();    for (int i=1;i<=m;i++) Q[i].l=read (), Q[i].r=read (), q[i].id=i;    Sort (q+1,q+1+m,cmp);    int r=0; for (int i=1;i<=m;i++) {while (R&LT;Q[I].R) R++,updatE (b[r],-1), update (r,1);    Ans[q[i].id]=ques (Q[I].R)-ques (Q[I].L-1);    } for (int i=1;i<=m;i++) printf ("%d\n", Ans[i]); return 0;}

Bzoj 1878: [Sdoi2009]hh's Necklace "Tree array"

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.