"POJ2104" k-th number

Source: Internet
Author: User

Ideas

Before you know this problem is used in <del> tears </del> partition tree to write the tat is so complicated I can't read it.

And then the president came to save us. ~\ (≧▽≦)/~

It's been a long time since I learned that the Chairman tree Qaq today, and T and W make me die.

Then we learned the new formulation of discretization, without the need for map.

The basic idea is to open a segment tree for each L of [1,l], recording the number of occurrences of each number in [1,l].

Then l+1 can take advantage of the vast majority of the points, different points to re-open the good.

The query is very good, just like splay.

  

#include <iostream>#include<cstring>#include<string>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<queue>#include<stack>#include<map>#include<Set>#include<list>#include<vector>#include<ctime>#include<functional>#definePRITNF printf#defineScafn scanf#defineSACNF scanf#definefor (i,j,k) for (int i= (j); i<= (k);(i) + +)#defineClear (a) memset (A,0,sizeof (a))using namespacestd;typedef unsignedintUint;Const intinf=0x3fffffff;Const Doubleeps=1e-Ten;///==============struct declaration==============structnode{Node*lc,*RC; intsum; Node () {LC=rc=null;sum=0;}};///==============var declaration=================Const intmaxn=100010; Node POOR[MAXN* -];inttop=0;//#define NEW (Node) (&poor[++top])intn,m,tot=0, k,v;intA[MAXN],SORTED[MAXN];intId[maxn];map<int,int>Mp; Node*seg_tree[maxn],*NULL;///==============function declaration============voidAdd_seg (Node *&l,node *&r,intLintR);voidQuery (Node *&prev,node *&o,intRank,intLintR);voidUpdate (Node *&o,intLintR);///==============main code=======================intMain () {#definefile__#ifdef file__ freopen ("Input.txt","R", stdin); Freopen ("output.txt","W", stdout);#endif scanf ("%d%d",&n,&m);  for(intI=1; i<=n;i++) {scanf ("%d", A +i); Sorted[i]=A[i]; }   NULL=New(Node); NULL->sum=0,NULL->lc=NULL,NULL->rc=NULL; Sort (Sorted+1, sorted+1+N); Tot=unique (sorted,sorted+1+n)-sorted-1; seg_tree[0]=New(Node);  for(intI=1; i<=n;i++) {Seg_tree[i]=New(Node); K=a[i];v=1; Add_seg (Seg_tree[i-1],seg_tree[i],1, tot); }    while(m--){      intL,R,K;SCANF ("%d%d%d",&l,&r,&j); Query (Seg_tree[l-1],seg_tree[r],k,1, tot); }   return 0;}///================fuction code====================voidAdd_seg (Node *&prev,node *&o,intLintR) {   intM= (l+r) >>1; if(prev==null) prev=NULL; if(o==null) o=New(Node); if(l==r) O->sum=v+prev->sum; Else{      if(sorted[m]>=k) {o->rc=prev->RC; o->lc=New(Node); Add_seg (prev->lc,o->lc,l,m); }        Else{o->lc=prev->LC; o->rc=New(Node); Add_seg (prev->rc,o->rc,m+1, R);   } update (O,L,R); }}voidUpdate (Node *&o,intLintR) {o->sum=0; if(o->lc!=null) o->sum+=o->lc->sum; if(o->rc!=null) o->sum+=o->rc->sum;}voidQuery (Node *&l,node *&r,intRank,intLintR) {   if(L==null) L=NULL; if(R==null) R=NULL; intlsum=0, m= (l+r) >>1; if(l==R) {printf ("%d\n", Sorted[l]); return ; }   if(R->lc!=null) Lsum+=r->lc->sum; if(L->lc!=null) Lsum-=l->lc->sum; if(lsum>=Rank) Query (L->lc,r->lc,rank,l,m); ElseQuery (L->rc,r->rc,rank-lsum,m+1, R);}
POJ2104

"POJ2104" k-th number

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.