NOIP2026前

· · 个人记录

2026.11.16

洛谷 NOIP 模拟赛。

T1

P17877

需要维护一大坨的线段树,场上想出了正解并花了 3.5h 实现,由于大样例太水导致我的 pushdown 写出了这坨答辩而不自知:

inline void pushdown(int x){
    if(tree[x].cov!=-1){
      tree[x<<1].cov=tree[x<<1|1].cov=
      tree[x<<1].val=tree[x<<1|1].val=tree[x].cov;
      tree[x].cov=-1;
    }
}

赛后发现乘上区间长度就 AC 了,痛失场蓝。

(这坨史有 70pts,lg 拿脚造的数据)

dif:5/11

T2

P17878

赛时什么都没打。

发现这个东西是可以二分答案的,我们不妨去二分这个中位数。

直接维护连通块是很困难的,这里给出一个很厉害的定义:

pii dp[N][2];
//dp[i][j]表示以i为根的子树,子树内连通块的奇偶性为j时,当前的权值和以及>=limit的连通块数量差 

然后要依次讨论删不删 (u,v) 这条边,以及这么做会对子树造成多大的贡献即可。

其余部分见题解。

dif:5.5/11

T3

P17879

赛时没打,赛后补了。

我们发现对于一个下标 i,如果 i 之前的数都不大于 i 上的元素,且 i 之后的数都不小于 i 上之后的元素,则 i 位置一定会被所有的最长不下降子序列选到。

先考虑填上 b_i>0 的位置。再用一些 b_i \le 0 的位置和不满足条件的 b_i>0 的位置组成逆序对。发现剩下 b_i>0 的位置中 a_i 是单调不降的,于是任意一个 b_i \le 0 的位置对应能匹配成逆序对的位置是一个区间。

然后就是求最大的区间并,设 dp_i 表示考虑前 i 个元素是区间并和的最大值,再维护 b_i 的前缀和,则有:

dp_i=\max(dp_{i-1},s_i+val+\max_{l-1 \le j \le i-1}dp_j-s_j)

线段树维护最大的 dp_i-s_i。

dif:6.5/11

T4

P17880

场上打了 4pts 的暴力。

dif:7.5/11

2026.11.17

打的超级烂,建议拿省四。

首先是没有想到将军的比赛会难度乱序,所以花了很长时间在一个大概率有紫的 T1 上面,一开始想的很简单,认为随便做一下就行了,然后发现有点假,开始想 Top Cluster,点分治,换根 DP 之类乱七八糟的东西。

最后想到了一个假的 O(n) 做法,但是在测大样例时发现只有几个过不去。于是我没有仔细思考自己做法的正确性,想要修补一下,结果修了三个多小时还没修出来。最后只能直接扔了。

这导致我都完全没怎么思考后三题,T2 虽然只花几秒就想到和二分以及因数有关,但是这他妈是道交互,然后就没时间调。

T3 看见是 DP 直接不知所措,乱写了一个比较对的式子,结果连 n=2 都过不去。然后又扔了。赛后发现是生成函数,那咋办。然后 TobyHu 给我讲了个 O(nm) 的做法,吓哭了。

(这是怎么赛后有 10pts 的)

看了眼 T4 马上想到用平衡树来维护,结果由于时间不够中间有些处理太乱了,然后也没什么分。

状态很差,警钟长鸣。

T1

给定一棵树,定义点对 (u,v) 的价值 f(u,v) 如下:

在 u 到 v 的简单路径上,若 u 为最大值且 v 为最小值,则价值为 a_v,否则为 0。

对于每个点 u,求 \sum_{u \ne v} f(u,v)。

题解不像人:

完全不会笛卡尔树,布丁。

dif:6/11

T2

q=9 诗人啊

来你告诉我什么叫“首先这个东西询问的是 Z 函数中 Z_i 的值”。

我不会 Z 函数。

不过题还是可以订的:

我们发现可以 O(1) 来 check 一个分割长度是否可能,然后又可以知道只有 n 的因数是有用的,因此可以尝试在这上面做文章。

发现这个 9 很特殊,大胆猜测是一坨质因子的乘积。

然后发现前几个小的质因子的乘积小于 n,发现可以对每个质因子的幂次进行二分,总次数小于 9 次。

::::info[code]

#include"period.h"
int solve(int n,int q){
    int ans=1,pre=n;
    for(int i=2;i<=n;i++){
        if(n%i) continue;
        int l=1,r=0,res=1;
        while(!(n%i)) n/=i,r++;
        while(l<=r){
            int mid=(l+r)>>1,p=1;
            for(int j=1;j<=mid;j++) p*=i;
            if(query(pre/p)+pre/p==pre) res=p,l=mid+1;
            else r=mid-1;
        }
        ans*=res;
    }
    return ans;
}

::::

dif:4/11

T3

题解生成函数没智力。

订了 TobyHu 的 O(m^2 \log m)。

::::info[code]

#include<bits/stdc++.h>
#define int long long 
namespace EVA{
    inline int read(){
        int x=0,f=1;
        char c=getchar();
        while(c<'0'||c>'9'){
            if(c=='-') f^=1;
            c=getchar();
        }
        while(c>='0'&&c<='9'){
            x=(x<<3)+(x<<1)+(c^48);
            c=getchar();
        }
        return f?x:~x+1;
    }
    inline void write(int x){
        if(x<0) x=~x+1,putchar('-');
        if(x>9) write(x/10);
        putchar(x%10^48);
    }
    inline void spaceput(int x){
        write(x);putchar(' ');
    }
    inline void output(int x){
        write(x);putchar('\n');
    }
} 
using namespace EVA;
using namespace std;
const int N=1e4+5;
const int mod=1e9+7;
int n,m;
int a[N],dp[N],g[N],c[N],inv[N];
inline void init(){
    inv[1]=1;
    for(int i=2;i<=m;i++) inv[i]=inv[mod%i]*(mod-mod/i)%mod;
}
signed main(){
    n=read(),m=read();init();
    for(int i=1;i<=n;i++) a[i]=read();
    dp[0]=c[0]=1;
    for(int i=1;i<=m;i++){
        if(!a[i]) continue;
        for(int j=1;j<=m/i;j++) c[j]=c[j-1]*(a[i]+j-1)%mod*inv[j]%mod;
        for(int j=0;j<=m/i;j++){
            for(int k=0;k+i*j<=m;k++) g[i*j+k]=(g[i*j+k]+c[j]*dp[k]%mod)%mod;
        } 
        for(int j=0;j<=m;j++) dp[j]=g[j],g[j]=0;
    }
    for(int i=1;i<=m;i++) spaceput(dp[i]);
    return 0;
}

::::

dif:5.5/11

T4

这种题都没切。

请问我赛场上在干什么。

不过这个特征值等于区间长度减众数的 trick 有点不好证扔给了翁陆恒让他帮我证。

翁陆恒证出来了。

::::info[code]

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
//#define int long long 
namespace EVA{
    inline int read(){
        int x=0,f=1;
        char c=getchar();
        while(c<'0'||c>'9'){
            if(c=='-') f^=1;
            c=getchar();
        }
        while(c>='0'&&c<='9'){
            x=(x<<3)+(x<<1)+(c^48);
            c=getchar();
        }
        return f?x:~x+1;
    }
    inline void write(int x){
        if(x<0) x=~x+1,putchar('-');
        if(x>9) write(x/10);
        putchar(x%10^48);
    }
    inline void spaceput(int x){
        write(x);putchar(' ');
    }
    inline void output(int x){
        write(x);putchar('\n');
    }
} 
using namespace EVA;
using namespace std;
using namespace __gnu_pbds;
const int N=2e5+5;
//const int inf=1e18;
int n,q;
int a[N];
multiset<int> s;
cc_hash_table<int,int> ct;
#define IT multiset<int>::iterator
inline void check(){
    auto it=s.rbegin();
    output(n-*it);
}
signed main(){
    n=read(),q=read();
    for(int i=1;i<=n;i++) a[i]=read(),ct[a[i]]++;
    for(auto v:ct) s.insert(v.second);
    check();
    for(int i=1;i<=q;i++){
        int p,x;p=read(),x=read();
        int res=ct[a[p]];
        IT it=s.lower_bound(res);
        s.erase(it);
        if(ct[a[p]]!=1) ct[a[p]]--,s.insert(ct[a[p]]);
        else ct.erase(a[p]); 
        if(ct.find(x)==ct.end()){
            s.insert(1);ct[x]=1;
        }
        else{
            res=ct[x];ct[x]++;
            it=s.lower_bound(res);
            s.erase(it);s.insert(ct[x]);
        }
        check();a[p]=x;
    }
    return 0;
}

::::

笑点解析:我正在思考用 cc_hash_table 还是 gp_hash_table 更快的时候,TobyHu 信誓旦旦地和我说 gp_hash_table 更快,结果就他的 gp_hash_table 会 TLE,其他的 map,unordered_map,cc_hash_table 都能过,太不堪了。

dif:4.25/11

2026.11.18

自由练习,VP 了 ZR Day 8。

ZR3729.看门狗

区间 chkmax 线段树模板,随便写写就切了。

::::info[code]

#include<bits/stdc++.h>
//#define int long long
#define ll long long
#define ull unsigned long long
#define i128 __int128
#ifdef __linux__
//#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define CK_TM cerr<<'\n'<<1.0*fabs(&ENDM-&STARTM)/(1024*1024)<<"MB"<<" "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms"<<'\n';
char buf[1<<25],*p1,*p2;
#define getchar() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin))?EOF:*p1++)
#define vector basic_string
#define pq priority_queue
#define mkpr make_pair
#define pii pair<int,int>
#define pb push_back
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define sz(h) (int)h.size()
#define popc(x) __builtin_popcount(x)
bool STARTM;
constexpr double eps=1e-10;
constexpr int i_inf=1e9;
constexpr ll ll_inf=1e18;
namespace AyanamiRei{
    inline int read(){
        int x=0,f=1;char c=getchar();
        while(c<'0'||c>'9') {if(c=='-') f^=1;c=getchar();}
        while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
        return f?x:~x+1;
    }
}namespace SoryuAsukaRangure{
    inline void write(int x){
        if(x<0) x=~x+1,putchar('-');
        if(x>9) write(x/10);
        return putchar(x%10^48),void();
    }
}namespace AyanamiYue{
    inline void spaceput(int x){
        SoryuAsukaRangure::write(x);putchar(' ');
    }
}namespace IkariYue{
    inline void output(int x){
        SoryuAsukaRangure::write(x);putchar('\n');
    }
}namespace IkariShinji{
    inline int max(int x,int y){
        return x>y?x:y;
    }
}namespace NagisaKaworu{
    inline int min(int x,int y){
        return x<y?x:y;
    }
}namespace MariMakinamiIllustious{
    inline double MAX(double x,double y){
        return x-y>eps?x:y;
    }
}namespace ShikinamiAsukaRangure{
    inline double MIN(double x,double y){
        return y-x>eps?x:y;
    }
}
using namespace std;
using namespace AyanamiRei;
using namespace SoryuAsukaRangure;
using namespace AyanamiYue;
using namespace IkariYue;
using namespace IkariShinji;
using namespace NagisaKaworu;
using namespace MariMakinamiIllustious;
using namespace ShikinamiAsukaRangure;
constexpr int N=1e6+5;
constexpr int the_birthday_of_Asuka=20011204;
constexpr int the_birthday_of_Ayanami=20010330;
mt19937 Asuka(the_birthday_of_Asuka+time(0));
mt19937 Ayanami(the_birthday_of_Ayanami+time(0));
int n,tot;
int a[N],cur[N];
vi eva[N];
bitset<N> vis;
struct smt{
    int mx,tag;
}tree[N<<2];
namespace SMT{
    inline void pushup(int x){
        tree[x].mx=max(tree[x<<1].mx,tree[x<<1|1].mx);
    }
    inline void pushdown(int x){
        if(tree[x].tag){
            tree[x<<1].tag=max(tree[x<<1].tag,tree[x].tag);
            tree[x<<1|1].tag=max(tree[x<<1|1].tag,tree[x].tag); 
            tree[x<<1].mx=max(tree[x<<1].mx,tree[x].tag);
            tree[x<<1|1].mx=max(tree[x<<1|1].mx,tree[x].tag);
            tree[x].tag=0;
        }
    }
    inline void build(int x,int l,int r){
        if(l==r) return tree[x].mx=-1,void();
        int mid=(l+r)>>1;
        build(x<<1,l,mid),build(x<<1|1,mid+1,r);
        pushup(x);
    }
    inline void modify(int x,int v,int L,int R,int l,int r){
        if(L<=l&&r<=R){
            tree[x].tag=max(tree[x].tag,v);
            tree[x].mx=max(tree[x].mx,v);
            return;
        }
        int mid=(l+r)>>1;pushdown(x);
        if(L<=mid) modify(x<<1,v,L,R,l,mid);
        if(mid<R) modify(x<<1|1,v,L,R,mid+1,r);
        pushup(x);
    }
    inline int query(int x,int L,int R,int l,int r){
        if(L<=l&&r<=R) return tree[x].mx;
        int mid=(l+r)>>1,res=-1;pushdown(x);
        if(L<=mid) res=max(res,query(x<<1,L,R,l,mid));
        if(mid<R) res=max(res,query(x<<1|1,L,R,mid+1,r));
        return res;
    }
}
bool ENDM;
signed main(){
//  system("fc patience.out ex_patience6.out /w");
//  freopen("ex_patience6.in","r",stdin);
//  freopen("patience.out","w",stdout);
    n=read();
    for(int i=1;i<=n;i++){
        a[i]=read();
        if(!vis[a[i]]) cur[++tot]=a[i];
        vis[a[i]]=1;eva[a[i]].pb(i);
    }
    if(vis.count()==1){
        for(int i=1;i<=n;i++) spaceput(a[i]);
        return 0;
    }
    sort(cur+1,cur+tot+1);
    SMT::build(1,1,n);
    for(int i=1;i<=tot;i++){
        sort(eva[cur[i]].begin(),eva[cur[i]].end());
        int len=i_inf,C=-i_inf;
        for(int j=0;j<sz(eva[cur[i]]);j++){
//          int A=eva[cur[i]][j],B=n-eva[cur[i]][j]+1,C=-i_inf;
            if(j) C=max(C,abs(eva[cur[i]][j]-eva[cur[i]][j-1]));
//          len=min(len,min(C,max(A,B)));
        }
        int A=eva[cur[i]][0],B=n-eva[cur[i]][sz(eva[cur[i]])-1]+1;
        len=min(len,max(C,max(A,B)));
//      cerr<<cur[i]<<" "<<len<<'\n';
        SMT::modify(1,cur[i],len,n,1,n);
    }
    for(int i=1;i<=n;i++) spaceput(SMT::query(1,i,i,1,n));
//  CK_TM;
    return 0;
}

::::

dif:3/11

ZR3730.百年好合/P14347

很厉害的题。

我的题解

dif:6/11

P5464

深刻。

你发现这个问题等价于选出若干条线段,使得集合中任意三条线段无交的数量。

树状数组维护即可,复杂度 O(n^2 \log n)。

dif:5.5/11

P6032

双指针。

ZR3732.合唱/P9293

这题神了。

没订完。

我草我线段树写错了。

这个东西还是看题解吧,思路太长了说是。

::::info[code]

#include<bits/stdc++.h>
#define int long long
#define ll long long
#define ull unsigned long long
#define i128 __int128
#ifdef __linux__
//#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define CK_TM cerr<<'\n'<<1.0*fabs(&ENDM-&STARTM)/(1024*1024)<<"MB"<<" "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms"<<'\n';
char buf[1<<25],*p1,*p2;
#define getchar() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin))?EOF:*p1++)
#define vector basic_string
#define pq priority_queue
#define mkpr make_pair
#define pii pair<int,int>
#define pb push_back
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define sz(h) (int)h.size()
#define popc(x) __builtin_popcount(x)
bool STARTM;
constexpr double eps=1e-10;
constexpr int i_inf=1e9;
constexpr ll ll_inf=1e18;
namespace AyanamiRei{
    inline int read(){
        int x=0,f=1;char c=getchar();
        while(c<'0'||c>'9') {if(c=='-') f^=1;c=getchar();}
        while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
        return f?x:~x+1;
    }
}namespace SoryuAsukaRangure{
    inline void write(int x){
        if(x<0) x=~x+1,putchar('-');
        if(x>9) write(x/10);
        return putchar(x%10^48),void();
    }
}namespace AyanamiYue{
    inline void spaceput(int x){
        SoryuAsukaRangure::write(x);putchar(' ');
    }
}namespace IkariYue{
    inline void output(int x){
        SoryuAsukaRangure::write(x);putchar('\n');
    }
}namespace IkariShinji{
    inline int max(int x,int y){
        return x>y?x:y;
    }
}namespace NagisaKaworu{
    inline int min(int x,int y){
        return x<y?x:y;
    }
}namespace MariMakinamiIllustious{
    inline double MAX(double x,double y){
        return x-y>eps?x:y;
    }
}namespace ShikinamiAsukaRangure{
    inline double MIN(double x,double y){
        return y-x>eps?x:y;
    }
}
using namespace std;
using namespace AyanamiRei;
using namespace SoryuAsukaRangure;
using namespace AyanamiYue;
using namespace IkariYue;
using namespace IkariShinji;
using namespace NagisaKaworu;
using namespace MariMakinamiIllustious;
using namespace ShikinamiAsukaRangure;
constexpr int N=6e5+5; 
constexpr int the_birthday_of_Asuka=20011204;
constexpr int the_birthday_of_Ayanami=20010330;
mt19937 Asuka(the_birthday_of_Asuka+time(0));
mt19937 Ayanami(the_birthday_of_Ayanami+time(0)); 
int n,sum,tot,L;
int ans[N<<1],rk[N],head[N],nxt[N];
vi eva[N];
set<int> s;
inline int manosaba(int Ema,int Hiro){
    return rk[Ema]<rk[Hiro];
}
struct smt{
    pii mx;
    int ct,lazy;
}tree[N<<2];
namespace SMT{
    inline void pushup(int x){
        tree[x].ct=tree[x<<1].ct+tree[x<<1|1].ct;
        tree[x].mx=std::max(tree[x<<1].mx,tree[x<<1|1].mx);
    }
    inline void pushdown(int x){
        if(tree[x].lazy){
            tree[x<<1].mx.first+=tree[x].lazy;
            tree[x<<1|1].mx.first+=tree[x].lazy;
            tree[x<<1].lazy+=tree[x].lazy;
            tree[x<<1|1].lazy+=tree[x].lazy;
            tree[x].lazy=0;
        }
    }
    inline void build(int x,int l,int r){
        if(l==r) return tree[x].mx=mkpr(-i_inf,l),void();
        int mid=(l+r)>>1;
        build(x<<1,l,mid),build(x<<1|1,mid+1,r);
        pushup(x); 
    }
    inline void update(int x,int p,int v,int c,int l,int r){
        if(l==r){
            tree[x].mx.first=v,tree[x].ct=c;
            return;
        }
        int mid=(l+r)>>1;pushdown(x);
        if(p<=mid) update(x<<1,p,v,c,l,mid);
        else update(x<<1|1,p,v,c,mid+1,r);
        pushup(x);
    }
    inline void add(int x,int v,int L,int R,int l,int r){
        if(L>r||l>R) return;
        if(L<=l&&r<=R){
            tree[x].lazy+=v,tree[x].mx.first+=v;
            return;
        }
        int mid=(l+r)>>1;pushdown(x);
        if(L<=mid) add(x<<1,v,L,R,l,mid);
        if(mid<R) add(x<<1|1,v,L,R,mid+1,r);
        pushup(x); 
    }
    inline int query(int x,int L,int R,int l,int r){
        if(L>r||l>R||L>R) return 0;
        if(L<=l&&r<=R) return tree[x].ct;
        int mid=(l+r)>>1,res=0;pushdown(x);
        if(L<=mid) res+=query(x<<1,L,R,l,mid);
        if(mid<R) res+=query(x<<1|1,L,R,mid+1,r);
        return res;
    }
}
namespace SET{
    inline void rebuild(int v){
        SMT::add(1,1,1,v-1,1,tot);
        SMT::update(1,v,SMT::query(1,v+1,sum,1,tot)+head[v],1,1,tot);
        s.insert(v);
    }
    inline void sword(int v){
        SMT::add(1,-1,1,v-1,1,tot);
        SMT::update(1,v,-i_inf,0,1,tot);
        s.erase(v);
    }
    inline int check(int v){
        if(s.empty()) return 1;
        pii cur=tree[1].mx;
        int lim=cur.first,g8c=cur.second,now=lim;
        if(lim>v) return 0;
        vi del;
        while(1){
            int f=*s.rbegin();
            sword(f),del.pb(f);
            ans[now--]=1;
            if(f==g8c) break;
        }
        if(nxt[g8c]) rebuild(nxt[g8c]);
        if(check(now)) return 1;
        if(nxt[g8c]) sword(nxt[g8c]);
        if(lim==v){
            for(int i=now+1;i<=lim;i++) ans[i]=0;
            for(int e:del) rebuild(e);
            return 0;
        }
        ans[lim+1]=1,ans[now+1]=0;
        return check(now+1);
    }
}
bool ENDM;
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        string w;cin>>w;reverse(w.begin(),w.end());
        int len=w.length();L=max(L,len);w=" "+w;
        for(int j=1;j<=len;j++){
            if(w[j]=='1') eva[j].pb(i),sum++;
        }
    }
    for(int i=1;i<=L;i++){
        sort(eva[i].begin(),eva[i].end(),manosaba);
        for(int v:eva[i]){
            tot++;
            nxt[tot]=rk[v];rk[v]=tot;head[tot]=i-1;
        }
    }
    SMT::build(1,1,tot);
    for(int i=1;i<=n;i++) SET::rebuild(rk[i]);
    SET::check(n+L);int flag=1;
    for(int i=L+n;i>=0;i--){
        if(ans[i]) write(ans[i]),flag=0;
        else if(!flag) write(ans[i]);
    }
    CK_TM;
    return 0;
}

::::

dif:8.5/11

ZR3731.墨山/P14382

不是题。

dif:7.5/11

2026.11.19

ZR Day 18

### T1 ![image.png](https://cdn.luogu.com.cn/upload/image_hosting/8dlh0ril.webp) 赛场上的想法不太自然,既维护了左边第一个 $S(1,pos) \ge S(pos+1,i)$ 的位置,又维护了右边第一个位置,导致式子没有推完。 看看题解是怎么处理这个东西的: ![image.png](https://cdn.luogu.com.cn/upload/image_hosting/wn5i4opw.webp) 感觉 *2300 左右吧,有点要脑子。 dif:6/11 ::::info[code] ```cpp #include<bits/stdc++.h> #define int long long #define ll long long #define ull unsigned long long #define i128 __int128 #ifdef __linux__ //#define getchar getchar_unlocked #define putchar putchar_unlocked #endif #define CK_TM cerr<<'\n'<<1.0*fabs(&ENDM-&STARTM)/(1024*1024)<<"MB"<<" "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms"<<'\n'; char buf[1<<25],*p1,*p2; #define getchar() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin))?EOF:*p1++) #define vector basic_string #define pq priority_queue #define mkpr make_pair #define pii pair<int,int> #define pb push_back #define umap unordered_map #define uset unordered_set #define vi vector<int> #define sz(h) (int)h.size() #define popc(x) __builtin_popcount(x) bool STARTM; constexpr double eps=1e-10; constexpr int i_inf=1e9; constexpr ll ll_inf=1e18; namespace AyanamiRei{ inline int read(){ int x=0,f=1;char c=getchar(); while(c<'0'||c>'9') {if(c=='-') f^=1;c=getchar();} while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();} return f?x:~x+1; } }namespace SoryuAsukaRangure{ inline void write(int x){ if(x<0) x=~x+1,putchar('-'); if(x>9) write(x/10); return putchar(x%10^48),void(); } }namespace AyanamiYue{ inline void spaceput(int x){ SoryuAsukaRangure::write(x);putchar(' '); } }namespace IkariYue{ inline void output(int x){ SoryuAsukaRangure::write(x);putchar('\n'); } }namespace IkariShinji{ inline int max(int x,int y){ return x>y?x:y; } }namespace NagisaKaworu{ inline int min(int x,int y){ return x<y?x:y; } }namespace MariMakinamiIllustious{ inline double MAX(double x,double y){ return x-y>eps?x:y; } }namespace ShikinamiAsukaRangure{ inline double MIN(double x,double y){ return y-x>eps?x:y; } } using namespace std; using namespace AyanamiRei; using namespace SoryuAsukaRangure; using namespace AyanamiYue; using namespace IkariYue; using namespace IkariShinji; using namespace NagisaKaworu; using namespace MariMakinamiIllustious; using namespace ShikinamiAsukaRangure; constexpr int N=1e6+5; constexpr int the_birthday_of_Asuka=20011204; constexpr int the_birthday_of_Ayanami=20010330; mt19937 Asuka(the_birthday_of_Asuka+time(0)); mt19937 Ayanami(the_birthday_of_Ayanami+time(0)); int n,mod; int pos[N],r[N],dp[N],pw2[N]; inline int C(int l,int r){ return (r-l+1)*(r+l)/2; } int tree[N<<2]; namespace SMT{ inline void pushup(int x){ tree[x]=tree[x<<1]+tree[x<<1|1]; } inline void update(int x,int p,int v,int l,int r){ if(l==r&&l==p) return tree[x]=v,void(); int mid=(l+r)>>1; if(p<=mid) update(x<<1,p,v,l,mid); else update(x<<1|1,p,v,mid+1,r); pushup(x); } inline int query(int x,int L,int R,int l,int r){ if(L>r||L>R||l>R) return 0; if(L<=l&&r<=R) return tree[x]; int mid=(l+r)>>1,res=0; if(L<=mid) res+=query(x<<1,L,R,l,mid); if(mid<R) res+=query(x<<1|1,L,R,mid+1,r); return res; } } bool ENDM; signed main(){ n=read(),mod=read(); if(mod==2){ for(int i=1;i<=n;i++) spaceput(0); return 0; } pw2[0]=1; for(int i=1;i<=n;i++) pw2[i]=pw2[i-1]*2%mod; pos[1]=1; for(int i=2;i<=n;i++){ pos[i]=pos[i-1]; while(pos[i]<i&&C(pos[i]+1,i)>C(1,pos[i])) pos[i]++; } r[1]=1; for(int i=2;i<=n;i++){ r[i]=r[i-1]; while(r[i]<n&&pos[r[i]+1]<=i) r[i]++; } dp[n]=2%mod;SMT::update(1,n,dp[n],1,n); for(int i=n-1;i>=1;i--){ dp[i]=(dp[i]+SMT::query(1,i+1,r[i],1,n))%mod; SMT::update(1,i,dp[i],1,n); } for(int i=1;i<=n;i++) spaceput(dp[i]*pw2[pos[i]-1]%mod); CK_TM; return 0; } ``` :::: ### T2 唯二像题的题。 ![image.png](https://cdn.luogu.com.cn/upload/image_hosting/v7av15w0.webp) ![image.png](https://cdn.luogu.com.cn/upload/image_hosting/e5wkqhmj.webp) 注意到本题的核心是定义出这两个东西: ```cpp //p[i]=f(p[i-1],i)为从0一路向上挑战的结果 //nxt[i]表示p[i]+1与p[i+1]重合的位置 ``` 我真的觉得这个 $p$ 数组和 $nxt$ 数组的定义非常巧妙,这样就可以把这道题转化为一个分讨数学题了。 dif:6.5/11 ::::info[code] ```cpp #include<bits/stdc++.h> #define int long long #define ll long long #define ull unsigned long long #define i128 __int128 #ifdef __linux__ //#define getchar getchar_unlocked #define putchar putchar_unlocked #endif #define CK_TM cerr<<'\n'<<1.0*fabs(&ENDM-&STARTM)/(1024*1024)<<"MB"<<" "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms"<<'\n'; char buf[1<<25],*p1,*p2; #define getchar() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin))?EOF:*p1++) #define vector basic_string #define pq priority_queue #define mkpr make_pair #define pii pair<int,int> #define pb push_back #define umap unordered_map #define uset unordered_set #define vi vector<int> #define sz(h) (int)h.size() #define popc(x) __builtin_popcount(x) bool STARTM; constexpr double eps=1e-10; constexpr int i_inf=1e9; constexpr ll ll_inf=1e18; namespace AyanamiRei{ inline int read(){ int x=0,f=1;char c=getchar(); while(c<'0'||c>'9') {if(c=='-') f^=1;c=getchar();} while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();} return f?x:~x+1; } }namespace SoryuAsukaRangure{ inline void write(int x){ if(x<0) x=~x+1,putchar('-'); if(x>9) write(x/10); return putchar(x%10^48),void(); } }namespace AyanamiYue{ inline void spaceput(int x){ SoryuAsukaRangure::write(x);putchar(' '); } }namespace IkariYue{ inline void output(int x){ SoryuAsukaRangure::write(x);putchar('\n'); } }namespace IkariShinji{ inline int max(int x,int y){ return x>y?x:y; } }namespace NagisaKaworu{ inline int min(int x,int y){ return x<y?x:y; } }namespace MariMakinamiIllustious{ inline double MAX(double x,double y){ return x-y>eps?x:y; } }namespace ShikinamiAsukaRangure{ inline double MIN(double x,double y){ return y-x>eps?x:y; } } using namespace std; using namespace AyanamiRei; using namespace SoryuAsukaRangure; using namespace AyanamiYue; using namespace IkariYue; using namespace IkariShinji; using namespace NagisaKaworu; using namespace MariMakinamiIllustious; using namespace ShikinamiAsukaRangure; constexpr int N=2e5+5; constexpr int the_birthday_of_Asuka=20011204; constexpr int the_birthday_of_Ayanami=20010330; mt19937 Asuka(the_birthday_of_Asuka+time(0)); mt19937 Ayanami(the_birthday_of_Ayanami+time(0)); int Case,n,q; int a[N],p[N],nxt[N]; //p[i]=f(p[i-1],i)为从0一路向上挑战的结果 //nxt[i]表示p[i]+1与p[i+1]重合的位置 bool ENDM; signed main(){ Case=read(),n=read(),q=read(); for(int i=1;i<=n;i++) a[i]=read(); p[0]=0;nxt[n]=n+1; for(int i=1;i<=n;i++) p[i]=(p[i-1]+a[i]+1)/2; for(int i=n-1;i>=0;i--){ if(p[i+1]==(p[i]+a[i+1]+2)/2) nxt[i]=i+1; else nxt[i]=nxt[i+1]; } while(q--){ int x,y,now;x=read(),y=read(); now=x;y=n-y+1; if(x>=a[y]){ output(0);continue; } int pos=upper_bound(a+1,a+n+1,x)-a; while(pos<=y&&x-p[pos-1]>1){ x=(x+a[pos]+1)/2;pos++; } if(pos<=y){ if(x==p[pos-1]||nxt[pos-1]<=y) x=p[y]; else x=p[y]+1; } output(x-now); } CK_TM; return 0; } ``` :::: ### T3 [原题](https://qoj.ac/problem/7302) ![image.png](https://cdn.luogu.com.cn/upload/image_hosting/563sjqnu.webp) ![image.png](https://cdn.luogu.com.cn/upload/image_hosting/43cvb3je.webp) ![image.png](https://cdn.luogu.com.cn/upload/image_hosting/kmryb8qx.webp) ![image.png](https://cdn.luogu.com.cn/upload/image_hosting/k5tmg76g.webp) ![image.png](https://cdn.luogu.com.cn/upload/image_hosting/4zpwa7s2.webp) [叉姐](https://www.luogu.com.cn/user/22682)的题,吓哭了。 最难的就是这个导出子图的分析,不过这题对于拼好分和观察性质的训练是很好的。 但是题解不像人。 dif:7.25/11 ::::info[code] ```cpp #include<bits/stdc++.h> #define int long long #define ll long long #define ull unsigned long long #define i128 __int128 #ifdef __linux__ //#define getchar getchar_unlocked #define putchar putchar_unlocked #endif #define CK_TM cerr<<'\n'<<1.0*fabs(&ENDM-&STARTM)/(1024*1024)<<"MB"<<" "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms"<<'\n'; char buf[1<<25],*p1,*p2; #define getchar() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin))?EOF:*p1++) #define vector basic_string #define pq priority_queue #define mkpr make_pair #define pii pair<int,int> #define pb push_back #define umap unordered_map #define uset unordered_set #define vi vector<int> #define sz(h) (int)h.size() #define popc(x) __builtin_popcount(x) bool STARTM; constexpr double eps=1e-10; constexpr int i_inf=1e9; constexpr ll ll_inf=1e18; namespace AyanamiRei{ inline int read(){ int x=0,f=1;char c=getchar(); while(c<'0'||c>'9') {if(c=='-') f^=1;c=getchar();} while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();} return f?x:~x+1; } }namespace SoryuAsukaRangure{ inline void write(int x){ if(x<0) x=~x+1,putchar('-'); if(x>9) write(x/10); return putchar(x%10^48),void(); } }namespace AyanamiYue{ inline void spaceput(int x){ SoryuAsukaRangure::write(x);putchar(' '); } }namespace IkariYue{ inline void output(int x){ SoryuAsukaRangure::write(x);putchar('\n'); } }namespace IkariShinji{ inline int max(int x,int y){ return x>y?x:y; } }namespace NagisaKaworu{ inline int min(int x,int y){ return x<y?x:y; } }namespace MariMakinamiIllustious{ inline double MAX(double x,double y){ return x-y>eps?x:y; } }namespace ShikinamiAsukaRangure{ inline double MIN(double x,double y){ return y-x>eps?x:y; } } using namespace std; using namespace AyanamiRei; using namespace SoryuAsukaRangure; using namespace AyanamiYue; using namespace IkariYue; using namespace IkariShinji; using namespace NagisaKaworu; using namespace MariMakinamiIllustious; using namespace ShikinamiAsukaRangure; constexpr int N=1e3+6e2+5; constexpr int the_birthday_of_Asuka=20011204; constexpr int the_birthday_of_Ayanami=20010330; mt19937 Asuka(the_birthday_of_Asuka+time(0)); mt19937 Ayanami(the_birthday_of_Ayanami+time(0)); int Case,n,m,ans; int S[10],F[10]={0,2,12,6,24,48,36,12,12,24},a[N],b[N]; bitset<N> vis[N]; inline int C2(int x){ return x*(x-1)/2; } inline int C3(int x){ return x*(x-1)*(x-2)/6; } bool ENDM; signed main(){ ios::sync_with_stdio(0); cin.tie(0),cout.tie(0); cin>>Case>>n; for(int i=1;i<=n;i++){ string s;cin>>s;s=" "+s; for(int j=1;j<=n;j++){ if(s[j]=='1') S[1]++,vis[i].set(j); } } for(int i=1;i<=n;i++){ int v1=vis[i].count(); S[2]+=C2(v1),S[7]+=C3(v1); for(int j=1;j<=n;j++){ if(i==j) continue; int v2=vis[j].count(),v3=(vis[i]&vis[j]).count(); if(vis[i][j]) S[3]+=(v1-1)*(v2-1)-v3,S[4]+=v3,S[6]+=C2(v3),a[i]+=v3,b[i]+=C2(v3); S[5]+=C2(v3),S[8]+=C2(v3)*(v1-2-vis[i][j]); } } for(int i=1;i<=n;i++) S[9]+=a[i]/2*(a[i]/2-1)/2-b[i]; S[1]/=2,S[3]/=2,S[4]/=6,S[5]/=4,S[6]/=2; for(int i=1;i<=9;i++) ans+=F[i]*S[i]; write(ans); CK_TM; return 0; } ``` :::: ### T4 [原题](https://qoj.ac/problem/7309) ![image.png](https://cdn.luogu.com.cn/upload/image_hosting/lft3n9z2.webp) ![image.png](https://cdn.luogu.com.cn/upload/image_hosting/v9mzlwbr.webp) ![image.png](https://cdn.luogu.com.cn/upload/image_hosting/wk4hj35v.webp) ![image.png](https://cdn.luogu.com.cn/upload/image_hosting/mimsvzft.webp) 依旧是[叉姐](https://www.luogu.com.cn/user/22682)的题。 原来的 $O(nm \log L)$ 很难写,但你发现这题正睿上开了 2s,且 $n,m,L \le 10^3$。 然后就学习了一个非常暴力的 $O(nmL)$ 做法。 ::::info[code] ```cpp #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/priority_queue.hpp> #include <ext/pb_ds/exception.hpp> #include <ext/pb_ds/hash_policy.hpp> #include <ext/pb_ds/list_update_policy.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/trie_policy.hpp> #define int long long #define ll long long #define ull unsigned long long #define i128 __int128 #ifdef __linux__ //#define getchar getchar_unlocked #define putchar putchar_unlocked #endif #define CK_TM cerr<<'\n'<<1.0*fabs(&ENDM-&STARTM)/(1024*1024)<<"MB"<<" "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms"<<'\n'; char buf[1<<25],*p1,*p2; #define getchar() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin))?EOF:*p1++) #define vector basic_string #define pq priority_queue #define mkpr make_pair #define pii pair<int,int> #define pb push_back #define umap unordered_map #define uset unordered_set #define vi vector<int> #define sz(h) (int)h.size() #define popc(x) __builtin_popcount(x) bool STARTM; constexpr double eps=1e-10; constexpr int i_inf=1e9; constexpr ll ll_inf=1e18; namespace AyanamiRei{ inline int read(){ int x=0,f=1;char c=getchar(); while(c<'0'||c>'9') {if(c=='-') f^=1;c=getchar();} while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();} return f?x:~x+1; } }namespace SoryuAsukaRangure{ inline void write(int x){ if(x<0) x=~x+1,putchar('-'); if(x>9) write(x/10); return putchar(x%10^48),void(); } }namespace AyanamiYue{ inline void spaceput(int x){ SoryuAsukaRangure::write(x);putchar(' '); } }namespace IkariYue{ inline void output(int x){ SoryuAsukaRangure::write(x);putchar('\n'); } }namespace IkariShinji{ inline int max(int x,int y){ return x>y?x:y; } }namespace NagisaKaworu{ inline int min(int x,int y){ return x<y?x:y; } }namespace MariMakinamiIllustious{ inline double MAX(double x,double y){ return x-y>eps?x:y; } }namespace ShikinamiAsukaRangure{ inline double MIN(double x,double y){ return y-x>eps?x:y; } } using namespace std; using namespace AyanamiRei; using namespace SoryuAsukaRangure; using namespace AyanamiYue; using namespace IkariYue; using namespace IkariShinji; using namespace NagisaKaworu; using namespace MariMakinamiIllustious; using namespace ShikinamiAsukaRangure; using namespace __gnu_pbds; constexpr int N=2e3+5; constexpr int the_birthday_of_Asuka=20011204; constexpr int the_birthday_of_Ayanami=20010330; mt19937 Asuka(the_birthday_of_Asuka+time(0)); mt19937 Ayanami(the_birthday_of_Ayanami+time(0)); int Case,T,n,m; cc_hash_table<int,int> mp; int a[N],b[N],p[N],q[N],sa[N],sb[N],dp[N][N]; bool ENDM; signed main(){ Case=read(),T=read(); while(T--){ n=read(),m=read(); mp.clear(); for(int i=1;i<=n;i++){ a[i]=read(),p[i]=read(); sa[i]=mp[a[i]];mp[a[i]]=i; } mp.clear(); for(int i=1;i<=m;i++){ b[i]=read(),q[i]=read(); sb[i]=mp[b[i]];mp[b[i]]=i; } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ dp[i][j]=max(dp[i-1][j],dp[i][j-1]); if(a[i]!=b[j]) continue; int A=i,B=j,l=0,r=0; while(A){ l+=p[A]; while(B){ r+=q[B]; dp[i][j]=max(dp[i][j],dp[A-1][B-1]+min(l,r)); if(r>l){ r-=q[B];break; } B=sb[B]; } A=sa[A]; } } } output(dp[n][m]); for(int i=1;i<=n;i++){ sa[i]=sb[i]=0; for(int j=1;j<=m;j++){ dp[i][j]=0; } } } CK_TM; return 0; } ``` :::: dif:5/11 $O(nmL)

dif:(8)? O(nm \log L)

2026.11.20

ZR Day 19

### T1 ![image.png](https://cdn.luogu.com.cn/upload/image_hosting/szhsp38r.webp) 二分第 $i$ 人能听到第 $i$ 首歌时环形最前面的位置,差分修改贡献。 ~~我又把差分当线段树写了~~ ::::info[code] ```cpp #include<bits/stdc++.h> #define int long long namespace manosaba{ inline int read(){ int x=0,f=1; char c=getchar(); while(c<'0'||c>'9'){ if(c=='-') f^=1; c=getchar(); } while(c>='0'&&c<='9'){ x=(x<<3)+(x<<1)+(c^48); c=getchar(); } return f?x:~x+1; } inline void write(int x){ if(x<0) putchar('-'),x=~x+1; if(x>9) write(x/10); putchar(x%10^48); } inline void spaceput(int x){ write(x);putchar(' '); } inline void output(int x){ write(x);putchar('\n'); } } using namespace manosaba; using namespace std; const int N=4e5+5; int n; int tree[N<<2],tag[N<<2],p[N],d[N],s[N]; inline void pushup(int x){ tree[x]=tree[x<<1]+tree[x<<1|1]; } inline void pushdown(int x,int ln,int rn){ if(tag[x]){ tag[x<<1]+=tag[x]; tag[x<<1|1]+=tag[x]; tree[x<<1]+=ln*tag[x]; tree[x<<1|1]+=rn*tag[x]; tag[x]=0; } } inline void modify(int x,int v,int L,int R,int l,int r){ if(L<=l&&r<=R){ tag[x]+=v;tree[x]+=(r-l+1)*v; return; } int mid=(l+r)>>1;pushdown(x,mid-l+1,r-mid); if(L<=mid) modify(x<<1,v,L,R,l,mid); if(mid<R) modify(x<<1|1,v,L,R,mid+1,r); pushup(x); } inline int query(int x,int L,int R,int l,int r){ if(L>r||l>R) return 0; if(L<=l&&r<=R) return tree[x]; int mid=(l+r)>>1;pushdown(x,mid-l+1,r-mid); return query(x<<1,L,R,l,mid)+query(x<<1|1,L,R,mid+1,r); } inline int check(int l,int i){ return (s[i+n]-s[l-1]<=d[i]); } signed main(){ // system("fc opening.out opening5.ans /w"); // freopen("opening.in","r",stdin); // freopen("opening.out","w",stdout); n=read(); for(int i=1;i<=n;i++) p[i]=read(),s[i]=s[i-1]+p[i]; for(int i=n+1;i<=2*n;i++) p[i]=p[i-n],s[i]=s[i-1]+p[i]; for(int i=1;i<=n;i++) d[i]=read(); for(int i=1;i<=n;i++){ if(p[i]>d[i]) continue; int l=i+1,r=i+n,p=i+n; while(l<=r){ int mid=(l+r)>>1; if(check(mid,i)) p=mid,r=mid-1; else l=mid+1; } // cerr<<p<<" "<<i+n<<'\n'; if(p>n){ modify(1,1,p-n,i,1,n); // cerr<<i<<": "<<p-n<<" "<<i<<'\n'; } else{ modify(1,1,p,n,1,n); // cerr<<i<<": "<<p<<" "<<n<<'\n'; modify(1,1,1,i,1,n); // cerr<<i<<": "<<1<<" "<<i<<'\n'; } } int ans=0,pos=1; for(int i=1;i<=n;i++){ // output(query(1,i,i,1,n)); int cur=query(1,i,i,1,n); if(cur>ans){ ans=cur;pos=i; } } spaceput(ans),output(pos); return 0; } ``` :::: dif:1.5/11 ### T2 ![image.png](https://cdn.luogu.com.cn/upload/image_hosting/xdcgrywh.webp) ![image.png](https://cdn.luogu.com.cn/upload/image_hosting/uekw24fn.webp) ~~不会用 checker.cpp,只能手写。~~ 看了一秒后就想到做法。 每轮按奇偶分组后双指针即可。 ::::info[code] ```cpp #include<bits/stdc++.h> //#define int long long namespace manosaba{ inline int read(){ int x=0,f=1; char c=getchar(); while(c<'0'||c>'9'){ if(c=='-') f^=1; c=getchar(); } while(c>='0'&&c<='9'){ x=(x<<3)+(x<<1)+(c^48); c=getchar(); } return f?x:~x+1; } inline void write(int x){ if(x<0) putchar('-'),x=~x+1; if(x>9) write(x/10); putchar(x%10^48); } inline void spaceput(int x){ write(x);putchar(' '); } inline void output(int x){ write(x);putchar('\n'); } } using namespace manosaba; using namespace std; #define mkpr make_pair #define pii pair<int,int> const int N=(1<<17)+3; const int K=20; int n,k,sum,tar,suki; int a[N],cnt[K]; vector<pii> eva[N]; struct sjy{ int v,id; inline friend bool operator < (sjy A,sjy B){ return A.v<B.v; } }Ema[N],Hiro[N]; signed main(){ // freopen("mean.in","r",stdin); // freopen("mean.out","w",stdout); k=read();n=(1<<k); for(int i=1;i<=n;i++) a[i]=read(),sum+=a[i]; if(!k) return write(0),0; if(abs(sum)%n!=0) return write(-1),0; // cerr<<"wyy"<<'\n'; tar=sum/n; for(int Ri=1;Ri<=k;Ri++){ int flag=1; for(int i=2;i<=n;i++){ if(a[i]!=a[i-1]){ flag=0;break; } } if(flag) break; suki=Ri; int ema=0,hiro=0; for(int i=1;i<=n;i++){ if(abs(a[i])%2==0) Ema[++ema]={a[i],i}; else Hiro[++hiro]={a[i],i}; } // cerr<<hiro<<'\n'; // for(int i=1;i<=hiro;i++) cerr<<Hiro[i].v<<' '<<Hiro[i].id<<'\n'; if(ema%2==1||hiro%2==1){ // cerr<<"sjy"<<'\n'; return write(-1),0; } sort(Ema+1,Ema+ema+1); sort(Hiro+1,Hiro+hiro+1); for(int i=1;i<=ema/2;i++){ int l=Ema[i].id,r=Ema[ema-i+1].id; int x=Ema[i].v,y=Ema[ema-i+1].v; if(x==y&&x==tar) continue; eva[Ri].emplace_back(mkpr(l,r)); a[l]=a[r]=(x+y)/2;cnt[Ri]++; } for(int i=1;i<=hiro/2;i++){ int l=Hiro[i].id,r=Hiro[hiro-i+1].id; int x=Hiro[i].v,y=Hiro[hiro-i+1].v; // cerr<<l<<" "<<x<<'\n'; // cerr<<r<<" "<<y<<'\n'; if(x==y&&x==tar) continue; eva[Ri].emplace_back(mkpr(l,r)); a[l]=a[r]=(x+y)/2;cnt[Ri]++; } // for(int i=1;i<=n;i++) cerr<<a[i]<<" ";cerr<<'\n'; // if(Ri==k+1&&cnt[Ri]) return write(-1),0; // if(!cnt[Ri]) break; } // if(suki==1) return write(0),0; int flag=1; for(int i=2;i<=n;i++){ if(a[i]!=a[i-1]){ flag=0;break; } } if(!flag) return write(-1),0; output(suki); for(int i=1;i<=suki;i++){ output(cnt[i]); for(auto S:eva[i]){ spaceput(S.first),output(S.second); } } // for(int i=1;i<=n;i++){ // cout<<a[i]<<'\n'; // } // for(int i=2;i<=n;i++){ // if(a[i]!=a[i-1]){ // cout<<"NO"<<'\n'; // break; // } // } return 0; } ``` :::: dif:3/11 ### T3 ![image.png](https://cdn.luogu.com.cn/upload/image_hosting/abfdq6re.webp) ![image.png](https://cdn.luogu.com.cn/upload/image_hosting/fig54qyb.webp) ![image.png](https://cdn.luogu.com.cn/upload/image_hosting/vl9w0rx4.webp) 朝鲜题。 赛场上把这题硬生生达成了找环后缩点作 DAG,然后在森林上倍增暴力跳连通块,简直是疯了。 又是和昨天 T2 类似的性质题,注意到每个小球一定会反转一段前缀或后缀,且这两段会逐步向中间扩散,直至整个序列只由两部分组成,即左边若干个向右的箭头和右边若干个向左的箭头。 ::::info[code] ```cpp #include<bits/stdc++.h> #define int long long #define ll long long #define ull unsigned long long #define i128 __int128 #ifdef __linux__ //#define getchar getchar_unlocked #define putchar putchar_unlocked #endif #define CK_TM cerr<<'\n'<<1.0*fabs(&ENDM-&STARTM)/(1024*1024)<<"MB"<<" "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms"<<'\n'; char buf[1<<25],*p1,*p2; #define getchar() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin))?EOF:*p1++) #define vector basic_string #define pq priority_queue #define mkpr make_pair #define pii pair<int,int> #define pb push_back #define umap unordered_map #define uset unordered_set #define vi vector<int> #define sz(h) (int)h.size() #define popc(x) __builtin_popcount(x) bool STARTM; constexpr double eps=1e-10; constexpr int i_inf=1e9; constexpr ll ll_inf=1e18; namespace AyanamiRei{ inline int read(){ int x=0,f=1;char c=getchar(); while(c<'0'||c>'9') {if(c=='-') f^=1;c=getchar();} while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();} return f?x:~x+1; } }namespace SoryuAsukaRangure{ inline void write(int x){ if(x<0) x=~x+1,putchar('-'); if(x>9) write(x/10); return putchar(x%10^48),void(); } }namespace AyanamiYue{ inline void spaceput(int x){ SoryuAsukaRangure::write(x);putchar(' '); } }namespace IkariYue{ inline void output(int x){ SoryuAsukaRangure::write(x);putchar('\n'); } }namespace IkariShinji{ inline int max(int x,int y){ return x>y?x:y; } }namespace NagisaKaworu{ inline int min(int x,int y){ return x<y?x:y; } }namespace MariMakinamiIllustious{ inline double MAX(double x,double y){ return x-y>eps?x:y; } }namespace ShikinamiAsukaRangure{ inline double MIN(double x,double y){ return y-x>eps?x:y; } } using namespace std; using namespace AyanamiRei; using namespace SoryuAsukaRangure; using namespace AyanamiYue; using namespace IkariYue; using namespace IkariShinji; using namespace NagisaKaworu; using namespace MariMakinamiIllustious; using namespace ShikinamiAsukaRangure; constexpr int the_birthday_of_Asuka=20011204; constexpr int the_birthday_of_Ayanami=20010330; mt19937 Asuka(the_birthday_of_Asuka+time(0)); mt19937 Ayanami(the_birthday_of_Ayanami+time(0)); int n,q,w,s; string ccc; bool ENDM; signed main(){ ios::sync_with_stdio(0); cin.tie(0),cout.tie(0); cin>>n>>q;cin>>ccc;ccc=" "+ccc; for(int i=1;i<=n;i++) if(ccc[i]=='>') w+=i,s++; int l=0,r=n+1,N=n+1; //l存连续>的最远前缀,r存连续<的最远前缀 while(q--){ int x,k;cin>>x>>k; int R=(s+k*x)/N,s_=(s+k*x)%N,w_=w; if(!R){ while(s<s_&&l+1<r){ if(ccc[++l]=='<') ccc[l]='>',s++,w_+=l; } if(l+1==r) l=s_,r=s_+1,w_=s_*(s_+1)/2; } else if(R==k){ while(s>s_&&l+1<r){ if(ccc[--r]=='>') ccc[r]='<',s--,w_-=r; } if(l+1==r) l=s_,r=s_+1,w_=s_*(s_+1)/2; } else l=s_,r=s_+1,w_=s_*(s_+1)/2; output(R*N*N-k*x*x+2*(w_-w)); s=s_,w=w_; } CK_TM; return 0; } ``` :::: dif:??? ### T4 ![image.png](https://cdn.luogu.com.cn/upload/image_hosting/pxo9gdkx.webp) ![image.png](https://cdn.luogu.com.cn/upload/image_hosting/ldf4yc18.webp) 韩国题。 ![image.png](https://cdn.luogu.com.cn/upload/image_hosting/oga4cxjn.webp) 以上是题解像人的部分。 ![image.png](https://cdn.luogu.com.cn/upload/image_hosting/ezaxb33z.webp) 以上是题解不像人的部分。 赛场上打了个 25pts 的状压,但是赛后题解那段觉得很错,遂布丁。 dif:7.5/11 ### [P17693](https://www.luogu.com.cn/problem/P17693) 太难了。 对着操作计数没前途,我们设 $dp_{i,j}$ 表示已经填了 $i$ 个数,前缀最大值为 $j$ 的方案数。 对于新填入的数字 $v$,首先必须要 $i \ge x_v$ 或 $j \ge y_v$ 才能转移。 - $v \le j$:设满足条件的数量为 $c$,则 $dp_{i,j} \leftarrow dp_{i,j} + dp_{i-1,j} \times (c - i + 1)

贴一个 O(n^3) 部分分的码:

::::info[O(n^3)]

for(int i=1;i<=n;i++){
    for(int j=1;j<=n;j++) dp[i][j]=0;
 }
dp[0][0]=1;
for(int i=1;i<=n;i++){
    for(int j=0;j<=n;j++){
  //    if(!dp[i-1][j]) continue;
        int cnt=0;
        for(int k=1;k<=j;k++){//当前填入的数 
            if(i>=x[k]||j>=y[k]) cnt++;
        }
        if(cnt>0) dp[i][j]=(dp[i][j]+dp[i-1][j]*(cnt-i+1)%mod)%mod;
        for(int k=j+1;k<=n;k++){
            if(i>=x[k]||j>=y[k]) dp[i][k]=(dp[i][k]+dp[i-1][j])%mod;
        }
    }
}
output(dp[n][n]%mod);

::::

然后发现这个东西感觉不怎么可以优化,那咋办

果断查看题解,发现一个非常厉害的性质:

相当深刻。

::::info[code]

#include<bits/stdc++.h>
#define int long long
#define ll long long
#define ull unsigned long long
#define i128 __int128
#ifdef __linux__
//#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define CK_TM cerr<<'\n'<<1.0*fabs(&ENDM-&STARTM)/(1024*1024)<<"MB"<<" "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms"<<'\n';
char buf[1<<25],*p1,*p2;
#define getchar() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin))?EOF:*p1++)
#define vector basic_string
#define pq priority_queue
#define mkpr make_pair
#define pii pair<int,int>
#define pb push_back
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define sz(h) (int)h.size()
#define popc(x) __builtin_popcount(x)
bool STARTM;
constexpr double eps=1e-10;
constexpr int i_inf=1e9;
constexpr ll ll_inf=1e18;
namespace AyanamiRei{
    inline int read(){
        int x=0,f=1;char c=getchar();
        while(c<'0'||c>'9') {if(c=='-') f^=1;c=getchar();}
        while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
        return f?x:~x+1;
    }
}namespace SoryuAsukaRangure{
    inline void write(int x){
        if(x<0) x=~x+1,putchar('-');
        if(x>9) write(x/10);
        return putchar(x%10^48),void();
    }
}namespace AyanamiYue{
    inline void spaceput(int x){
        SoryuAsukaRangure::write(x);putchar(' ');
    }
}namespace IkariYue{
    inline void output(int x){
        SoryuAsukaRangure::write(x);putchar('\n');
    }
}namespace IkariShinji{
    inline int max(int x,int y){
        return x>y?x:y;
    }
}namespace NagisaKaworu{
    inline int min(int x,int y){
        return x<y?x:y;
    }
}namespace MariMakinamiIllustious{
    inline double MAX(double x,double y){
        return x-y>eps?x:y;
    }
}namespace ShikinamiAsukaRangure{
    inline double MIN(double x,double y){
        return y-x>eps?x:y;
    }
}
using namespace std;
using namespace AyanamiRei;
using namespace SoryuAsukaRangure;
using namespace AyanamiYue;
using namespace IkariYue;
using namespace IkariShinji;
using namespace NagisaKaworu;
using namespace MariMakinamiIllustious;
using namespace ShikinamiAsukaRangure;
constexpr int N=5e3+5;
constexpr int mod=998244353;
constexpr int the_birthday_of_Asuka=20011204;
constexpr int the_birthday_of_Ayanami=20010330;
mt19937 Asuka(the_birthday_of_Asuka+time(0));
mt19937 Ayanami(the_birthday_of_Ayanami+time(0));
int n,ans;
int x[N],s[N],y[N],c[N],dp[N][N];
bool ENDM;
signed main(){
    int T;T=read();
    while(T--){
        n=read();
        for(int i=1;i<=n;i++) x[i]=read();
        for(int i=1;i<=n;i++) y[i]=read();
        for(int i=0;i<=n;i++){
            for(int j=0;j<=n;j++){
                dp[i][j]=0;
            }
        }
        for(int i=0;i<=n;i++) dp[0][i]=1;
        for(int i=1;i<=n;i++){
            for(int j=0;j<=n;j++) s[j]=c[j]=0;
            for(int j=1;j<=n;j++){
                if(x[j]>i) c[0]++,c[y[j]]--;
                if(x[j]<=i) s[0]++,s[j]--;
                else if(y[j]<j) s[y[j]]++,s[j]--;
            }
            for(int j=1;j<=n;j++) s[j]+=s[j-1],c[j]+=c[j-1];
            for(int j=i;j<=n;j++) dp[i][j]=(dp[i-1][j]-dp[i-1][j-1]+mod)%mod*max(0ll,n-i+1-s[j]-c[j])%mod;
            for(int j=i;j<=n;j++){
                if(x[j]<=i) dp[i][j]=(dp[i][j]+dp[i-1][j-1])%mod;
                else if(y[j]<j) dp[i][j]=(dp[i][j]+dp[i-1][j-1]-dp[i-1][y[j]-1]+mod)%mod;
            }
            for(int j=0;j<i;j++) dp[i][j]=0;
            for(int j=i;j<=n;j++) dp[i][j]=(dp[i][j]+dp[i][j-1])%mod;
        }
        output(dp[n][n]%mod);
    }
    CK_TM;
    return 0;
}

::::

dif:6.75/11

P12751

【模板】根号分治

::::info[code]

#include<bits/stdc++.h>
//#define int long long
#define ll long long
#define ull unsigned long long
#define i128 __int128
#ifdef __linux__
//#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define CK_TM cerr<<'\n'<<1.0*fabs(&ENDM-&STARTM)/(1024*1024)<<"MB"<<" "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms"<<'\n';
char buf[1<<25],*p1,*p2;
#define getchar() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin))?EOF:*p1++)
#define vector basic_string
#define pq priority_queue
#define mkpr make_pair
#define pii pair<int,int>
#define pb push_back
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define sz(h) (int)h.size()
#define popc(x) __builtin_popcount(x)
bool STARTM;
constexpr double eps=1e-10;
constexpr int i_inf=1e9;
constexpr ll ll_inf=1e18;
namespace AyanamiRei{
    inline int read(){
        int x=0,f=1;char c=getchar();
        while(c<'0'||c>'9') {if(c=='-') f^=1;c=getchar();}
        while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
        return f?x:~x+1;
    }
}namespace SoryuAsukaRangure{
    inline void write(int x){
        if(x<0) x=~x+1,putchar('-');
        if(x>9) write(x/10);
        return putchar(x%10^48),void();
    }
}namespace AyanamiYue{
    inline void spaceput(int x){
        SoryuAsukaRangure::write(x);putchar(' ');
    }
}namespace IkariYue{
    inline void output(int x){
        SoryuAsukaRangure::write(x);putchar('\n');
    }
}namespace IkariShinji{
    inline int max(int x,int y){
        return x>y?x:y;
    }
}namespace NagisaKaworu{
    inline int min(int x,int y){
        return x<y?x:y;
    }
}namespace MariMakinamiIllustious{
    inline double MAX(double x,double y){
        return x-y>eps?x:y;
    }
}namespace ShikinamiAsukaRangure{
    inline double MIN(double x,double y){
        return y-x>eps?x:y;
    }
}
using namespace std;
using namespace AyanamiRei;
using namespace SoryuAsukaRangure;
using namespace AyanamiYue;
using namespace IkariYue;
using namespace IkariShinji;
using namespace NagisaKaworu;
using namespace MariMakinamiIllustious;
using namespace ShikinamiAsukaRangure;
constexpr int N=1e5+3e4;
constexpr int SQN=sqrt(N/2)+1;
constexpr int the_birthday_of_Asuka=20011204;
constexpr int the_birthday_of_Ayanami=20010330;
mt19937 Asuka(the_birthday_of_Asuka+time(0));
mt19937 Ayanami(the_birthday_of_Ayanami+time(0));
int n,k,B;
int c[N],s[N][SQN];
bool ENDM;
signed main(){
    n=read(),k=read();B=sqrt(n/2);
    for(int i=1;i<=k;i++){
        int a,l,d;a=read(),l=read(),d=read();
        if(d>B) for(int j=1;j<=l;j++) c[a+(j-1)*d]++;
        else s[a][d]++,s[a+l*d][d]--;
    }
    for(int j=1;j<=B;j++){
        for(int i=j;i<=n;i++) s[i][j]+=s[i-j][j];
        for(int i=1;i<=n;i++) c[i]+=s[i][j];
    }
    for(int i=1;i<=n;i++) spaceput(c[i]);
    CK_TM;
    return 0;
}

::::

dif:2.5/11

2026.11.21

自由练习。

P4362

很典但是很难的树形 dp。

设 dp_{i,j,0/1} 表示在以 i 为根的子树,大头吃了 j 个果子,且没吃/吃了 i 果子的最小难受值。

转移时分为两种情况:

大头不吃 u 节点:

dp_{u,j,0}=\min(dp_{u,j,0},\min(dp_{v,s,0}+dp_{u,j-s,0}+[m=2] \times w),dp_{v,s,1}+dp_{u,j-s,0})

大头吃 u 节点:

dp_{u,j,1}=\min(dp_{u,j,1},\min(dp_{v,s,1}+dp_{u,j-s,1}+w,dp_{v,s,0}+dp_{u,j-s,1}))

::::info[code]

#include<bits/stdc++.h>
#define int long long
#define ll long long
#define ull unsigned long long
#define i128 __int128
#ifdef __linux__
//#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define CK_TM cerr<<'\n'<<1.0*fabs(&ENDM-&STARTM)/(1024*1024)<<"MB"<<" "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms"<<'\n';
char buf[1<<25],*p1,*p2;
#define getchar() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin))?EOF:*p1++)
#define vector basic_string
#define pq priority_queue
#define mkpr make_pair
#define pii pair<int,int>
#define pb push_back
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define sz(h) (int)h.size()
#define popc(x) __builtin_popcount(x)
bool STARTM;
constexpr double eps=1e-10;
constexpr int i_inf=1e9;
constexpr ll ll_inf=1e18;
namespace AyanamiRei{
    inline int read(){
        int x=0,f=1;char c=getchar();
        while(c<'0'||c>'9') {if(c=='-') f^=1;c=getchar();}
        while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
        return f?x:~x+1;
    }
}namespace SoryuAsukaRangure{
    inline void write(int x){
        if(x<0) x=~x+1,putchar('-');
        if(x>9) write(x/10);
        return putchar(x%10^48),void();
    }
}namespace AyanamiYue{
    inline void spaceput(int x){
        SoryuAsukaRangure::write(x);putchar(' ');
    }
}namespace IkariYue{
    inline void output(int x){
        SoryuAsukaRangure::write(x);putchar('\n');
    }
}namespace IkariShinji{
    inline int max(int x,int y){
        return x>y?x:y;
    }
}namespace NagisaKaworu{
    inline int min(int x,int y){
        return x<y?x:y;
    }
}namespace MariMakinamiIllustious{
    inline double MAX(double x,double y){
        return x-y>eps?x:y;
    }
}namespace ShikinamiAsukaRangure{
    inline double MIN(double x,double y){
        return y-x>eps?x:y;
    }
}
using namespace std;
using namespace AyanamiRei;
using namespace SoryuAsukaRangure;
using namespace AyanamiYue;
using namespace IkariYue;
using namespace IkariShinji;
using namespace NagisaKaworu;
using namespace MariMakinamiIllustious;
using namespace ShikinamiAsukaRangure;
constexpr int N=3e2+5;
constexpr int the_birthday_of_Asuka=20011204;
constexpr int the_birthday_of_Ayanami=20010330;
mt19937 Asuka(the_birthday_of_Asuka+time(0));
mt19937 Ayanami(the_birthday_of_Ayanami+time(0));
int n,m,k,tot=1;
int head[N],dp[N][N][2],cur[N][2];
//dp[i][j][k]表示对于i号节点为根的树,大头吃了j个果子,且根节点不是/是大头吃的最小难受值
//ans:dp[1][k][1]
struct edge{
    int to,val,next;
}e[N<<1];
inline void addedge(int u,int v,int w){
    tot++;
    e[tot].to=v;
    e[tot].val=w;
    e[tot].next=head[u];
    head[u]=tot;
}
void dfs(int u,int f){
    dp[u][0][0]=dp[u][1][1]=0;
    for(int i=head[u];i;i=e[i].next){
        int v=e[i].to,w=e[i].val;
        if(v==f) continue;
        dfs(v,u);
        for(int j=0;j<=n;j++){
            for(int s=0;s<2;s++){
                cur[j][s]=dp[u][j][s];
                dp[u][j][s]=ll_inf;
            }
        }
        for(int j=0;j<=k;j++){
            for(int s=0;s<=j;s++){
                dp[u][j][0]=min(dp[u][j][0],min(dp[v][s][0]+cur[j-s][0]+(m==2)*w,dp[v][s][1]+cur[j-s][0]));
                dp[u][j][1]=min(dp[u][j][1],min(dp[v][s][1]+cur[j-s][1]+w,dp[v][s][0]+cur[j-s][1]));
            }
        }
    }
}
bool ENDM;
signed main(){
    n=read(),m=read(),k=read();
    for(int i=1;i<n;i++){
        int u,v,w;u=read(),v=read(),w=read();
        addedge(u,v,w),addedge(v,u,w);
    }
    if(n-k<m-1) return write(-1),0;
    for(int i=0;i<=n;i++){
        for(int j=0;j<=n;j++){
            for(int s=0;s<2;s++){
                dp[i][j][s]=ll_inf;
            }
        }
    }
    dfs(1,0);
    write(dp[1][k][1]);
    CK_TM;
    return 0;
}

::::

dif:5.75/11

P10210/P10144

超级厉害的题。

如果你的思路被锁死在枚举这个二元组,那么很显然至少是 O(n^2) 级别的,况且还要乘上 check 函数里一堆不知道什么东西的东西,无疑是炸了。

进行思考查看题解,我们发现可以对每对相邻位置作分讨:

转化一下,就是:

发现这个东西有一些性质,可以做非法检验:

容易发现这题是神题。

::::info[code]

#include<bits/stdc++.h>
#define int long long
#define ll long long
#define ull unsigned long long
#define i128 __int128
#ifdef __linux__
//#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define CK_TM cerr<<'\n'<<1.0*fabs(&ENDM-&STARTM)/(1024*1024)<<"MB"<<" "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms"<<'\n';
char buf[1<<25],*p1,*p2;
#define getchar() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin))?EOF:*p1++)
#define vector basic_string
#define pq priority_queue
#define mkpr make_pair
#define pii pair<int,int>
#define pb push_back
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define sz(h) (int)h.size()
#define popc(x) __builtin_popcount(x)
bool STARTM;
constexpr double eps=1e-10;
constexpr int i_inf=1e9;
constexpr ll ll_inf=1e18;
namespace AyanamiRei{
    inline int read(){
        int x=0,f=1;char c=getchar();
        while(c<'0'||c>'9') {if(c=='-') f^=1;c=getchar();}
        while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
        return f?x:~x+1;
    }
}namespace SoryuAsukaRangure{
    inline void write(int x){
        if(x<0) x=~x+1,putchar('-');
        if(x>9) write(x/10);
        return putchar(x%10^48),void();
    }
}namespace AyanamiYue{
    inline void spaceput(int x){
        SoryuAsukaRangure::write(x);putchar(' ');
    }
}namespace IkariYue{
    inline void output(int x){
        SoryuAsukaRangure::write(x);putchar('\n');
    }
}namespace IkariShinji{
    inline int max(int x,int y){
        return x>y?x:y;
    }
}namespace NagisaKaworu{
    inline int min(int x,int y){
        return x<y?x:y;
    }
}namespace MariMakinamiIllustious{
    inline double MAX(double x,double y){
        return x-y>eps?x:y;
    }
}namespace ShikinamiAsukaRangure{
    inline double MIN(double x,double y){
        return y-x>eps?x:y;
    }
}
using namespace std;
using namespace AyanamiRei;
using namespace SoryuAsukaRangure;
using namespace AyanamiYue;
using namespace IkariYue;
using namespace IkariShinji;
using namespace NagisaKaworu;
using namespace MariMakinamiIllustious;
using namespace ShikinamiAsukaRangure;
constexpr int N=5e5+5;
constexpr int LOGN=23; 
constexpr int the_birthday_of_Asuka=20011204;
constexpr int the_birthday_of_Ayanami=20010330;
mt19937 Asuka(the_birthday_of_Asuka+time(0));
mt19937 Ayanami(the_birthday_of_Ayanami+time(0));
int n;
int lg2[N],a[N],mx[LOGN][N],mn[LOGN][N];
inline int qmax(int l,int r){
    int k=lg2[r-l+1];
    return max(mx[k][l],mx[k][r-(1<<k)+1]);
}
inline int qmin(int l,int r){
    int k=lg2[r-l+1];
    return min(mn[k][l],mn[k][r-(1<<k)+1]);
}
bool ENDM;
signed main(){
    for(int i=2;i<N;i++) lg2[i]=lg2[i/2]+1;
    n=read();
    for(int i=1;i<=n;i++) a[i]=read();
    for(int i=2;i<n;i++){
        mx[0][i]=-ll_inf;
        mn[0][i]=ll_inf;
        if(a[i]>=a[i-1]&&a[i]>=a[i+1]) mx[0][i]=a[i]+min(a[i-1],a[i+1]);
        if(a[i]<=a[i-1]&&a[i]<=a[i+1]) mn[0][i]=a[i]+max(a[i-1],a[i+1]);
    }
    for(int j=1;(1<<j)<=n;j++){
        for(int i=1;i+(1<<j)-1<=n;i++){
            mx[j][i]=max(mx[j-1][i],mx[j-1][i+(1<<(j-1))]);
            mn[j][i]=min(mn[j-1][i],mn[j-1][i+(1<<(j-1))]);
        }
    }
    int ans=0;
    for(int i=1;i<n;i++){
        int l=i+1,r=n,pos=0;
        while(l<=r){
            int mid=(l+r)>>1;
            if(mid-i<=1||qmax(i+1,mid-1)<qmin(i+1,mid-1)) pos=mid,l=mid+1;
            else r=mid-1;
        }
        ans+=pos-i;
    }
    output(ans);
    CK_TM;
    return 0;
}

::::

dif:7.5/11

P1441

不会 bitset,预处理后随便 dp 就切了。

dif:-1/11

P14593/CF1601D

做过的最牛逼的思维题。

::::info[提示] :::info[按什么排序?] 按 \max(a_i,b_i) 升序排序,再逐渐按 a_i,b_i 升序。 ::: :::success[证明:] 如果 \max(a_i,b_i)>\max(a_{i+1},b_{i+1}),则可能:

- $b_i>a_{i+1}$:交换一定不会少打,可能会多打

- $a_i>b_{i+1}$:无影响

::: :::info[fun fact] 发现这个东西就是邻项交换(exchange argument) ::: ::::

dif:7.75/11

2026.11.22

CF Div 2

惊天一车区,其中我是最傻逼的。

赛时的无效思考/假算太多了,我是奶龙。

A

注意到奇数一定赢,偶数只有存在着 u,v \in \N,u\mod 2 = 0,v\mod 2=0 时一定赢。

B

第 i 个元素的取值下标集合为 [i,n-k+i],只要判区间内所有元素是否均相等即可。

C

直接找以每个点向左向右能扩展到的最远区间。

D

总和永远不变,合法状况为 s_1 \ge 0,\forall i \in [1,n],s_i \ge s_{i-1}。

注意到每次操作相当于交换相邻两项前缀和,则合法答案数等价于逆序对数。

E

对每个节点维护用堆子树内可能胜利的最大编号,每次比较当前点和另一个子树相同层节点集合,然后启发式合并即可。

2026.11.23

P15567

转移: $$ dp_{i,j,k,0}=\sum_{p=1}^{n}dp_{p,j,k-1,1}-dp_{i,j,k-1,1} $$ $$ dp_{i,j,k,1}=\sum_{p=1}^{n}dp_{i,p,k-1,0}-dp_{i,j,k-1,1} $$ 这个东西可以前缀和优化。 现在考虑算重的情况:从左上角开始跳一个矩形后回来。 对于第 $i$ 行和第 $j$ 行,记两行均为 $1$ 的位置数量为 $s$,则非法矩形为 $s \times (s-1) \times 4$。这个求 $s$ 的过程直接 bitset 优化即可。 复杂度 $O(nm+\frac{n^2m}{ω})$。 ::::info[code] ```cpp #include<bits/stdc++.h> #define int long long #define ll long long #define ull unsigned long long #define i128 __int128 #ifdef __linux__ //#define getchar getchar_unlocked #define putchar putchar_unlocked #endif #define CK_TM cerr<<'\n'<<1.0*fabs(&ENDM-&STARTM)/(1024*1024)<<"MB"<<" "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms"<<'\n'; //char buf[1<<25],*p1,*p2; //#define getchar() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin))?EOF:*p1++) #define vector basic_string #define pq priority_queue #define mkpr make_pair #define pii pair<int,int> #define pb push_back #define umap unordered_map #define uset unordered_set #define vi vector<int> #define sz(h) (int)h.size() #define popc(x) __builtin_popcount(x) bool STARTM; constexpr double eps=1e-10; constexpr int i_inf=1e9; constexpr ll ll_inf=1e18; namespace AyanamiRei{ inline int read(){ int x=0,f=1;char c=getchar(); while(c<'0'||c>'9') {if(c=='-') f^=1;c=getchar();} while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();} return f?x:~x+1; } }namespace SoryuAsukaRangure{ inline void write(i128 x){ if(x<0) x=~x+1,putchar('-'); if(x>9) write(x/10); return putchar(x%10^48),void(); } }namespace AyanamiYue{ inline void spaceput(i128 x){ SoryuAsukaRangure::write(x);putchar(' '); } }namespace IkariYue{ inline void output(i128 x){ SoryuAsukaRangure::write(x);putchar('\n'); } }namespace IkariShinji{ inline int max(int x,int y){ return x>y?x:y; } }namespace NagisaKaworu{ inline int min(int x,int y){ return x<y?x:y; } }namespace MariMakinamiIllustious{ inline double MAX(double x,double y){ return x-y>eps?x:y; } }namespace ShikinamiAsukaRangure{ inline double MIN(double x,double y){ return y-x>eps?x:y; } } using namespace std; using namespace AyanamiRei; using namespace SoryuAsukaRangure; using namespace AyanamiYue; using namespace IkariYue; using namespace IkariShinji; using namespace NagisaKaworu; using namespace MariMakinamiIllustious; using namespace ShikinamiAsukaRangure; constexpr int N=2e3+5; constexpr int the_birthday_of_Asuka=20011204; constexpr int the_birthday_of_Ayanami=20010330; mt19937 Asuka(the_birthday_of_Asuka+time(0)); mt19937 Ayanami(the_birthday_of_Ayanami+time(0)); int n,m; i128 ans; int dp[N][N][6][2];//dp[i][j][k][0/1]表示当前位于(i,j),已经走了k步,上一次跳的是横/竖的方案数 i128 s[2][N];//s[0/1][i]为第i行/第i列dp数组的前缀和 char mp[N][N]; bitset<N> vis[N]; bool ENDM; signed main(){ ios::sync_with_stdio(0); cin.tie(0),cout.tie(0); cin>>n>>m; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin>>mp[i][j]; vis[i][j]=mp[i][j]-'0'; if(!vis[i][j]) continue; s[0][i]++,s[1][j]++; dp[i][j][1][0]=dp[i][j][1][1]=1; } } for(int k=2;k<=5;k++){ for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(!vis[i][j]) continue; dp[i][j][k][1]=s[0][i]-dp[i][j][k-1][0]; dp[i][j][k][0]=s[1][j]-dp[i][j][k-1][1]; } } for(int i=1;i<=n;i++) s[0][i]=0; for(int i=1;i<=m;i++) s[1][i]=0; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(!vis[i][j]) continue; s[0][i]+=dp[i][j][k][0]; s[1][j]+=dp[i][j][k][1]; } } } for(int i=1;i<=n;i++) ans+=s[0][i]; for(int i=1;i<=m;i++) ans+=s[1][i]; for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++){ i128 c=(vis[i]&vis[j]).count(); ans-=(c-1)*c*4; } } output(ans); CK_TM; return 0; } ``` :::: dif:5/11 ### [P7263](https://www.luogu.com.cn/problem/P7263) ::::info[fun fact] 一开始把这题看错了,以为是 Catalan 板子。 :::: 实际上确实是板子。 这个程序是把一个不合法位置到它后面第一个合法的位置去 reverse,那么可能被转化成目标的情况,就是将左括号看成 $1$,右括号看成 $-1$ 后,前缀和为 $0$ 的位置数量 $ct$ 即为合法数。 又已知原序列的数量为 $\binom{2n}{n}$。 答案即为 $\frac{ct}{\binom{2n}{n}}$。 dif:2/11 ### [P6162](https://www.luogu.com.cn/problem/P6162) 朴素 dp: ```cpp //dp[i][j]表示当前第i个数,前面填了j个数的方案数 //转移:当前不填:dp[i][j]=dp[i-1][j] // 当前填:dp[i][j]=dp[i-1][j-1]*(i-j+1) ``` 发现这个东西长得是[第二类 stirling 数递推式](https://oi-wiki.org/math/combinatorics/stirling/#%E9%80%92%E6%8E%A8%E5%BC%8F),然后用[通项公式](https://oi-wiki.org/math/combinatorics/stirling/#%E9%80%9A%E9%A1%B9%E5%85%AC%E5%BC%8F)计算即可。 ::::info[code] ```cpp #include<bits/stdc++.h> #define int long long #define ll long long #define ull unsigned long long #define i128 __int128 #ifdef __linux__ //#define getchar getchar_unlocked #define putchar putchar_unlocked #endif #define CK_TM cerr<<'\n'<<1.0*fabs(&ENDM-&STARTM)/(1024*1024)<<"MB"<<" "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms"<<'\n'; //char buf[1<<25],*p1,*p2; //#define getchar() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin))?EOF:*p1++) #define vector basic_string #define pq priority_queue #define mkpr make_pair #define pii pair<int,int> #define pb push_back #define umap unordered_map #define uset unordered_set #define vi vector<int> #define sz(h) (int)h.size() #define popc(x) __builtin_popcount(x) bool STARTM; constexpr double eps=1e-10; constexpr int i_inf=1e9; constexpr ll ll_inf=1e18; namespace AyanamiRei{ inline int read(){ int x=0,f=1;char c=getchar(); while(c<'0'||c>'9') {if(c=='-') f^=1;c=getchar();} while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();} return f?x:~x+1; } }namespace SoryuAsukaRangure{ inline void write(int x){ if(x<0) x=~x+1,putchar('-'); if(x>9) write(x/10); return putchar(x%10^48),void(); } }namespace AyanamiYue{ inline void spaceput(int x){ SoryuAsukaRangure::write(x);putchar(' '); } }namespace IkariYue{ inline void output(int x){ SoryuAsukaRangure::write(x);putchar('\n'); } }namespace IkariShinji{ inline int max(int x,int y){ return x>y?x:y; } }namespace NagisaKaworu{ inline int min(int x,int y){ return x<y?x:y; } }namespace MariMakinamiIllustious{ inline double MAX(double x,double y){ return x-y>eps?x:y; } }namespace ShikinamiAsukaRangure{ inline double MIN(double x,double y){ return y-x>eps?x:y; } } using namespace std; using namespace AyanamiRei; using namespace SoryuAsukaRangure; using namespace AyanamiYue; using namespace IkariYue; using namespace IkariShinji; using namespace NagisaKaworu; using namespace MariMakinamiIllustious; using namespace ShikinamiAsukaRangure; constexpr int N=1e6+5; constexpr int mod=998244353; constexpr int the_birthday_of_Asuka=20011204; constexpr int the_birthday_of_Ayanami=20010330; mt19937 Asuka(the_birthday_of_Asuka+time(0)); mt19937 Ayanami(the_birthday_of_Ayanami+time(0)); int n,k; //int dp[N][N]; //dp[i][j]表示当前第i个数,前面填了j个数的方案数 /* 转移:当前不填:dp[i][j]=dp[i-1][j] 当前填:dp[i][j]=dp[i-1][j-1]*(i-j+1) …… 这是第二类stirling数 直接求一个S2(n,n-k) */ int m,ans; int fac[N],ifac[N]; inline int qpow(int x,int y){ int res=1; while(y){ if(y&1) res=res*x%mod; x=x*x%mod,y>>=1; } return res%mod; } bool ENDM; signed main(){ // dp[1][0]=dp[1][1]=1; // n=read()-1,k=read(); // for(int i=2;i<=n;i++){ // for(int j=0;j<=i;j++){ // dp[i][j]=dp[i-1][j]; // if(j) dp[i][j]=(dp[i][j]+dp[i-1][j-1]*(i-j+1)%mod)%mod; // } // } // write(dp[n][k]%mod); n=read(),k=read();m=n-k; fac[0]=1; for(int i=1;i<=n;i++) fac[i]=fac[i-1]*i%mod; ifac[n]=qpow(fac[n],mod-2); for(int i=n-1;i>=0;i--) ifac[i]=ifac[i+1]*(i+1)%mod; for(int i=0;i<=m;i++){ int X=1; if((m-i)&1) X=-1; ans=(ans+X*qpow(i,n)%mod*ifac[i]%mod*ifac[m-i]%mod+mod)%mod; } write(ans%mod); CK_TM; return 0; } ``` :::: dif:4/11 ### [P8725](https://www.luogu.com.cn/problem/P8725) $dp_{i,j}$ 表示当前在第 $i$ 秒,还剩 $j$ 体力的方案数。 $dp_{i,j}=dp_{i-1,j}+dp_{i-1,j}$,转移条件是 $nowd \le (i-(m-j))-(m-j)$。 dif:1.5/11 ### [P1758](https://www.luogu.com.cn/problem/P1758) 火力肘击王。 第一眼看到这个 $\sum_i a_i^2$ 直接傻了,完全没有任何的有效思考。 然后瞄了眼题解,惊人地发现它等价于: ::::info[VERY AD-HOC] 两个人在两个独立的装置里取球,输出队列相同的方案数。 :::: 然后我们设 $dp_{i,j,k}$ 表示两个人都取了 $i$ 个球,第一个人在第一行取了 $j$ 个球,第二个人在第一行取了 $k$ 个球的方案数。 转移如下: ![image.png](https://cdn.luogu.com.cn/upload/image_hosting/2vznnazi.webp) $ans = dp_{n+m,n,n}$,记得用滚动数组。 ::::info[code] ```cpp #include<bits/stdc++.h> #define int long long #define ll long long #define ull unsigned long long #define i128 __int128 #ifdef __linux__ //#define getchar getchar_unlocked #define putchar putchar_unlocked #endif #define CK_TM cerr<<'\n'<<1.0*fabs(&ENDM-&STARTM)/(1024*1024)<<"MB"<<" "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms"<<'\n'; //char buf[1<<25],*p1,*p2; //#define getchar() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin))?EOF:*p1++) #define vector basic_string #define pq priority_queue #define mkpr make_pair #define pii pair<int,int> #define pb push_back #define umap unordered_map #define uset unordered_set #define vi vector<int> #define sz(h) (int)h.size() #define popc(x) __builtin_popcount(x) bool STARTM; constexpr double eps=1e-10; constexpr int i_inf=1e9; constexpr ll ll_inf=1e18; namespace AyanamiRei{ inline int read(){ int x=0,f=1;char c=getchar(); while(c<'0'||c>'9') {if(c=='-') f^=1;c=getchar();} while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();} return f?x:~x+1; } }namespace SoryuAsukaRangure{ inline void write(int x){ if(x<0) x=~x+1,putchar('-'); if(x>9) write(x/10); return putchar(x%10^48),void(); } }namespace AyanamiYue{ inline void spaceput(int x){ SoryuAsukaRangure::write(x);putchar(' '); } }namespace IkariYue{ inline void output(int x){ SoryuAsukaRangure::write(x);putchar('\n'); } }namespace IkariShinji{ inline int max(int x,int y){ return x>y?x:y; } }namespace NagisaKaworu{ inline int min(int x,int y){ return x<y?x:y; } }namespace MariMakinamiIllustious{ inline double MAX(double x,double y){ return x-y>eps?x:y; } }namespace ShikinamiAsukaRangure{ inline double MIN(double x,double y){ return y-x>eps?x:y; } } using namespace std; using namespace AyanamiRei; using namespace SoryuAsukaRangure; using namespace AyanamiYue; using namespace IkariYue; using namespace IkariShinji; using namespace NagisaKaworu; using namespace MariMakinamiIllustious; using namespace ShikinamiAsukaRangure; constexpr int N=5e2+5; constexpr int mod=1024523; constexpr int the_birthday_of_Asuka=20011204; constexpr int the_birthday_of_Ayanami=20010330; mt19937 Asuka(the_birthday_of_Asuka+time(0)); mt19937 Ayanami(the_birthday_of_Ayanami+time(0)); int n,m; int dp[2][N][N]; //dp[i][j][k]表示两个人加起来取了i个球,第一个人第一行取了j个,第二个人第一行取了k个 char a[N],b[N]; bool ENDM; signed main(){ ios::sync_with_stdio(0); cin.tie(0),cout.tie(0); cin>>n>>m; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<=m;i++) cin>>b[i]; reverse(a+1,a+n+1),reverse(b+1,b+m+1); dp[0][0][0]=1; for(int i=1;i<=n+m;i++){ int u=i&1; for(int j=0;j<=n;j++){ for(int k=0;k<=n;k++){ dp[u][j][k]=0; } } for(int j=0;j<=n;j++){ for(int k=0;k<=n;k++){ if(j&&k&&a[j]==a[k]) dp[u][j][k]=(dp[u][j][k]+dp[u^1][j-1][k-1])%mod; if(j&&i>k&&a[j]==b[i-k]) dp[u][j][k]=(dp[u][j][k]+dp[u^1][j-1][k])%mod; if(k&&i>j&&a[k]==b[i-j]) dp[u][j][k]=(dp[u][j][k]+dp[u^1][j][k-1])%mod; if(i>j&&i>k&&b[i-j]==b[i-k]) dp[u][j][k]=(dp[u][j][k]+dp[u^1][j][k])%mod; } } } write(dp[(n+m)&1][n][n]); CK_TM; return 0; } ``` :::: dif:-/11 任何用难度衡量这题的行为,都是对人类智慧的亵渎。 ### [T188453 十二重计数法 O(nm)](https://www.luogu.com.cn/problem/T188453) #### Q $\text{I}$:球之间互不相同,盒子之间互不相同。 $\text{II}$:球之间互不相同,盒子之间互不相同,每个盒子至多装一个球。 $\text{III}$:球之间互不相同,盒子之间互不相同,每个盒子至少装一个球。 $\text{IV}$:球之间互不相同,盒子全部相同。 $\text{V}$:球之间互不相同,盒子全部相同,每个盒子至多装一个球。 $\text{VI}$:球之间互不相同,盒子全部相同,每个盒子至少装一个球。 $\text{VII}$:球全部相同,盒子之间互不相同。 $\text{VIII}$:球全部相同,盒子之间互不相同,每个盒子至多装一个球。 $\text{IX}$:球全部相同,盒子之间互不相同,每个盒子至少装一个球。 $\text{X}$:球全部相同,盒子全部相同。 $\text{XI}$:球全部相同,盒子全部相同,每个盒子至多装一个球。 $\text{XII}$:球全部相同,盒子全部相同,每个盒子至少装一个球。 #### A 1.$m^n$。 2.每个盒子找当前没有放过球的盒子,答案为 $\frac{n!}{(n-m)!}$。 3.容斥,设有 $i$ 个盒子是空盒,则答案为: $$ \sum_{i=0}^{m}(-1)^i \binom{m}{i} (m-i)^n $$ 4.第二类斯特林数 $S(n,m)$ 表示 $n$ 个不同的元素划分成 $m$ 个集合的方案数。 $$ ans=\sum_{i=1}^{m} S(n,i) $$ 5.$[n \le m]$。 6.$S(n,m)$。 7.插板法,$\binom{n+m-1}{m-1}$。 8.$\binom{m}{n}

9.先给每个位置塞一个球后插板法,\binom{n-1}{m-1}。

10.划分数 p_{n,m} 表示将 n 划分成 m 个自然数之和的方案数。

递推式:

p_{i,j}=p_{i,j-1}+p_{i-j,j}

答案即为 p_{n,m}。

11.[n \le m]。

12.先在每个盒子中都放一个球,然后转成问题 10,答案为 p_{n-m,m}。

dif:不知道。

2026.11.24

P5748

题目里相当于求一个:

\sum_{i=0}^{n}S(n,i)

发现这个东西至少是 O(Tn^2) 或 O(n^3) 的,过不了。

由于我不想用多项式科技,于是我尝试把它优化到 T(n),但是失败了。

查看题解,发现了一个很厉害的 O(Tn) 做法,贴一下:

O(Tn)

::::info[code]

#include<bits/stdc++.h>
//#define int long long
#define ll long long
#define ull unsigned long long
#define i128 __int128
#ifdef __linux__
//#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define CK_TM cerr<<'\n'<<1.0*fabs(&ENDM-&STARTM)/(1024*1024)<<"MB"<<" "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms"<<'\n';
char buf[1<<25],*p1,*p2;
#define getchar() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin))?EOF:*p1++)
#define vector basic_string
#define pq priority_queue
#define mkpr make_pair
#define pii pair<int,int>
#define pb push_back
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define sz(h) (int)h.size()
#define popc(x) __builtin_popcount(x)
bool STARTM;
constexpr double eps=1e-10;
constexpr int i_inf=1e9;
constexpr ll ll_inf=1e18;
namespace AyanamiRei{
    inline int read(){
        int x=0,f=1;char c=getchar();
        while(c<'0'||c>'9') {if(c=='-') f^=1;c=getchar();}
        while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
        return f?x:~x+1;
    }
}namespace SoryuAsukaRangure{
    inline void write(int x){
        if(x<0) x=~x+1,putchar('-');
        if(x>9) write(x/10);
        return putchar(x%10^48),void();
    }
}namespace AyanamiYue{
    inline void spaceput(int x){
        SoryuAsukaRangure::write(x);putchar(' ');
    }
}namespace IkariYue{
    inline void output(int x){
        SoryuAsukaRangure::write(x);putchar('\n');
    }
}namespace IkariShinji{
    inline int max(int x,int y){
        return x>y?x:y;
    }
}namespace NagisaKaworu{
    inline int min(int x,int y){
        return x<y?x:y;
    }
}namespace MariMakinamiIllustious{
    inline double MAX(double x,double y){
        return x-y>eps?x:y;
    }
}namespace ShikinamiAsukaRangure{
    inline double MIN(double x,double y){
        return y-x>eps?x:y;
    }
}
using namespace std;
using namespace AyanamiRei;
using namespace SoryuAsukaRangure;
using namespace AyanamiYue;
using namespace IkariYue;
using namespace IkariShinji;
using namespace NagisaKaworu;
using namespace MariMakinamiIllustious;
using namespace ShikinamiAsukaRangure;
constexpr int N=1e6+5;
constexpr int mod=998244353;
constexpr int the_birthday_of_Asuka=20011204;
constexpr int the_birthday_of_Ayanami=20010330;
mt19937 Asuka(the_birthday_of_Asuka+time(0));
mt19937 Ayanami(the_birthday_of_Ayanami+time(0));
int tot,n;
int fac[N],ifac[N],prime[N],S[N];
bitset<N> vis;
inline int qpow(int x,int y){
    int res=1;
    while(y){
        if(y&1) res=1ll*res*x%mod;
        x=1ll*x*x%mod,y>>=1;
    }
    return res%mod;
}
inline void init(){
    fac[0]=ifac[0]=1;
    for(int i=1;i<N;i++) fac[i]=1ll*fac[i-1]*i%mod;
    ifac[N-1]=qpow(fac[N-1],mod-2);
    for(int i=N-2;i>=1;i--) ifac[i]=1ll*ifac[i+1]*(i+1)%mod;
    for(int i=2;i<N;i++){
        if(!vis[i]) prime[++tot]=i;
        for(int j=1;j<=tot&&1ll*i*prime[j]<N;j++){
            vis[i*prime[j]]=1;
            if(i%prime[j]==0) break;
        }
    }
}
bool ENDM;
signed main(){
    init();
    int T;T=read();
    while(T--){
        n=read();
        S[0]=0,S[1]=1;
        for(int i=2;i<=n;i++){
            if(!vis[i]) S[i]=qpow(i,n);
            for(int j=1;j<=tot&&prime[j]<=i&&1ll*i*prime[j]<=n;j++){
                S[i*prime[j]]=1ll*S[i]*S[prime[j]]%mod;
                if(i%prime[j]==0) break;
            }
        }
        int ans=0,sum=0;
        for(int i=n;i>=0;i--){
            int X=((n-i)&1?-1:1);
            sum=(sum+1ll*X*ifac[n-i]%mod+mod)%mod;
            ans=(ans+1ll*sum%mod*S[i]%mod*ifac[i]%mod+mod)%mod;
        }
        output((ans+mod)%mod);
    }
    CK_TM;
    return 0;
}

::::

dif:5.75/11

P8118

一道关联性极其强需要大眼观察的智力题。

我们发现这题和 P2893 超级像,唯一的区别就是有了这个 k 的限制。

考虑这么处理:我们直接令 a_i \leftarrow a_i-i \times k,这样就变成了上面那个题。

考虑上面那个题(仅单调不降)的做法怎么做:我们有一个 O(n \log n) 的神仙做法:

从左到右,把当前位置的数放进大根堆,然后比较这个数和堆顶的大小:

乍一眼这个东西是神经病,于是我们尝试证明:

以下内容来自 deepseek:

::::info[prof]

证明

记原数组为 A,调整后的数组为 B。题目中的 k 限制等价于

B_{i+1}-B_i\ge k.

令

X_i=A_i-i k,\qquad C_i=B_i-i k.

则

B_{i+1}-B_i\ge k \iff B_{i+1}-(i+1)k\ge B_i-i k \iff C_{i+1}\ge C_i.

也就是说,对 B 的限制恰好变成了对 C 的“单调不降”限制。同时修改代价不变:

|A_i-B_i| =|A_i-i k-(B_i-i k)| =|X_i-C_i|.

因此原问题完全等价于:把序列 X_i=A_i-i k 修改成单调不降序列 C_i,最小化 \sum |X_i-C_i|。这就是 P2893 的做法。

下面证明大根堆贪心的正确性。

引理: 对任意序列 X_1,X_2,\dots,X_m,维护一个大根堆 H,初始为空,代价 cost=0。从左到右处理:

  1. 把 X_i 放入 H;
  2. 若 X_i< H_{\max},则令 cost\leftarrow cost+(H_{\max}-X_i),

    弹出堆顶,再把 X_i 放入堆中。

处理完前 i 个数后,cost 就是

\min_{Y_1\le Y_2\le\cdots\le Y_i} \sum_{j=1}^i |X_j-Y_j|

的最小值。

证明:

设

f_i(t)=\min_{\substack{Y_1\le\cdots\le Y_{i-1}\le Y_i=t}} \sum_{j=1}^i |X_j-Y_j|.

即 f_i(t) 表示处理完前 i 个数、并且强制第 i 个调整后的值等于 t 时的最小代价。

由于绝对值函数是凸函数,且“前缀非降”约束是线性约束,所以 f_i(t) 是关于 t 的分段线性凸函数。它可以用它的“拐点”来描述。大根堆 H 中维护的正是当前 f_i 的左侧拐点集合。

初始时没有元素,H=\varnothing,cost=0。

考虑从 f_{i-1} 推到 f_i。加入 X_i 时,相当于在原来的凸函数上叠加一个绝对值函数 |X_i-t|。此时有两种情况:

归纳可知,处理完前 i 个数后,堆中维护的拐点集合正确,且 cost 恰好等于 f_i 的最小值,也就是前 i 个数调整为非降序列的最小代价。引理得证。

回到本题。代码中先把

a_i\leftarrow a_i-i k,

然后对变换后的序列执行上述堆贪心。每次产生的增量

q_{\max}-a_i

累加到 ans_i 中,最后再做前缀和:

for(int i=1;i<=n;i++) ans[i]+=ans[i-1];

因此 ans_i 就是前 i 个数变成满足限制的序列所需要的最小代价。

所以算法正确。复杂度为

O(n\log n).
这样证明部分就完整了:先说明 a_i\leftarrow a_i-i k 的转化为什么合法,再用凸函数/拐点说明大根堆贪心为什么能得到最小代价,最后说明代码里的前缀和输出对应每个前缀的答案。

::::info[code]

#include<bits/stdc++.h>
#define int long long
#define ll long long
#define ull unsigned long long
#define i128 __int128
#ifdef __linux__
//#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define CK_TM cerr<<'\n'<<1.0*fabs(&ENDM-&STARTM)/(1024*1024)<<"MB"<<" "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms"<<'\n';
char buf[1<<25],*p1,*p2;
#define getchar() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin))?EOF:*p1++)
#define vector basic_string
#define pq priority_queue
#define mkpr make_pair
#define pii pair<int,int>
#define pb push_back
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define sz(h) (int)h.size()
#define popc(x) __builtin_popcount(x)
bool STARTM;
constexpr double eps=1e-10;
constexpr int i_inf=1e9;
constexpr ll ll_inf=1e18;
namespace AyanamiRei{
    inline int read(){
        int x=0,f=1;char c=getchar();
        while(c<'0'||c>'9') {if(c=='-') f^=1;c=getchar();}
        while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
        return f?x:~x+1;
    }
}namespace SoryuAsukaRangure{
    inline void write(int x){
        if(x<0) x=~x+1,putchar('-');
        if(x>9) write(x/10);
        return putchar(x%10^48),void();
    }
}namespace AyanamiYue{
    inline void spaceput(int x){
        SoryuAsukaRangure::write(x);putchar(' ');
    }
}namespace IkariYue{
    inline void output(int x){
        SoryuAsukaRangure::write(x);putchar('\n');
    }
}namespace IkariShinji{
    inline int max(int x,int y){
        return x>y?x:y;
    }
}namespace NagisaKaworu{
    inline int min(int x,int y){
        return x<y?x:y;
    }
}namespace MariMakinamiIllustious{
    inline double MAX(double x,double y){
        return x-y>eps?x:y;
    }
}namespace ShikinamiAsukaRangure{
    inline double MIN(double x,double y){
        return y-x>eps?x:y;
    }
}
using namespace std;
using namespace AyanamiRei;
using namespace SoryuAsukaRangure;
using namespace AyanamiYue;
using namespace IkariYue;
using namespace IkariShinji;
using namespace NagisaKaworu;
using namespace MariMakinamiIllustious;
using namespace ShikinamiAsukaRangure;
constexpr int N=1e6+5;
constexpr int the_birthday_of_Asuka=20011204;
constexpr int the_birthday_of_Ayanami=20010330;
mt19937 Asuka(the_birthday_of_Asuka+time(0));
mt19937 Ayanami(the_birthday_of_Ayanami+time(0));
int n,k,T;
int a[N],ans[N];
pq<int> q;
bool ENDM;
signed main(){
    n=read(),k=read();
    for(int i=1;i<=n;i++) a[i]=read()-i*k;
    T=read();
    for(int i=1;i<=n;i++){
        q.push(a[i]);
        if(a[i]<q.top()){
            ans[i]+=q.top()-a[i];
            q.pop();q.push(a[i]);
        }
    }
    for(int i=1;i<=n;i++) ans[i]+=ans[i-1];
    if(!T) for(int i=1;i<=n;i++) output(ans[i]);    
    else write(ans[n]);
    CK_TM;
    return 0;
}

::::

dif:7.5/11

P6715

这 dp 好强,目测不输于这题。

发现判定性问题是可做的,但直接构造方案二分答案来判定的话,由于内部人的分布会影响决策,因此这样思考是没有前途的。

注意到一个非常重要的转化:我们从左向右考虑问题,如果一个人能从 >i 的位置来到 i,视作直接把这个入放在 i,问题转化为没有任何一个人能向左,然后就砍掉了一边的情况,即 dp 无后效性。

设 dp_{i,j} 表示第 i 个位置放了 j 个人时,前 i 个位置加起来最多能放多少个人。

转移有:

::::info[code]

#include<bits/stdc++.h>
#define int long long
#define ll long long
#define ull unsigned long long
#define i128 __int128
#ifdef __linux__
//#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define CK_TM cerr<<'\n'<<1.0*fabs(&ENDM-&STARTM)/(1024*1024)<<"MB"<<" "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms"<<'\n';
char buf[1<<25],*p1,*p2;
#define getchar() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin))?EOF:*p1++)
#define vector basic_string
#define pq priority_queue
#define mkpr make_pair
#define pii pair<int,int>
#define pb push_back
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define sz(h) (int)h.size()
#define popc(x) __builtin_popcount(x)
bool STARTM;
constexpr double eps=1e-10;
constexpr int i_inf=1e9;
constexpr ll ll_inf=1e18;
namespace AyanamiRei{
    inline int read(){
        int x=0,f=1;char c=getchar();
        while(c<'0'||c>'9') {if(c=='-') f^=1;c=getchar();}
        while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
        return f?x:~x+1;
    }
}namespace SoryuAsukaRangure{
    inline void write(int x){
        if(x<0) x=~x+1,putchar('-');
        if(x>9) write(x/10);
        return putchar(x%10^48),void();
    }
}namespace AyanamiYue{
    inline void spaceput(int x){
        SoryuAsukaRangure::write(x);putchar(' ');
    }
}namespace IkariYue{
    inline void output(int x){
        SoryuAsukaRangure::write(x);putchar('\n');
    }
}namespace IkariShinji{
    inline int max(int x,int y){
        return x>y?x:y;
    }
}namespace NagisaKaworu{
    inline int min(int x,int y){
        return x<y?x:y;
    }
}namespace MariMakinamiIllustious{
    inline double MAX(double x,double y){
        return x-y>eps?x:y;
    }
}namespace ShikinamiAsukaRangure{
    inline double MIN(double x,double y){
        return y-x>eps?x:y;
    }
}
using namespace std;
using namespace AyanamiRei;
using namespace SoryuAsukaRangure;
using namespace AyanamiYue;
using namespace IkariYue;
using namespace IkariShinji;
using namespace NagisaKaworu;
using namespace MariMakinamiIllustious;
using namespace ShikinamiAsukaRangure;
constexpr int N=1e3+5;
constexpr int M=1e4+5;
constexpr int the_birthday_of_Asuka=20011204;
constexpr int the_birthday_of_Ayanami=20010330;
mt19937 Asuka(the_birthday_of_Asuka+time(0));
mt19937 Ayanami(the_birthday_of_Ayanami+time(0));
int n,m,k,ans;
int a[N],b[N],dp[N][M<<2];
bool ENDM;
signed main(){
    n=read(),k=read();m=k;
    for(int i=1;i<n;i++){
        a[i]=read(),b[i]=read();
        m=max(m,a[i]+b[i]);
    }
    for(int i=0;i<k;i++) dp[1][i]=i;
    for(int i=1;i<n;i++){
        int mx=-ll_inf;
        for(int j=0;j<m;j++){
            if(j>=a[i]+b[i]) dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
            else if(j>=a[i]) dp[i+1][j-a[i]]=max(dp[i+1][j-a[i]],dp[i][j]);
            else mx=max(mx,dp[i][j]),dp[i+1][j+b[i]]=max(dp[i+1][j+b[i]],dp[i][j]+b[i]);
        }
        for(int j=0;j<b[i];j++) dp[i+1][j]=max(dp[i+1][j],mx+j);
    }
    for(int i=0;i<m;i++) ans=max(ans,dp[n][i]);
    write(ans);
    CK_TM;
    return 0;
}

::::

dif:7.25/11

P4137

自从会用 bitset 以后,碰见有 mex 的 ds 题我就没有不是一遍 AC 的,轮椅真强。

dif:3.5/11

2026.11.25

ZR Day 20

我有失智症。

又是传奇挂分大赛。

和绿红鱼加起来挂了将近 250pts,梦回去年在 ZR 和火魔以及宋叫人加起来挂 800pts 的时光。

发现自己的细节处理有点粗,很多东西要么想多要么想少,导致流失了很多时间。

希望 noip 能把挂的分加回来。

::::info[但是]

火魔真的强

T1

赛时失智。

这个东西可以先做一遍前缀和,然后操作的含义就进行了修改:

1.全局 -1。

2/3/4.选择一个位置 +1/-1。

我们无非要让操作 1 出现的次数小:然后这个东西等价于数轴上有一些点,你需要选出一个位置,使得这个位置到所有点的距离和最小。

直接取 sa_i-sb_i 的中位数即可,用 sort 或 nth_element 可做到 O(n \log n) 或 O(n)。

::::info[code]

#include<bits/stdc++.h>
#define int long long
#define ll long long
#define ull unsigned long long
#define i128 __int128
#ifdef __linux__
//#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define CK_TM cerr<<'\n'<<1.0*fabs(&ENDM-&STARTM)/(1024*1024)<<"MB"<<" "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms"<<'\n';
char buf[1<<25],*p1,*p2;
#define getchar() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin))?EOF:*p1++)
#define vector basic_string
#define pq priority_queue
#define mkpr make_pair
#define pii pair<int,int>
#define pb push_back
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define sz(h) (int)h.size()
#define popc(x) __builtin_popcount(x)
bool STARTM;
constexpr double eps=1e-10;
constexpr int i_inf=1e9;
constexpr ll ll_inf=1e18;
namespace AyanamiRei{
    inline int read(){
        int x=0,f=1;char c=getchar();
        while(c<'0'||c>'9') {if(c=='-') f^=1;c=getchar();}
        while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
        return f?x:~x+1;
    }
}namespace SoryuAsukaRangure{
    inline void write(int x){
        if(x<0) x=~x+1,putchar('-');
        if(x>9) write(x/10);
        return putchar(x%10^48),void();
    }
}namespace AyanamiYue{
    inline void spaceput(int x){
        SoryuAsukaRangure::write(x);putchar(' ');
    }
}namespace IkariYue{
    inline void output(int x){
        SoryuAsukaRangure::write(x);putchar('\n');
    }
}namespace IkariShinji{
    inline int max(int x,int y){
        return x>y?x:y;
    }
}namespace NagisaKaworu{
    inline int min(int x,int y){
        return x<y?x:y;
    }
}namespace MariMakinamiIllustious{
    inline double MAX(double x,double y){
        return x-y>eps?x:y;
    }
}namespace ShikinamiAsukaRangure{
    inline double MIN(double x,double y){
        return y-x>eps?x:y;
    }
}
using namespace std;
using namespace AyanamiRei;
using namespace SoryuAsukaRangure;
using namespace AyanamiYue;
using namespace IkariYue;
using namespace IkariShinji;
using namespace NagisaKaworu;
using namespace MariMakinamiIllustious;
using namespace ShikinamiAsukaRangure;
constexpr int N=2e5+5;
constexpr int the_birthday_of_Asuka=20011204;
constexpr int the_birthday_of_Ayanami=20010330;
mt19937 Asuka(the_birthday_of_Asuka+time(0));
mt19937 Ayanami(the_birthday_of_Ayanami+time(0));
int n;
int a[N],b[N],c[N];
bool ENDM;
signed main(){
    int T,Case;T=read(),Case=read();
    while(T--){
        n=read();
        for(int i=1;i<=n;i++) a[i]=read()+a[i-1];
        for(int i=1;i<=n;i++) b[i]=read()+b[i-1];
        for(int i=1;i<=n;i++) c[i]=a[i]-b[i];
        c[n+1]=0;int mid=n/2+1,ori=c[n];
        nth_element(c+1,c+mid,c+n+1+1);
        int ans=0,ave=max(0ll,max(c[mid],ori));
        for(int i=1;i<=n+1;i++) ans+=abs(ave-c[i]);
        output(ans);
    } 
    CK_TM;
    return 0;
}

::::

dif:4/11

T2

这个连边方式真的是创造性的。

赛场上想出了不可能到后代再坐地铁回到祖先的性质,但是建图完全是智力缺陷的患者都干不出的行为,用了 smt+odt 优化建图,直接被 ZR 评测机把 DSC 甩得到处都是。

事实证明我的图论真是一坨希望 noip 不要考

不过这个 n \le 2 \times 10^6,我直接朴素堆优化的 dijkstra 也能蠕动过。

又因为 fread 开小调了一会,我联赛用它我直接吃

::::info[code]

#include<bits/stdc++.h>
#define int long long
#define ll long long
#define ull unsigned long long
#define i128 __int128
#ifdef __linux__
//#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define CK_TM cerr<<'\n'<<1.0*fabs(&ENDM-&STARTM)/(1024*1024)<<"MB"<<" "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms"<<'\n';
//char buf[1<<25],*p1,*p2;
//#define getchar() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin))?EOF:*p1++)
#define vector basic_string
#define pq priority_queue
#define mkpr make_pair
#define pii pair<int,int>
#define pb push_back
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define sz(h) (int)h.size()
#define popc(x) __builtin_popcount(x)
bool STARTM;
constexpr double eps=1e-10;
constexpr int i_inf=1e9;
constexpr ll ll_inf=1e18;
namespace AyanamiRei{
    inline int read(){
        int x=0,f=1;char c=getchar();
        while(c<'0'||c>'9') {if(c=='-') f^=1;c=getchar();}
        while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
        return f?x:~x+1;
    }
}namespace SoryuAsukaRangure{
    inline void write(int x){
        if(x<0) x=~x+1,putchar('-');
        if(x>9) write(x/10);
        return putchar(x%10^48),void();
    }
}namespace AyanamiYue{
    inline void spaceput(int x){
        SoryuAsukaRangure::write(x);putchar(' ');
    }
}namespace IkariYue{
    inline void output(int x){
        SoryuAsukaRangure::write(x);putchar('\n');
    }
}namespace IkariShinji{
    inline int max(int x,int y){
        return x>y?x:y;
    }
}namespace NagisaKaworu{
    inline int min(int x,int y){
        return x<y?x:y;
    }
}namespace MariMakinamiIllustious{
    inline double MAX(double x,double y){
        return x-y>eps?x:y;
    }
}namespace ShikinamiAsukaRangure{
    inline double MIN(double x,double y){
        return y-x>eps?x:y;
    }
}
using namespace std;
using namespace AyanamiRei;
using namespace SoryuAsukaRangure;
using namespace AyanamiYue;
using namespace IkariYue;
using namespace IkariShinji;
using namespace NagisaKaworu;
using namespace MariMakinamiIllustious;
using namespace ShikinamiAsukaRangure;
constexpr int N=2e6+5;
constexpr int the_birthday_of_Asuka=20011204;
constexpr int the_birthday_of_Ayanami=20010330;
mt19937 Asuka(the_birthday_of_Asuka+time(0));
mt19937 Ayanami(the_birthday_of_Ayanami+time(0));
int n,m,a,b,tot;
int dep[N],dis[N],head[N];
bitset<N> vis;
vector<signed> g[N];
pair<signed,signed> r[N];
struct edge{
    signed to,next;
    int val;
}e[N*6];
struct node{
    int idx,w;
    inline friend bool operator < (node A,node B){
        return A.w>B.w;
    }
};pq<node> q;
void dfs(int u,int f){
    dep[u]=dep[f]+1;
    for(auto v:g[u]){
        if(v==f) continue;
        dfs(v,u);
    }
}
inline void addedge(int u,int v,int w){
    tot++;
    e[tot].to=v;
    e[tot].val=w;
    e[tot].next=head[u];
    head[u]=tot;
}
inline void dijkstra(int s){
    vis.reset();
    for(int i=1;i<=n;i++) dis[i]=ll_inf;
    while(!q.empty()) q.pop();
    dis[s]=0;q.push({s,dis[s]});
    while(!q.empty()){
        int u=q.top().idx;q.pop();
        if(vis[u]) continue;
        vis.set(u);
        for(int i=head[u];i;i=e[i].next){
            int v=e[i].to,w=e[i].val;
            if(dis[v]>dis[u]+w){
                dis[v]=dis[u]+w;
                q.push({v,dis[v]});
            }
        }
    }
    for(int i=1;i<=n;i++) spaceput(dis[i]);
    putchar('\n');
}
inline void CCC(){
    tot=0;
    for(int i=0;i<=n+1;i++){
        dep[i]=head[i]=0;r[i]=mkpr(0,0);
        g[i].clear();g[i].shrink_to_fit();
    }
}
bool ENDM;
signed main(){
    int T,Case;T=read(),Case=read();
    while(T--){
        n=read(),m=read(),a=read(),b=read();
        CCC();
        for(int i=1;i<n;i++){
            int u,v;u=read(),v=read();
            g[u].pb(v),g[v].pb(u);
            r[i]=mkpr(u,v);
        }
        dfs(1,0);
        for(int i=1;i<n;i++){
            int x=r[i].first,y=r[i].second;
            if(dep[x]>dep[y]) swap(x,y);
            addedge(x,y,a),addedge(y,x,0);
        }
        for(int i=1;i<=m;i++){
            int u,v;u=read(),v=read();
            addedge(u,v,2*b);
        }
        dijkstra(1);
    }
    CK_TM;
    return 0;
}

::::

dif:5.75/11

T3

我不会 NTT,别搞。

有空写个 O(n^2)。

不对这题根本不是题。

更严重的问题来了:我声称我不会二项式反演。

发了个讨论版,得到了一些解答。

大概是将恰好转成至少之后作容斥并合并等价类。

然后我不会……咋办

可能明天练计数的时候订正吧。

T4

好题。

首先证明一个性质:如果某个区间内有 ka 个 0 和 kb 个 1,则这个区间一定是可消去的。

::::info[prof] 比如有一个序列:

其中 a=b=1。

当前要取这一段:

发现在长度为 L 的小窗口从左向右滑动时,总存在一对 (l,r),r-l+1=L,且区间内出现 a 个 0 和 b 个 1。证明这样的情况一定存在是显然的。

然后把这个区间删掉,两端合并后就变成了一个 k-1 子问题。

因此一定可行。

::::

下设 L=a+b。

先将 0 的权值看成 b,再将 1 的权值看成 -a,则区间可消去等价于区间和为 0。而 ? 的位置可以任意选。

维护 mx_i 和 mn_i 表示到 i 的最大前缀和与最小前缀和,这个东西是通过给 ? 赋不同的值弄出来的。

然后我们可以设 dp_i 表示前 i 个位置最多可以执行几次这样的消去操作,转移方程为:

dp_i=\max_{mn_{i-kL} \le 0 \le mx_{i-kL}}(dp_{i-kL}+k)

贴个 O(n^2) 的码:

::::info[O(n^2)]

#include<bits/stdc++.h>
#define int long long
#define ll long long
#define ull unsigned long long
#define i128 __int128
#ifdef __linux__
//#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define CK_TM cerr<<'\n'<<1.0*fabs(&ENDM-&STARTM)/(1024*1024)<<"MB"<<" "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms"<<'\n';
//char buf[1<<25],*p1,*p2;
//#define getchar() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin))?EOF:*p1++)
#define vector basic_string
#define pq priority_queue
#define mkpr make_pair
#define pii pair<int,int>
#define pb push_back
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define sz(h) (int)h.size()
#define popc(x) __builtin_popcount(x)
bool STARTM;
constexpr double eps=1e-10;
constexpr int i_inf=1e9;
constexpr ll ll_inf=1e18;
namespace AyanamiRei{
    inline int read(){
        int x=0,f=1;char c=getchar();
        while(c<'0'||c>'9') {if(c=='-') f^=1;c=getchar();}
        while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
        return f?x:~x+1;
    }
}namespace SoryuAsukaRangure{
    inline void write(int x){
        if(x<0) x=~x+1,putchar('-');
        if(x>9) write(x/10);
        return putchar(x%10^48),void();
    }
}namespace AyanamiYue{
    inline void spaceput(int x){
        SoryuAsukaRangure::write(x);putchar(' ');
    }
}namespace IkariYue{
    inline void output(int x){
        SoryuAsukaRangure::write(x);putchar('\n');
    }
}namespace IkariShinji{
    inline int max(int x,int y){
        return x>y?x:y;
    }
}namespace NagisaKaworu{
    inline int min(int x,int y){
        return x<y?x:y;
    }
}namespace MariMakinamiIllustious{
    inline double MAX(double x,double y){
        return x-y>eps?x:y;
    }
}namespace ShikinamiAsukaRangure{
    inline double MIN(double x,double y){
        return y-x>eps?x:y;
    }
}
using namespace std;
using namespace AyanamiRei;
using namespace SoryuAsukaRangure;
using namespace AyanamiYue;
using namespace IkariYue;
using namespace IkariShinji;
using namespace NagisaKaworu;
using namespace MariMakinamiIllustious;
using namespace ShikinamiAsukaRangure;
constexpr int N=2e6+5;
constexpr int the_birthday_of_Asuka=20011204;
constexpr int the_birthday_of_Ayanami=20010330;
mt19937 Asuka(the_birthday_of_Asuka+time(0));
mt19937 Ayanami(the_birthday_of_Ayanami+time(0));
int n,a,b,L;
int mx[N],mn[N],dp[N];
//前缀最大值/最小值 
//dp[i]表示前i个位置最多能消几次 
char c[N];
bool ENDM;
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int T,Case;cin>>T>>Case;
    while(T--){
        cin>>n>>a>>b;L=a+b;
        for(int i=1;i<=n;i++) dp[i]=mx[i]=mn[i]=0;
        for(int i=1;i<=n;i++){
            cin>>c[i];
            mx[i]=mx[i-1],mn[i]=mn[i-1];
            if(c[i]!='?'){
                if(c[i]=='0') mx[i]+=b,mn[i]+=b;
                else mx[i]-=a,mn[i]-=a;
            }
            else mx[i]+=b,mn[i]-=a;
        } 
        for(int i=L;i<=n;i++){
            dp[i]=dp[i-1]; 
            int k=i/L;
            for(int j=1;j<=k;j++){
                int p=i-j*L+1;
                if(mn[i]-mn[p-1]<=0&&mx[i]-mx[p-1]>=0) dp[i]=max(dp[i],dp[p-1]+j);
            }
        }
        output(n-L*dp[n]);
    }
    CK_TM;
    return 0;
}

::::

思考这个东西怎样优化:

我们发现 dp 可以转移的情况,当且仅当 [mn_{i-kL},mx_{i-kL}] \subset [mn_i,mx_i]。

然后问题就变成了:维护一个 ds,其可以插入,删除区间,并查询区间的包含关系。

set(odt),动态开点值域线段树,平衡树啥的都能写,是单老哥的。

当然如果你没有维护区间而是暴力维护等价类 \max 转移的话,这个东西会变成双老哥的二维偏序问题,CDQ 和 bitset 应该都可以写。

由于线段数量是 n \log n 级别,且每条线段至多被插入和删除各一次,则复杂度是 O(n \log n) 的。

贴下 O(n \log n) 的 set 做法因为姜老师已经写了线段树写法,大家快去%%%。

::::info[O(n \log n) set]

#include<bits/stdc++.h>
#define int long long
#define ll long long
#define ull unsigned long long
#define i128 __int128
#ifdef __linux__
//#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define CK_TM cerr<<'\n'<<1.0*fabs(&ENDM-&STARTM)/(1024*1024)<<"MB"<<" "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms"<<'\n';
//char buf[1<<25],*p1,*p2;
//#define getchar() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin))?EOF:*p1++)
#define vector basic_string
#define pq priority_queue
#define mkpr make_pair
#define pii pair<int,int>
#define pb push_back
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define sz(h) (int)h.size()
#define popc(x) __builtin_popcount(x)
bool STARTM;
constexpr double eps=1e-10;
constexpr int i_inf=1e9;
constexpr ll ll_inf=1e18;
namespace AyanamiRei{
    inline int read(){
        int x=0,f=1;char c=getchar();
        while(c<'0'||c>'9') {if(c=='-') f^=1;c=getchar();}
        while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
        return f?x:~x+1;
    }
}namespace SoryuAsukaRangure{
    inline void write(int x){
        if(x<0) x=~x+1,putchar('-');
        if(x>9) write(x/10);
        return putchar(x%10^48),void();
    }
}namespace AyanamiYue{
    inline void spaceput(int x){
        SoryuAsukaRangure::write(x);putchar(' ');
    }
}namespace IkariYue{
    inline void output(int x){
        SoryuAsukaRangure::write(x);putchar('\n');
    }
}namespace IkariShinji{
    inline int max(int x,int y){
        return x>y?x:y;
    }
}namespace NagisaKaworu{
    inline int min(int x,int y){
        return x<y?x:y;
    }
}namespace MariMakinamiIllustious{
    inline double MAX(double x,double y){
        return x-y>eps?x:y;
    }
}namespace ShikinamiAsukaRangure{
    inline double MIN(double x,double y){
        return y-x>eps?x:y;
    }
}
using namespace std;
using namespace AyanamiRei;
using namespace SoryuAsukaRangure;
using namespace AyanamiYue;
using namespace IkariYue;
using namespace IkariShinji;
using namespace NagisaKaworu;
using namespace MariMakinamiIllustious;
using namespace ShikinamiAsukaRangure;
constexpr int N=2e6+5;
constexpr int the_birthday_of_Asuka=20011204;
constexpr int the_birthday_of_Ayanami=20010330;
mt19937 Asuka(the_birthday_of_Asuka+time(0));
mt19937 Ayanami(the_birthday_of_Ayanami+time(0));
int n,a,b;
int mx[N],mn[N],dp[N];
//前缀最大值/最小值 
//dp[i]表示前i个位置最多能消去的长度 
char c[N];
set<pii> s[N];
#define IT set<pii>::iterator 
inline int cov(int id,int l,int r){
    if(id<0) return 0;
    IT it=s[id].lower_bound(mkpr(l,-ll_inf));
    if(it!=s[id].end()&&it->second<=r) return 1;
    return 0;
}
inline void add(int id,int l,int r){
    if(id<0) return;
    IT it=s[id].lower_bound(mkpr(l,-ll_inf));
    if(it!=s[id].end()&&it->second<=r) return;
    while(it!=s[id].begin()){
        IT pos=prev(it);
        if(pos->second<r) break;
        s[id].erase(pos);
    }
    s[id].insert(mkpr(l,r));
}
bool ENDM;
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int T,Case;cin>>T>>Case;s[0].insert(mkpr(0,0));
    while(T--){
        cin>>n>>a>>b;
        for(int i=1;i<=n;i++) dp[i]=mx[i]=mn[i]=0;
        for(int i=1;i<=n;i++){
            cin>>c[i];
            mx[i]=mx[i-1],mn[i]=mn[i-1];
            if(c[i]!='?'){
                if(c[i]=='0') mx[i]+=b,mn[i]+=b;
                else mx[i]-=a,mn[i]-=a;
            }
            else mx[i]+=b,mn[i]-=a;
        }
        for(int i=1;i<=n;i++){
            dp[i]=dp[i-1];
            if(cov(i-dp[i]-a-b,mn[i],mx[i])) dp[i]+=a+b;
            add(i-dp[i],mn[i],mx[i]);
        }
        output(n-dp[n]);
        for(int i=1;i<=n;i++) s[i].clear();
    }
    CK_TM;
    return 0;
}

::::

dif:7.5/11

P3674

我在此宣称我是 lpzx bitset rank 1

bitset 板子题。

维护 Ema 和 Hiro,其中 Ema_i=Hiro_{V-i}。

1.等价于(Ema&(Ema<<x)).any()

2.等价于(Ema&(Hiro>>(V-x))).any()

3.直接暴力枚举因数 check

复杂度 O(\frac{n^2}{ω}+n \sqrt n)。

::::info[code]

#include<bits/stdc++.h>
#define int long long
#define ll long long
#define ull unsigned long long
#define i128 __int128
#ifdef __linux__
//#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define CK_TM cerr<<'\n'<<1.0*fabs(&ENDM-&STARTM)/(1024*1024)<<"MB"<<" "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms"<<'\n';
char buf[1<<25],*p1,*p2;
#define getchar() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin))?EOF:*p1++)
#define vector basic_string
#define pq priority_queue
#define mkpr make_pair
#define pii pair<int,int>
#define pb push_back
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define sz(h) (int)h.size()
#define popc(x) __builtin_popcount(x)
bool STARTM;
constexpr double eps=1e-10;
constexpr int i_inf=1e9;
constexpr ll ll_inf=1e18;
namespace AyanamiRei{
    inline int read(){
        int x=0,f=1;char c=getchar();
        while(c<'0'||c>'9') {if(c=='-') f^=1;c=getchar();}
        while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
        return f?x:~x+1;
    }
}namespace SoryuAsukaRangure{
    inline void write(int x){
        if(x<0) x=~x+1,putchar('-');
        if(x>9) write(x/10);
        return putchar(x%10^48),void();
    }
}namespace AyanamiYue{
    inline void spaceput(int x){
        SoryuAsukaRangure::write(x);putchar(' ');
    }
}namespace IkariYue{
    inline void output(int x){
        SoryuAsukaRangure::write(x);putchar('\n');
    }
}namespace IkariShinji{
    inline int max(int x,int y){
        return x>y?x:y;
    }
}namespace NagisaKaworu{
    inline int min(int x,int y){
        return x<y?x:y;
    }
}namespace MariMakinamiIllustious{
    inline double MAX(double x,double y){
        return x-y>eps?x:y;
    }
}namespace ShikinamiAsukaRangure{
    inline double MIN(double x,double y){
        return y-x>eps?x:y;
    }
}
using namespace std;
using namespace AyanamiRei;
using namespace SoryuAsukaRangure;
using namespace AyanamiYue;
using namespace IkariYue;
using namespace IkariShinji;
using namespace NagisaKaworu;
using namespace MariMakinamiIllustious;
using namespace ShikinamiAsukaRangure;
constexpr int N=1e5+5;
constexpr int V=1e5;
constexpr int the_birthday_of_Asuka=20011204;
constexpr int the_birthday_of_Ayanami=20010330;
mt19937 Asuka(the_birthday_of_Asuka+time(0));
mt19937 Ayanami(the_birthday_of_Ayanami+time(0));
int n,m,B;
int a[N],ans[N],ct[N];
bitset<N> Ema,Hiro; 
struct ask{
    int op,l,r,x,id;
    inline friend bool operator < (ask U,ask V){
        if(U.l/B!=V.l/B) return U.l<V.l;
        else if(U.l/B%2==1) return U.r<V.r;
        return U.r>V.r; 
    }
}q[N];
inline void add(int x){
    ct[x]++;
    if(ct[x]==1) Ema.set(x),Hiro.set(V-x);
}
inline void del(int x){
    ct[x]--;
    if(!ct[x]) Ema.reset(x),Hiro.reset(V-x);
}
bool ENDM;
signed main(){
    n=read(),m=read();B=sqrt(n);
    Ema.reset(),Hiro.reset();
    for(int i=1;i<=n;i++) a[i]=read();
    for(int i=1;i<=m;i++) q[i].op=read(),q[i].l=read(),q[i].r=read(),q[i].x=read(),q[i].id=i;
    sort(q+1,q+m+1);
    for(int i=1,l=1,r=0;i<=m;i++){
        while(l>q[i].l) add(a[--l]);
        while(r<q[i].r) add(a[++r]);
        while(l<q[i].l) del(a[l++]);
        while(r>q[i].r) del(a[r--]);
        if(q[i].op==1){
            int x=q[i].x;
            int flag=(Ema&(Ema<<x)).any();
            ans[q[i].id]=flag;
        }
        else if(q[i].op==2){
            int x=q[i].x;
            int flag=(Ema&(Hiro>>(V-x))).any();
//          if(x%2==0) flag|=Ema[x/2];
            ans[q[i].id]=flag;
        }
        else{
            int x=q[i].x,flag=0;
            for(int i=1;i<=sqrt(x);i++){
                if(x%i!=0) continue;
                if(Ema[i]&&Ema[x/i]){
                    flag=1;break;
                }
            }
            ans[q[i].id]=flag;
        }
    }
    for(int i=1;i<=m;i++){
        if(ans[i]) cout<<"hana"<<'\n';
        else cout<<"bi"<<'\n';
    }
    CK_TM;
    return 0;
}

::::

dif:4.5

2026.11.26

P5355

和 P3674 差不多,但是加了一个除法操作,难了一倍。

考虑根号分治:

视 n 与 V 同阶,复杂度为 O(n \sqrt n + \frac{n^2}{\omega})。

::::info[code]

#include<bits/stdc++.h>
#define int long long
#define ll long long
#define ull unsigned long long
#define i128 __int128
#ifdef __linux__
//#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define CK_TM cerr<<'\n'<<1.0*fabs(&ENDM-&STARTM)/(1024*1024)<<"MB"<<" "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms"<<'\n';
char buf[1<<25],*p1,*p2;
#define getchar() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin))?EOF:*p1++)
#define vector basic_string
#define pq priority_queue
#define mkpr make_pair
#define pii pair<int,int>
#define pb push_back
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define sz(h) (int)h.size()
#define popc(x) __builtin_popcount(x)
bool STARTM;
constexpr double eps=1e-10;
constexpr int i_inf=1e9;
constexpr ll ll_inf=1e18;
namespace AyanamiRei{
    inline int read(){
        int x=0,f=1;char c=getchar();
        while(c<'0'||c>'9') {if(c=='-') f^=1;c=getchar();}
        while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
        return f?x:~x+1;
    }
}namespace SoryuAsukaRangure{
    inline void write(int x){
        if(x<0) x=~x+1,putchar('-');
        if(x>9) write(x/10);
        return putchar(x%10^48),void();
    }
}namespace AyanamiYue{
    inline void spaceput(int x){
        SoryuAsukaRangure::write(x);putchar(' ');
    }
}namespace IkariYue{
    inline void output(int x){
        SoryuAsukaRangure::write(x);putchar('\n');
    }
}namespace IkariShinji{
    inline int max(int x,int y){
        return x>y?x:y;
    }
}namespace NagisaKaworu{
    inline int min(int x,int y){
        return x<y?x:y;
    }
}namespace MariMakinamiIllustious{
    inline double MAX(double x,double y){
        return x-y>eps?x:y;
    }
}namespace ShikinamiAsukaRangure{
    inline double MIN(double x,double y){
        return y-x>eps?x:y;
    }
}
using namespace std;
using namespace AyanamiRei;
using namespace SoryuAsukaRangure;
using namespace AyanamiYue;
using namespace IkariYue;
using namespace IkariShinji;
using namespace NagisaKaworu;
using namespace MariMakinamiIllustious;
using namespace ShikinamiAsukaRangure;
constexpr int N=1e5+5;
constexpr int V=1e5;
constexpr int the_birthday_of_Asuka=20011204;
constexpr int the_birthday_of_Ayanami=20010330;
mt19937 Asuka(the_birthday_of_Asuka+time(0));
mt19937 Ayanami(the_birthday_of_Ayanami+time(0));
int n,m,B;
int a[N],ans[N],ct[N],lst[N],res[N];
bitset<N> Ema,Hiro,Yuki; 
vi exe,eva[N];
struct ask{
    int op,l,r,x,id;
    inline friend bool operator < (ask U,ask V){
        if(U.l/B!=V.l/B) return U.l<V.l;
        else if(U.l/B%2==1) return U.r<V.r;
        return U.r>V.r; 
    }
}q[N];
inline void add(int x){
    ct[x]++;
    if(ct[x]==1) Ema.set(x),Hiro.set(V-x);
}
inline void del(int x){
    ct[x]--;
    if(!ct[x]) Ema.reset(x),Hiro.reset(V-x);
}
bool ENDM;
signed main(){
    n=read(),m=read();B=sqrt(n);
    Ema.reset(),Hiro.reset();
    for(int i=1;i<=n;i++) a[i]=read();
    for(int i=1;i<=m;i++) q[i].op=read(),q[i].l=read(),q[i].r=read(),q[i].x=read(),q[i].id=i;
    sort(q+1,q+m+1);
    for(int i=1,l=1,r=0;i<=m;i++){
        while(l>q[i].l) add(a[--l]);
        while(r<q[i].r) add(a[++r]);
        while(l<q[i].l) del(a[l++]);
        while(r>q[i].r) del(a[r--]);
        if(q[i].op==1){
            int x=q[i].x;
            int flag=(Ema&(Ema<<x)).any();
            ans[q[i].id]=flag;
        }
        else if(q[i].op==2){
            int x=q[i].x;
            int flag=(Ema&(Hiro>>(V-x))).any();
            ans[q[i].id]=flag;
        }
        else if(q[i].op==3){
            int x=q[i].x,flag=0;
            for(int i=1;i<=sqrt(x);i++){
                if(x%i!=0) continue;
                if(Ema[i]&&Ema[x/i]){
                    flag=1;break;
                }
            }
            ans[q[i].id]=flag;
        }
        else{
            int x=q[i].x;
            if(x>=B){
                int flag=0;
                for(int i=1;i*x<=V;i++){
                    if(Ema[i]&&Ema[i*x]){
                        flag=1;break;
                    }
                }
                ans[q[i].id]=flag;
            }
            else{//根号分治 
                if(!x){
                    ans[q[i].id]=0;continue;
                }
                eva[x].pb(i);
                if(!Yuki[x]) Yuki.set(x),exe.pb(x);
            }
        }
    }
    for(auto v:exe){
        for(int i=1;i<=V;i++) lst[i]=res[i]=-1;
        int mx=-1;
        for(int i=1;i<=n;i++){
            lst[a[i]]=i;
            if(a[i]*v<=V) mx=max(mx,lst[a[i]*v]);
            if(a[i]%v==0) mx=max(mx,lst[a[i]/v]);
            res[i]=mx;
        }
        for(auto i:eva[v]) ans[q[i].id]=(q[i].l<=res[q[i].r]);
    }
    for(int i=1;i<=m;i++){
        if(ans[i]) cout<<"yuno"<<'\n';
        else cout<<"yumi"<<'\n';
    }
    CK_TM;
    return 0;
}

::::

dif:5/11

P14015

我这辈子都忘不了这个 trick 了。

当要对 01 String 进行相邻同项操作时,可以考虑对奇(偶)数位取反,转化为相邻异项操作。

然后小小分讨,懒得打字了贴个结论:

dif:5.5/11

P17777

诈骗神题。

依旧期望的线性性发力:我们无需关心选择的顺序,只需要将每个人分别考虑后相加即可。

由于 E(XY)=E(X)+E(Y),答案即为 \sum_{i=1}^{n}\frac{1}{a_i}。

dif:4.75/11

P6163

好妙的贪心,太难了。

先将原来的矩形拆成一半,这样输出 ans \times 2 即可。

对于 x_i 升序排序后:

ans=\sum_{i=1}^{n}\sum_{j=i+1}^{n}|x_i-x_j|

考虑每个 i 被算了多少次:

ans=\sum_{i=1}^{\frac{n}{2}}|x_{n-i+1}-x_i| \times (n-2 \times i+1)

我们想要令绝对值(其实没必要)最小,则将左端点降序排序,右端点升序排序,贪心匹配即可。

::::info[code]

#include<bits/stdc++.h>
#define int long long
#define ll long long
#define ull unsigned long long
#define i128 __int128
#ifdef __linux__
//#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define CK_TM cerr<<'\n'<<1.0*fabs(&ENDM-&STARTM)/(1024*1024)<<"MB"<<" "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms"<<'\n';
char buf[1<<25],*p1,*p2;
#define getchar() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin))?EOF:*p1++)
#define vector basic_string
#define pq priority_queue
#define mkpr make_pair
#define pii pair<int,int>
#define pb push_back
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define sz(h) (int)h.size()
#define popc(x) __builtin_popcount(x)
bool STARTM;
constexpr double eps=1e-10;
constexpr int i_inf=1e9;
constexpr ll ll_inf=1e18;
namespace AyanamiRei{
    inline int read(){
        int x=0,f=1;char c=getchar();
        while(c<'0'||c>'9') {if(c=='-') f^=1;c=getchar();}
        while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
        return f?x:~x+1;
    }
}namespace SoryuAsukaRangure{
    inline void write(int x){
        if(x<0) x=~x+1,putchar('-');
        if(x>9) write(x/10);
        return putchar(x%10^48),void();
    }
}namespace AyanamiYue{
    inline void spaceput(int x){
        SoryuAsukaRangure::write(x);putchar(' ');
    }
}namespace IkariYue{
    inline void output(int x){
        SoryuAsukaRangure::write(x);putchar('\n');
    }
}namespace IkariShinji{
    inline int max(int x,int y){
        return x>y?x:y;
    }
}namespace NagisaKaworu{
    inline int min(int x,int y){
        return x<y?x:y;
    }
}namespace MariMakinamiIllustious{
    inline double MAX(double x,double y){
        return x-y>eps?x:y;
    }
}namespace ShikinamiAsukaRangure{
    inline double MIN(double x,double y){
        return y-x>eps?x:y;
    }
}
using namespace std;
using namespace AyanamiRei;
using namespace SoryuAsukaRangure;
using namespace AyanamiYue;
using namespace IkariYue;
using namespace IkariShinji;
using namespace NagisaKaworu;
using namespace MariMakinamiIllustious;
using namespace ShikinamiAsukaRangure;
constexpr int N=3e5+5;
constexpr int the_birthday_of_Asuka=20011204;
constexpr int the_birthday_of_Ayanami=20010330;
mt19937 Asuka(the_birthday_of_Asuka+time(0));
mt19937 Ayanami(the_birthday_of_Ayanami+time(0));
int n,ans;
int l[N],r[N];
inline int cmp(int Ema,int Hiro){
    return Ema>Hiro;
}
bool ENDM;
signed main(){
    n=read();
    for(int i=1;i<=n;i++) l[i]=read(),r[i]=read();
    sort(l+1,l+n+1,cmp);sort(r+1,r+n+1);
    for(int i=1;i<=n/2&&l[i]>r[i];i++) ans+=(n-2*i+1)*(l[i]-r[i]);
    write(2*ans);
    CK_TM;
    return 0;
}

::::

dif:5.75/11

P13364

这道题告诉我们两件事情:

1.打比赛有做法要敢去写

2.证明时思路要严谨

这个东西不独立,所以任何说所谓“长度为 k 的大置换环中,每步操作独立,期望为 k \times \frac{1}{k} \times k=k”之类的说法都是在放屁。

直接搬 Ri 的题解:

代码红,思维至少紫,建议评灰。

CF620E

写到【模板】树链剖分了。

可以用状压线段树或 ODT 直接过,注意 ODT 要加上剪枝才能过。