前排提醒
注意数据规模,若每个点维护的信息都比较多,考虑分块。
以下模板仅支持基本操作,但代码好写,常数较小。 支持区间查/改,容易增加线段树二分。
考场必备版本
#include<bits/stdc++.h>
#define TG 自行修改lazytag相关信息
typedef unsigned int i4;
const i4 MX=100005<<2;
i4 n,m,N,D,TG;
struct Node{/*自行修改*/}A[MX];
inline Node operator+(const Node x,const Node y){
/*自行修改*/
}inline void pup(i4 x){A[x]=A[x<<1]+A[x<<1|1];}
inline void upd(i4 x,const i4 TG){
/*自行修改*/
}inline void pdn(i4 x){
upd(x<<1,TG),upd(x<<1|1,TG);
/*自行修改(清空lazy_tag)*/
}inline void push(i4 l,i4 r){for(i4 i=D;i;i--)pdn(l>>i),pdn(r>>i);}
inline void mdf(i4 l,i4 r,const i4 TG){
for(push(l+=N-1,r+=N+1);l^r^1;pup(l>>=1),pup(r>>=1)){
if(~l&1)upd(l^1,TG);if(r&1)upd(r^1,TG);
}while(l>>=1)pup(l);
}inline Node que(i4 l,i4 r){
Node L,R;L=R=/*自行修改*/;
for(push(l+=N-1,r+=N+1);l^r^1;l>>=1,r>>=1){
if(~l&1)L=L+A[l^1];if(r&1)R=A[r^1]+R;
}return L+R;
}int main(){
std::ios::sync_with_stdio(0);std::cin.tie(0),std::cout.tie(0);
std::cin>>n>>m;N=1<<(D=std::__lg(n+1)+1);
for(i4 i=N+1,x;i<=N+n;i++)
std::cin>>x,A[i]=/*自行修改*/,TG;
for(i4 i=N-1;i;i--)pup(i),TG;
while(m--){
/*自行修改*/
}return 0;
}稍微卡常
P2572 [SCOI2010] 序列操作 ---- 最优解
#include<bits/stdc++.h>
typedef unsigned int i4;
char buf[1<<21],*p1=buf,pbuf[1<<21],*pp=pbuf;
inline i4&in(i4&x){while(!isdigit(*p1++));x=*(p1-1)^48;
while(isdigit(*p1))x=x*10+((*p1++)^48);return x;
}inline void out(uint64_t x){
static char s[20];i4 t=0;do s[t++]=x%10,x/=10;while(x);
while(t)*pp++=s[--t]^48;*pp++=10;
}const i4 MX=100005<<2;
i4 n,m,N,D,cls[MX],flip[MX];
struct Node{i4 s0,s1,m0,m1,l0,l1,r0,r1;}A[MX];
inline Node operator+(const Node x,const Node y){
return {x.s0+y.s0,x.s1+y.s1,
std::max(std::max(x.m0,y.m0),x.r0+y.l0),
std::max(std::max(x.m1,y.m1),x.r1+y.l1),
x.l0+(x.s1?0:y.l0),x.l1+(x.s0?0:y.l1),
y.r0+(y.s1?0:x.r0),y.r1+(y.s0?0:x.r1)};
}inline void pup(i4 x){A[x]=A[x<<1]+A[x<<1|1];}
inline void upd(i4 x,const i4 cl,const i4 fl){
Node&X=A[x];
if(cl<2){const i4 l=cl*(X.s0+X.s1),r=(!cl)*(X.s0+X.s1);
X={r,l,r,l,r,l,r,l},cls[x]=cl,flip[x]=0;
}if(fl)std::swap(X.s0,X.s1),std::swap(X.m0,X.m1),
std::swap(X.l0,X.l1),std::swap(X.r0,X.r1),flip[x]^=1;
}inline void pdn(i4 x){
upd(x<<1,cls[x],flip[x]),upd(x<<1|1,cls[x],flip[x]);
cls[x]=2,flip[x]=0;
}inline void push(i4 l,i4 r){for(i4 i=D;i;i--)pdn(l>>i),pdn(r>>i);}
inline void mdf(i4 l,i4 r,const i4 cl,const i4 fl){
for(push(l+=N-1,r+=N+1);l^r^1;pup(l>>=1),pup(r>>=1)){
if(~l&1)upd(l^1,cl,fl);if(r&1)upd(r^1,cl,fl);
}while(l>>=1)pup(l);
}inline Node que(i4 l,i4 r){
Node L,R;L=R={0,0,0,0,0,0,0,0};
for(push(l+=N-1,r+=N+1);l^r^1;l>>=1,r>>=1){
if(~l&1)L=L+A[l^1];if(r&1)R=A[r^1]+R;
}return L+R;
}int main(){
fread(buf,1,1<<21,stdin);
N=1<<(D=std::__lg(in(n)+1)+1),in(m);
for(i4 i=N+1,x;i<=N+n;i++)
in(x),A[i]={!x,x,!x,x,!x,x,!x,x},cls[i]=2;
for(i4 i=N-1;i;i--)pup(i),cls[i]=2;
while(m--){i4 op,x,y;in(op),in(x)++,in(y)++;
if(op<3){mdf(x,y,op,op==2);continue;}
Node X=que(x,y);out(op==3?X.s1:X.m1);
}fwrite(pbuf,1,pp-pbuf,stdout);
return 0;
}#include<bits/stdc++.h>
typedef unsigned int i4;
typedef unsigned long long i8;
char buf[1<<23],*p1=buf,pbuf[1<<22],*pp=pbuf;
inline i4&in(i4&x){while(!isdigit(*p1++));x=*(p1-1)^48;
while(isdigit(*p1))x=x*10+((*p1++)^48);return x;
}inline void out(i8 x){
static char s[20];i4 t=0;do s[t++]=x%10,x/=10;while(x);
while(t)*pp++=s[--t]^48;*pp++=10;
}i4 A6017[32]={1,3,6,13,24,52,103,222,384,832,1648,3552,6237,13563,26511,56906,98304,212992,421888,909312,1596672,3472128,6786816,14567936,25190110,54589881,108036850,232800673,408783316,888883132,1737454078,3729449897};
i4 h[1<<16],l[1<<16];
inline void init_nim(){
for(i4 i=0;i<16;i++)l[1<<i]=A6017[i],h[1<<i]=A6017[i+16];
for(i4 i=0;i<1<<16;i++)l[i]=l[i&i-1]^l[i&-i],h[i]=h[i&i-1]^h[i&-i];
}i4 nim(i4 x){return h[x>>16]^l[x&65535];}
i4 n,q,N,D,cnt[1<<19];
struct Node{i4 rot=0,Xor[32]={};i8 Sum[32]={};}A[1<<19];
inline void pup(i4 x){
const i4 l=A[x<<1].rot,r=A[x<<1|1].rot;A[x].rot=0;
for(i4 i=0;i<32;i++)
A[x].Xor[i]=A[x<<1].Xor[(i+l)&31]^A[x<<1|1].Xor[(i+r)&31],
A[x].Sum[i]=A[x<<1].Sum[(i+l)&31]+A[x<<1|1].Sum[(i+r)&31];
}inline void upd(i4 x,const i4 c){
if(!c)return;(A[x].rot+=c)&=31;(cnt[x]+=c)&=31;
}inline void pdn(i4 x){upd(x<<1,cnt[x]),upd(x<<1|1,cnt[x]);cnt[x]=0;}
inline void push(i4 l,i4 r){for(i4 i=D;i;i--)pdn(l>>i),pdn(r>>i);}
inline void mdf(i4 l,i4 r){
for(push(l+=N-1,r+=N+1);l^r^1;pup(l>>=1),pup(r>>=1)){
if(~l&1)upd(l^1,1);if(r&1)upd(r^1,1);
}while(l>>=1)pup(l);
}inline i8 queXor(i4 l,i4 r){
i8 X=0;for(push(l+=N-1,r+=N+1);l^r^1;l>>=1,r>>=1){
if(~l&1)X=X^A[l^1].Xor[A[l^1].rot];if(r&1)X=X^A[r^1].Xor[A[r^1].rot];
}return X;
}inline i8 queSum(i4 l,i4 r){
i8 X=0;for(push(l+=N-1,r+=N+1);l^r^1;l>>=1,r>>=1){
if(~l&1)X=X+A[l^1].Sum[A[l^1].rot];if(r&1)X=X+A[r^1].Sum[A[r^1].rot];
}return X;
}int main(){
fread(buf,1,1<<23,stdin);init_nim();
N=1<<(D=std::__lg(in(n)+1)+1),in(q);
for(i4 i=N+1;i<=N+n;i++){A[i].Sum[0]=in(A[i].Xor[0]);
for(i4 j=1;j<32;j++)A[i].Sum[j]=A[i].Xor[j]=nim(A[i].Xor[j-1]);
}for(i4 i=N-1;i;i--)pup(i);
while(q--){i4 t,l,r;in(t),in(l),in(r);
if(t==1){mdf(l,r);continue;}
if(t==2){out(queXor(l,r));continue;}
if(t==3){out(queSum(l,r));continue;}
}fwrite(pbuf,1,pp-pbuf,stdout);
return 0;
}