BZOJ-2743: [HEOI2012] Picking flowers (tree array or tle MO team)

Source: Internet
Author: User

2743: [HEOI2012] Pick flower time limit:15 Sec Memory limit:128 MB
submit:2512 solved:1292
[Submit] [Status] [Discuss] 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

The first line is four spaces separated by an integer 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 5
Sample Output2
0010
"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.

Source

See the topic at the first glance ... MMP it's not a stupid team. So anger godless a MO team up, the evaluation of the time Laj also want to say Laj and do a bzoj on a water problem, MMP results first PE results are all tle. A look at the data MMP 1e6 MO team a jb ah (ok this stupid problem has made me two and a half lessons to do well qaq)

First put a team's tle code.

1#include"bits/stdc++.h"2 using namespacestd;3typedefLong LongLL;4 Const intmax=1e6+5;5 intN,m,faq;6 intA[max],b[max],pos[max],bas,ans,an[max];7 structnode{8     intID;9     intL,r;Ten     BOOL operator< (ConstNode &tt)Const { One         if(pos[l]!=POS[TT.L]) A             returnpos[l]<POS[TT.L]; -         returnr<TT.R; -     } the }que[max]; -InlineintRead () { -     intan=0, x=1;CharC=GetChar (); -      while(c<'0'|| C>'9') {if(c=='-') x=-1; c=GetChar ();} +      while(c>='0'&& c<='9') {an=an*Ten+c-'0'; c=GetChar ();} -     returnan*x; + } A voidUpdateintXinty) { atans-= (b[a[x]]>1?1:0); -b[a[x]]+=y; -ans+= (b[a[x]]>1?1:0); - } - intMain () { -Freopen ("flower.in","R", stdin); inFreopen ("Flower.out","W", stdout); -Registerinti,j; toN=read (), Faq=read (), M=read (); Bas= (int) sqrt (n1.0); +      for(i=1; i<=n;++i) A[i]=read (), pos[i]=i/Bas; -      for(i=1; i<=m;++i) { theQue[i].id=I, *Que[i].l=read (), que[i].r=read (); $     }Panax NotoginsengSort (que+1, que+m+1); -memset (b,0,sizeof(b)); theRegisterintL=1, r=0; +      for(i=1; i<=m;++i) { A          while(R&LT;QUE[I].R) Update (++R,1); the          while(L&GT;QUE[I].L) Update (--L,1); +          while(R&GT;QUE[I].R) Update (R--,-1); -          while(L&LT;QUE[I].L) Update (l++,-1); $an[que[i].id]=ans; $     } -      for(i=1; i<m;++i) -printf"%d\n", An[i]); theprintf"%d", An[m]); -     return 0;Wuyi}

Okay, all right, let's go. Write a tree-like array, see the hzwer hh necklace and this question, and Luogu on the tle countless times finally AC MMP

Why not write ideas ... Because I can not tell, imaginative achievement here, inexpressible Ovo should say HH necklace is initialized and each operation is to put the next 1, the first initialization on the next subscript 1 then the operation is to take the next bit (that is, the bottom of the next bit) to remove, and then the current bit of the next subscript 1 ...

1#include"bits/stdc++.h"2 using namespacestd;3typedefLong LongLL;4 Const intmax=1e6+5;5 intN,m,faq;6 intA[max],b[max],next[max],p[max],an[max];7 structnode{8     intID;9     intL,r;Ten     BOOL operator< (ConstNode &tt)Const{returnl<TT.L;} One }que[max]; AInlineintRead () { -     intan=0, x=1;CharC=GetChar (); -      while(c<'0'|| C>'9') {if(c=='-') x=-1; c=GetChar ();} the      while(c>='0'&& c<='9') {an=an*Ten+c-'0'; c=GetChar ();} -     returnan*x; - } - voidAddintXintY) { for(; x<=n;x+= (x&-x)) b[x]+=y;} +InlineintSearchintx) {intan=0; for(;x>0; x-= (X&-x)) an+=b[x];returnan ;} - intMain () { +Freopen ("flower.in","R", stdin); AFreopen ("Flower.out","W", stdout); atRegisterinti,j; -N=read (), Faq=read (), m=read (); -      for(i=1; i<=n;++i) a[i]=read (); -      for(i=1; i<=m;++i) { -Que[i].id=I, -Que[i].l=read (), que[i].r=read (); in     } -Sort (que+1, que+m+1); tomemset (b,0,sizeof(b)); Memset (P,0,sizeof(P)); +      for(i=n;i>=1; i--) next[i]=p[a[i]],p[a[i]]=i; -      for(i=1; i<=faq;i++)if(Next[p[i]]) Add (Next[p[i]],1); the     intL=1; *      for(i=1; i<=m;i++){ $          while(l<QUE[I].L) {Panax Notoginseng             if(Next[l]) Add (next[l],-1); -             if(Next[next[l]]) Add (Next[next[l]],1); thel++; +         } AAn[que[i].id]=search (QUE[I].R)-search (que[i].l-1); the     } +      for(i=1; i<=m;i++) printf ("%d\n", An[i]); -     return 0; $}

BZOJ-2743: [HEOI2012] Picking flowers (tree array or tle MO team)

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.