COJ 0358 XJR test data structure (Root 3) segment Tree interval modification

Source: Internet
Author: User

XJR Test your data structure (radical 3)
Difficulty level: C; operating time limit: 1000ms; 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) Change the number of Addl to addr to 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 "C" 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
C 3 3 5
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

Problem: Line tree and Flood

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];intMi,mx,sm,add,Set, Siz;node () {mi=inf;mx=-inf;sm=0;Set=inf;} -     voidSett (intTAG) {mi=mx=Set=tag;sm=tag*siz;return;} the     voidDown () { -         if(Set!=inf) {Ch{ch[d]->sett (Set);}Set=inf;} -         return; -     } +     voidUpdate () { -mi=inf;mx=-inf;sm=0; +Ch{mi=min (Mi,ch[d]->mi); Mx=max (MX,CH[D]-&GT;MX);sm+=ch[d]->SM;} A         return; at     } -}seg[maxnode],*nodecnt=seg,*Root; - intA[MAXN],QL,QR,CV,TP; - voidBuild (Node*&x,intLintR) { -x=nodecnt++; -     if(l==r) x->mi=x->mx=x->sm=A[l]; in     Else{ -         intM=l+r>>1; toBuild (x->ch[0],l,m); +Build (x->ch[1],m+1, R); -X->update (); the} x->siz=r-l+1;return; * } $ voidUpdate (NODE*&AMP;X,intLintR) {Panax Notoginseng     if(QL&LT;=L&AMP;&AMP;R&LT;=QR) x->sett (CV); -     Else{ the         intM=l+r>>1; +X->Down (); A         if(ql<=m) Update (x->ch[0],l,m); the         if(qr>m) Update (x->ch[1],m+1, R); +X->update (); -     } $     return; $ } - int_mi,_mx,_sm; - voidQuery (Node*x,intLintR) { the     if(ql<=l&&r<=qr) { -_mi=min (_mi,x->mi);Wuyi_mx=max (_mx,x->MX); the_sm+=x->SM; -     } Wu     Else{ -         intM=l+r>>1; AboutX->Down (); $         if(ql<=m) query (x->ch[0],l,m); -         if(qr>m) query (x->ch[1],m+1, R); -}return; - } AInlineintRead () { +     intx=0, sig=1;CharCh=GetChar (); the      while(!isdigit (CH)) {if(ch=='-') sig=-1; ch=GetChar ();} -      while(IsDigit (CH)) x=Ten*x+ch-'0', ch=GetChar (); $     returnx*Sig; the } 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; -      for(inti=len-1; i>=0; i--) Putchar (buf[i]+'0');return; in } theInlineCharREADC () { the     Charx=GetChar (); About      while(!isalpha (x)) x=GetChar (); the     returnx; the } the intn,q; + voidinit () { -n=read (); the      for(intI=1; i<=n;i++) a[i]=read ();BayiBuild (Root,1, n); the     return; the } - voidWork () { -Q=read ();Charch; the      while(q--){ theCH=READC (); Ql=read (); qr=read (); the         if(ql>qr) swap (QL,QR); the         if(ch=='C'){ -cv=read (); theUpdate (Root,1, n); the         } the         Else{94_mi=inf;_mx=-inf;_sm=0; theQuery (Root,1, n); theprintf"Maxnum:%d, Minnum:%d, Sum:%d\n", _MX,_MI,_SM); the         }98     } About     return; - }101 voidprint () {102     return;103 }104 intMain () {init (); work ();p rint ();return 0;}

COJ 0358 XJR test data structure (Root 3) segment Tree interval modification

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.