塔元庄的潮水 题解

· · 题解

20\% 部分分

直接 \mathcal O(n) 暴力修改,写个快速幂做查询,总复杂度 \mathcal O(nq\log x)

性质A&B

性质A就等价于询问操作只询问区间和,所以我们直接线段树区间修改、区间覆盖、区间求和即可

性质B就是把区间和变成区间和&区间平方和

令这个区间原本的区间和和区间平方和为 S 和 S_2,我们考虑这样一个式子

(a+b)^2=a^2+2ab+b^2

我们把它扩展到区间上

\displaystyle\sum_{i=l}^r (a_i+c)^2 \\ =\displaystyle\sum_{i=l}^r a_i^2+c^2+2a_ic\\ =\displaystyle\sum_{i=l}^r a_i^2 +\displaystyle\sum_{i=l}^r c^2+\displaystyle\sum_{i=l}^r 2a_ic\\ =S_2+(r-l+1)c^2+2cS S_{\text{new}}=S+(r-l+1)c

所以我们维护区间平方和和区间和,即可 \mathcal O(1) 完成一个区间的 S 和 S_2 的更新,如果再使用懒标记,即可轻松做到总复杂度为 \mathcal O(q\log n)。

正解

说了那么多,其实暴力和正解完全没有关系,正解是留给勇敢者和强者的奖励(事实上本题是CF896C弱化版,因为那个题的复杂度分析过难,但是那个题的代码完全可以通过本题)。

我们考虑使用 set 来维护连续段,每个连续段只需要记录其左右端点和值即可,然后我们每次操作直接暴力。

对于操作 1,我们分裂出位于 l 到 r 的段,然后直接删去,并加入一个新段,复杂度 \mathcal O(\log n)

对于操作 2,我们分裂出位于 l 到 r 的段,然后逐个枚举修改值,令 m 为当前段的数量,则复杂度为 \mathcal O(m)

对于操作 3,我们分裂出位于 l 到 r 的段,然后逐个枚举加入答案,令 m 为当前段的数量,则复杂度为 \mathcal O(m\log x)

复杂度证明

我们这样考虑,因为数据完全随机,所以三种操作是均匀出现且均匀分布在 [1,n] 的,那我们不妨以一次三种操作为一个单位来进行考虑。

我们假设原本有 m 段,进行 1 操作后变为 m' 段,因为后两种操作不会让 m 发生变化,所以顺序随意,我们考虑两种操作顺序

  1. 3$ $2$ $1
  2. 1$ $2$ $3

第一种顺序的复杂度是 \mathcal O(m\log x)+\mathcal O(m)+\mathcal O(\log x),至少会让段数减少 \frac{1}{3}(n-2)

第二种顺序的复杂度是 \mathcal O(m'\log x)+\mathcal O(m')+\mathcal O(\log x),至少会让段数减少 \frac{1}{3}n-2

其中第一种复杂度在大数据下明显更大,故我们以第一种来分析,令初始 m=n,其总复杂度为

\mathcal O(\frac{1}{3}\displaystyle\sum_{i=1}^q ((n(\frac{2}{3})^{i-1})\log x+(n(\frac{2}{3})^{i-1})+\log x)

使用等比数列求和化简得到这个式子

\mathcal O({n\left(1 - \left(\frac{2}{3}\right)^q\right)(\log x + 1) + \frac{1}{3}q\log x})

令第一项中的 q 趋于正无穷,可知其复杂度一定小于

\mathcal O({n\log x + n + \frac{1}{3}q\log x})

这显然是可以接受的复杂度,所以我们证明了我们看似暴力的做法是正确的

本题教给我们要多多分析复杂度

代码:

#include <bits/stdc++.h>
using namespace std;
namespace MySpace{
    #define lowbit(x) (x&(-x))
    #define sqrt sqrtl
    using pii=pair<int,int>;
    #define whileu(p) while (p--)
    #define forl(a,b,c) for (auto a=b;a<=c;a=a+1)
    inline void read() {}
    template <typename T, typename... R>
    inline void read(T &x,R &... oth){
        x=0;T f=1;
        char c=getchar();
        while(c<'0' || c>'9') {
            if(c=='-') f=-1;
            c=getchar();
        }
        while(c>='0' && c<='9') x=(x<<1)+(x<<3)+(c&15),c=getchar();
        x*=f;
        read(oth...);
        return;
    }
    template<typename T>
    T qpow(T a,T n,T p){
        T res=1;a%=p;
        while (n){
            if (n&1) res=res*a%p;
            a=a*a%p;
            n>>=1;
        }
        return res;
    }
    template<typename T>
    T gcd(T a,T b){return (b>0?gcd(b,a%b):a);}
    template<typename T>
    T exgcd(T a,T b,T& x,T& y){
        if (b<=0ll){
            x=1ll,y=0ll;
            return a;
        }
        T d=exgcd(b,a%b,y,x);
        y-=(a/b)*x;
        return d;
    }
    template<typename T>
    T inv(T a,T p){
        if (a==1) return 1ll;
        T x,y;
        if (exgcd(a,p,x,y)!=1ll) return -1ll;
        else return (x+p)%p;
    }
}
using namespace MySpace;
//const int INF=0x66CCFF66;
#ifndef ONLINE_JUDGE
#include "G:\CodeFloder\debug.h"
using namespace DEBUGs;
#endif
#define int long long
const int mod=1e9+7;
const int maxn=1e5+5;
struct node{
    int l,r;
    mutable int v;
    node(int A,int B,int C){l=A,r=B,v=C;}
    friend bool operator<(node a,node b){
        return a.l<b.l;
    }
};
using sit=set<node>::iterator;
set<node> s;
sit split(int p){
    sit x=s.lower_bound(node(p,0,0));
    if (x!=s.end() && x->l==p) return x;
    x--;
    if (x->r<p) return s.end();
    int l=x->l,r=x->r,v=x->v;
    s.erase(x);
    s.insert(node(l,p-1,v));
    return s.insert(node(p,r,v)).first;
}
void assign(int l,int r,int v){
    sit R=split(r+1),L=split(l);
    s.erase(L,R);
    s.insert(node(l,r,v));
    return;
}
void add(int l,int r,int v){
    sit R=split(r+1),L=split(l);
    for (sit i=L;i!=R;i++) i->v+=v;
    return;
}
int query(int l,int r,int x){
    sit R=split(r+1),L=split(l);
    int res=0;
    for (sit i=L;i!=R;i++) 
        res=(res+qpow(i->v,x,mod)*(i->r-i->l+1)%mod)%mod;
    return res;
}
int n,q,a[maxn];
signed main(){
    read(n,q);
    for (int i=1;i<=n;i++){
        read(a[i]);
        s.insert(node(i,i,a[i]));
    }
    while (q--){
        string opt;cin>>opt;
        int l,r,x;read(l,r,x);
        if (opt=="update") add(l,r,x);
        if (opt=="assign") assign(l,r,x);
        if (opt=="query") printf("%lld\n",query(l,r,x));
    }
    return 0;
}