[Bzoj 1878] [SDOI2009] The Necklace of HH

Source: Internet
Author: User

Title Link: BZOJ-1878

Problem analysis

The question is the number of colors in a certain interval, so we want each color in this interval to be counted only once, so we select the element count for the first occurrence of each color in the Ask interval, and then the color that has already appeared in the Ask interval is no longer counted. Consider an off-line algorithm, if we sort all the queries according to the left endpoint of the Ask interval, then the left end of all queries is not decremented and goes right. Begins by preprocessing the first element after each element that has the same color as it, and adds the first element of all occurrences of the color to the tree array. Then the maintenance interval starts at 1. Each time a query is processed, if the currently maintained interval left endpoint is more left than the left end of the queried interval, then the left endpoint should be shifted right to the left end of the query. Each time you move the left endpoint one block to the right and an element is removed, it must be the first element of its color in the interval maintained at this time, so it must exist in the tree array, then we will delete it in the tree array and add its next color-colored element to the tree array. Then for the query (L, R) with the left endpoint L, which is now maintained, as long as the prefix and Get (r) of the R is queried with the tree array, the prefix and all the elements in the range are the first occurrence of a color in the interval starting from L, and there will be no repetition of a color count.

Code
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include < Algorithm> #include <cmath> using namespace Std; const int maxn = 50000 + 5, MaxQ = 200000 + 5, Maxtype = 1000000 + 5; int N, M, A, B;int A[MAXN], NEXT[MAXN], T[MAXN], Last[maxtype], ans[maxq]; struct QUERY{&NBSP;&NB Sp;  int L, R, Num;} Q[maxq]; bool CMP (query Q1, query Q2) {    return q1.l < Q2.L;}  void Add (int x, int num) {    for (int i = x; i <= n; i + = i & (i)) &NBSP;&NBSP;&NBSP;&N BSP;&NBSP;&NBSP;&NBSP;&NBSP;T[I] + = num;  } int Get (int x) {    int ret = 0;    for (int i = x; I >= 1; I-= i & (-i))         ret + = T[i];    return ret;}  int Main () {    scanf ("%d", &n),     for (int i = 1; I <= n; i++) scan F ("%d", &a[I]); &NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d", &m);     for (int i = 1; I <= m; i++) {  & NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d%d", &a, &b);        &NBSP;Q[I].L = A; Q[I].R = B;        q[i].num = i;    }     sort (q + 1, q + M + 1, CMP),     memset (t, 0, sizeof (t)),     memset ( Last, 0, sizeof (last));     for (int i = 1; I <= n; i++) {      &nbsp ;  if (Last[a[i]] = = 0) Add (i, 1);         else next[last[a[i]] = i;         last[a[i]] = i;    }    int x = 1 ;     for (int i = 1; I <= m; i++) {        while (x < Q[ I].L) {       &nbsP;    add (x,-1);             if (Next[x]) ADD (Next[x], 1);            ++x;         }        ans[q[i].num] = Get (Q[I].R);     }    for (int i = 1; I <= m; i++) printf ("%d\n", Ans[i]);   & Nbsp; return 0;}

[Bzoj 1878] [SDOI2009] The Necklace of HH

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.