| XJR Test your data structure (radical 2) |
| Difficulty level: C; operating time limit: 3000ms; operating space limit: 51200KB; code length limit: 2000000B |
| Question Description |
Please write a data structure to complete the following functions: 1) Find the maximum, minimum, and continuous and in the number of L to R. 2) Increase the number of Addl to addr by V. |
| Input |
First line: n, number of digits Second line: Spaces separate each number of AI Line three: Q, indicates the number of operations After Q line: First enter a letter, If the letter is "Q" followed by two numbers, respectively L and R If the letter "A" is followed by three numbers, respectively, ADDL,ADDR and V |
| Output |
Several rows, sequentially outputting the maximum, minimum, and continuous, respectively, for each query (Q) operation. Each line follows the following format (no spaces at the end of the line): Maxnum: Integer, Minnum: Integer, Sum: Integer |
| Input example |
5 1 2 3) 4 5 3 Q 1 4 A 3 3 2 Q 1 5
|
| Output example |
Maxnum:4, Minnum:1, sum:10 Maxnum:5, Minnum:1, sum:17
|
| Other Notes |
0 < N, q < 100001 0 < Ai < 2001 0 < L, R < n + 1 |
The puzzle: hahaha haha (someone has a stem spicy ~)
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <algorithm>5#include <queue>6#include <cstring>7 #definePAU Putchar (")8 #defineENT Putchar (' \ n ')9 #defineCH for (int d=0;d<=1;d++) if (Ch[d])Ten using namespacestd; One Const intmaxn=100000+Ten, maxnode=200000+Ten, inf=-1u>>1; A structnode{ -node*ch[2];Long LongMi,mx,sm,add,siz;node () {mi=inf;mx=-inf;sm=add=0;} - voidSett (Long Longtag) {Mi=mx=sm=tag;return;} the voidAddtLong Longtag) {add+=tag;mi+=tag;mx+=tag;sm+=tag*siz;return;} - voidDown () {if(add) {CH{CH[D]->ADDT (add);} Add=0;}return;} - voidUpdate () { -mi=inf;mx=-inf;sm=0; +Ch{mi=min (Mi,ch[d]->mi); Mx=max (MX,CH[D]->MX);sm+=ch[d]->SM;} - return; + } A}seg[maxnode],*nodecnt=seg,*Root; at intA[MAXN],QL,QR,CV; - voidBuild (Node*&x,intLintR) { -x=nodecnt++; - if(l==r) x->sett (a[l]); - Else{ - intM=l+r>>1; inBuild (x->ch[0],l,m); -Build (x->ch[1],m+1, R); toX->update (); + } -x->siz=r-l+1;return; the } * voidUpdate (NODE*&X,intLintR) { $ if(QL<=L&&R<=QR) x->ADDT (CV);Panax Notoginseng Else{ - intM=l+r>>1; theX->Down (); + if(ql<=m) Update (x->ch[0],l,m); A if(qr>m) Update (x->ch[1],m+1, R); theX->update (); + } - return; $ } $ Long Long_mi,_mx,_sm; - voidQuery (Node*x,intLintR) { - if(ql<=l&&r<=qr) { the_mi=min (_mi,x->mi); -_mx=max (_mx,x->MX);Wuyi_sm+=x->SM; the } - Else{ Wu intM=l+r>>1; -X->Down (); About if(ql<=m) query (x->ch[0],l,m); $ if(qr>m) query (x->ch[1],m+1, R); -}return; - } -InlineintRead () { A intx=0, sig=1;CharCh=GetChar (); + while(!isdigit (CH)) {if(ch=='-') sig=-1; ch=GetChar ();} the while(IsDigit (CH)) x=Ten*x+ch-'0', ch=GetChar (); - returnx*=Sig; $ } theInlinevoidWriteintx) { the if(x==0) {Putchar ('0');return;}if(x<0) Putchar ('-'), x=-x; the intlen=0, buf[ the]; while(x) buf[len++]=x%Ten, x/=Ten; the for(inti=len-1; i>=0; i--) Putchar (buf[i]+'0');return; - } inInlineCharREADC () { the CharCh=GetChar (); the while(!isalpha (CH)) ch=GetChar (); About returnch; the } the intn,q; the voidinit () { +n=read (); - for(intI=1; i<=n;i++) a[i]=read (); theBuild (Root,1, n);Bayi return; the } the voidWork () { -Q=read ();CharTP; - while(q--){ theTP=READC (); Ql=read (); qr=read (); the if(ql>qr) swap (QL,QR); the if(tp=='A'){ thecv=read (); -Update (Root,1, n); the } the Else{ the_mi=inf;_mx=-inf;_sm=0;94Query (Root,1, n); theprintf"maxnum:%lld, Minnum:%lld, Sum:%lld\n", _MX,_MI,_SM); the } the }98 return; About } - voidprint () {101 return;102 }103 intMain () {init (); work ();p rint ();return 0;}
COJ 0359 XJR test data structure (root 2) segment tree interval increase