Persistent arrays • Abandon dignity

Source: Internet
Author: User

#include <cstdio>#defineMXN 1000000+1#defineMid (A, b) ((A+B) >>1)intRead () {intx=0, w=1; CharC=GetChar ();  while(c<'0'|| C>'9'){        if(c=='-') w=-1; C=GetChar (); }     while(c>='0'&&c<='9') {x= (x<<3) + (x<<1) + (C-'0'); C=GetChar (); }    returnx*W;}intarr[mxn];unsignedintnum,q;structnode{intVal; intLeft,right;} node[ +*MXN];intROOT[MXN];intnode_num=-1, rtop=-1;voidUpdateintNow ) {    intNl,nr; NL=Node[now].left; NR=Node[now].right; if(nl!=-1) node[now].val=Node[nl].val; if(nr!=-1) node[now].val=Node[nr].val; return;}intNewNode () {Node*N; N=&node[++Node_num]; N->val=0; N->left=-1; N->right=-1; returnNode_num;}intNewNodeintNow ) {Node*N; N=&node[++Node_num]; N->val=Node[now].val; N->left=Node[now].left; N->right=Node[now].right; returnNode_num;}voidBuildintNowintBlintBR) {    if(bl==1&&br==num) root[++rtop]=Now ; if(bl==R) {Node[now].val=ARR[BL]; return; }    intNl,nr; NL=NewNode (); NR=NewNode (); Node[now].left=NL; Node[now].right=nr;    Build (Nl,bl,mid (BL,BR)); Build (Nr,mid (BL,BR)+1, BR);    Update (now); return;}intChangeintNowintKintAlintArintLintR) {    if(l>ar| | R<al)return-1; intN; if(l<=al&&ar<=R) {N=NewNode (now); }    if(al<=l&&r<=ar) {Node[n].val=K; }    Else{        intLs,rs; LS=Node[n].left; RS=Node[n].right; if(node[n].left!=-1&&l<=al&&ar<=Mid (L,r)) {Node[n].left=newnode (LS); Node[n].left=Change (Node[n].left,k,al,ar,l,mid (l,r)); }        if(node[n].right!=-1&&mid (L,R) +1<=al&&ar<=R) {Node[n].right=NewNode (RS); Node[n].right=change (Node[n].right,k,al,ar,mid (l,r) +1, R);    } update (n); }    if(l==1&&r==num) root[++rtop]=N; returnN;}intQues (intNowintQlintQrintLintR) {    if(r<ql| | QR&LT;L)returnNow ; intN; if(l<=ql&&qr<=R) {N=NewNode (now); }    if(QL&LT;=L&AMP;&AMP;R&LT;=QR)returnN; intLs,rs; LS=Node[n].left; RS=Node[n].right; if(node[n].left!=-1&&l<=ql&&qr<=Mid (L,r)) {Node[n].left=newnode (LS); Node[n].left=ques (Node[n].left,ql,qr,l,mid (l,r)); }    if(node[n].right!=-1&&mid (L,R) +1<=ql&&qr<=R) {Node[n].right=NewNode (RS); Node[n].right=ques (Node[n].right,ql,qr,mid (l,r) +1, R);    } update (n); if(l==1&&r==num) root[++rtop]=N; returnN;}intFindintNowintQlintQrintLintR) {    if(r<ql| | QR&LT;L)return 0; if(QL&LT;=L&AMP;&AMP;R&LT;=QR)returnNode[now].val; if(node[now].left!=-1&&l<=ql&&qr<=Mid (L,r)) {        returnFind (Node[now].left,ql,qr,l,mid (l,r)); }    if(node[now].right!=-1&&mid (L,R) +1<=ql&&qr<=R) {        returnFind (Node[now].right,ql,qr,mid (l,r) +1, R); }}intmain () {num=read (); Q=read ();  for(intI=1; i<=num;i++) arr[i]=read (); Build (NewNode (),1, num);  for(intI=0; i<q;i++){        intedt,p,a,b; EDT=read (); P=read (); if(p==1) {a=read (); b=read (); Change (Root[edt],b,a,a,1, num); }        if(p==2) {a=read (); Ques (Root[edt],a,a,1, num); printf ("%d\n", Find (Root[rtop],a,a,1, num)); }    }    return 0;}
give up your dignity

This time I gave up as a pointer to the party's dignity, chose the array implementation, a.

I'm in a very complicated mood.

But the time is not really good, perhaps my chairman of the tree is still too large to write the constant.

Cried.

But I'll probably use more pointers later on.

Persistent arrays • Abandon dignity

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.