BZOJ_2743_[HEOI2012] Pick Flower _ offline + Tree array

Source: Internet
Author: User

BZOJ_2743_[HEOI2012] Pick Flower _ offline + Tree array

Description Shing is the princess of Z country, usually a big hobby is picking flowers. It was sunny and sunny today, and the princess went to the new garden in the palace to pick up the flowers in the morning. The garden is large enough to accommodate n flowers, with a C color (denoted by an integer 1-c), and the flowers lined up so that the princess can pick flowers. Every time the princess collected flowers will be counted the color of the flowers, the more the number of colors she will be happier! At the same time, she has a hobby, she does not allow the last to pick up the flowers, a color of only one flower. To this end, the princess each picked a flower, either had already picked up the color of the flower, or have quite the right instinct to tell her that she will be able to pick up the color of the flowers again. As a result of time, the princess can only walk through the garden for a continuous period of mining flowers, then let the Maid Fu Han to arrange the trip. Fu Han Jie comprehensive a variety of factors to develop a m-itinerary, and then to you ask the princess how many flowers can be picked (she knows you are a master programmer, will be able to quickly give the answer!) ), and finally choose the itinerary that makes the Princess happy (in order to get more bonuses!). )。 Input first line four spaces separated by integers n, c, and M. The next line of n spaces separated by the integer, each number in [1, c], the number of I represents the color of the first flower. The next M-line of two spaces separated by the integer L and R (L≤r), indicating that the maid arranged the trip for the princess through the L to the r flower to be picked flowers. Output a total of M rows, one integer per line, and the number of numbers representing the color of the flower that the princess can pick up in the first trip of the maid. Sample Input5 3 5
1 2 2) 3 1
1 5
1 2
2 2
2 3
3 5Sample Output2
0
0
1
0
"Sample description"
Inquiry [1, 5]: The princess picked the color for 1 and 2 flowers, because the color 3 flower only one flower, the princess does not pick; Inquiry [1, 2]: color 1 and color 2 flowers are only one, the princess does not pick;
Inquiry [2, 2]: color 2 flower Only one flower, the princess does not pick;
Inquiry [2, 3]: Because the color 2 flower has two flower, the princess picked the color 2 flower;
Inquiry [3, 5]: Color 1, 2, 3 flowers each, the princess does not pick.
HINT

"Data Range"

For 100% of data, 1≤n≤10^6,c≤n,m≤10^6.

Analysis:

It looks like the team can do it, but the data range is for the card, so we consider another way of doing it offline.

First for each flower, find the next occurrence of the same color of the position of the flower $nxt[]$, if not the $nxt[]$ is 0.

Then get the questions down and sort them in ascending order by the left endpoint of the query.

In the beginning, I counted only the contribution of each flower when it first appeared.

Sweep the array, assuming that I am currently in $i$ this position, from my recent inquiry left endpoint is $j$, then we have to abandon $i$ to $j-1$ this part of the contribution.

Delete the number of $i$, to the right end of the $i+1\thicksim nxt[i]-1$ these need $-1$, the right endpoint in $nxt[i]\thicksim nxt[nxt[i]]-1$ this part $+1$, and then the tree array maintenance of the entire operation.

Code:

#include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h>using namespace STD; #define N 1000050int n,v[n],c,m,b[n],nxt[n],now[n],ans[n];struct A {int l,r,pos;}    A[n];bool CMP (const a &x,const a &y) {if (X.L==Y.L) return x.r<y.r; return X.L&LT;Y.L;} void fix (int x,int v) {for (;x<=n;x+=x& (x)) B[x]+=v;}    int inq (int x) {int re=0;    For (;x;x-=x& (x)) re+=b[x]; return re;}    int main () {scanf ("%d%d%d", &n,&c,&m);    int i;    for (i=1;i<=n;i++) scanf ("%d", &v[i]);    for (i=1;i<=m;i++) {scanf ("%d%d", &AMP;A[I].L,&AMP;A[I].R); a[i].pos=i;    } sort (a+1,a+m+1,cmp);    for (i=n;i;i--) {nxt[i]=now[v[i]]; now[v[i]]=i;        (i=1;i<=n;i++) {if (Nxt[i]) fix (nxt[i],1);    if (Nxt[nxt[i]]) fix (nxt[nxt[i]],-1);    } int fafa=1;            Fix (i=1;i<=m;i++) {while (FAFA&LT;A[I].L) {if (Nxt[fafa]) (nxt[fafa],-1); if (Nxt[nxt[fafa]]) fix (NXT[nxt[fafa]],1];        fafa++;    } ans[a[i].pos]=inq (A[I].R)-inq (A[I].L-1); } for (i=1;i<=m;i++) printf ("%d\n", Ans[i]);}

BZOJ_2743_[HEOI2012] Pick Flower _ offline + 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.