跳到正文

剪贴板

剪贴板 ha010umu

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#pragma GCC optimize("Ofast")
//#pragma GCC optimize(2,3,"Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("inline","unroll-loops")
#include<immintrin.h>
#include<iostream>
#include<iomanip>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstdio>
#include<cstdlib>
#include<climits>
//#define NDEBUG
#include<cassert>
#include<cstring>
#include<complex>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<bitset>
//#define LL __int128
#define LL long long
#define uint unsigned int
#define ULL unsigned LL
//#define int LL
//#define double long double
#define mkp make_pair
#define par pair<int,int>
#define psb push_back
#define epb emplace_back
#define f(x) ((x).first)
#define s(x) ((x).second)
using namespace std;
#define Lbt(x) ((x)&(-(x)))
#define Swap(x,y) (x^=y^=x^=y)
const int Mxxx=1e5;
inline char gc()
{
	static char buf[Mxxx],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,Mxxx,stdin),p1==p2)?EOF:*p1++;
}
inline char pc(char ch,bool fl=false)
{
	static char buf[Mxxx],*p1=buf,*p2=buf+Mxxx;
	return (fl||((*p1++=ch)&&p1==p2))&&(fwrite(buf,1,p1-buf,stdout),p1=buf),0;
}
//#define gc getchar
//#define pc(x,y) y?0:(putchar(x),0)
#define output pc('!',true)
inline int read()
{
	char ch=gc();
	int gans=0,gflag=0;
	for(;ch<'0'||'9'<ch;gflag|=ch=='-',ch=gc());
	for(;'0'<=ch&&ch<='9';gans=(gans<<1)+(gans<<3)+(ch^48),ch=gc());
	return gflag?-gans:gans;
}
template<typename T>
inline char read(T&gans)
{
	char ch=gc();
	int gflag=0;gans=0;
	for(;ch<'0'||'9'<ch;gflag|=ch=='-',ch=gc());
	for(;'0'<=ch&&ch<='9';gans=(gans<<1)+(gans<<3)+(ch^48),ch=gc());
	return gans=gflag?-gans:gans,ch;
}
template<typename T>
inline void write(T x)
{
	if(x>9)write(x/10);
	pc(x%10^48);
}
template<typename T>
inline void writenum(T x,char ch)
{
	if(x<0)pc('-'),x=-x;
	write(x);pc(ch);
}
inline void writechar(char x,char ch)
{
	pc(x);pc(ch);
}
template<typename T>
inline T Max(T x,T y)
{
	return x>y?x:y;
}
template<typename T>
inline T Min(T x,T y)
{
	return x<y?x:y;
}
template<typename T>
inline T Abs(T x)
{
	return x<0?-x:x;
}
template<typename T>
inline void ckmx(T&x,T y)
{
	x=Max(x,y);
}
template<typename T>
inline void ckmn(T&x,T y)
{
	x=Min(x,y);
}
namespace IS
{
	#define Is __m256i//Instruction Set
	#define Is_set _mm256_set_epi64x//notice:x
	#define Is_add _mm256_add_epi64//notice:no x
	#define Is_set_val(v) Is_set(v,v,v,v)
	#define Is_set_arr(a) Is_set(a[3],a[2],a[1],a[0])
	//-------------------------Is read-------------------------
	const int Mx=1e5;
	const int Mn=sqrt(Mx);
	const int P=2;
	const int B=1<<P;
	const int D=B-1;
	inline Is spe_read(int x=B)
	{
		int i;static int a[B]={0};
		for(i=0;i<x;i++)a[i]=read();
		return Is_set_arr(a);
	}
	const int Mxx=Mx>>P;
	const int Mnn=Mn>>P;
	Is vl[Mxx+5],sm[Mnn+5],tg[Mnn+5];LL spe_tg;
	int al,blk,blg[Mx+5],lft[Mn+5],rgt[Mn+5],len[Mn+5];
	//-------------------------Is tag-------------------------
	#define T(x) ((LL*)(tg+((x)>>P)+1))[((x)&D)-1]
	inline LL Get_tag(int p)
	{
		return p==al?spe_tg:T(p);
	}
	inline void Add_tag(int p,LL v)
	{
		if(p==al)return (void)(spe_tg+=v);
		T(p)+=v;
	}
	//-------------------------Is Ask-------------------------
	//Interval Ask:
	//corner block:vl+tg
	//whole block:sm
	#define W(x) ((LL*)(sm+((x)>>P)+1))[((x)&D)-1]
	inline LL Ask_trv(int l,int r)
	{//whole block:only sm!
		if(l>r)return 0;
		LL s=0;
		for(;((l-1)&D)&&l<=r;l++)s+=W(l);
		for(;(r&D)&&l<=r;r--)s+=W(r);
		if(l>r)return s;
		Is tps=Is_set_val(0);
		for(;l<=r;)tps=Is_add(tps,sm[((l+=B)>>P)]);
		for(l=0;l<B;l++)s+=((LL*)&tps)[l];
		return s;
	}
	#define V(x) ((LL*)(vl+((x)>>P)+1))[((x)&D)-1]
	inline LL Ask(int l,int r,int f=1)//f:use for half constant number
	{//corner block:vl+tg!
		if(l>r)return 0;
		int bl=blg[l],ln=len[bl];assert(bl==blg[r]);
		if(f&&r-l+1>(ln>>1))return Ask_trv(bl,bl)-Ask(lft[bl],l-1)-Ask(r+1,rgt[bl]);
		LL s=Get_tag(bl)*(r-l+1);//how to know the position?     try using 1,2,3,4->a[1][0],a[1][1],a[1][2],a[1][3]
		for(;((l-1)&D)&&l<=r;l++)s+=V(l);
		for(;(r&D)&&l<=r;r--)s+=V(r);
		if(l>r)return s;
		Is tps=Is_set_val(0);//1:a[1] 5:a[2]
		for(;l<=r;)tps=Is_add(tps,vl[((l+=B)>>P)]);
		for(l=0;l<B;l++)s+=((LL*)&tps)[l];
		return s;
	}
	inline LL Ask_bl(int p,int f=1)
	{
		return Ask(lft[p],rgt[p],f);
	}
	inline LL Ask_al(int l,int r)
	{
		int bl=blg[l],br=blg[r];
		return bl==br?Ask(l,r):Ask(l,rgt[bl])+Ask_trv(bl+1,br-1)+Ask(lft[br],r);
	}
	//-------------------------Is pre-------------------------
	inline Is spe_pre(int p,int x=B)
	{
		int i;static LL a[B]={0};
		for(i=0;i<x;i++)a[i]=Ask_bl(p-D+i,0);
		return Is_set_arr(a);
	}
	//-------------------------Is Add-------------------------
	//Interval Add:
	//corner block:vl+sm
	//whole block:tg+sm
	inline void Add_trv(int l,int r,int v)
	{//whole block:sm+tg!
		if(l>r)return;
		for(;((l-1)&D)&&l<=r;l++)W(l)+=v*len[l],Add_tag(l,v);
		if(l>r)return;
		if(r==al)W(r)+=v*len[r],Add_tag(r,v),r--;
		for(;(r&D)&&l<=r;r--)W(r)+=v*len[r],Add_tag(r,v);
		if(l>r)return;
		LL w=(LL)v*blk;
		Is tps=Is_set_val(v),tpss=Is_set_val(w);
		for(;l<=r;l+=B)tg[(l>>P)+1]=Is_add(tps,tg[(l>>P)+1]),sm[(l>>P)+1]=Is_add(tpss,sm[(l>>P)+1]);
		return;
	}
	inline void Add(int l,int r,int v)
	{//corner block:vl+sm!
		if(l>r)return;
		int bl=blg[l],ln=len[bl];assert(bl==blg[r]);
		if(r-l+1>(ln>>1))return Add_trv(bl,bl,v),Add(lft[bl],l-1,-v),Add(r+1,rgt[bl],-v);
		W(bl)+=(LL)v*(r-l+1);
		for(;((l-1)&D)&&l<=r;l++)V(l)+=v;
		for(;(r&D)&&l<=r;r--)V(r)+=v;
		if(l>r)return;
		Is tps=Is_set_val(v);
		for(;l<=r;l+=B)vl[(l>>P)+1]=Is_add(tps,vl[(l>>P)+1]);
		return;
	}
	inline void Add_bl(int p,int v)
	{
		Add(lft[p],rgt[p],v);
	}
	inline void Add_al(int l,int r,int v)
	{
		int bl=blg[l],br=blg[r];
		return bl==br?Add(l,r,v):(Add(l,rgt[bl],v),Add_trv(bl+1,br-1,v),Add(lft[br],r,v));
	}
	//-------------------------Is End.-------------------------
	inline void Pre(int n)
	{
		int i,bl;blk=sqrt(n);
		for(i=1;i<=n;i++)
		{
			bl=blg[i]=(i-1)/blk+1;
			if(bl^blg[i-1])
			{
				lft[bl]=i;rgt[blg[i-1]]=i-1;
				if(bl)len[bl-1]=i-lft[bl-1];
			}
		}rgt[al=blg[n]]=n;len[al]=n-lft[al]+1;
		for(i=B;i<=n;i+=B)vl[i>>P]=spe_read();
		vl[i>>P]=spe_read(n&D);
		for(i=B;i<=al;i+=B)sm[i>>P]=spe_pre(i);
		sm[i>>P]=spe_pre(i,al&D);
	}
}
int n,Q;
signed main()
{
	#ifndef ONLINE_JUDGE
	freopen("_.in","r",stdin);
//	freopen("_.out","w",stdout);
	#endif
	int l,r;
	n=read();Q=read();IS::Pre(n);
	for(;Q;Q--)
	{
		if(read()-1)
		{
			l=read();r=read();
			writenum(IS::Ask_al(l,r),10);
		}
		else
		{
			l=read();r=read();
			IS::Add_al(l,r,read());
		}
	}
	return output;
}
/*
5 2
1 5 4 2 3
1 2 3 2
2 3 4
*/
/*
5 1
1 5 4 2 3
2 2 4
*/
/*
5 5
1 5 4 2 3
2 2 4
1 2 3 2
2 3 4
1 1 5 1
2 1 4
*/