Bzoj 1878: [Sdoi2009]hh's Necklace

Source: Internet
Author: User

1#include <cstdio>2#include <iostream>3#include <algorithm>4 #defineM 10000085 using namespacestd;6 intsum[m],mx,n,next[m],p[m],a[m],m;7 structData8 {9     intL,r,o,ans;Ten }q[m]; One BOOLCMP (data a1,data A2) A { -     if(a1.l==a2.l) -       returna1.r<A2.R; the     returna1.l<A2.L; - } - BOOLCMP1 (Data a1,data A2) - { +     returna1.o<a2.o; - } + voidAddintA1,intA2) A { at      for(inti=a1;i<=n;i+=i&-i) -sum[i]+=A2; -     return; - } - intXunintA1) - { in     intsu=0; -      for(inti=a1;i;i-=i&-i) tosu+=Sum[i]; +     returnsu; - } the intMain () * { $scanf"%d",&n);Panax Notoginseng      for(intI=1; i<=n;i++) -       { thescanf"%d",&a[i]); +mx=Max (mx,a[i]); A       } the      for(inti=n;i;i--) +       { -next[i]=P[a[i]]; $p[a[i]]=i; $       } -scanf"%d",&m); -      for(intI=1; i<=m;i++) the       { -scanf"%d%d",&q[i].l,&Q[I].R);Wuyiq[i].o=i; the       } -      for(intI=1; i<=mx;i++) Wu       if(P[i]) -Add (P[i],1); AboutSort (q+1, q+m+1, CMP); $     intL=1; -      for(intI=1; i<=m;i++) -       { -          for(;l<q[i].l;) A           { +             if(Next[l]) theAdd (Next[l],1); -l++; $           } theQ[i].ans=xun (Q[I].R)-xun (q[i].l-1); the       } theSort (q+1, q+m+1, CMP1); the      for(intI=1; i<=m;i++) -printf"%d\n", Q[i].ans); in     return 0; the}

Will read into offline processing, sorted first, tree-like array maintained.

Bzoj 1878: [Sdoi2009]hh's Necklace

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.