题解:P15412 「TBOI Round 1」Niton & Galgame

· · 题解

这也太魔怔了。

考虑刻画答案,一个数有贡献当且仅当其不为后缀最大(小)值且末尾不为这个后缀的最大(小)值,那么我们可以用单调栈维护。

对于 B 性质,建出操作树,发现弹栈是 O(n),这也太菜了。实际上我们并不关注弹出了什么,我们需要的只有修改后栈顶的位置而已,那么每次修改的位置是 O(1) 了,每次二分栈顶的位置,是好维护的。

但是要强制在线啊。考虑类似可持久化状物的思路,对于修改的地方新建节点,整个单调栈就是从根出发的一条路径,二分改成树上倍增即可,总复杂度 O(m\log m)。

::::success[code]

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define int long long
#define pii pair<int,int>
#define pdd pair<double,double>
#define rep(i,l,r) for(int i=(l);i<=(r);i++)
#define dep(i,r,l) for(int i=(r);i>=(l);i--)
#define ls (u<<1)
#define rs (u<<1|1)
#define fi first
#define se second
#define FLAG 0
using namespace std;
const int N=1e6+15,mod=998244353;const ll inf=1e15;const long double eps=1e-9;
namespace I_O {
#define IOSIZE 100000
    char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
    template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
    template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
    inline bool read(double &x){x=0.0;int w = 1;double y=0.1;char ch=getchar(); for(;!isdigit(ch) and (ch != EOF);ch=getchar()) if(ch=='-') w=-1;if (ch == EOF) return false; for(;isdigit(ch);ch=getchar()) x=x*10+(ch^48);if(ch!='.'){x*=w;return true;} for(ch=getchar();isdigit(ch);ch=getchar(),y/=10) x+=y*(ch-48);x*=w;return true;}
    template<typename T> inline void put(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) put(x / 10); putchar(x % 10 + 48); }
    inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
    inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
    inline void put(char x) { putchar(x); }
    inline void put(char *x) { while (*x) putchar(*x++); }
    inline void put(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
    inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
    inline void put(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
    inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
    inline void put(bool b) { putchar(b+48); }
    template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
    template<typename T, typename... T1> inline void put(T a, T1... other) { put(a), put(other...); }
    struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
    template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
    template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return put(b), io; }
#define cout io
#define cin io
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
    inline int Max(const int &a,const int &b){return a>b?a:b;}
    inline int Min(const int &a,const int &b){return a<b?a:b;}
} using namespace I_O;

int n,o,f1[N],f2[N];

struct sta{
    int fa[N][21],f[N],w[N],tot,now,s[N],sum[N],p[N];
    bool fl;

    inline int F(){return fa[now][0];}
    inline int fs(){return (s[f[now]]-sum[f[now]]+mod)%mod;}
    bool cmp(int x,int y){if(!x) return 0;return fl?w[x]>y:w[x]<y;}

    int ins(int x){
        ++tot;
        s[tot]=(s[now]+x)%mod,w[tot]=x;
        dep(i,20,0) if(cmp(fa[now][i],x)) now=fa[now][i];
        if(cmp(now,x)) now=fa[now][0];
        fa[tot][0]=now,sum[tot]=(sum[now]+x)%mod;
        if(w[now]==x) f[tot]=f[now];
        else f[tot]=now;
        rep(i,1,20) fa[tot][i]=fa[fa[tot][i-1]][i-1];
        return now=tot;
    }

    int get(int x){
        int u=f[now];
        dep(i,20,0) if(x<fa[u][i]) u=fa[u][i];
        u=fa[u][0];
        return sum[u];
    }
}st1,st2;

void solve(){
    st2.fl=1;
    cin>>n>>o;
    int lt=0;
    rep(i,1,n){
        int op,x;cin>>op>>x;
        x^=lt*o;
        if(op==1){
            f1[i]=st1.ins(x);
            f2[i]=st2.ins(x);
            if(min(st1.F(),st2.F())==0){cout<<(lt=0)<<'\n';continue;}
            if(st1.f[st1.now]<st2.f[st2.now]) lt=st1.fs()-st2.get(st1.f[st1.now]);
            else lt=st2.fs()-st1.get(st2.f[st2.now]);
            lt=(lt%mod+mod)%mod;
            cout<<lt<<'\n';
        }else{
            st1.now=f1[i]=f1[x];
            st2.now=f2[i]=f2[x];
        }
    }
}

signed main(){
    int _=1;if(FLAG) cin>>_;
    while(_--) solve();
    return 0;
}

::::