[COGS257] Dynamic ranking System tree Array set Chairman tree

Source: Internet
Author: User

257. Dynamic ranking System

Time limit: 5 s memory limit: MB

[Problem description]
Given a known sequence of length N a[i] (1<=i<=n), this sequence is required to be maintained and capable of supporting the following two operations:
1, query a[i],a[i+1],a[i+2],..., a[j] (1<=i<=j<=n), ranked the number of K in ascending order.
2, modify the value of A[i] is J.
The so-called rank K, refers to some numbers in ascending order, the number of k digits. For example, the sequence {6,1,9,6,6}, the 3rd number is 6, the 5th number is 9.
[Input format]
The first line contains an integer D (0<=d<=4) that represents the number of test data. Then there is the D set of test data, each set of test data, the first is two integer n (1<=n<=50000), M (1<=m<=10000), indicating the length of the sequence is n, there are M operations. The next n is not greater than 1,000,000,000 positive integers, and the first I represents the initial value of sequence a[i]. Then the M-line, one action per behavior
Q I J K or
C I J
For query a[i],a[i+1],a[i+2],..., a[j] (1<=i<=j<=n), the number of digits in ascending order, and the value of modify A[i] is J.
[Output FORMAT]
For each query, output a single line of integers, which is the result of the query. There should be no blank lines between test data.
[Sample input]
2
5 3
3 2 1) 4 7
Q 1 4 3
C 2 6
Q 2 5 3
5 3
3 2 1) 4 7
Q 1 4 3
C 2 6
Q 2 5 3
[Sample Output]
3
6
3
6

Exercises

If you have not heard the concept of "tree-shaped array set of Chairman tree", you can choose to walk my explanation

The topic is a tree array of the chairman of the tree set of the board, if you use the pointer to the operation can be used to save the vector to change a few trees, to modify

Query time and ordinary static query is similar, but the number of sub-tree data and the sum of the comparison with K

These things still can be better understood by code ... Code See below:

1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <vector>5 using namespacestd;6 Const intn=10100, maxn=1000000000LL;7 intN,VAL[N],TP;8 structnode9 {TenNode *ch[2];intCNT; OneNode () {cnt=0; ch[1]=ch[0]=NULL;} AInlinevoidUpdate () {cnt=ch[0]->cnt+ch[1]->CNT;} -}*NULL=NewNode (), *root[n*2]; -vector<node*>v[5]; theInline node*NewNode () - { -Node *o=Newnode (); -o->ch[0]=o->ch[1]=NULL; +     returno; - } +InlineintLowbit (intA) {returna&-A;} AInlinevoidGet_query (intIdintPOS) at { - v[id].clear (); -      while(POS) V[id].push_back (Root[pos]), pos-=Lowbit (POS); - } -InlinevoidGet_set (intIdintPOS) - { in v[id].clear (); -      while(pos<=n) V[id].push_back (Root[pos]), pos+=Lowbit (POS); to } +Inlinevoid Set(Vector<node*> O,intLintRintPosintopt) - { the      while(l<R) *     { $         intMi= (l+r) >>1;Panax Notoginseng          for(intI=0, Len=o.size (); i<len;i++)if(o[i]!=NULL) o[i]->cnt+=opt; -         if(pos<=mi) the         { +R=mi; A              for(intI=0, Len=o.size (); i<len;o[i]=o[i]->ch[0],i++) the                 if(o[i]->ch[0]==NULL) o[i]->ch[0]=NewNode (); +         } -         Else $         { $l=mi+1; -              for(intI=0, Len=o.size (); i<len;o[i]=o[i]->ch[1],i++) -                 if(o[i]->ch[1]==NULL) o[i]->ch[1]=NewNode (); the         } -     }Wuyi      for(intI=0, Len=o.size (); i<len;i++) the     { -         if(o[i]==NULL) o[i]=NewNode (); Wuo[i]->cnt+=opt; -     } About } $InlineintQueryintAintBintLintRintk) - { -Get_query (1, A-1); Get_query (2, b); -      while(l<R) A     { +         inttmp=0, mi= (l+r) >>1; the          for(intI=0, len=v[1].size (); i<len;i++)if(v[1][i]!=NULL) tmp-=v[1][i]->ch[0]->CNT; -          for(intI=0, len=v[2].size (); i<len;i++)if(v[2][i]!=NULL) tmp+=v[2][i]->ch[0]->CNT; $         if(tmp>=k) the         { the              for(intI=0, len=v[1].size (); i<len;i++) v[1][i]=v[1][i]->ch[0]; the              for(intI=0, len=v[2].size (); i<len;i++) v[2][i]=v[2][i]->ch[0]; theR=mi; -         } in         Else the         { the              for(intI=0, len=v[1].size (); i<len;i++) v[1][i]=v[1][i]->ch[1]; About              for(intI=0, len=v[2].size (); i<len;i++) v[2][i]=v[2][i]->ch[1]; thek-=tmp,l=mi+1; the         } the     } +     returnR; - } theInlinevoidChangeintAintb)Bayi{Get_set (1, a);Set(v[1],1, maxn,val[a],-1);Set(v[1],1, Maxn,b,1); val[a]=b;} the intMain () the { -     intM,a,b,c;Chars[3]; -     NULL->ch[0]=NULL->ch[1]=NULL; thescanf"%d%d",&n,&m); the      for(intI=1; i<=n;i++) root[i]=NewNode (); the      for(intI=1; i<=n;i++) scanf ("%d", &val[i]), Get_set (1, i),Set(v[1],1, Maxn,val[i],1); the      while(m--) -     { thescanf"%s", s); the         if(s[0]=='Q') scanf ("%d%d%d", &a,&b,&c), printf ("%d\n", query (A, B,1, Maxn,c)); the         Elsescanf"%d%d",&a,&b), change (a, b);94     } the}

[COGS257] Dynamic ranking System tree Array set Chairman 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.