[JZOJ4216]平方和

题目大意

给定一个 n 个数构成的序列{ai},有 m 次操作,操作有以下三种:
Insert y x: 在序列的第 y 个数之前加入数x
Add l r x : 对序列中第 l 个数到第r个数,每个数都加上 x
Query l r : 询问序列中第 l 个数到第r个数的平方和


题目分析

直接将标记 (x+a)2 拆成 x2+2ax+a2 ,使用splay维护就好了。
时间复杂度 O(nlogn)


代码实现

#include <iostream>
#include <cstdio>
#include <cctype>
#include <stack>

using namespace std;

inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while (!isdigit(ch)) f=ch=='-'?-1:f,ch=getchar();
    while (isdigit(ch)) x=x*10+ch-'0',ch=getchar();
    return x*f;
}

int buf[30];

inline void write(int x)
{
    if (x<0) putchar('-'),x=-x;
    for (;x;x/=10) buf[++buf[0]]=x%10;
    if (!buf[0]) buf[++buf[0]]=0;
    for (;buf[0];putchar('0'+buf[buf[0]--]));
}

const int P=7459;
const int N=100050;
const int Q=100050;
const int S=N+Q;

int a[N];
int n,q;

inline int sqr(int x){return x*x%P;}

struct SPLAY
{
    int son[S][2],fa[S],size[S],sum[S][2],val[S],tag[S];
    stack<int> st;
    int root,tot;

    inline bool side(int x){return son[fa[x]][1]==x;}

    inline void update(int x)
    {
        size[x]=size[son[x][0]]+size[son[x][1]]+1;
        sum[x][1]=(sum[son[x][0]][1]+sum[son[x][1]][1]+sqr(val[x]))%P;
        sum[x][0]=(sum[son[x][0]][0]+sum[son[x][1]][0]+val[x])%P;
    }

    inline int newnode(int x,int l,int r)
    {
        fa[++tot]=0,val[tot]=x;
        fa[l]=l?tot:0,fa[r]=r?tot:0,son[tot][0]=l,son[tot][1]=r;
        return update(tot),tot;
    }

    inline void ADD(int x,int y){(sum[x][1]+=y*sum[x][0]*2%P+sqr(y)*size[x]%P)%=P,(sum[x][0]+=size[x]*y)%=P,(val[x]+=y)%=P,(tag[x]+=y)%=P;}

    inline void clear(int x)
    {
        if (tag[x])
        {
            if (son[x][0]) ADD(son[x][0],tag[x]);
            if (son[x][1]) ADD(son[x][1],tag[x]);
            tag[x]=0;
        }
    }

    inline void pushdown(int x,int y)
    {
        for (;x!=y;st.push(x),x=fa[x]);
        for (;!st.empty();clear(st.top()),st.pop());
    }

    inline void rotate(int x)
    {
        int y=fa[x];bool s=side(x);
        if (fa[y]) son[fa[y]][side(y)]=x;
        if (son[x][s^1]) fa[son[x][s^1]]=y;
        son[y][s]=son[x][s^1],son[x][s^1]=y;
        fa[x]=fa[y],fa[y]=x;
        update(y),update(x);
    }

    inline void splay(int x,int y)
    {
        for (pushdown(x,y);fa[x]!=y;rotate(x))
            if (fa[fa[x]]!=y)
                if (side(x)==side(fa[x])) rotate(fa[x]);
                else rotate(x);
    }

    inline int kth(int x,int y)
    {
        clear(x);
        if (size[son[x][0]]+1==y) return x;
        return size[son[x][0]]>=y?kth(son[x][0],y):kth(son[x][1],y-size[son[x][0]]-1);
    }

    inline void split(int x,int y,int &l,int &r)
    {
        if (!y) l=0,r=x;
        else splay(l=kth(x,y),0),fa[r=son[l][1]]=0,son[l][1]=0,update(l);
    }

    inline void merge(int x,int y,int &rt)
    {
        if (!x) rt=y;
        else if (!y) rt=x;
        else
        {
            splay(x=kth(x,size[x]),0);
            son[fa[y]=x][1]=y,update(rt=x);
        }
    }

    inline void modify(int l,int r,int delta)
    {
        int x,y,mid;
        split(root,r,x,y),split(x,l-1,x,mid),ADD(mid,delta),merge(x,mid,x),merge(x,y,root);
    }

    inline int query(int l,int r)
    {
        int x,y,mid,ret;
        split(root,r,x,y),split(x,l-1,x,mid),ret=sum[mid][1],merge(x,mid,x),merge(x,y,root);
        return ret;
    }

    inline void insert(int x,int y)
    {
        int l,r;
        split(root,y-1,l,r),root=newnode(x,l,r);
    }

    inline void init()
    {
        root=0;
        for (int i=n;i>=1;--i) root=newnode(a[i],0,root);
        splay(1,0),root=1;
    }
}t;

int main()
{
    freopen("sum.in","r",stdin),freopen("sum.out","w",stdout);
    n=read();
    for (int i=1;i<=n;++i) a[i]=(read()+P)%P;
    t.init();
    for (q=read();q--;)
    {
        char opt=getchar();
        for (;!isalpha(opt);opt=getchar());
        switch (opt)
        {
            case 'I':
            {
                int y=read(),x=(read()+P)%P;
                t.insert(x,y);
                break;
            }
            case 'A':
            {
                int l=read(),r=read(),x=(read()+P)%P;
                t.modify(l,r,x);
                break;
            }
            case 'Q':
            {
                int l=read(),r=read();
                write(t.query(l,r)),putchar('\n');
                break;
            }
        }
    }
    fclose(stdin),fclose(stdout);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值