bzoj1901--tree-shaped array set of Chairman tree

Source: Internet
Author: User

Tree Array Set Chairman Tree Template title ...

Main topic:

Given a sequence of n A[1],a[2],a[3]......a[n], the program must answer this question: For a given i,j,k, the small number of K in A[i],a[i+1],a[i+2]......a[j] is (1≤k≤j-i+1), and, You can change the value of some a[i], and after the change, the program can continue to answer the above question for the changed a. You need to make a program that reads the sequence a from the input file and then reads in a series of instructions, including the Ask and modify instructions. For each inquiry instruction, you must output the correct answer. The first line has two positive integers n (1≤n≤10000), M (1≤m≤10000). Indicates the length of the sequence and the number of instructions, respectively. The second line has n number, which means a[1],a[2]......a[n], these numbers are smaller than 10^9. The next M-line describes each instruction, and the format of each line is one of the following two formats. Q I j K or C i t Q i j K (I,j,k is a number, 1≤i≤j≤n, 1≤k≤j-i+1) indicates an inquiry instruction asking for a small number of k in the A[i],a[i+1]......a[j]. C i T (1≤i≤n,0≤t≤10^9) means to change a[i] into T.

Ideas:

If there is no modification, it is clear that the Chairman tree can be solved. However, with the modification operation, because each node of the Chairman tree is related to the previous node, the Brute force modification is O (N*LOGN), which clearly expires. So use a tree-like array to set the Chairman tree.

We are no longer a node that has a connection to the edge of the front node, but is modified on the original basis (the original node is not saved). Set a tree-like array outside the Chairman tree so that the value of each node needs to be queried just one side of the tree array. The modification is O (logn).

Also the value of the discrete a array.

Specific look at the code

Code:

#include <cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespaceStd;inlineCharNc () {Static Charbuf[100000],*p1=buf,*p2=buf; if(p1==p2) {P2= (p1=buf) +fread (buf,1,100000, stdin); if(P1==P2)returnEOF; }    return*p1++;} InlinevoidRead (int&x) {    CharC=Nc ();  for(;c<'0'|| C>'9'; c=Nc ());  for(x=0; c>='0'&&c<='9'; X= (x<<3) + (x<<1) +c- -, c=Nc ());} InlinevoidRead (Char&C) {    CharC=Nc ();  while(c!='Q'&&c!='C') c=Nc (); C=C;}#defineN 10001structgj{intL,r,w;} C[n* -];structjob{intX,y,k;} B[n];intrt[n],i,j,k,n,m,x,y,hash[n<<1],tot=1,num,a[n],s[n<<1],s,l[n],r[n],l1,l2;CharC;BOOLF[n];inlineintLowbit (intx) {    returnx&-x;} InlineintFind (intx) {    intL=1, r=Tot,mid;  while(l<=R) {Mid=l+r>>1; if(X>hash[mid]) l=mid+1;Elser=mid-1; }    returnl;} InlinevoidUpdate (int& Node,intLintRintLast,intXinty) {c[++num]=c[last]; Node=Num; C[NODE].W+=y; if(L==R)return; intMid=l+r>>1; if(X<=mid) Update (C[node].l,l,mid,c[last].l,x,y);ElseUpdate (c[node].r,mid+1, r,c[last].r,x,y);} InlineintQuery (intLintRintk) {    if(L==R)returnl; intsum=0,mid=l+r>>1;  for(intI=1; i<=l1;i++) sum-=C[C[L[I]].L].W;  for(intI=1; i<=l2;i++) sum+=C[C[R[I]].L].W; if(sum>=k) {         for(intI=1; i<=l1;i++) l[i]=C[L[I]].L;  for(intI=1; i<=l2;i++) r[i]=C[R[I]].L; returnQuery (l,mid,k); }Else{         for(intI=1; i<=l1;i++) l[i]=C[L[I]].R;  for(intI=1; i<=l2;i++) r[i]=C[R[I]].R; returnQuery (mid+1, r,k-Sum); }}Charss[ -];intLen;inlinevoidPrint (intx) {    if(x==0) {Putchar ('0');p Utchar ('\ n'); return; }     for(len=0; x;x/=Ten) ss[++len]=x%Ten;  for(; Len;) Putchar (ss[len--]+ -); Putchar ('\ n');}intMain () {Read (n);    Read (m);  for(i=1; i<=n;i++) Read (A[i]), s[++s]=A[i];  for(i=1; i<=m;i++) {Read (C); Read (b[i].x);        Read (B[I].Y); if(c=='Q') {Read (B[I].K); b[i].x--;f[i]=1; }Elses[++s]=b[i].y; } sort (S+1, s+s+1); hash[1]=s[1];  for(i=2; i<=s;i++)    if(s[i]!=s[i-1]) hash[++tot]=S[i];  for(i=1; i<=n;i++) {x=Find (A[i]);  for(J=i;j<=n;j+=lowbit (j)) Update (Rt[j],1, Tot,rt[j],x,1); }     for(i=1; i<=m;i++)    if(!F[i]) {x=Find (a[b[i].x]);  for(J=b[i].x;j<=n;j+=lowbit (j)) Update (Rt[j],1, tot,rt[j],x,-1); A[b[i].x]=b[i].y; X=Find (B[I].Y);  for(J=b[i].x;j<=n;j+=lowbit (j)) Update (Rt[j],1, Tot,rt[j],x,1); }Else{L1=l2=0;  for(J=b[i].x;j;j-=lowbit (j)) l[++l1]=Rt[j];  for(J=b[i].y;j;j-=lowbit (j)) r[++l2]=Rt[j]; Print (Hash[query (1, TOT,B[I].K)]); }    return 0;}
bzoj1901

bzoj1901--tree-shaped array set of 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.