1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| #include<bits/stdc++.h> #define Tp template<typename Ty> #define Ts template<typename Ty,typename... Ar> #define W while #define I inline #define RI register int #define LL long long #define Cn const #define CI Cn int& #define gc getchar #define D isdigit(c=gc()) #define pc(c) putchar((c)) #define min(x,y) ((x)<(y)?(x):(y)) #define max(x,y) ((x)>(y)?(x):(y)) using namespace std; namespace Debug{ Tp I void _debug(Cn char* f,Ty t){cerr<<f<<'='<<t<<endl;} Ts I void _debug(Cn char* f,Ty x,Ar... y){W(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);} Tp ostream& operator<<(ostream& os,Cn vector<Ty>& V){os<<"[";for(Cn auto& vv:V) os<<vv<<",";os<<"]";return os;} #define gdb(...) _debug(#__VA_ARGS__,__VA_ARGS__) }using namespace Debug; namespace FastIO{ Tp I void read(Ty& x){char c;int f=1;x=0;W(!D) f=c^'-'?1:-1;W(x=(x<<3)+(x<<1)+(c&15),D);x*=f;} Ts I void read(Ty& x,Ar&... y){read(x),read(y...);} Tp I void write(Ty x){x<0&&(pc('-'),x=-x,0),x<10?(pc(x+'0'),0):(write(x/10),pc(x%10+'0'),0);} Tp I void writeln(Cn Ty& x){write(x),pc('\n');} }using namespace FastIO; Cn int N=1e6,S=1272,M=N/S,V=1e5+1,QQ=5e5; int n,m,b[M+5][S+5],sz[M+5],tot,F[S+5],G[S+5],H[V+5],mx,T,Ans[QQ+5]; struct Que{int opt,l,r,x,id;}q[QQ+5]; I int find(CI k,CI x){return x==F[x]?x:F[x]=find(k,F[x]);} I void Z(CI k,CI l,CI r,CI x){ RI i;for(i=0;i<sz[k];i++) G[H[b[k][i]=b[k][find(k,i)]]]=0;for(mx=0,i=0;i<sz[k];i++) H[b[k][i]]=F[i]=-1,b[k][i]-=T; for(i=l;i<=r;i++) b[k][i]>x&&(b[k][i]-=x);for(T=0,i=0;i<sz[k];i++) mx=max(mx,b[k][i]),~H[b[k][i]]?++G[F[i]=H[b[k][i]]]:G[F[i]=H[b[k][i]]=i]=1; } I void Merge(CI k,CI x,CI y){~H[y]&&(~H[x]?G[F[H[y]]=H[x]]+=G[H[y]],G[H[y]]=0:b[k][H[x]=H[y]]=x,H[y]=-1);} I void U(RI l,RI r,CI x){ RI i,j,bL=l/S+1,bR=r/S+1;if(l-=(bL-1)*S,r-=(bR-1)*S,lr<sz[bL]-1) return Z(bL,l,r,x); if(2*x>mx-T){for(j=T+x+1;j<=mx;j++) Merge(bL,j-x,j);mx=min(mx,T+x);} else{for(j=T+x;j>=T+1;j--) Merge(bL,j+x,j);T+=x;} } I int Y(CI k,CI l,CI r,CI x){RI X=0,i;for(i=l;i<=r;i++) b[k][find(k,i)]==T+x&&++X;return X;} I int Q(RI l,RI r,CI x){ RI i,bL=l/S+1,bR=r/S+1,X=0;if(l-=(bL-1)*S,r-=(bR-1)*S,lr<sz[bL]-1) return Y(bL,l,r,x); for(i=bL;i<=bR;i++) T+x<=V&&~H[T+x]&&(X+=G[H[T+x]]);return X; } int main(){ RI i,j,p,k,opt,l,r,x;for(read(n,m),i=0;i<n;i++) read(x),b[i/S+1][sz[i/S+1]++]=x;memset(F,-1,sizeof(F)),memset(H,-1,sizeof(H)); for(i=1;i<=m;i++) read(q[i].opt,q[i].l,q[i].r,q[i].x),q[i].l--,q[i].r--,q[i].id=i;for(i=1;i<=(n-1)/S+1;i++){ memset(H,-1,sizeof(H)),memset(G,0,sizeof(G)),memset(F,-1,sizeof(F)); for(T=mx=0,j=0;j<sz[i];j++) ~H[b[i][j]]?++G[F[j]=H[b[i][j]]]:G[F[j]=H[b[i][j]]=j]=1,mx=max(mx,b[i][j]); for(j=1;j<=m;j++) if((i-1)*S<=q[j].r&&q[j].l<=min(n-1,i*S-1)) if(q[j].opt==1) U(max(q[j].l,(i-1)*S),min(min(n-1,i*S-1),q[j].r),q[j].x); else Ans[q[j].id]+=Q(max(q[j].l,(i-1)*S),min(min(n-1,i*S-1),q[j].r),q[j].x); }for(i=1;i<=m;i++) if(q[i].opt==2) writeln(Ans[i]);return 0; }
|