比赛 新春水题赛 评测结果 AAWWAWWWWW
题目名称 快速红包变换 最终得分 30
用户昵称 Fancy 运行时间 0.146 s
代码语言 C++ 内存使用 12.90 MiB
提交时间 2016-02-07 18:07:17
显示代码纯文本
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n,m,x,y,z,tot=1;
char s[10];
struct node{
    int l,r,ls,rs,sum,max,min,nmax,nmin,mark,tag;
}t[300001];
void go_down(int root)
{
    if(!t[root].mark&&!t[root].tag) return;
    int M=t[root].mark,L=t[root].ls,R=t[root].rs;
    if(!L) return ;
    if(!t[root].tag)
    {
    	t[L].mark+=M;t[L].max+=M;t[L].min+=M;
	    t[L].sum+=(t[L].r-t[L].l+1)*M;
        t[R].mark+=M;t[R].max+=M;t[R].min+=M;
	    t[R].sum+=(t[R].r-t[R].l+1)*M;
        t[root].mark=0;
    }
   	else
   	{
   		t[L].mark=M;t[L].tag=1;t[L].max=t[L].min=M;
	    t[L].sum=(t[L].r-t[L].l+1)*M;t[L].nmax=t[L].nmin=t[L].r-t[L].l+1;
        t[R].mark=M;t[L].tag=1;t[R].max=t[R].min=M;
	    t[R].sum=(t[R].r-t[R].l+1)*M;t[R].nmax=t[R].nmin=t[R].r-t[R].l+1;
        t[root].mark=t[root].tag=0;
   	}
}
void up_down(int root)
{
	int L=t[root].ls,R=t[root].rs;
	t[root].sum=t[L].sum+t[R].sum;
	t[root].max=max(t[L].max,t[R].max);
	t[root].min=min(t[L].min,t[R].min);
	t[root].nmax=t[root].nmin=0;
	if(t[root].max==t[L].max) t[root].nmax+=t[L].nmax;
	if(t[root].max==t[R].max) t[root].nmax+=t[R].nmax;
	if(t[root].min==t[L].min) t[root].nmin+=t[L].nmin;
	if(t[root].min==t[R].min) t[root].nmin+=t[R].nmin;
}
void Build(int root,int L,int R)
{
    t[root].l=L;t[root].r=R;
    if(L==R)
    {
        int k;scanf("%d",&k);
        t[root].sum=t[root].max=t[root].min=k;
        t[root].nmax=t[root].nmin=1;
		return;
    }
    int mid=(L+R)>>1;
    t[root].ls=++tot;Build(t[root].ls,L,mid);
    t[root].rs=++tot;Build(t[root].rs,mid+1,R);
    up_down(root);
}
void Add(int root,int L,int R,int x)
{
    if(L<=t[root].l&&t[root].r<=R)
    {
        t[root].mark+=x;t[root].sum+=x*(t[root].r-t[root].l+1);
        t[root].max+=x;t[root].min+=x;
		t[root].nmax=t[root].nmin=t[root].r-t[root].l+1;
        return;
    }
    int mid=(t[root].l+t[root].r)>>1;
    go_down(root);
    if(L<=mid) Add(t[root].ls,L,R,x);
    if(R>mid)  Add(t[root].rs,L,R,x);
    up_down(root);
}
void Change(int root,int L,int R,int x)
{
	if(L<=t[root].l&&t[root].r<=R)
    {
        t[root].mark=x;t[root].tag=1;
		t[root].sum=x*(t[root].r-t[root].l+1);
        t[root].max=t[root].min=x;
        t[root].nmax=t[root].nmin=t[root].r-t[root].l+1;
        return;
    }
    int mid=(t[root].l+t[root].r)>>1;
    go_down(root);
    if(L<=mid) Change(t[root].ls,L,R,x);
    if(R>mid)  Change(t[root].rs,L,R,x);
    up_down(root);
}
void Cmax(int root,int L,int R,int x)
{
	if(L<=t[root].l&&t[root].r<=R&&t[root].nmax==t[root].r-t[root].l+1)
    {
		t[root].sum=max(t[root].sum,x*(t[root].r-t[root].l+1));
		t[root].max=t[root].min=t[root].mark=max(t[root].max,x);
		t[root].tag=1;
        return;
    }
    int mid=(t[root].l+t[root].r)>>1;
    go_down(root);
    if(L<=mid) Cmax(t[root].ls,L,R,x);
    if(R>mid)  Cmax(t[root].rs,L,R,x);
    up_down(root);
}
void Cmin(int root,int L,int R,int x)
{
	if(L<=t[root].l&&t[root].r<=R&&t[root].nmax==t[root].r-t[root].l+1)
    {
        t[root].sum=min(t[root].sum,x*(t[root].r-t[root].l+1));
        t[root].max=t[root].min=t[root].mark=min(t[root].min,x);
        t[root].tag=1;
        return;
    }
    int mid=(t[root].l+t[root].r)>>1;
    go_down(root);
    if(L<=mid) Cmin(t[root].ls,L,R,x);
    if(R>mid)  Cmin(t[root].rs,L,R,x);
    up_down(root);
}
int Sum(int root,int L,int R)
{
    if(L<=t[root].l&&t[root].r<=R) return t[root].sum;
    int mid=(t[root].l+t[root].r)>>1,ans=0;
    go_down(root);
    if(L<=mid) ans+=Sum(t[root].ls,L,R);
    if(R>mid)  ans+=Sum(t[root].rs,L,R);
    return ans;
}
int Max(int root,int L,int R,int &nmax)
{
	if(L<=t[root].l&&t[root].r<=R)
	{
		nmax=t[root].nmax;return t[root].max;
	} 
    int mid=(t[root].l+t[root].r)>>1,max1=0,max2=0,n1=0,n2=0;
    go_down(root);
    if(L<=mid) max1=Max(t[root].ls,L,R,n1);
    if(R>mid)  max2=Max(t[root].rs,L,R,n2);
    if(max1>=max2) nmax+=n1;
    if(max1<=max2) nmax+=n2;
    return max(max1,max2);
}
int Min(int root,int L,int R,int &nmin)
{
	if(L<=t[root].l&&t[root].r<=R)
	{
		nmin=t[root].nmin;return t[root].min;
	} 
    int mid=(t[root].l+t[root].r)>>1,min1=0x7fffffff,min2=0x7fffffff,n1=0,n2=0;
    go_down(root);
    if(L<=mid) min1=Min(t[root].ls,L,R,n1);
    if(R>mid)  min2=Min(t[root].rs,L,R,n2);
    if(min1<=min2) nmin+=n1;
    if(min1>=min2) nmin+=n2;
    return min(min1,min2);
}
int main()
{
	freopen("redbag.in","r",stdin);
	freopen("redbag.out","w",stdout);
    scanf("%d",&n);Build(1,1,n);
    scanf("%d",&m);
    while(m--)
    {
        scanf("%s",s);
        if(s[0]=='C')
        {
            scanf("%d%d%d",&x,&y,&z);
            if(s[1]=='a') Add(1,x,y,z);
            else if(s[1]=='c') Change(1,x,y,z);
            else if(s[3]=='a') Cmax(1,x,y,z);
            else Cmin(1,x,y,z);
        }
        else
        {
            scanf("%d%d",&x,&y);
            if(s[1]=='s') printf("%d\n",Sum(1,x,y));
            else if(s[3]=='a')
            {
            	int nmax=0,maxx=Max(1,x,y,nmax);
            	if(s[1]=='w') printf("%d\n",maxx);
            	else printf("%d\n",nmax);
            }
            else
            {
            	int nmin=0,minn=Min(1,x,y,nmin);
            	if(s[1]=='w') printf("%d\n",minn);
            	else printf("%d\n",nmin);
            }
        }
    }
    return 0;
}