Shandong Province selected Depressed Little J

Source: Internet
Author: User

Little J is a librarian in the National Library, and his job is to manage a huge bookshelf. Although he is very hard-working, but because the bookshelf is very large, so his efficiency is always very low, so that he is facing the risk of dismissal, which is what he depressed.

Specifically, the bookshelf consists of n book bits, numbered from 1 to N. Each book has a book, and each book has a specific code.

There are two types of work for Little J:

    1. The library often buys new books, and the shelves are full at any moment, so they have to take away the books from a certain location and replace them with the newly purchased ones.
    2. Little J needs to answer the customer's query, and the customer will ask how many copies of a particular coded book in a sequential book position.

For example, a total of 5 book positions, at the beginning of the book position on the book encoded as 1,2,3,4,5

A customer asked the book bit 1 to the book bit 3 encoded as "2" the total number of books, the answer is: 1

A customer asked the book bit 1 to the book bit 3 encoded as "1" the total number of books, the answer is: 1

At this time, the library bought a book encoded as "1", and put it to the number 2nd book position.

A customer asked the book bit 1 to the book bit 3 encoded as "2" the total number of books, the answer is: 0

A customer asked the book bit 1 to the book bit 3 encoded as "1" the total number of books, the answer is: 2

......

Your task is to write a program to answer each customer's inquiry.

input

The first row of two integers n,m, representing altogether n book bits, M operations.

The next line is a total of n integers a1,a2 ... An,ai indicates the encoding of the book at the beginning of position I.

Next m lines, each row represents one operation, one character at the beginning of each line

If the character is ' C ', it means that the library buys a new book, followed by two integers a (1<=a<=n), p, indicating that the book was placed on position A, and that the code of the book is p.

If the character is ' Q ', it represents a customer query, followed by three integers a,b,k (1<=a<=b<=n), which indicates how many copies of the book from book A to book B (including A and b) are encoded in K.

(1<=n,m<=100000, all occurrences of the book are encoded as a positive number that is not greater than 2147483647.) )

Output

For each customer's query, output an integer that represents the result of the customer's query.

This is the subject of a balanced tree. Just to practice the Chairman's tree and write it by the way. Memory consumption is much larger than the balance tree.

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;#defineMAXN 1000005structnode{intval,ch[2],cnt;} REC[MAXN<<5];introot[maxn],num[maxn],v[maxn],vsz,tot=0, n,m;structopt{Charops[3]; intL,r,k;} OPZ[MAXN];#defineLowbit (x) (x&-x)voidInsertint&now,intPosintLintRintFlag//no last argument. {rec[++tot]=Rec[now]; now=tot; inttemp=Now ;  while(l<r) {intMid= (l+r) >>1; if(pos<=mid) {rec[++tot]=rec[rec[temp].ch[0]]; rec[temp].ch[0]=tot; Temp=tot; R=mid; }        Else{rec[++tot]=rec[rec[temp].ch[1]]; rec[temp].ch[1]=tot; Temp=tot; L=mid+1; }} rec[tot].cnt+=Flag;}intQueryintNowintPosintLintR) {     while(l!=r) {intMid= (l+r) >>1; if(pos<=mid) {R=mid; now=rec[now].ch[0]; }        Else{ Now=rec[now].ch[1]; L=mid+1; }    }    returnrec[now].cnt;}intGetans (intXintPOS) {    intans=0;  while(x>0) {ans+=query (Root[x],pos,1, vsz); X-=lowbit (x); }    returnans;}voidUpdintXintPosintflag) {     while(x<=N) {insert (Root[x],pos,1, Vsz,flag); X+=lowbit (x); }}intMain () {scanf ("%d%d",&n,&m);  for(intI=0; i<n;i++) {scanf ("%d",&Num[i]); V[i]=Num[i]; } vsz=N;  for(intI=1; i<=m;i++) {scanf ("%s", Opz[i].ops); if(opz[i].ops[0]=='C') {scanf ("%d%d",&opz[i].l,&OPZ[I].K); V[vsz++]=OPZ[I].K; }        Elsescanf" %d%d%d",&opz[i].l,&opz[i].r,&OPZ[I].K); } sort (V,v+vsz); Vsz=unique (V,V+VSZ)-v;  for(intI=0; i<n;i++)    {        intPos=lower_bound (V,v+vsz,num[i])-v+1; UPD (i+1Pos1); }    intT1,t2,t3;  for(intI=1; i<=m;i++)    {        if(opz[i].ops[0]=='C') {T1=opz[i].l,t2=num[t1-1]; T2=lower_bound (V,V+VSZ,T2)-v+1; UPD (T1,t2,-1); T2=OPZ[I].K; Num[t1-1]=T2; T2=lower_bound (V,V+VSZ,T2)-v+1; UPD (T1,t2,1); }            Else{T1=opz[i].l,t2=opz[i].r,t3=OPZ[I].K; T3=lower_bound (V,V+VSZ,T3)-v+1; printf ("%d\n", Getans (T2,T3)-getans (t1-1, T3)); }    }return 0;}

Shandong Province selected Depressed Little J

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.