Loading

NOIP 模拟 $87\; \rm 技术情报局$

题解 \(by\;zj\varphi\)

将问题转化成在笛卡尔树上。

建立一棵大根笛卡尔树,那么一个节点管辖的所有儿子就是它管辖的区间。

区间合并类似于线段树的 pushup 可以手模一下。

复杂度 \(\mathcal{O\rm (n)}\)

Code
#include<bits/stdc++.h>
#define ri signed
#define pd(i) ++i
#define bq(i) --i
#define func(x) std::function<x>
namespace IO{
    char buf[1<<21],*p1=buf,*p2=buf;
    #define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?(-1):*p1++
    #define debug1(x) std::cerr << #x"=" << x << ' '
    #define debug2(x) std::cerr << #x"=" << x << std::endl
    #define Debug(x) assert(x)
    struct nanfeng_stream{
        template<typename T>inline nanfeng_stream &operator>>(T &x) {
            bool f=false;x=0;char ch=gc();
            while(!isdigit(ch)) f|=ch=='-',ch=gc();
            while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=gc();
            return x=f?-x:x,*this;
        }
    }cin;
}
using IO::cin;
namespace nanfeng{
    #define FI FILE *IM
    #define FO FILE *OUT
    template<typename T>inline T cmax(T x,T y) {return x>y?x:y;}
    template<typename T>inline T cmin(T x,T y) {return x>y?y:x;}
    using ll=long long;
    static const int N=1e7+2;
    int a[N],st[N],n,s,l,r,p;
    ll ans;
    namespace GenHelper{
        unsigned z1, z2, z3, z4, b;
        auto Rand=[]() {
            b=((z1<<6)^z1)>>13;
            z1=((z1&4294967294U)<<18)^b;
            b=((z2<<2)^z2)>>27;
            z2=((z2&4294967288U)<<2)^b;
            b=((z3<<13)^z3)>>21;
            z3=((z3&4294967280U)<<7)^b;
            b=((z4<<3)^z4)>>12;
            z4=((z4&4294967168U)<<13)^b;
            return z1^z2^z3^z4;
        };
    }
    auto Get=[](int n,unsigned s,int l,int r) {
        using namespace GenHelper;
        z1=s;
        z2=unsigned((~s)^0x233333333U);
        z3=unsigned(s^0x1234598766U);
        z4=(~s)+51;
        for (ri i(1);i<=n;pd(i)) {
            int x=Rand()&32767;
            int y=Rand()&32767;
            a[i]=(l+(x*32768+y)%(r-l+1));
        }
    };
    struct node{int sum1,sum2,mx;};
    struct Cartesiantree{
        #define ls(x) T[x].l
        #define rs(x) T[x].r
        #define fa(x) T[x].fa
        struct dt{node w;int l,r,fa;}T[N];
        func(void(void)) build=[&]() {
            int tp=0;
            for (ri i(1);i<=n;pd(i)) {
                while(tp&&a[st[tp]]<a[i]) --tp;
                int fa=st[tp];
                ls(i)=rs(fa);
                fa(ls(i))=i;
                rs(fa)=i;
                fa(i)=fa;
                st[++tp]=i;
            }
        };
        func(void(int)) dfs=[&](int x) {
            T[x].w.sum1=T[x].w.sum2=a[x];
            T[x].w.mx=a[x];
            if (ls(x)) {
                dfs(ls(x));
                T[x].w.sum1=(T[ls(x)].w.sum1+1ll*T[ls(x)].w.mx*T[x].w.sum1%p)%p;
                T[x].w.sum2=(1ll*T[ls(x)].w.sum2*T[x].w.mx%p+T[x].w.sum2)%p;
                T[x].w.mx=1ll*T[ls(x)].w.mx*T[x].w.mx%p;
            }
            if (rs(x)) {
                dfs(rs(x));
                T[x].w.sum1=(T[x].w.sum1+1ll*T[rs(x)].w.sum1*T[x].w.mx%p)%p;
                T[x].w.sum2=(1ll*T[rs(x)].w.mx*T[x].w.sum2%p+T[rs(x)].w.sum2)%p;
                T[x].w.mx=1ll*T[rs(x)].w.mx*T[x].w.mx%p;
            }
            int s1=T[ls(x)].w.sum2,s2=T[rs(x)].w.sum1;
            s1=a[x]+1ll*s1*a[x]%p,s2=a[x]+1ll*s2*a[x]%p;
            ll tk=1ll*s1*s2%p;
            ans+=tk;
        };
    }T;
    inline int main() {
        FI=freopen("tio.in","r",stdin);
        FO=freopen("tio.out","w",stdout);
        cin >> n >> s >> l >> r >> p;
        Get(n,s,l,r);
        T.build();
        T.dfs(T.rs(0));
        printf("%lld\n",ans%p);
        return 0;
    }
}
int main() {return nanfeng::main();}
posted @ 2021-11-01 16:40  ナンカエデ  阅读(56)  评论(0编辑  收藏  举报