"Poj2104-for the K-large number of intervals (not modified)" Chairman Tree/Sustainable segment tree

Source: Internet
Author: User

The first chairman of the tree ~ however is the Tao compared to the water ... Because it doesn't have to be modified ...

Reproduced a Let me understand the chairman of the tree to explain it: http://blog.csdn.net/regina8023/article/details/41910615 (unauthorized, infringement delete)

--------------------------------------------------------------------------------------------------------------- ------

So what if you ask the number of times between i-j?

Because each segment of the tree line is the same, it is required that the number of occurrences of the numbers between l-r as long as the number of occurrences of the first R bit minus the number of occurrences of the first l-1 , is ans

But what if there is a modification operation?

If you follow the above practice, then the modification operation is O (NLOGN), the query is O (1), the modification will take a long time ...

Prefix and Lenovo to the tree-like array , then the prefix and the tree-like array to maintain the modified is O (LOGN*LOGN), Query O (logn), the query time is longer, but the change of time to shorten a lot!!

Attention:

Array of functional segment trees to open up a bit!!

--------------------------------------------------------------------------------------------------------------- ------

This problem is the template problem, first discrete, to find the interval K big time lx=root[l-1],rx=root[r], both sides at the same time to continue to make poor, look at the number of left children, if K is more large to reduce the left child to have the right child to find.

Code:

1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <cmath>5#include <iostream>6#include <algorithm>7 using namespacestd;8 9 Const intn=100010, inf= (int) 1e9+ -;Ten structtrnode{ One     intlc,rc,cnt; A}t[ -*N]; - structnode{ -     intD,id; the }p[n]; - intn,m,tl,mx; - intA[n],num[n],root[n]; -  + BOOLCMP (node X,node y) {returnx.d<Y.d;} -  + intBtintLintR) A { at     intx=++tl; -     //a[x].l=l;a[x].r=r; -T[x].lc=t[x].rc=0; -T[x].cnt=0; -     if(l<R) -     { in         intMid= (L+R)/2; -T[x].lc=bt (L,MID); toT[X].RC=BT (mid+1, R); +     } -     returnx; the } *  $ intAddintRtintx)Panax Notoginseng { -     intnow=++tl,tmp=Now ; thet[now].cnt=t[rt].cnt+1; +     intL=1, r=Mx,mid; A      while(l<R) the     { +Mid= (L+R)/2; -         if(x<=mid) $         { $t[now].lc=++tl; -T[now].rc=t[rt].rc; -rt=t[rt].lc; thenow=tl; -R=mid;Wuyi         } the         Else -         { WuT[now].lc=t[rt].lc; -t[now].rc=++tl; Aboutrt=t[rt].rc; $now=tl; -L=mid+1; -         } -t[now].cnt=t[rt].cnt+1; A     } +     returntmp; the } -  $ intQueryintLxintRxintk) the { the     intL=1, r=Mx,mid; the      while(l<R) the     { -Mid= (L+R)/2; in         if(t[t[rx].lc].cnt-t[t[lx].lc].cnt>=k) the         { theR=mid; Aboutlx=t[lx].lc; therx=t[rx].lc; the         } the         Else +         { -L=mid+1; thek-=t[t[rx].lc].cnt-t[t[lx].lc].cnt;Bayilx=t[lx].rc; therx=t[rx].rc; the         } -     } -     returnl; the } the  the voidOutputintx) the { -printf"x =%d LC =%d rc =%d cnt =%d\n", x,t[x].lc,t[x].rc,t[x].cnt); the     if(T[X].LC) output (T[X].LC); the     if(t[x].rc) output (t[x].rc); the }94  the intMain () the { theFreopen ("a.in","R", stdin);98      while(SCANF ("%d%d", &n,&m)! =EOF) About     { -Tl=0; mx=0;101          for(intI=1; i<=n;i++)102         {103scanf"%d",&a[i]);104P[i].d=a[i];p [i].id=i; the         }106Sort (p+1, p+1+n,cmp);107p[0].d=INF;108          for(intI=1; i<=n;i++)109         { the             if(p[i].d!=p[i-1].D) mx++,num[mx]=p[i].d;111a[p[i].id]=MX; the         }113root[0]=BT (1, MX); the          for(intI=1; i<=n;i++) theRoot[i]=add (root[i-1],a[i]); the         //output (root[5]);117          for(intI=1; i<=m;i++)118         {119             intl,r,k; -scanf"%d%d%d",&l,&r,&k);121printf"%d\n", Num[query (root[l-1],root[r],k)]);122         }123     }124     return 0; the}

"Poj2104-for the K-large number of intervals (not modified)" Chairman Tree/Sustainable segment tree

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.