中山游记

· · 生活·游记

2025.07.29

T1

P9790

断环为链,暴力dp 11 分不难,然后线段树维护即可。

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#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
using namespace std;
const int N=4e4+15,mod=998244353;const ll inf=2e9;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(stdstring& 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(stdstring 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))
#define int long long
    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;
ll a[N];
struct Mat{
    ll v[4][4];
    Mat(){rep(i,0,3)rep(j,0,3)v[i][j]=-inf;}
    Mat(const vector<vector<ll> >&w){
        rep(i,0,3)rep(j,0,3)
            v[i][j]=w[i][j];
    }
    Mat operator*(const Mat&b)const{
        Mat res;
        rep(i,0,3) rep(j,0,3) rep(k,0,3)
            res.v[i][j]=max(res.v[i][j],v[k][j]+b.v[i][k]);
        return res;
    }
};
struct tree{
    int l,r;
    Mat v[4];
}t[N<<2];

void up(int u){
    rep(i,0,3) t[u].v[i]=t[ls].v[i]*t[rs].v[i];
}

void change(int u){
    int l=t[u].l;
    rep(i,0,3){
        if(l<=i){
            t[u].v[i]=vector<vector<ll> >{{0,-inf,-inf,-inf},{-inf,0,-inf,-inf},{-inf,-inf,0,-inf},{-inf,-inf,-inf,0}};
        }else if(l==i+1){
            int sum=0;
            rep(j,1,i) sum+=a[j];
            t[u].v[i]=vector<vector<ll> >{{sum,-inf,-inf,-inf},{-inf,0,-inf,-inf},{-inf,-inf,0,-inf},{-inf,-inf,-inf,0}};
        }else{
            t[u].v[i]=vector<vector<ll> >{{0,0,0,0},{a[l],-inf,-inf,-inf},{-inf,a[l],-inf,-inf},{-inf,-inf,a[l],-inf}};
        }
    }
}

void build(int u,int l,int r){
    t[u].l=l,t[u].r=r;
    if(l==r){change(u);return ;}
    int mid=(l+r)>>1;
    build(ls,l,mid),build(rs,mid+1,r);
    up(u);
}

void update(int u,int x){
    if(t[u].l==t[u].r){
        change(u);
        return ;
    }
    int mid=(t[u].l+t[u].r)>>1;
    if(x<=mid) update(ls,x);
    if(x>mid) update(rs,x);
    up(u);
}

ll query(){
    ll ans=0;
    rep(i,0,3) rep(k,0,3-i) ans=max(ans,t[1].v[i].v[k][0]);
    return ans;
}

signed main(){
    cin>>n;
    rep(i,1,n) cin>>a[i];
    build(1,1,n);
    cout<<query()<<'\n';
    int T;cin>>T;
    while(T--){
        int x,y;cin>>x>>y;
        a[x]=y;
        if(x>4) update(1,x);
        else rep(i,1,4) update(1,i);
        cout<<query()<<'\n';
    }
    return 0;
}

T2

P4284

树形 dp ,推柿子

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#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
using namespace std;
const int N=5e5+15,mod=998244353;const ll inf=2e9;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(stdstring& 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(stdstring 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))
//#define int long long
    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;
vector<pair<int,int> > e[N];
int fat[N];
double f[N];

void dfs(int u,int fa){
    for(auto h:e[u]){
        int v=h.fi,w=h.se;
        if(v==fa) continue;
        dfs(v,u);
        f[u]=f[u]+1.0*w/100*f[v]-f[u]*1.0*w*f[v]/100;
    }
}

void dfs_(int u,int fa){
    for(auto h:e[u]){
        int v=h.fi,w=h.se;
        if(v==fa) continue;
        if(f[v]*1.0*w/100!=1){
            double x=1.0*w/100*(f[u]-f[v]*1.0*w/100)/(1-f[v]*1.0*w/100);
            f[v]=f[v]+x-f[v]*x;
        }
        dfs_(v,u);
    }
}

signed main(){
//  freopen("charger.in","r",stdin);
//  freopen("charger.out","w",stdout);
    cin>>n;
    rep(i,1,n-1){
        int u,v,w;cin>>u>>v>>w;
        e[u].push_back({v,w});
        e[v].push_back({u,w});
    }
    rep(i,1,n){
        int x;cin>>x;
        f[i]=1.0*x/100;
    }
    dfs(1,0);
    dfs_(1,0);
    double sum=0;
    rep(i,1,n) sum+=f[i];
    printf("%.6lf\n",sum);
    return 0;
}

T3

P1879 加强版

need 轮廓线 dp

正常状压肯定过不去,使用轮廓线 dp 优化,可以将状态个数减少一半,但剩余的状态就很多,我们预处理出所有可能的情况,使每次枚举情况的数量减少,就可以通过此题。

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define rep(i,l,r) for(register int i=(l);i<=(r);++i)
#define dep(i,r,l) for(register int i=(r);i>=(l);--i)
#define ls (u<<1)
#define rs (u<<1|1)
#define fi first
#define se second
using namespace std;
const int N=4e4+15,mod=100000000;const ll inf=2e9;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(stdstring& 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(stdstring 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))
//#define int long long
    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,m;
int f[2][130000];
int a[121][22];
int cnt,fd[130000],nxt[1<<21];

bool check(int x){
    bool fl=0,ff=0;
    while(x){
        if(x&1){
            if(fl){if(ff) return 0;else ff=1;}
            fl=1;
        }else fl=0;
        x>>=1;
    }
    return 1;
}

signed main(){
    freopen("cowfood.in","r",stdin);
    freopen("cowfood.out","w",stdout);
    cin>>n>>m;
    rep(i,1,n) rep(j,1,m) cin>>a[i][j];
    int mx=(1<<m)-1;
    memset(nxt,-1,sizeof nxt);
    rep(i,0,mx) if(check(i)) fd[cnt]=i,nxt[i]=cnt++;
    cnt--;
    f[0][0]=1;
    int now=0;
    rep(i,1,n) rep(j,1,m){
        now^=1;
        rep(k,0,cnt) f[now][k]=0;
        rep(k,0,cnt){
            int lh=(1<<(j-1))&fd[k],h=(j==1?0:(1<<(j-2))&fd[k]);
            if(i==1&&lh) continue;
            if(lh){
                if(nxt[fd[k]^(1<<(j-1))]==-1) continue;
                f[now][nxt[fd[k]^(1<<(j-1))]]+=f[now^1][k];
                f[now][nxt[fd[k]^(1<<(j-1))]]%=mod;
            }else if(h||!a[i][j]){
                f[now][k]+=f[now^1][k];
                f[now][k]%=mod;
            }else{
                f[now][k]+=f[now^1][k];
                f[now][k]%=mod;
                if(nxt[fd[k]^(1<<(j-1))]==-1) continue;
                f[now][nxt[fd[k]^(1<<(j-1))]]+=f[now^1][k];
                f[now][nxt[fd[k]^(1<<(j-1))]]%=mod;
            }
        }
    }
    ll ans=0;
    rep(i,0,cnt) ans+=f[now][i];
    cout<<ans%mod;
    return 0;
}

2025.07.30

T1

正解是线段树内统计可能改变贡献的组合的数量(反正不好写,数据范围是 1e5,分块怎么都能够冲过去了。

直接维护加 0~3 次时块内的贡献(加 4 次相当于没加,散块暴力重构即可。

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#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
using namespace std;
const int N=1e5+15,mod=998244353;const ll inf=2e9;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(stdstring& 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(stdstring 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))
//#define int long long
    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;

inline int qpow(int a,int b){
    int res=1;
    while(b){
        if(b&1) res=1ll*res*a%mod;
        a=1ll*a*a%mod,b>>=1;
    }
    return res;
}

#define M(x) ((x)>=4?(x)-4:(x))

int n,q,a[N];
int pos[N],len,bl,L[400],R[400];
int w[400][4],cnt[400];
string s;
int fac[N],inv[N];
inline int C(int n,int m){
    if(m<0||n<0) return 0;
    if(m>n) return 1;
    return 1ll*fac[n]*inv[m]%mod*inv[n-m]%mod;
}

void build(int id){
    cnt[id]=w[id][0]=w[id][1]=w[id][2]=w[id][3]=0;
    rep(i,L[id]+1,R[id]) if(a[i]<=a[i-1]) w[id][0]++;
    a[L[id]]==3?a[L[id]]=0:a[L[id]]+=1;
    rep(i,L[id]+1,R[id]){a[i]==3?a[i]=0:a[i]++;if(a[i]<=a[i-1]) w[id][1]++;}
    a[L[id]]==3?a[L[id]]=0:a[L[id]]+=1;
    rep(i,L[id]+1,R[id]){a[i]==3?a[i]=0:a[i]++;if(a[i]<=a[i-1]) w[id][2]++;}
    a[L[id]]==3?a[L[id]]=0:a[L[id]]+=1;
    rep(i,L[id]+1,R[id]){a[i]==3?a[i]=0:a[i]++;if(a[i]<=a[i-1]) w[id][3]++;}
    a[L[id]]==3?a[L[id]]=0:a[L[id]]+=1;
    rep(i,L[id]+1,R[id]) a[i]==3?a[i]=0:a[i]++;
}

void rebuild(int id){
    rep(i,L[id],R[id]){a[i]+=cnt[id];if(a[i]>3) a[i]%=4;}
    build(id);
}

void add(int id){
    cnt[id]++;
    if(cnt[id]>3) cnt[id]=0;
}

void change(int id,int l,int r){
    l=max(l,L[id]),r=min(r,R[id]);
    rep(i,l,r){a[i]++;if(a[i]>3)a[i]=0;}
    rebuild(id);
}

void update(int l,int r){
    int ld=pos[l],rd=pos[r];
    if(ld==rd){
        change(ld,l,r);
        return ;
    }
    change(ld,l,r),change(rd,l,r);
    rep(i,ld+1,rd-1) add(i);
}

int query(int l,int r){
    int ld=pos[l],rd=pos[r];
    int res=0;
    if(ld==rd){
        rep(i,l+1,r) if(M(a[i]+cnt[ld])<=M(a[i-1]+cnt[ld])) res++;
        return res;
    }
    rep(i,l+1,R[ld]) if(M(a[i]+cnt[ld])<=M(a[i-1]+cnt[ld])) res++;
//    cout<<l<<' '<<R[ld]<<' ';
//    cout<<res<<'\n';
    rep(i,L[rd]+1,r) if(M(a[i]+cnt[rd])<=M(a[i-1]+cnt[rd])) res++;
//    cout<<L[rd]<<' '<<r<<' ';
//    cout<<res<<'\n';
    if(M(a[L[rd]]+cnt[rd])<=M(a[R[rd-1]]+cnt[rd-1])) res++;
//    cout<<res<<'\n';
    rep(i,ld+1,rd-1){
        res+=w[i][cnt[i]];
        if(M(a[L[i]]+cnt[i])<=M(a[R[i-1]]+cnt[i-1])) res++;
    }
    return res;
}

signed main(){
    cin>>n>>q;
    cin>>s;len=sqrt(n);bl=n/len;
    fac[0]=inv[0]=1;
    rep(i,1,n) fac[i]=1ll*fac[i-1]*i%mod;
    inv[n]=qpow(fac[n],mod-2);
    dep(i,n-1,1) inv[i]=1ll*inv[i+1]*(i+1)%mod;
    rep(i,1,n) a[i]=s[i-1]-'A';
    rep(i,1,bl){
        L[i]=R[i-1]+1,R[i]=R[i-1]+len;
        rep(j,L[i],R[i]) pos[j]=i;
    }
    if(R[bl]<n){
        bl++;
        L[bl]=R[bl-1]+1,R[bl]=n;
        rep(j,L[bl],R[bl]) pos[j]=bl;
    }
    rep(i,1,bl) build(i);
    while(q--){
        int opt,l,r;cin>>opt>>l>>r;
        if(opt==1){
            update(l,r);
        }else{
            int k;cin>>k;
//            rep(i,l,r) cout<<M(a[i]+cnt[pos[i]])<<' ';cout<<'\n';
            int h=query(l,r);
//            rep(i,l,r) cout<<M(a[i]+cnt[pos[i]])<<' ';cout<<'\n';
//            cout<<r-l<<' '<<h<<'\n';
            cout<<C(r-l-h,k-1-h)<<'\n';
        }
    }
    return 0;
}

T2

直接统计取相同的两段。

长的一段形如 ABA。

枚举分界线及其 r,可以写出朴素的 O(n^3) dp。

预处理出 LCS,可以只枚举两个 A 后面的分界点。然后可以 O(n^2)

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#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
using namespace std;
const int N=5000+15,mod=998244353;const ll inf=2e9;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(stdstring& 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(stdstring 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))
#define int long long
    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;

string s,t;
int n,m;
int f[N][N],g[N][N];
int sum[N],sum_[N];
int ans;

void solve(int p){
    memset(f,0,sizeof f);
    memset(g,0,sizeof g);
}

signed main(){
    cin>>s>>t;
    n=s.size(),m=t.size();
    s=" "+s,t=" "+t;
    dep(i,n,1) dep(j,n,1) if(s[i]==s[j]) f[i][j]=f[i+1][j+1]+1;
    rep(i,1,n) rep(j,1,m) if(s[i]==t[j]) g[i][j]=g[i-1][j-1]+1;
    rep(i,1,n){
        rep(j,0,m) sum[j]=sum_[j]=0;
        rep(j,1,m){
            if(!g[i][j]) continue;
            sum[g[i][j]]++,sum_[g[i][j]]+=g[i][j];
        }
        rep(j,1,m){
            sum[j]+=sum[j-1];
            sum_[j]+=sum_[j-1];
        }
        rep(j,1,i){
            int r=i+1-j,l=max(1,r-f[i+1][j]);
            if(l>r||r>m) continue;
            ans+=sum_[r]-sum_[l-1]-(sum[r]-sum[l-1])*(l-1);
            ans+=(sum[m]-sum[r])*(r-l+1);
        }
    }
    reverse(s.begin()+1,s.end());
    reverse(t.begin()+1,t.end());
    swap(s,t),swap(n,m);
    memset(f,0,sizeof f);
    memset(g,0,sizeof g);
    dep(i,n,1) dep(j,n,1) if(s[i]==s[j]) f[i][j]=f[i+1][j+1]+1;
    rep(i,1,n) rep(j,1,m) if(s[i]==t[j]) g[i][j]=g[i-1][j-1]+1;
    rep(i,1,n){
        rep(j,0,m) sum[j]=sum_[j]=0;
        rep(j,1,m){
            if(!g[i][j]) continue;
            sum[g[i][j]]++,sum_[g[i][j]]+=g[i][j];
        }
        rep(j,1,m){
            sum[j]+=sum[j-1];
            sum_[j]+=sum_[j-1];
        }
        rep(j,1,i){
            int r=i+1-j-1,l=max(1,r-f[i+1][j]+1);
            if(l>r||r>m) continue;
            ans+=sum_[r]-sum_[l-1]-(sum[r]-sum[l-1])*(l-1);
            ans+=(sum[m]-sum[r])*(r-l+1);
        }
    }
    cout<<ans;
    return 0;
}

2025.08.01

???

T1

嘴巴一下,就 Trie 树合并,剩下的部分不难维护。

T4

打表,找性质

把最大化的东西写成函数,求导,做完。

2025.08.03

T2

去重(大区间包含小区间,大区间无用),然后求 k 级儿子,双指针跑一遍。

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#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
using namespace std;
const int N=2e5+15,mod=998244353;const ll inf=2e9;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(stdstring& 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(stdstring 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))
//#define int long long
    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,m,f[N],fa[N];
int deep[N];
pair<int,int> tg[N];
int cnt,MX;
map<pair<int,int>,int> mp;
set<int> g[N],e[N],d[N];
vector<pair<int,int> > h[N];
set<int>iterator it;
bool fl=1;
vector<int> t[N];
int bfn[N],tot,L[N],R[N],dfn[N],LL[N],RR[N];

int find(int x){
    if(x==fa[x]) return x;
    return fa[x]=find(fa[x]);
}

void mergy(int x,int y,int X){
    if(g[x].size()>=g[y].size()){
        for(it=g[y].begin();it!=g[y].end();it++){
            if(e[X].find(*it)!=e[X].end()) d[X].insert(*it);
            else g[x].insert(*it);
        }
        fa[y]=x;
    }else{
        for(it=g[x].begin();it!=g[x].end();it++){
            if(g[y].find(*it)!=g[y].end()&&e[X].find(*it)!=e[X].end()) d[X].insert(*it);
            else g[y].insert(*it);
        }
        fa[x]=y;
    }
}

void bfs(){
    queue<int> q;q.push(1);
    while(!q.empty()){
        int u=q.front();q.pop();
        bfn[++tot]=u;if(!L[deep[u]]) L[deep[u]]=tot;
        R[deep[u]]=tot;
        for(auto v:t[u]) q.push(v);
    }
}

void dfs(int u){
    dfn[u]=++tot;
    LL[u]=tot;
    for(auto v:t[u]) dfs(v);
    RR[u]=tot;
}

int find_l(int l,int r,int u){
    int ans=l;
    while(l<=r){
        int mid=(l+r)>>1;
        if(dfn[bfn[mid]]>=LL[u]) ans=mid,r=mid-1;
        else l=mid+1;
    }
    return ans;
}

int find_r(int l,int r,int u){
    int ans=r;
    while(l<=r){
        int mid=(l+r)>>1;
        if(dfn[bfn[mid]]>RR[u]) r=mid-1;
        else ans=mid,l=mid+1;
    }
    return ans;
}

signed main(){
    freopen("lirililarila.in","r",stdin);
    freopen("lirililarila.out","w",stdout);
    cin>>n;deep[1]=fa[1]=1;
    rep(i,2,n) cin>>f[i],fl&=(f[i]==i-1),deep[i]=deep[f[i]]+1,fa[i]=i,t[f[i]].push_back(i),MX=max(MX,deep[i]);
    if(fl){
        int L=inf,R=0;
        cin>>m;
        while(m--){
            int u,k;cin>>u>>k;
            L=min(L,u+k),R=max(R,u+k);
        }
        cout<<L<<' '<<R<<'\n';
    }else{
        cin>>m;
        rep(i,1,m){
            int u,k;cin>>u>>k;
            g[u].insert(deep[u]+k);
            e[u].insert(deep[u]+k);
        }
        dep(i,n,2) mergy(find(f[i]),find(i),f[i]);
        bfs();tot=0;dfs(1);
        rep(i,1,n){
            for(it=e[i].begin();it!=e[i].end();it++){
                if(d[i].find(*it)==d[i].end()){
                    int l=find_l(L[*it],R[*it],i),r=find_r(L[*it],R[*it],i);
                    rep(k,l,r) tg[bfn[k]]={*it,i},cnt+=((++mp[tg[bfn[k]]])==1);
                }
            }
        }
        int nL=1,nR=0,res=0;
        mp.clear();
        while(res^cnt){
            nR++;
            if(tg[nR].fi){
                mp[tg[nR]]++;
                res+=(mp[tg[nR]]==1);
            }
        }
        while(mp[tg[nL]]>1||tg[nL].fi==0){
            mp[tg[nL]]--;nL++;
        }
        int l=nL,r=nR;
        rep(i,nR+1,n){
            mp[tg[i]]++;
            while(mp[tg[nL]]>1||tg[nL].fi==0){
                mp[tg[nL]]--;nL++;
            }
            if(i-nL+1<r-l+1) l=nL,r=i;
        }
        cout<<l<<' '<<r<<'\n';
    }
    return 0;
}

2025.8.5

T1

这个很明显可以倒着DP。

设f[i][j]表示a串i~n都匹配完成,b串用到了j~n(有些可能不动,有些可能抽了出来)我们考虑怎么转移:

我们知道i,j的状态只可能从(i+1,j),(i+1,j+1),(i,j+1)转移过来

  1. 从(i+1,j)转移过来,那么就是说抽出来的可以把i填满,那么判断一下现在b串这个字符的数量是否比a串这个字符的数量大(在a的i+1~n和b的j~n,预处理一个数组减一下–b[s[i]][j]-a[s[i]][i+1]),然后f[i][j]就与f[i+1][j]取一个min。
  2. 从(i+1,j+1)转移过来,那么就是说当前的这个位置可以不用动s[i]=st[j],那么f[i][j]与f[i+1][j+1]取一个min上面的是不用抽出来的情况(不用贡献+1),所以要先更新状态,否则会有问题
  3. 从(i,j+1)转移过来,就是说现在只把j抽出来不匹配,那么f[i][j]和f[i][j+1]+1去一个min还要预处理一下,f[n+1]的预处理,f[n+1][n+1]=0,f[n+1][n]=1,……,f[n+1][1]=n,就是一个个抽出来不匹配。

然后f[1][1]就是答案。

T2

https://www.luogu.com.cn/problem/P10652

嘟嘟嘟,乱搞

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#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
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(stdstring& 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(stdstring 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))
//#define int long long
    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,m;
vector<pair<int,int> > g[N],e[N];
bitset<N> vis;
int dis[N],in[N];
ll f[N];

void spfa(){
    memset(dis,0x3f,sizeof dis);
    priority_queue<pair<int,int> > q;q.push({0,1});
    dis[1]=0;
    while(!q.empty()){
        int u=q.top().se;q.pop();
        if(vis[u]) continue;
        vis[u]=1;
        for(auto p:g[u]){
            int v=p.fi;
            if(dis[v]>dis[u]+p.se){
                dis[v]=dis[u]+p.se;
                q.push({-dis[v],v});
            }
        }
    }
}

void build(){
    rep(u,1,n) for(auto p:g[u]){
        int v=p.fi;
        if(dis[v]==dis[u]+p.se) e[u].push_back({v,p.se}),in[v]++;
    }
}

void topo(){
    queue<int>q;
    rep(i,1,n){if(!in[i]) q.push(i);f[i]=-inf;}
    f[1]=0;
    while(!q.empty()){
        int u=q.front();q.pop();
//        cout<<u<<'\n';
        for(auto p:e[u]){
            int v=p.fi;
            in[v]--;
            f[v]=max(f[v],f[u]+1ll*p.se*p.se);
//            cout<<v<<' '<<f[v]<<'\n';
            if(!in[v]) q.push(v);
        }
    }
}

signed main(){
    cin>>n>>m;
    rep(i,1,m){
        vector<int>a,b;
        int p,x;cin>>p>>x;a.push_back(x);
        rep(j,1,p){
            cin>>x;b.push_back(x);
            cin>>x;a.push_back(x);
        }
        rep(j,1,p){
            int res=0;
            rep(k,j,min(p,p/j+300)){
//                cout<<j-1<<' '<<a[j-1]<<' '<<k<<' '<<a[k]<<'\n';
                res+=b[k-1];
                g[a[j-1]].push_back({a[k],res});
            }
//            cout<<'\n';
        }
    }
    spfa();
    cout<<dis[n]<<' ';
    build(),topo();
    cout<<f[n]<<'\n';
    return 0;
}

2025.8.6

T1

分组处理,分成四个组,必然有一个组是相同的才可能符合条件。

因为数据随机,所以相同的数量不多。

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#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
using namespace std;
const int N=150000+15,mod=998244353;const ll inf=2e9;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(stdstring& 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(stdstring 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))
// #define int ull
    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;

const int P=(1<<16)-1;
int n,ans;
int p,cnt,h[5][70001],idx,b[600001];
ull a[N],x;
struct node{
    ull x;
    int v,ne;
}e[600001];

inline bool check(ull n){
    short c=0;
    while(n&&c<=3){
        c++;
        n&=(n-1);
    }
    return c==3;
}

void insert(ull x,int y,int p){
    e[++idx]=(node){x,y,h[p][x]};h[p][x]=idx;
}

signed main(){
    freopen("hashing.in","r",stdin);
    freopen("hashing.out","w",stdout);
    cin>>n;
    rep(z,1,n){
        cin>>a[z];ans=0;
        x=a[z];cnt=0;
        rep(i,1,4){
            p=x&P;
            x>>=16;
            for(int j=h[i][p];j;j=e[j].ne) b[++cnt]=e[j].v;
            insert(p,z,i);
        }
        sort(b+1,b+1+cnt);
        cnt=unique(b+1,b+cnt+1)-b-1;
        rep(i,1,cnt) if(check(a[b[i]]^a[z])) ans++;
        cout<<ans<<'\n';
    }
    return 0;
}

T2

圆方树+dp(很板就是了

也可以使用点双缩点,要考录的多些。

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#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
using namespace std;
const int N=2e5+15,mod=998244353;const ll inf=2e9;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(stdstring& 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(stdstring 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))
#define int long long
    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,m;
int dfn[N],low[N],tim;
int st[N],top_;
vector<int> g[N],e[N];
int tot,dis[N],wei[N];
int sz[N],f[N];

void tarjan(int u){
    dfn[u]=low[u]=++tim;
    st[++top_]=u;
    for(auto v:g[u]){
        if(!dfn[v]){
            tarjan(v);
            if(low[v]>=dfn[u]){
                tot++;
                wei[tot]=0;
                e[tot].clear();
                e[tot].push_back(u);
                e[u].push_back(tot);
                wei[u]=1;
                sz[u]=1;
                int c;
                do{
                    c=st[top_--];
                    e[tot].push_back(c);
                    e[c].push_back(tot);
                    wei[c]=1;
                    sz[c]=1;
                }while(c!=v);
            }
            low[u]=min(low[u],low[v]);
        }
        else low[u]=min(low[u],dfn[v]);
    }
}

void dfs(int u,int fa){
    for(auto v:e[u]){
        if(v==fa) continue;
        dfs(v,u);
        sz[u]+=sz[v];
    }
}

void dfs_(int u,int fa){
    for(auto v:e[u]){
        if(v==fa) continue;
        dfs_(v,u);
        f[u]+=sz[v]*(n-sz[v]-1);
    }
    f[u]+=(n-sz[u])*(sz[u]-1);
    f[u]>>=1;
    f[u]+=n-1;
}

signed main(){
    cin>>n>>m;tot=n;
    rep(i,1,m){
        int u,v;cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    tarjan(1);
    dfs(1,0);
    dfs_(1,0);
    rep(i,1,n) cout<<f[i]<<'\n';
    return 0;
}

T3

https://www.luogu.com.cn/problem/P1791

讲过的原题。

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#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
using namespace std;
const int N=1000+15,mod=998244353;const ll inf=2e9;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(stdstring& 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(stdstring 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))
#define int long long
    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,m,s,t,deep[N],cur[N];ll ans;
struct edge{int v,rev;ll w;};
vector<edge> g[N];

void add(int u,int v,int w){
    g[u].push_back({v,g[v].size(),w});
    g[v].push_back({u,g[u].size()-1,0});
}

int bfs(){
    queue<int> q;q.push(s);
    memset(deep,0,sizeof deep);
    deep[s]=1;
    while(!q.empty()){
        int u=q.front();q.pop();
        cur[u]=0;
        for(int i=0;i<g[u].size();i++){
            int v=g[u][i].v;
            if(g[u][i].w&&!deep[v]) deep[v]=deep[u]+1,q.push(v);
        }
    }
    return deep[t];
}

inline ll dfs(int u,ll in){
    if(u==t||!in) return in;
    ll out=0;
    for(int i=cur[u];i<g[u].size()&&in;i++){
        cur[u]=i;
        int v=g[u][i].v;
        if(g[u][i].w&&deep[u]+1==deep[v]){
            ll num=dfs(v,min(in,g[u][i].w));
            in-=num,out+=num;
            g[u][i].w-=num,g[v][g[u][i].rev].w+=num;
        }
    }
    return out;
}

signed main(){
    cin>>n;s=0,t=n+1;
    rep(i,1,n){
        int x;cin>>x;
        add(s,i,x);
    }
    int S=0;
    rep(i,1,n){
        int sum=0;
        rep(j,1,n){
            int x;cin>>x;
            sum+=x;
            add(i,j,x<<1);
        }
        add(i,t,sum);
        S+=sum;
    }
    while(bfs()) ans+=dfs(s,inf);
    cout<<S-ans;
    return 0;
}

2025.8.8

T1

直接推贡献

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#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
using namespace std;
const int N=2e5+15,mod=998244353;const ll inf=2e9;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(stdstring& 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(stdstring 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))
#define int long long
    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;

inline int qpow(int a,int b){
    int res=1;
    while(b){
        if(b&1) res=1ll*res*a;
        a=1ll*a*a,b>>=1;
    }
    return res;
}

int k,m,cnt,f,b,w=1;

void Co(int x){
    while(x) x/=10,cnt++;
}

signed main(){
    cin>>k>>m;m--;
//    cout<<k<<'\n';
    Co(k);
    rep(i,1,cnt-1){
        f=k/qpow(10,cnt-i);
        b+=f-w+1;
//    cout<<qpow(10,cnt-i)<<' '<<f<<' '<<w<<'\n';
        w*=10;
    }
    if(b+k-w>m||(b+k-w<m&&k==w)) cout<<0;
    else{
        if(b+k-w==m){cout<<k;return 0;}
        f=k;
        while(b+f-w<m){
            b+=f-w;
            f*=10,w*=10;
        }
        cout<<w+(m-b-1);
    }
    return 0;
}

2025.8.10

T1

最大费用可行流

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#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
using namespace std;
const int N=5e4+15,mod=998244353;const int inf=1061109567;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(stdstring& 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(stdstring 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))
//#define int long long
    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,m,a[32][32],s,t,tot,I[2][32];
int dis[N],cur[N];
bitset<N> vis;
int ans;
struct node{int v,w,c,rev;};
vector<node> g[N];

#define id(i,j) ((i-1)*m+j)

void add(int u,int v,int w,int c){
    g[u].push_back({v,w,c,g[v].size()});
    g[v].push_back({u,0,-c,g[u].size()-1});
}

bool spfa(){
    memset(dis,0x3f,sizeof dis);
    memset(cur,0,sizeof cur);
    vis=0;
    queue<int> q;q.push(t);
    vis[t]=1,dis[t]=0;
    while(!q.empty()){
        int u=q.front();q.pop();vis[u]=0;
        for(int i=0;i<g[u].size();i++){
            int v=g[u][i].v;
            if(g[v][g[u][i].rev].w&&(dis[v]==inf||dis[u]-g[u][i].c<dis[v])){
                dis[v]=dis[u]-g[u][i].c;
                if(!vis[v]) vis[v]=1,q.push(v);
            }
        }
    }
    return dis[s]!=inf;
}

int dfs(int u,int in){
//    cout<<u<<'\n';
    if(u==t||!in) return in;
    vis[u]=1;
    int out=0;
    for(int i=cur[u];i<g[u].size()&&in;i++){
        cur[u]=i;
        int v=g[u][i].v,c=g[u][i].c;
        if(g[u][i].w&&dis[u]-c==dis[v]&&!vis[v]){
            int num=dfs(v,min(in,g[u][i].w));
            in-=num,out+=num;
            g[u][i].w-=num,g[v][g[u][i].rev].w+=num;
            ans+=c*num;
        }
    }
    return out;
}

signed main(){
    freopen("pick.in","r",stdin);
    freopen("pick.out","w",stdout);
    cin>>n>>m;s=0,t=n*m+1,tot=n*m+1;
//    cout<<s<<' '<<t<<'\n';
    rep(i,1,n) I[0][i]=++tot,add(s,I[0][i],2,0),add(I[0][i],t,2,0);//,cout<<I[0][i]<<' ';cout<<'\n';
    rep(i,1,m) I[1][i]=++tot,add(I[1][i],t,2,0);//,cout<<I[1][i]<<' ';cout<<'\n';
    rep(i,1,n) rep(j,1,m){
        cin>>a[i][j];
//        cout<<id(i,j)<<' ';
        add(I[0][i],I[1][j],1,-a[i][j]);
    }
//    cout<<'\n';
    int res=0;
    while(spfa()){
        do{
            vis=0;
            res+=dfs(s,inf);
        }while(vis[t]);
    }
    cout<<-ans<<'\n';
    return 0;
}

T2

莫比乌斯反演

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#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
using namespace std;
const int N=4e5+15,mod=998244353;const ll inf=2e9;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(stdstring& 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(stdstring 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))
#define int long long
    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,ans;
int cnt,prime[N],phi[N];
int num[N];
bool st[N];

void getprime(int MX){
    phi[1]=1,st[1]=1;
    for(int i=2;i<=MX;i++){
        if(!st[i]) prime[++cnt]=i,phi[i]=i-1;
        for(int j=1;prime[j]<=MX/i;j++){
            st[prime[j]*i]=1;
            if(i%prime[j]==0){phi[prime[j]*i]=prime[j]*phi[i];break;}
            phi[prime[j]*i]=phi[i]*phi[prime[j]];
        }
    }
}

signed main(){
    freopen("gcd.in","r",stdin);
    freopen("gcd.out","w",stdout);
    cin>>n;
    getprime(sqrt(n)+1);
    for(int i=1;i*i<=n;i++){
        int p=n/i/i,sum=0;
        for(int l=1,r;l<=p;l=r+1){
            r=p/(p/l);
            sum+=(r-l+1)*(p/l);
        }
        ans+=phi[i]*sum;
    }
    cout<<ans;
    return 0;
}

T3

简单 ds 题,分块、线段树都能过。

区间减,区间取 max。

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#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
using namespace std;
const int N=2e5+15,mod=998244353;const ll inf=2e9;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(stdstring& 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(stdstring 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))
//#define int long long
    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,m,al,a[N],ans;
int pos[N],len,bl,L[400],R[400];
int mn[400],ad[400];
bool fl[400],f[400],vis[N];

void build(int id){
    mn[id]=inf,fl[id]=0,ad[id]=0;
    rep(i,L[id],R[id]) if(a[i]>0) mn[id]=min(mn[id],a[i]),vis[i]=1;else fl[id]=1,vis[i]=0;
    if(mn[id]==inf) f[id]=1;
}

void rebuild(int id){
    if(f[id]) return ;
    rep(i,L[id],R[id]) if(vis[i]) a[i]+=ad[id];
    build(id);
}

void add(int id,int x){
    if(f[id]) return ;
    if(mn[id]+x>0) mn[id]+=x,ad[id]+=x;
    else ad[id]+=x,rebuild(id);
}

void change(int id,int l,int r,int x){
    if(f[id]) return ;
    l=max(l,L[id]),r=min(r,R[id]);
    rep(i,l,r) if(vis[i]) a[i]+=x;
    rebuild(id);
}

void update(int l,int r,int x){
    int ld=pos[l],rd=pos[r];
    if(ld==rd){
        change(ld,l,r,x);
        return ;
    }
    change(ld,l,r,x),change(rd,l,r,x);
    rep(i,ld+1,rd-1) add(i,x);
}

void rebuild_(int id){
    if(f[id]) return ;
    build(id);
}

void cover(int id,int x){
    if(f[id]||mn[id]>=x) return ;
    rep(i,L[id],R[id]) if(vis[i]) a[i]+=ad[id];
    rep(i,L[id],R[id]) if(vis[i]) a[i]=max(a[i],x);
    rebuild_(id);
}

void change_(int id,int l,int r,int x){
    if(f[id]||mn[id]>=x) return ;
    l=max(l,L[id]),r=min(r,R[id]);
    rep(i,L[id],R[id]) if(vis[i]) a[i]+=ad[id];
    rep(i,l,r) if(vis[i]) a[i]=max(a[i],x);
    rebuild_(id);
}

void update_(int l,int r,int x){
    int ld=pos[l],rd=pos[r];
    if(ld==rd){
        change_(ld,l,r,x);
        return ;
    }
    change_(ld,l,r,x),change_(rd,l,r,x);
    rep(i,ld+1,rd-1) cover(i,x);
}

bool query(int l,int r){
    int ld=pos[l],rd=pos[r];
    int res=0;
    if(ld==rd){
        rep(i,l,r) res|=!vis[i];
        return !res;
    }
    rep(i,l,R[ld]) res|=!vis[i];
    rep(i,L[rd],r) res|=!vis[i];
    rep(i,ld+1,rd-1){
        res|=fl[i];
    }
    return !res;
}

signed main(){
    freopen("road.in","r",stdin);
    freopen("road.out","w",stdout);
    cin>>n>>m>>al;len=sqrt(n);
    bl=n/len;
    rep(i,1,n) a[i]=al;
    rep(i,1,bl){
        L[i]=R[i-1]+1,R[i]=R[i-1]+len;
        rep(j,L[i],R[i]) pos[j]=i;
    }
    if(R[bl]<n){
        bl++;
        L[bl]=R[bl-1]+1,R[bl]=n;
        rep(j,L[bl],R[bl]) pos[j]=bl;
    }
    rep(i,1,bl) build(i);
    while(m--){
        int opt,s,t,x;cin>>opt>>s>>t>>x;
        if(opt==1){
            int p=query(s,t);
            if(p) ans++,update(s,t,-x);
        }else if(opt==2){
            update(s,t,x);
        }else update_(s,t,x);
//        rep(i,1,n) cout<<mn[i]<<' '<<a[i]<<' '<<ad[i]<<';';
//        cout<<'\n';
    }
    cout<<ans;
    return 0;
}

2025.8.12

先考虑暴力连边,树剖跳 LCA,O(n^2) \text{\textasciitilde} O(\log n^2)

平衡一下复杂度,只建 a_i\le\sqrt n ~~ or ~~ b_j\le\sqrt n 的点的树,树剖,其余暴力跳,O(n\sqrt n) \text{\textasciitilde} O(\sqrt n + \log \sqrt n)

当然,你可以不树剖,处理最远跳到哪里,然后变成大常数根号 n 做法。

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#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
using namespace std;
const int N=1e5+15,mod=998244353;const ll inf=2e9;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(stdstring& 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(stdstring 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))
//#define int long long
    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;

#define deep(i,j) ((i)+(j)-1)

const int B=325;
int n,q,a[N],b[N],f[B][N],c[B],p;

pair<int,int> LCA(int x,int y,int i,int j){
    if(deep(x,y)>deep(i,j)) swap(x,i),swap(y,j);
    while(a[x]>p&&b[y]>p&&a[i]>p&&b[j]>p){
        if(x==i&&y==j) return {x,y};
        if(deep(x,y)>deep(i,j)) swap(x,i),swap(y,j);
        if(a[i]>b[j]) i--;
        else j--;
    }
    if(a[i]<=p||b[j]<=p) swap(x,i),swap(y,j);
    while(a[i]>p&&b[j]>p){
        if(a[i]>b[j]) i--;
        else j--;
    }
//    cout<<p<<' '<<a[x]<<' '<<b[y]<<' '<<a[i]<<' '<<b[j]<<'\n';
    while(1){
//        cout<<x<<' '<<y<<' '<<i<<' '<<j<<'\n';
        int tx,ty,ti,tj;
        if(a[x]<=p&&f[a[x]][y]!=y) ty=f[a[x]][y],tx=x;
        else tx=f[b[y]][x],ty=y;
        if(a[i]<=p&&f[a[i]][j]!=j) tj=f[a[i]][j],ti=i;
        else ti=f[b[j]][i],tj=j;
//        cout<<tx<<' '<<ty<<' '<<ti<<' '<<tj<<'\n';
        if(deep(tx,ty)>deep(ti,tj)) swap(x,i),swap(y,j);
        if(a[x]<=p&&a[i]<=p&&a[x]==a[i]) if(f[a[x]][y]==f[a[i]][j]){
            if(deep(x,y)>deep(i,j)) return {i,j};
            return {x,y};
        }
        if(b[y]<=p&&b[j]<=p&&b[y]==b[j]) if(f[b[y]][x]==f[b[j]][i]){
            if(deep(x,y)>deep(i,j)) return {i,j};
            return {x,y};
        }
        if(x==i&&y==j) return {x,y};
//        cout<<'~'<<i<<' '<<j<<' '<<f[a[i]][j]<<' '<<a[i]<<' '<<b[j]<<' '<<(a[i]<=p)<<' '<<(f[a[i]][j]!=j)<<'\n';
        if(a[i]<=p&&f[a[i]][j]!=j) j=f[a[i]][j];
        else i=f[b[j]][i];
    }
}

signed main(){
    freopen("dis.in","r",stdin);
    freopen("dis.out","w",stdout);
    cin>>n;p=sqrt(n);
    rep(i,1,n){
        cin>>a[i];
        if(a[i]<=p) c[a[i]]=i;
    }
    rep(i,1,n){
        cin>>b[i];
        if(b[i]<=p) c[b[i]]=i;
    }
    rep(i,1,p){
        int tp=1;
        if(a[c[i]]==i){
            rep(j,1,n){
                if(b[j]<i) tp=j;
                f[i][j]=tp;
            }
        }
        if(b[c[i]]==i){
            rep(j,1,n){
                if(a[j]<i) tp=j;
                f[i][j]=tp;
            }
        }
    }
//    rep(i,1,p){rep(j,1,n) cout<<f[i][j]<<' ';cout<<'\n';}
    cin>>q;
    while(q--){
        int x,y,i,j;cin>>x>>y>>i>>j;
        auto lca=LCA(x,y,i,j);
//        cout<<lca.fi<<' '<<lca.se<<'\n';
//        cout<<deep(x,y)<<' '<<deep(i,j)<<'\n';
        cout<<deep(x,y)+deep(i,j)-2*deep(lca.fi,lca.se)<<'\n';
    }
    return 0;
}
/*
保证 a,b 在满足条件的情况下随机生成
那我挑前 sqrt n 个数的期望复杂度在 n sqrt n ~ q sqrt n
*/

2025.8.13

T1

找两个最长的有交的回文串,manacher 时记录以它为左右端点时最长是多长。

T2

pupil 发现对于一个十进制数,无论怎么将其的数字重新排列,均不影响其是不是 3 的倍数。他想研究对于二进制,是否也有类似的性质。

于是他生成了一个长为 n 的二进制串,希望你对于这个二进制串的一个子区间,能求出其有多少位置不同的连续子串,满足在重新排列后(可包含前导 0)是一个 3 的倍数。

两个位置不同的子区间指开始位置不同或结束位置不同。

由于他想尝试尽量多的情况,他有时会修改串中的一个位置,并且会进行多次询问。

只有奇数个 1 且 0 小于 2 个和只有一个 1 是不可以的,把这一部分用 ddp 计算出来,容斥掉两者相重叠的部分即可。

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#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
using namespace std;
const int N=1e5+15,mod=998244353;const ll inf=2e9;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(stdstring& 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(stdstring 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))
//#define int long long
    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,m,a[N];
struct tree{
    int l,r;
    int L,R,L_,R_,C,C_;
    int v[2][2],v_[2][2];
    ll ans;

    tree(){l=r=L=R=L_=R_=C=C_=v[0][0]=v[1][0]=v[0][1]=v[1][1]=v_[0][0]=v_[1][0]=v_[0][1]=v_[1][1]=0;}

    tree(tree x,tree y){
        l=x.l,r=y.r;
        C=x.C+y.C,C_=x.C_+y.C_;
        L=x.L+(!x.C_?y.L:0),R=y.R+(!y.C_?x.R:0);
        L_=x.L_+(!x.C_?y.L_:0)+(x.C_==1?y.L:0);
        R_=y.R_+(!y.C_?x.R_:0)+(y.C_==1?x.R:0);
        rep(i,0,1) rep(j,0,1){
            v[i][j]=x.v[i][j]+(i>=x.C?y.v[i-x.C][j^(x.C_&1)]:0);
            v_[i][j]=y.v_[i][j]+(i>=y.C?x.v_[i-y.C][j^(y.C_&1)]:0);
        }
        ans=x.ans+y.ans;
        ans+=1ll*x.R*y.L_+1ll*x.R_*y.L;
        ans+=1ll*x.v_[0][0]*(y.v[0][1]+y.v[1][1])+1ll*x.v_[0][1]*(y.v[0][0]+y.v[1][0]); 
        ans+=1ll*x.v_[1][0]*y.v[0][1]+1ll*x.v_[1][1]*y.v[0][0];
        if(a[x.r]+a[y.l]==1) ans--;
    }
}t[N<<2];

void build(int u,int l,int r){
    if(l==r){
        t[u].l=l,t[u].r=r;
        if(a[l]) t[u].L_=t[u].R_=t[u].C_=t[u].v[0][1]=t[u].v_[0][1]=t[u].ans=1;
        else t[u].L=t[u].R=t[u].C=t[u].v[1][0]=t[u].v_[1][0]=1;
        return ;
    }
    int mid=(l+r)>>1;
    build(ls,l,mid),build(rs,mid+1,r);
    t[u]=tree(t[ls],t[rs]);
}

void update(int u,int x){
    if (t[u].l==t[u].r){
        int l=t[u].l,r=t[u].r;
        t[u]=tree();
        t[u].l=l,t[u].r=r;
        if(a[l]) t[u].L_=t[u].R_=t[u].C_=t[u].v[0][1]=t[u].v_[0][1]=t[u].ans=1;
        else t[u].L=t[u].R=t[u].C=t[u].v[1][0]=t[u].v_[1][0]=1;
        return;
    }
    int mid=(t[u].l+t[u].r)>>1;
    if (x<=mid) update(ls,x);
    else update(rs,x);
    t[u]=tree(t[ls],t[rs]);
}

tree query(int u,int x,int y){
    if(x<=t[u].l&&t[u].r<=y) return t[u];
    int mid=(t[u].l+t[u].r)>>1;
    if(y<=mid) return query(ls,x,y);
    if(mid<x) return query(rs,x,y);
    return tree(query(ls,x,y),query(rs,x,y));
}

signed main(){
    cin>>n;
    rep(i,1,n) cin>>a[i];
    build(1,1,n);
    cin>>m;
    while(m--){

        int opt;cin>>opt;
        if(opt==1){
            int x;cin>>x;
            a[x]^=1;update(1,x);
        }else{
            int l,r;cin>>l>>r;
            ll sum=1ll*(r-l+1)*(r-l+2)/2; 
            cout<<sum-query(1,l,r).ans<<'\n';
        }
    }
    return 0;
}

2025.8.15

T1

不会讲(

是数论题,知道无限循环小数的结论就比较好推了。

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#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
using namespace std;
const int N=1e6+15,mod=998244353;const ll inf=2e9;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(stdstring& 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(stdstring 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))
#define int long long
    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 gcd(int x,int y){
    return y==0?x:gcd(y,x%y);
}

inline int qpow(int a,int b,int mod){
    int res=1;
    while(b){
        if(b&1) res=(__int128)res*a%mod;
        a=(__int128)a*a%mod,b>>=1;
    }
    return res;
}

int phi(int n){
    int ans=n;
    for(int i=2;i*i<=n;i++) if(n%i==0){
        ans=ans/i*(i-1);
        while(n%i== 0) n/=i;
    }
    if(n>1) ans=ans/n*(n-1);
    return ans;
}

ll a,b,k,m,r;
int h[N],cnt[N],tot;

void solve(){
    cin>>a>>b>>k;tot=0;
    int e=b/gcd(a,b),g=e;
    for(int i=2;i*i<=e;i++){
        if(e%i==0){
            h[++tot]=i,cnt[tot]=0;
            while(e%i==0) e/=i,cnt[tot]++;
        }
    }
    if(e>1) h[++tot]=e,cnt[tot]=1;
    m=0;
    rep(i,1,tot){
        if(k%h[i]==0){
            int c=0,w=k;
            while(w%h[i]==0) w/=h[i],c++;
            m=max(m,(cnt[i]-1)/c+1);
            while(g%h[i]==0) g/=h[i];
        }
    }
    if(g==1){cout<<m<<' '<<0<<'\n';return ;}
    int p=phi(g);
    for(int i=1;i*i<=p;i++){
        if(p%i==0&&qpow(k,i,g)==1){
            cout<<m<<' '<<i<<'\n';
            return ;
        }
    }
    dep(i,sqrt(p),1){
        if(p%i==0&&qpow(k,p/i,g)==1){
            cout<<m<<' '<<p/i<<'\n';
            return ;
        }
    }
}

signed main(){
    int T;cin>>T;
    while(T--){
        solve();
    }
    return 0;
}

T2

四维偏序问题,cdq 套 cdq (不想写了

more

新学到的知识

  1. 带花树

  2. 轮廓线 dp 及其优化

    温习了不太熟练的最小斯坦纳树

trick

  1. Trie 树合并
  2. 二分求 lca
  3. 对于数据随机情况的一些应用

仍有的缺点

  1. 无法准确识别 ddp
  2. 无法在考场上快速写出码量较大的题目