真的,各位兄弟,我马上就要退役了。在退役前想做到题,可惜有错误,希望各位可以帮帮我。WA,20分。
#include<bits/stdc++.h>
namespace fast_IO
{
#define Getchar() p1==p2 and (p2=(p1=Inf)+fread(Inf,1,1<<21,stdin),p1==p2)?EOF:*p1++
#define Putchar(c) p3==p4 and (fwrite(Ouf,1,1<<21,stdout),p3=Ouf),*p3++=c
char Inf[1<<21],Ouf[1<<21],*p1,*p2,*p3=Ouf,*p4=Ouf+(1<<21);
inline void read(int &x,char c=Getchar())
{
bool f=c!=45;
x=0;
while(c<48 or c>57) c=Getchar(),f&=c!=45;
while(c>=48 and c<=57) x=(x<<3)+(x<<1)+(c^48),c=Getchar();
x=f?x:-x;
}
inline void write(int x)
{
if(x<0) Putchar(45),x=-x;
if(x>=10) write(x/10),x%=10;
Putchar(x^48);
}
inline void read(short &x,char c=Getchar())
{
bool f=c!=45;
x=0;
while(c<48 or c>57) c=Getchar(),f&=c!=45;
while(c>=48 and c<=57) x=(x<<3)+(x<<1)+(c^48),c=Getchar();
x=f?x:-x;
}
inline void write(short x)
{
if(x<0) Putchar(45),x=-x;
if(x>=10) write(x/10),x%=10;
Putchar(x^48);
}
inline void read(long long &x,char c=Getchar())
{
bool f=c!=45;
x=0;
while(c<48 or c>57) c=Getchar(),f&=c!=45;
while(c>=48 and c<=57) x=(x<<3)+(x<<1)+(c^48),c=Getchar();
x=f?x:-x;
}
inline void write(long long x)
{
if(x<0) Putchar(45),x=-x;
if(x>=10) write(x/10),x%=10;
Putchar(x^48);
}
inline bool inrange(const char &ch)
{
if(ch>=48 and ch<=57 or ch>=65 and ch<=90 or ch>=97 and ch<=122 or ch==45 or ch==43 or ch==35 or ch==94 or ch==38 or ch==42 or ch==40 or ch==41 or ch==47) return true;
return false;
}
inline void read(std::string &st,char c=Getchar())
{
st.clear();
while(!inrange(c)) c=Getchar();
while(inrange(c)) st+=c,c=Getchar();
}
inline void write(std::string st)
{
for(int i=0;i<st.size();i++) Putchar(st[i]);
}
inline void read(char &ch)
{
ch=Getchar();
while(!inrange(ch)) ch=Getchar();
}
inline void write(const char &ch)
{
Putchar(ch);
}
class fastin
{
public:
template<typename T>
inline fastin &operator>>(T &x)
{
read(x);
return *this;
}
};
class fastout
{
public:
template<typename T>
inline fastout &operator<<(T x)
{
write(x);
return *this;
}
};
fastin in;
fastout out;
};
using namespace fast_IO;
int n,m;
template<typename T>
inline void swap(T &_x,T &_y)
{
T tmp=_x;
_x=_y,_y=tmp;
}
struct fhq_node
{
int val,maxi,siz,lazy,rev_lazy,w;
fhq_node *lc,*rc;
inline fhq_node()
{
val=maxi=0,siz=1,lazy=rev_lazy=0,w=rand(),lc=rc=nullptr;
}
inline void pushup()
{
maxi=val;
if(lc!=nullptr) maxi=std::max(maxi,lc->maxi);
if(rc!=nullptr) maxi=std::max(maxi,rc->maxi);
siz=(lc==nullptr?0:lc->siz)+(rc==nullptr?0:rc->siz)+1;
}
inline void pushdown()
{
if(rev_lazy)
{
if(lc!=nullptr) lc->rev_lazy^=1;
if(rc!=nullptr) rc->rev_lazy^=1;
swap(lc,rc),rev_lazy=0;
}
if(lazy)
{
if(lc!=nullptr) lc->val+=lazy,lc->maxi+=lazy,lc->lazy+=lazy;
if(rc!=nullptr) rc->val+=lazy,rc->maxi+=lazy,rc->lazy+=lazy;
lazy=0,this->pushup();
}
}
};
class fhq_treap
{
#define ls l,mid-1
#define rs mid+1,r
private:
fhq_node *root;
inline fhq_node *merge(fhq_node *l,fhq_node *r)
{
if(l==nullptr && r==nullptr) return nullptr;
if(l==nullptr)
{
r->pushdown();
return r;
}
if(r==nullptr)
{
l->pushdown();
return l;
}
if(l->w<r->w)
{
l->pushdown(),l->rc=merge(l->rc,r),l->pushup();
return l;
}else
{
r->pushdown(),r->lc=merge(l,r->lc),r->pushup();
return r;
}
}
inline std::pair<fhq_node *,fhq_node *> split(fhq_node *rt,const int &k)
{
if(rt==nullptr) return std::make_pair(nullptr,nullptr);
std::pair<fhq_node *,fhq_node *> ret;
rt->pushdown();
int left=(rt->lc==nullptr?0:rt->lc->siz);
if(k<=left) ret=split(rt->lc,k),rt->lc=ret.second,rt->pushup(),ret.second=rt;
else ret=split(rt->rc,k-left-1),rt->rc=ret.first,rt->pushup(),ret.first=rt;
return ret;
}
inline fhq_node *build(int l,int r)
{
if(l>r) return nullptr;
if(l==r) return new fhq_node();
int mid=(l+r)/2;
fhq_node *rt=new fhq_node();
rt->lc=build(ls),rt->rc=build(rs);
rt->pushup();
return rt;
}
public:
inline void build()
{
root=build(1,n);
}
inline void fix(const int &L,const int &R,const int &val)
{
std::pair<fhq_node *,fhq_node *> a,b;
a=split(root,R),b=split(a.first,L-1);
b.second->val+=val,b.second->lazy+=val,b.second->maxi+=val;
root=merge(merge(b.first,b.second),a.second);
}
inline void overturn(const int &L,const int &R)
{
std::pair<fhq_node *,fhq_node *> a,b;
a=split(root,R),b=split(a.first,L-1);
b.second->lazy^=1;
root=merge(merge(b.first,b.second),a.second);
}
inline int ask(const int &L,const int &R)
{
std::pair<fhq_node *,fhq_node *> a,b;
a=split(root,R),b=split(a.first,L-1);
int ret=b.second->maxi;
root=merge(merge(b.first,b.second),a.second);
return ret;
}
};
fhq_treap tree;
int main()
{
srand(time(0));
in>>n>>m,tree.build();
for(int i=1,op,l,r,v;i<=m;i++)
{
in>>op>>l>>r;
if(op==1) in>>v,tree.fix(l,r,v);
else if(op==2) tree.overturn(l,r);
else out<<tree.ask(l,r)<<'\n';
}
fwrite(Ouf,1,p3-Ouf,stdout),fflush(stdout);
return 0;
}