bzoj1251 sequence Terminator Splay

Source: Internet
Author: User

Wrote again splay feel more experience speed incredibly and stuck into the home.

The question to note is that the initial value should be-inf (MX and V)

#include <cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#defineRep (i,l,r) for (int i=l;i<r;i++)#defineCLR (a,x) memset (A,x,sizeof (a))using namespacestd;Const intmaxn=50050, maxm=100050, inf=0x3fffffff;intn,m;structnode*NULL,*pt;structnode{Node*ch[2]; intv,s,add,mx; BOOLRev; InlineintcmpintKConst{k-=ch[0]->s; if(k==1)return-1; returnk<=0?0:1; } Inlinevoidmaintain () {s=ch[0]->s+ch[1]->s+1; MX=max (V,max (ch[0]->mx,ch[1]->mx)); } Inlinevoidpushdown () {if(rev) {Rev=0; ch[0]->rev^=1; ch[1]->rev^=1; Swap (ch[0],ch[1]); }        if(add) {if(ch[0]!=NULL) {ch[0]->add+=add; ch[0]->v+=add; ch[0]->mx+=add; }            if(ch[1]!=NULL) {ch[1]->add+=add; ch[1]->mx+=add; ch[1]->v+=add; } Add=0; }}};node*NewNode () {pt->ch[0]=NULL; PT->ch[1]=NULL; PT->mx=0; PT->add=0; PT->v=0; PT->s=1; PT->rev=0; returnpt++;} Node*Root;node X[MAXN];voidRotate (Node*&o,intd) {Node*k=o->ch[d^1]; o->ch[d^1]=k->Ch[d]; K->ch[d]=o; o->maintain ();k->maintain (); o=K;}voidSplay (Node*&o,intk) {o-pushdown (); intD=o->CMP (k); if(d==-1)return; if(d==1) k-=o->ch[0]->s+1; Node*p=o->Ch[d]; P-pushdown (); intD2=p->CMP (k); intk2=d2?k-p->ch[0]->s-1: K; if(d2!=-1) {splay (P-ch[d2],k2); D==d2?rotate (o,d^1): Rotate (o->ch[d],d); } Rotate (O,d^1);} Node* Build (intLintR) {        if(L&GT;=R)return NULL; intMid= (l+r) >>1; Node*o=NewNode (); if(L<mid) o->ch[0]=build (L,mid); if(mid+1&LT;R) o->ch[1]=build (mid+1, R); o-maintain (); returno;}intRead () {CharC; intf=1, ans=0; C=GetChar ();  while(!IsDigit (c)) {        if(c=='-') f=-1; C=GetChar (); }     while(IsDigit (c)) {ans=ans*Ten+c-'0'; C=GetChar (); }    returnf*ans;}voidinit () {PT=x; NULL=NewNode (); NULL->v=-inf; NULL->mx=-inf; NULL->s=0; Root=build (0, n+2);} intMain () {n=read (), m=read ();    Init ();  while(m--){        intopt=read (); if(opt==1){            intL=read (), R=read (), delta=read ();            Splay (root,l); Splay (Root->ch[1],r+1-root->ch[0]->s); Root->ch[1]->ch[0]->add+=Delta; Root->ch[1]->ch[0]->v+=Delta; Root->ch[1]->ch[0]->mx+=Delta; }Else if(opt==2){            intL=read (), r=read (); if(L==R)Continue;            Splay (root,l); Splay (Root->ch[1],r+1-root->ch[0]->s); Root->ch[1]->ch[0]->rev^=1; }Else{            intL=read (), r=read ();            Splay (root,l); Splay (Root->ch[1],r+1-root->ch[0]->s); printf ("%d\n", root->ch[1]->ch[0]->MX); }    }    return 0;}
View Code 1251: Sequence terminator time limit:20 Sec Memory limit:162 MB
submit:2791 solved:1110
[Submit] [Status] [Discuss] Description

There are many questions on the web, that is, given a sequence, you need to support several operations: A, B, C, D. Look at another question, and another sequence to support several operations: D, C, B, A. In particular, we have someone here, out of the mock test, incredibly also out of one such, really no technical content ... So I also out of a problem, I came out this purpose is to let everyone do this topic has a "library" can rely on, there is no other meaning. This problem is called sequence Terminator. "Problem description" given a sequence of length n, the element of each sequence is an integer (nonsense). To support the following three actions: 1. Add all the numbers in this range [L,r] to V. 2. Flip [l,r] this interval, e.g. 1 2 3 4 to 4 3 2 1. 3. Ask for the maximum value in this interval [l,r]. At the very beginning, all elements are 0.

Input

The first row of two integers n,m. M is the number of operations. The following m lines, with a maximum of four integers per line, are k,l,r,v. K is the number of operations, if not the 1th operation then there are only two digits after K.

Output

For each of the 3rd operations, give the correct answer.

Sample Input4 4
1 1 3 2
1 2 4-1
2 1 3
3 2 4

Sample Output2
"Data Range"
n<=50000,m<=100000.

HINT Source

Splay

[Submit] [Status] [Discuss]

bzoj1251 sequence Terminator Splay

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.