"BZOJ1901" Dynamic rankings, tree-shaped array set of Chairman tree

Source: Internet
Author: User

time:2016.05.09
Author:xiaoyimi
Reprint Note Source Thank

Portal (permissions)

Surface
1901:zju2112 Dynamic Rankings

Time Limit:10 Sec Memory limit:128 MB
submit:6678 solved:2777
[Submit] [Status] [Discuss]
Description

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.

Input

For each query, you need to output his answer, with each output occupying a separate line.

Output

Sample Input

5 3

3 2 1) 4 7

Q 1 4 3

C 2 6

Q 2 5 3

Sample Output

3

6

HINT

20% of the data, m,n≤100; 40% of the data, m,n≤1000; 100% of the data, m,n≤10000.

Ideas:
Tree-shaped array set of Chairman tree
Always tangled is the prefix to build or use a tree-like array to build, and small dinosaurs, dad3zz discussed for a long time (was criticized as a tangle no use of things), see a few people's template feel uncomfortable, finally still feel Huang long more comfortable
Attention:
Each modification is actually a new chairman tree, so the chairman tree representing the corresponding elements in the sequence is constantly being updated, so an array is opened in real time to record the elements corresponding to the root of the Chairman tree.
Code:

#include <bits/stdc++.h>#define M 20004#define LS (x) a[x].ch[0]#define RS (x) a[x].ch[1]#define LOW (x) (x& (×))using namespace Std;intNm, tot,cnt;intC[m>>1],id[m],s1[m],s2[m],root[m];struct ask{int x,yK bool opt;}Q[m>>1]; struct disc{intData,id; BOOL operator < (const disc other) const {returndata<other.data; }}b[m];struct chairman_tree{intsiz,ch[2];} A[m*250];intBuildintNowintLintRintKintVal) {intrt=++cnt;    A[rt].siz=a[now].siz+k; LS (RT) =ls (now), RS (RT) =rs (now);if(L==R)returnRtintMid= (l+r) >>1;if(mid>=val) LS (RT) =build (LS (now), l,mid,k,val);ElseRS (RT) =build (RS (now), mid+1, R,k,val);returnRT;}intGetintBeginintEndintK) {if(Begin==end)returnEndintMid= (begin+end) >>1, t=0; for(intI=1; i<=s1[0];i++) T-=a[ls (s1[i])].siz; for(intI=1; i<=s2[0];i++) T+=a[ls (s2[i])].siz;if(k<=t) { for(intI=1; i<=s1[0];i++) S1[i]=ls (S1[i]); for(intI=1; i<=s2[0];i++) S2[i]=ls (S2[i]);returnGet (BEGIN,MID,K); }Else{ for(intI=1; i<=s1[0];i++) S1[i]=rs (S1[i]); for(intI=1; i<=s2[0];i++) S2[i]=rs (S2[i]);returnGet (mid+1, end,k-t); }}main () {scanf ("%d%d",&n,&m); for(intI=1; i<=n;i++) scanf ("%d", C+i), b[i].id=i,b[i].data=c[i];int x,y; Tot=n; for(intI=1; i<=m; i++) {char ch=getchar (); while(ch!=' Q '&&ch!=' C ') Ch=getchar (); scanf"%d%d",&Q[i].x,&Q[i].y);if(ch==' C ')Q[i]. opt=1, B[++tot].id=i+n,b[tot].data=Q[i].y;Elsescanf"%d",&Q[i]. k); }Sort(b +1, b+tot+1); for(intI=1; i<=tot;i++) {id[i]=b[i].data;if(b[i].id>n)Q[b[i]. Id-n].y=i;ElseC[b[i].id]=i; } root[0]=++cnt; for(intI=1; i<=n;i++) for(intJ=i;j<=n;j+=low (j)) Root[j]=build (Root[j],1Tot1, C[i]); for(intI=1; i<=m; i++) {if(!Q[i]. opt) {s1[0]=0; s2[0]=0; for(intj=Q[i].x-1; J;j-=low (j)) s1[++s1[0]]=ROOT[J]; for(intj=Q[i].y; J;j-=low (j)) s2[++s2[0]]=ROOT[J];printf("%d\ n", Id[get (1TotQ[i]. k)]); }Else{ for(intj=Q[i].x; J<=n;j+=low (j)) Root[j]=build (Root[j],1, tot,-1, c[Q[i].x]); c[Q[i].x]=Q[i].y; for(intj=Q[i].x; J<=n;j+=low (j)) Root[j]=build (Root[j],1Tot1, c[Q[i].x]); }    }}

"BZOJ1901" Dynamic rankings, 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.