比赛 cdcqの省选膜你赛 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 新史「新幻想史 -现代史-」 最终得分 100
用户昵称 chad 运行时间 4.631 s
代码语言 C++ 内存使用 30.83 MiB
提交时间 2017-04-11 11:55:38
显示代码纯文本
/*
吓得我差点用可持久化线段树
*/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<bitset>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define up(i,j,n) for(ll i=j;i<=n;i++)
#define FILE "cdcq_a"
#define db double
#define pii pair<ll,ll>
#define M 16
ll read(){
    ll x=0,f=1,ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
    return f*x;
}
const int maxn=(ll)4e5+20,inf=(ll)1e9,mod=(ll)1e9+7,limit=(ll)1e6;
template<class T> inline bool cmax(T& a,T b){return a<b?a=b,true:false;}
template<class T> inline bool cmin(T& a,T b){return a>b?a=b,true:false;}
int n,m;
ll c[maxn],c2[maxn],v[maxn];
int lowbit(ll x){return x&-x;}
ll getsum(int x){
	ll ans=0,y=x;
	while(x)ans+=c[x],x-=lowbit(x);
	ans*=(y+1);x=y;
	while(x)ans-=c2[x],x-=lowbit(x);
	return ans;
}
void add(int x,ll d){
	ll y=x;
	while(x<=n*2)c[x]+=d,x+=lowbit(x);
	x=y;
	while(x<=n*2)c2[x]+=d*y,x+=lowbit(x);
}
struct node{
	ll op,l,r,v,t,ans,id;
}a[maxn];
inline bool cmp(const node& a,const node& b){return a.id<b.id;}
inline bool cmp2(const node& a,const node& b){return a.t<b.t;}
void divide(int l,int r){
	if(l>=r)return;
	int mid=(l+r)>>1;
	sort(a+l,a+mid+1,cmp2);
	sort(a+mid+1,a+r+1,cmp2);
	int R=mid+1;
	for(int i=l;i<=mid;i++){
		if(a[i].op==1)continue;
		while(a[R].op==2&&R<=r)R++;
		if(a[i].t<=a[R].t||R>r){
			add(a[i].l,a[i].v);
			add(a[i].r+1,-a[i].v);
			continue;
		}
		if(a[i].t>a[R].t&&R<=r)
			a[R].ans+=getsum(a[R].r)-getsum(a[R].l-1),i--,R++;
	}
	while(R<=r){
		while(a[R].op==2&&R<=r)R++;if(R>r)break;
		a[R].ans+=getsum(a[R].r)-getsum(a[R].l-1);
		R++;
	}
	for(int i=l;i<=mid;i++)
		if(a[i].op==2)add(a[i].l,-a[i].v),add(a[i].r+1,a[i].v);
	sort(a+l,a+r+1,cmp);
	divide(l,mid);
	divide(mid+1,r);
}
int main(){
    freopen(FILE".in","r",stdin);
    freopen(FILE".out","w",stdout);
	n=read(),m=read();ll cnt=0;
	up(i,1,n){
		v[i]=read();
		a[++cnt].l=i;
		a[cnt].op=2;
		a[cnt].r=i;
		a[cnt].v=v[i];
		a[cnt].id=cnt;
		a[cnt].t=-1;
	}
	char ch;
	up(i,1,m){
		scanf(" %c",&ch);
		if(ch=='Q'){
			a[++cnt].op=1;
			a[cnt].l=read(),a[cnt].r=read();
			a[cnt].t=read();
			a[cnt].id=cnt;
		}
		if(ch=='M'){
			a[++cnt].op=2;
			a[cnt].l=read(),a[cnt].r=read();a[cnt].v=-read();
			a[++cnt].op=2;
			a[cnt].l=read(),a[cnt].r=read();a[cnt].v=read();
			a[cnt].t=a[cnt-1].t=read();
			a[cnt].id=cnt;a[cnt-1].id=cnt-1;
		}
	}
	divide(1,cnt);
	sort(a+1,a+cnt+1,cmp);
	up(i,1,cnt)
		if(a[i].op==1)printf("%lld\n",a[i].ans);
    return 0;
}