Bzoj 1878 [Sdoi2009]hh's Necklace offline + tree-shaped array

Source: Internet
Author: User

Test Instructions: Give a sequence of n numbers, M queries, each asking for the number of different numbers within an interval. Method: Offline + tree-like array parsing: After reading the question did not have a clue, think of the line tree to engage, but it seems very troublesome, and then listen to their offline down to engage. It was almost clear that 1 more lessons were pushed. The offline and online gap is really big. If offline, all the intervals are linear. What is the general idea? Is that every number, we can preprocess where he was last seen. Then for an interval of inquiry [L,r], we can think of this to ask: the number of the last occurrence of the l~r in the number of the left of L. This will be good, first of all the interval by the right end of the order. Each time the element is added, the value of the position +1 where it was last appeared is added 1, then the value in its own position +1 is hit the 1 mark, which is a linear evaluation, that is, every time Getsum (A[I].L).
#include <stdio.h>#include <algorithm>using namespace STD;structnode{intLintR;intID;}; Node a[200100] ;intpre[200100] ;intcol[200100] ;intc[200100] ;intans[200100] ;intlast[1000100] ;intNintLowbit (intx) {returnx& (-X);}voidUpdateintXintP) { while(x <= N)        {C[x] + = p;    x + = Lowbit (x); }}intGetsum (intx) {intsum =0; while(x) {sum + = c[x];    X-= Lowbit (x); }returnsum;}intCMP (node asdf, Node B) {returnASDF.R < B.R;}intMain () {scanf("%d", &n); for(inti =1; I <= N; i++) {scanf("%d", &col[i]);        Pre[i] = last[col[i]];    Last[col[i]] = i; }intm;scanf("%d", &m); for(inti =1; I <= m; i++) {scanf("%d%d", &AMP;A[I].L, &AMP;A[I].R);    A[i].id = i; } sort (A +1, A +1+ M, CMP);intnow =0; for(inti =1; I <= m; i++) { while(Now < A[I].R)            {now + +; Update (Pre[now] +1,1) ;if(now! = N) {Update (now +1,-1) ;}    } Ans[a[i].id] = Getsum (A[I].L); } for(inti =1; I <= m; i++) {printf("%d\n", Ans[i]); }}

Bzoj 1878 [Sdoi2009]hh's Necklace offline + tree-shaped 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.