随机化做题记录

· · 算法·理论

1.AT_abc367_f [ABC367F] Rearrange Query

其实就是查询区间本质是否相同。考虑随机和哈希。给每一个数一个特定的权值,权值越大越好,这样的话可以避免哈希冲突,然后如果他们的权值的和相等我们就认为这两个区间本质相同。

建议使用

mt19937_64 Rand(time(0));

::::success[一种可能的实现方法]

/*
It's been a long day.
Without my friend.
And I'll tell you all about it when I see you again.
We've come a long way,
From where we began.
Oh I'll tell you all about it when I see you again.
When I see you again.
Oh,who know,all the planes we flew,
good things we had been drew
That something told me right here talking with you.
*/

//#pragma GCC optimize(1)   
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define rep(i,x,y) for(int i=x;i<=y;++i)
#define dep(i,x,y) for(int i=x;i>=y;--i)
#define int long long
#define N 200005
#define endl "\n"
#define MAX 1145141919810   
using namespace std;
int n,q,a[N],b[N],x;
int vis[N],l,r,L,R;
inline void Feather(){
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    mt19937_64 Rand(time(0));
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>q;
    rep(i,1,n)vis[i]=Rand();
    rep(i,1,n){
        cin>>x;
        a[i]=a[i-1]+vis[x];
    }
    rep(i,1,n){
        cin>>x;
        b[i]=b[i-1]+vis[x];
    }
    while(q--){
        cin>>l>>r>>L>>R;
        if(a[r]-a[l-1]==a[R]-a[L-1])cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
}
signed main(){Feather();return 0;}

:::: 另外的话,可以可考虑维护一段区间的性质,比如最值,和,平方和,立方和等等,线段树可以维护。

2.CF2014H Robin Hood Archery

我们模拟一下,发现只有数是成对出现时,后手才可以赢。这样的话他们的异或值为 0。 但是注意到这样有反例,例如 1,2,5,6,所以我们考虑给它随机赋权值。

推荐使用纳秒级别的

mt19937_64 Rand(chrono::system_clock::now().time_since_epoch().count());

::::success[CODE]

/*
It's been a long day.
Without my friend.
And I'll tell you all about it when I see you again.
We've come a long way,
From where we began.
Oh I'll tell you all about it when I see you again.
When I see you again.
Oh,who know,all the planes we flew,
good things we had been drew
That something told me right here talking with you.
*/

//#pragma GCC optimize(1)
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define rep(i,x,y) for(int i=x;i<=y;++i)
#define dep(i,x,y) for(int i=x;i>=y;--i)
#define int long long
#define endl "\n"
#define MAX 1145141919810
using namespace std;
int T,n,q,x;
int a[200005];
int vis[1000006];
int l,r;
inline void Feather(){
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    mt19937_64 Rand(chrono::system_clock::now().time_since_epoch().count());
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    rep(i,0,1000000)vis[i]=Rand();
    cin>>T;
    while(T--){
        cin>>n>>q;
        rep(i,1,n){
            cin>>x;
            a[i]=a[i-1]^vis[x];
        }
        while(q--){
            cin>>l>>r;
            if(a[l-1]==a[r])cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
    }
}
signed main(){Feather();return 0;}

::::

3.P13667 [GCPC 2023] Balloon Darts

注意到三条直线的数量非常小,有两点确定一条直线。根据鸽巢原理,当三条直线能穿过所有点时,一定有一条直线至少穿过了 \frac{n}{3} 个点。

所以我们去随机选取两个点,假设他们在同一条直线上。

然后我们假设三条直线上的点的数量分别为 a,b,c,那么概率为 (\frac{a}{a+b+c})^2+(\frac{b}{a+b+c})^2+(\frac{c}{a+b+c})^2。

根据基本不等式,有 a^2+b^2+c^2\ge \frac{1}{3} (a+b+c)^2。

所以说,随机一次正确的概率严格大于 \frac{1}{3}。

多随几次就可以了,那就随 30 次。

::::success[一种解法]

/*
It's been a long day.
Without my friend.
And I'll tell you all about it when I see you again.
We've come a long way,
From where we began.
Oh I'll tell you all about it when I see you again.
When I see you again.
Oh,who know,all the planes we flew,
good things we had been drew
That something told me right here talking with you.
*/

//#pragma GCC optimize(1)
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define rep(i,x,y) for(int i=x;i<=y;++i)
#define dep(i,x,y) for(int i=x;i>=y;--i)
#define int long long
#define N 10005
#define endl "\n"
#define MAX 1145141919810
using namespace std;
int n,x[N],y[N],wz[N];//wz表示还没又纳入直线的点的下标
bool vis[N];//来记录一个点是否被纳入直线
inline bool check(int i,int j,int k){
    return ((y[j]-y[i])*(x[k]-x[j])==(x[j]-x[i])*(y[k]-y[j]));
}
//这个是用来判断三个点是不是在同一条线上,用乘法防止精度问题
inline void Feather(){
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    mt19937_64 Rand(chrono::system_clock::now().time_since_epoch().count());
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n;
    rep(i,1,n)cin>>x[i]>>y[i];
    rep(Q,1,30){//随30次
        memset(vis,false,sizeof(vis));
        int tot=0;//统计几个点可以被纳入
        rep(dnt,1,3){//三条直线
            int cnt=0;
            rep(i,1,n)if(!vis[i])wz[++cnt]=i;
      //看看还有哪些点没有被加入
            if(cnt<2){tot=n;break;}
      //如果只剩一个点或者没有点了那就可以直接退出了,因为过一个点有无数条直线
            int xx=Rand()%cnt+1,yy=Rand()%cnt+1;//这里是拿来随两个点
            while(xx==yy)yy=Rand()%cnt+1;
            int sx=wz[xx],sy=wz[yy];
            rep(i,1,n){
                if(vis[i])continue;//玩过的别玩了
                if(check(sx,sy,i)){++tot;vis[i]=true;}//三点一线就加入
            }
//扩展所有可能
        }
        if(tot==n){
            cout<<"possible"; 
            return;
        }
    }
    cout<<"impossible"; 
}
signed main(){Feather();return 0;}

::::

4.P13810 [CERC 2022] Differences

我们考虑给每一个字符串随一个比较大的唯一的权值。 由于字符集很小,我们可以对每一位预处理出为 ABCD 的每个串的哈希值总和。

我们遍历每一个串的每一位, 计算出与其该位字符不一样的串的哈希值总和。并对每一位累加。

若最后的和等于 sum\times k-wi\times k,那么说明该串合法。

这个式子的意思就是,每个串都刚好有 k 个位置不同,与题目相符。

::::success[Code]

/*
It's been a long day.
Without my friend.
And I'll tell you all about it when I see you again.
We've come a long way,
From where we began.
Oh I'll tell you all about it when I see you again.
When I see you again.
Oh,who know,all the planes we flew,
good things we had been drew
That something told me right here talking with you.
*/

//#pragma GCC optimize(1)
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define rep(i,x,y) for(int i=x;i<=y;++i)
#define dep(i,x,y) for(int i=x;i>=y;--i)
#define int long long
#define N 10005
#define endl "\n"
#define MAX 1145141919810
using namespace std;
int n,m,k,total;
string a[100005];
int qzhi[100005];
int HASH[100005][5];
inline void Feather(){
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    mt19937 Rand(time(0));
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n>>m>>k;
    rep(i,1,n){
        cin>>a[i];
        qzhi[i]=Rand();
        if(qzhi[i]<0)qzhi[i]=-qzhi[i];
        total+=qzhi[i];
//      cout<<qzhi[i]<<' ';
    }
//  cout<<endl;
    rep(j,0,m-1){
        int A,B,C,D;
        A=B=C=D=0;
        rep(i,1,n)
            if(a[i][j]=='A')A+=qzhi[i];
            else if(a[i][j]=='B')B+=qzhi[i];
            else if(a[i][j]=='C')C+=qzhi[i];
            else D+=qzhi[i];
//      cout<<A<<' '<<B<<' '<<C<<' '<<D<<endl; 
        HASH[j][0]=A;
        HASH[j][1]=B;
        HASH[j][2]=C;
        HASH[j][3]=D;
        HASH[j][4]=A+B+C+D;
    }
    rep(i,1,n){
        int ans=0;
        rep(j,0,m-1)ans+=HASH[j][4]-HASH[j][a[i][j]-'A'];
        if(ans+qzhi[i]*k==total*k)cout<<i<<endl;
    }
}
signed main(){Feather();return 0;}

/*
A B
A C
B D
C A 
*/

::::

5.P10102 [GDKOI2023 提高组] 矩阵

直接矩阵乘的话复杂度是立方级别的,不可以接受。所以考虑随机出一个一维矩阵,这样的话复杂度是平方级别的。

注意到数据范围很大,所以说注意不要用 memset。

还有就是乘的顺序。

::::error[TLE]

/*
It's been a long day.
Without my friend.
And I'll tell you all about it when I see you again.
We've come a long way,
From where we began.
Oh I'll tell you all about it when I see you again.
When I see you again.
Oh,who know,all the planes we flew,
good things we had been drew
That something told me right here talking with you.
*/

//#pragma GCC optimize(1)
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define rep(i,x,y) for(int i=x;i<=y;++i)
#define dep(i,x,y) for(int i=x;i>=y;--i)
#define int long long
#define N 3005
#define mod 998244353
#define endl "\n"
#define MAX 1145141919810
using namespace std;
inline int read(){
    int ret=0,sgn=0,ch=getchar();
    while(!isdigit(ch))sgn|=ch=='-',ch=getchar();
    while(isdigit(ch))ret=ret*10+ch-'0',ch=getchar();
    return sgn?-ret:ret;
} 
struct Matrix{
    int M[N][N];
    void clear(){memset(M,0,sizeof(M));}
}A,B,C,D,E;
int T,n;
inline void Feather(){
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    mt19937 Rand(time(0));
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    T=read(); 
    while(T--){
        n=read();E.clear();
        rep(i,1,n)rep(j,1,n)A.M[i][j]=read();
        rep(i,1,n)rep(j,1,n)B.M[i][j]=read();
        rep(i,1,n)rep(j,1,n)C.M[i][j]=read();
        rep(i,1,n)D.M[i][1]=Rand();
//      B=mul(B,D);
//      A=mul(A,B);
//      C=mul(C,D);
        rep(i,1,n)
            rep(k,1,n)
                E.M[i][1]=(E.M[i][1]+B.M[i][k]*D.M[k][1]%mod)%mod;
        B.clear();
        rep(i,1,n)
            rep(k,1,n)
                B.M[i][1]=(B.M[i][1]+A.M[i][k]*E.M[k][1]%mod)%mod;
        A.clear();
        rep(i,1,n)
            rep(k,1,n)
                A.M[i][1]=(A.M[i][1]+C.M[i][k]*D.M[k][1]%mod)%mod;
        bool flag=true;
        rep(i,1,n)
            if(B.M[i][1]!=A.M[i][1]){
                cout<<"No"<<endl;
                flag=false;
                break;
            }
        if(flag)cout<<"Yes"<<endl;
    }
}
signed main(){Feather();return 0;}

::::

::::success[Code]

/*
It's been a long day.
Without my friend.
And I'll tell you all about it when I see you again.
We've come a long way,
From where we began.
Oh I'll tell you all about it when I see you again.
When I see you again.
Oh,who know,all the planes we flew,
good things we had been drew
That something told me right here talking with you.
*/

//#pragma GCC optimize(1)
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define rep(i,x,y) for(int i=x;i<=y;++i)
#define dep(i,x,y) for(int i=x;i>=y;--i)
#define int long long
#define N 3005
#define mod 998244353
#define endl "\n"
#define MAX 1145141919810
using namespace std;
inline int read(){
    int ret=0,sgn=0,ch=getchar();
    while(!isdigit(ch))sgn|=ch=='-',ch=getchar();
    while(isdigit(ch))ret=ret*10+ch-'0',ch=getchar();
    return sgn?-ret:ret;
} 
struct Matrix{
    int M[N][N];
    void clear(){memset(M,0,sizeof(M));}
}A,B,C;
int T,n;
int D[N],E[N],F[N],G[N];
inline void Feather(){
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    mt19937 Rand(time(0));
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    T=read(); 
    while(T--){
        n=read();
        rep(i,1,n)rep(j,1,n)A.M[i][j]=read();
        rep(i,1,n)rep(j,1,n)B.M[i][j]=read();
        rep(i,1,n)rep(j,1,n)C.M[i][j]=read();
        rep(i,1,n)D[i]=Rand();
//      B=mul(B,D);
//      A=mul(A,B);
//      C=mul(C,D);
        rep(i,1,n){
            E[i]=0;
            rep(k,1,n)
                E[i]=(E[i]+B.M[i][k]*D[k]%mod)%mod;
        }
        rep(i,1,n){
            F[i]=0;
            rep(k,1,n)
                F[i]=(F[i]+A.M[i][k]*E[k]%mod)%mod;
        }
        rep(i,1,n){
            G[i]=0;
            rep(k,1,n)
                G[i]=(G[i]+C.M[i][k]*D[k]%mod)%mod;
        }
        bool flag=true;
        rep(i,1,n)
            if(F[i]!=G[i]){
                cout<<"No"<<endl;
                flag=false;
                break;
            }
        if(flag)cout<<"Yes"<<endl;
    }
}
signed main(){Feather();return 0;}

::::

6.CF1175F The Number of Subpermutations

异或哈希。外面还要套一个莫队板子。

毕竟窝不会莫队,所以就这样吧。。。

::::success[Code]

/*
It's been a long day.
Without my friend.
And I'll tell you all about it when I see you again.
We've come a long way,
From where we began.
Oh I'll tell you all about it when I see you again.
When I see you again.
Oh,who know,all the planes we flew,
good things we had been drew
That something told me right here talking with you.
*/

//#pragma GCC optimize(1)
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define rep(i,x,y) for(int i=x;i<=y;++i)
#define dep(i,x,y) for(int i=x;i>=y;--i)
#define int long long
#define N 300005
#define endl "\n"
#define MAX 1145141919810
using namespace std;
inline int read(){
    int ret=0,sgn=0,ch=getchar();
    while(!isdigit(ch))sgn|=ch=='-',ch=getchar();
    while(isdigit(ch))ret=ret*10+ch-'0',ch=getchar();
    return sgn?-ret:ret;
}

int n,a[N],ans;
int v[N],hs[N],pm[N];
vector<int> pos;
inline void Feather(){
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    mt19937 Rand(time(0));
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    rep(i,1,n)cin>>a[i];
    rep(i,1,n)v[i]=Rand();
    rep(i,1,n)pm[i]=pm[i-1]^(i*v[i]);
    rep(i,1,n)hs[i]=hs[i-1]^(a[i]*v[a[i]]);
    rep(i,1,n)if(a[i]==1)pos.push_back(i);
    rep(k,0,pos.size()-1){
        int mx=1,ll=(k==0?1:pos[k-1]+1),rr=(k==pos.size()-1?n:pos[k+1]-1);
        dep(i,pos[k]-1,ll){
            mx=max(mx,a[i]);
            int l=i,r=i+mx-1;
            if(r<=n&&r>=pos[k]&&(hs[r]^hs[l-1])==pm[mx])++ans;
        }
        mx=1;
        rep(i,pos[k]+1,rr){         
            mx=max(mx,a[i]);
            int l=i-mx+1,r=i;
            if(l>=1&&l<=pos[k]&&(hs[r]^hs[l-1])==pm[mx])++ans;
        }
    }
    cout<<ans+pos.size()<<endl;
}
signed main(){Feather();return 0;}

::::

7.CF1305F Kuroni and the Punishment

结论一:答案不可能 \ge n。

证明:将所有数变为同奇偶显然不需要 n 次。

结论二,总有超过一半的数未被操作,或被加了一次,或被减了一次。

反证。若超过一半的数操作超过 1 次,那么总共至少操作了 n 次,与结论一矛盾。

随机出哪个数符合结论二,将三种情况 x-1,x,x+1 质因数分解,对每种因数作为 gcd 计算一次答案。

::::success[code]

/*
It's been a long day.
Without my friend.
And I'll tell you all about it when I see you again.
We've come a long way,
From where we began.
Oh I'll tell you all about it when I see you again.
When I see you again.
Oh,who know,all the planes we flew,
good things we had been drew
That something told me right here talking with you.
*/

//#pragma GCC optimize(1)
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define rep(i,x,y) for(int i=x;i<=y;++i)
#define dep(i,x,y) for(int i=x;i>=y;--i)
#define int long long
#define endl "\n"
#define MAX 1145141919810
using namespace std;
const int N = 200010;

int n, ans, top, a[N], stk[N];

inline int read() {
    int asd = 0, qwe = 1;
    char zxc;
    while (!isdigit(zxc = getchar())) if (zxc == '-') qwe = -1;
    while (isdigit(zxc)) asd = asd * 10 + zxc - '0', zxc = getchar();
    return asd * qwe;
}

inline void calc(int x) {
    for (int i = 2; i * i <= x; i++) {
        if (x % i == 0)
            stk[++top] = i;
        while (x % i == 0)
            x /= i;
    }
    if (x != 1) stk[++top] = x;
}

inline void solve(int x) {
    int sum = 0;
    for (int i = 1; i <= n; i++)
        sum += min((a[i] >= x ? a[i] % x : (int)1e17), x - a[i] % x);
    ans = min(ans, sum);
}

inline void Feather() {
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    mt19937 Rand(time(0));
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    n = read();
    for (int i = 1; i <= n; i++)
        a[i] = read(), ans += (a[i] & 1);
    int res = 50;
    while (res--) {
        int x = rand() * rand() % n + 1;
        calc(a[x]), calc(a[x] + 1), calc(a[x] - 1);
    }
    sort(stk + 1, stk + top + 1);
    top = unique(stk, stk + top + 1) - stk - 1;
    for (int i = 1; i <= top; i++) solve(stk[i]);
    cout << ans << endl;
}
signed main() {
    Feather();
    return 0;
}

::::

8.P7606 [THUPC 2021] 混乱邪恶

还是先考虑暴力 dp。为了方便处理,先把坐标轴改一改。

设 f_{x,i,j,l,r} 表示当前走了 x 步,到达坐标轴上 (i,j) 点,两种权值分别是 (l,r) 是否可行。暴 力转移是好做的。

之后第一步,由于这个是可行性 dp,可以使用 bitset 优化。

根据上一题的性质,本题坐标轴的移动还是正负一序列上的问题,可以随机打乱后再 dp。复杂度就会降下来,只会达到根号级别。

::::success[Code]

/*
It's been a long day.
Without my friend.
And I'll tell you all about it when I see you again.
We've come a long way,
From where we began.
Oh I'll tell you all about it when I see you again.
When I see you again.
Oh,who know,all the planes we flew,
good things we had been drew
That something told me right here talking with you.
*/

//#pragma GCC optimize(1)
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define rep(i,x,y) for(int i=x;i<=y;++i)
#define dep(i,x,y) for(int i=x;i>=y;--i)
#define int long long
#define N -114514
#define endl "\n"
#define MAX 1145141919810
using namespace std;
inline int read() {
    int ret = 0, sgn = 0, ch = getchar();
    while (!isdigit(ch))sgn |= ch == '-', ch = getchar();
    while (isdigit(ch))ret = ret * 10 + ch - '0', ch = getchar();
    return sgn ? -ret : ret;
}
const int maxn = 100;
int n, p, a[maxn + 10][maxn / 4];
bitset < maxn / 2 > f[2][maxn + 2][maxn + 2][maxn / 2];
#define mod(x,y) ((x-y+p)%p)
inline void Feather() {
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    mt19937 Rand(time(0));
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> p;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= 12; ++j)cin >> a[i][j];
    int sq = (int)sqrt(n) + 2;
    random_shuffle(a + 1, a + 1 + n);
    f[0][0][0][sq][sq] = 1;
    for (int i = 1; i <= n; ++i) {
        for (int l = 0; l < p; ++l) {
            for (int g = 0; g < p; ++g) {
                for (int x = 0; x <= sq << 1; ++x) {
                    f[i & 1][l][g][x].reset();
                    f[i & 1][l][g][x] |= f[(i & 1) ^ 1][mod(l, a[i][1])][mod(g, a[i][2])][x] << 1;
                    f[i & 1][l][g][x] |= f[(i & 1) ^ 1][mod(l, a[i][3])][mod(g, a[i][4])][x + 1];
                    f[i & 1][l][g][x] |= f[(i & 1) ^ 1][mod(l, a[i][5])][mod(g, a[i][6])][x + 1] >> 1;
                    f[i & 1][l][g][x] |= f[(i & 1) ^ 1][mod(l, a[i][7])][mod(g, a[i][8])][x] >> 1;
                    f[i & 1][l][g][x] |= f[(i & 1) ^ 1][mod(l, a[i][9])][mod(g, a[i][10])][x - 1];
                    f[i & 1][l][g][x] |= f[(i & 1) ^ 1][mod(l, a[i][11])][mod(g, a[i][12])][x - 1] << 1;
                }
            }
        }
    }
    int l_, g_;
    cin >> l_ >> g_;
    cout << (f[n & 1][l_][g_][sq][sq] ? "Chaotic Evil" : "Not a true problem setter");
}
signed main() {
    Feather();
    return 0;
}

::::

9.P9047 [PA 2021] Poborcy podatkowi

考虑树形 DP。设 f_{i,0/1/2/3} 表示从结点 i 上传了长度为多少的链。每次转移相当于从子树中选 2 与 2 匹配,1 与 3 匹配。然后最后匹配完。

可以设 g_{k,0/1} 表示所有儿子中上传长度 1 的比上传长度 3 的多 k 个,以及上传长度 2 儿子的个数奇偶性的最大值。然后不难转移,相当于一个背包,最后的答案是 g_{0,0}。

现在考虑一次转移的复杂度,需要枚举 g 的第一维,这是 O(siz^2) 的,过不了。

观察到 g 的第一维,每次转移时只会 +1/-1。有结论,随机正负 1 序列的前缀绝对最大值期望在根号级别。也就是说,我们可以将子树 dp 的顺序随机打乱,最优情况转移时 k 的取值在根号级别,大于根号的范围我们没必要考虑。

那么就优化完成了,相当于是坐标轴随机游走问题。 ::::success[Code]

/*
It's been a long day.
Without my friend.
And I'll tell you all about it when I see you again.
We've come a long way,
From where we began.
Oh I'll tell you all about it when I see you again.
When I see you again.
Oh,who know,all the planes we flew,
good things we had been drew
That something told me right here talking with you.
*/

//#pragma GCC optimize(1)
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define rep(i,x,y) for(int i=x;i<=y;++i)
#define dep(i,x,y) for(int i=x;i>=y;--i)
#define int long long
#define endl "\n"
#define MAX 1145141919810
using namespace std;
inline int read() {
    int ret = 0, sgn = 0, ch = getchar();
    while (!isdigit(ch))sgn |= ch == '-', ch = getchar();
    while (isdigit(ch))ret = ret * 10 + ch - '0', ch = getchar();
    return sgn ? -ret : ret;
}
const int N = 2e5 + 10;
const int B = 2000;
struct node {
    int to, nxt, w;
} edge[N * 2];
int head[N], cnt;
void add(int u, int v, int w) {
    edge[++cnt].to = v;
    edge[cnt].w = w;
    edge[cnt].nxt = head[u];
    head[u] = cnt;
}
void adde(int u, int v, int w) {
    add(u, v, w);
    add(v, u, w);
}
int dp[N][4];
int g[B << 1 + 10][2];
int tmp[B << 1 + 10][2];
int faw[N];
void dfs(int u, int f) {
    vector<int>q;
    for (int i = head[u]; i; i = edge[i].nxt) {
        int v = edge[i].to;
        if (v == f) continue;
        faw[v] = edge[i].w;
        dfs(v, u);
        q.push_back(v);
    }
    random_shuffle(q.begin(), q.end());
    int l = B, r = B, del = q.size() / 2 + 1;
    for (int i = 0; i <= B * 2; i++) for (int j = 0; j <= 1; j++) g[i][j] = -1e18;
    g[B][0] = 0;
    for (auto v : q) {
        for (int i = l - 1; i <= r + 1; i++) tmp[i][0] = tmp[i][1] = -1e18;
        for (int i = l - 1; i <= r + 1; i++) for (int j = 0; j <= 1; j++) if (g[i][j] != -1e18) {
                    tmp[i][j] = max(tmp[i][j], g[i][j] + dp[v][0]);
                    tmp[i - 1][j] = max(tmp[i - 1][j], g[i][j] + dp[v][1]);
                    tmp[i][1 - j] = max(tmp[i][1 - j], g[i][j] + dp[v][2]);
                    tmp[i + 1][j] = max(tmp[i + 1][j], g[i][j] + dp[v][3]);
                }
        l = max(l - 1, B - del);
        l = max(l, 1ll);
        r = min(r + 1, B + del);
        r = min(r, B * 2);
        for (int i = l; i <= r; i++) g[i][0] = tmp[i][0], g[i][1] = tmp[i][1];
    }
    if (u != 1) dp[u][0] = max(max(0ll, g[B][0]), g[B + 1][0] + faw[u]);
    else dp[u][0] = max(0ll, g[B][0]);
    dp[u][1] = max(0ll, g[B][0]) + faw[u];
    dp[u][2] = g[B - 1][0] + faw[u];
    dp[u][3] = g[B][1] + faw[u];
}

inline void Feather() {
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    mt19937 Rand(time(0));
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    for (int i = 1; i < n; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        adde(u, v, w);
    }
    dfs(1, 0);
    cout << dp[1][0] << "\n";
}
signed main() {
    Feather();
    return 0;
}

::::

10.CF840D Destiny

我们注意到 k 的值非常小只有 5,所以说我们考虑随机化。

答案在序列中的占比大于 \frac{1}{5}。考虑随机检查 [l,r] 区间里的 h 个数字,再检查它的出现次数是否大于 \frac{r−l+1}{k} 。最坏情况下有 \frac{1}{k} 的概率能够统计到答案。

那就直接干它 100 次。

外面还要套一个莫队。

真该学一点莫队。。。

::::success[Code]

/*
It's been a long day.
Without my friend.
And I'll tell you all about it when I see you again.
We've come a long way,
From where we began.
Oh I'll tell you all about it when I see you again.
When I see you again.
Oh,who know,all the planes we flew,
good things we had been drew
That something told me right here talking with you.
*/

//#pragma GCC optimize(1)
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define int long long
#define N -114514
#define endl "\n"
#define MAX 1145141919810
#define up(l, r, i) for(int i = l, END##i = r;i <= END##i;++ i)
#define dn(r, l, i) for(int i = r, END##i = l;i >= END##i;-- i)
using namespace std;
typedef long long i64;
const int INF = 2147483647;
const int MAXN = 3e5 + 3;
int A[MAXN], B[MAXN], h = 100;
vector <int> E[MAXN];
vector <int> F[MAXN];
int qread() {
    int w = 1, c, ret;
    while ((c = getchar()) >  '9' || c <  '0') w = (c == '-' ? -1 : 1);
    ret = c - '0';
    while ((c = getchar()) >= '0' && c <= '9') ret = ret * 10 + c - '0';
    return ret * w;
}
mt19937 MT;
vector <tuple<int, int, int> > Q[MAXN];
inline int read() {
    int ret = 0, sgn = 0, ch = getchar();
    while (!isdigit(ch))sgn |= ch == '-', ch = getchar();
    while (isdigit(ch))ret = ret * 10 + ch - '0', ch = getchar();
    return sgn ? -ret : ret;
}
inline void Feather() {
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    mt19937 Rand(time(0));
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n = qread(), m = qread();
    up(1, n, i) A[i] = qread();
    up(1, m, i) {
        int l = qread(), r = qread(), k = qread();
        Q[r].push_back(make_tuple(l, k, i));
    }
    up(1, n, i) {
        F[A[i]].push_back(i);
        for (auto &q : Q[i]) {
            int l = get<0>(q);
            int r = i;
            int k = get<1>(q);
            int x = get<2>(q);
            int t = (r - l + 1) / k + 1;
            int ans = INF;
            uniform_int_distribution <int> U(l, r);
            up(1, h, j) {
                int p = A[U(MT)], g;
                if ((g = F[p].size()) >= t && F[p][g - t] >= l) {
                    if (p < ans)
                        ans = p;
                }
            }
            if (ans == INF)
                B[x] = -1;
            else
                B[x] = ans;
        }
    }
    up(1, m, i)cout<<B[i]<<' ';
}
signed main() {
    Feather();
    return 0;
}

::::