跳到正文

个人记录

一些 Trick

Bocchi_Offical约 11 分钟
原文

这是 2026年9月27日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年9月25日
收录
2026年9月27日
更新
2026年11月13日
篇幅
约 11 分钟

版本

  1. v2当前
  2. v1
  1. 求 kk 大子段和或者子段异或和

例题:

P2048 [NOI 2010] 超级钢琴 区间 kk 大子段和之和

P5283 [十二省联考 2019] 异或粽子 区间 kk 大异或和

对于子段和之和,我们可以利用前缀和,RMQ,堆等策略,刚开始我们可以对原数组求前缀和,或者根据题意求一些其他的东西,对 nn 个起始点分别将题目给定的范围进行一次 RMQ 查询(ST 表),将 kk 大的结果压入堆中,对于每个从 ii 起点开始的最优解,在得到答案的同时,也需要在最优解位置的左边和右边寻找可能存在的较优解,注意要判断区间的合法性。

对于求 kk 大的子段异或,可以考虑用可持久 Trie 树,其实逻辑都是类似的,刚开始先维护一个前缀异或,然后像上题的逻辑一样,先枚举起始点,利用 Trie 找最大的位置,然后在对于每一个最优解利用最优解的位置找较优解即可。

总而言之,kk 大子段和/子段异或都可以用这个做法。

P2048 std

此处 RMQ1 其实是多余的,但是我懒得把他去掉(

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
struct node{
    int mx,pl,l,r,x;
    bool operator<(const node A)const{
        return mx<A.mx;
    }
};
int n,k,l,r;
int a[1000005];
int sum[1000005];
int f[1000005][21];
int p[1000005][21];
priority_queue<node> q;
int logg[1000005];
int RMQ1(int ll,int rr)//获取最大值
{
    int lo=logg[rr-ll+1];
    return max(f[ll][lo],f[rr-(1<<lo)+1][lo]);
}
int RMQ2(int ll,int rr)//获取最大值的位置
{
    int lo=logg[rr-ll+1];
    if(f[ll][lo]>f[rr-(1<<lo)+1][lo])
    {
        return p[ll][lo];
    }
    else
    {
        return p[rr-(1<<lo)+1][lo];
    }
}
signed main()
{
    for(int i=2;i<=1000000;i++)
    {
        logg[i]=logg[i>>1]+1;
    }
    cin>>n>>k>>l>>r;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        sum[i]=sum[i-1]+a[i];
    }
    // 对前缀和数组维护一个 ST 表
    for(int i=1;i<=n;i++)
    {
        f[i][0]=sum[i];
        p[i][0]=i;
    }
    for(int j=1;(1<<j)<=n;j++)
    {
        for(int i=1;i+(1<<j)-1<=n;i++)
        {
            // 这边分类讨论获取最优解的位置
            // f[i][j]=max(f[i][j-1],f[i+(1<<j-1)][j-1])
            if(f[i][j-1]>f[i+(1<<(j-1))][j-1])
            {
                f[i][j]=f[i][j-1];
                p[i][j]=p[i][j-1];
            }
            else
            {
                f[i][j]=f[i+(1<<(j-1))][j-1];
                p[i][j]=p[i+(1<<(j-1))][j-1];
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(i+l-1>n) continue;
        q.push({RMQ1(min(i+l-1,n),min(i+r-1,n))-sum[i-1],RMQ2(min(i+l-1,n),min(i+r-1,n)),min(i+l-1,n),min(i+r-1,n),i});
    }
    // while(!q.empty())
    // {
    //     node t=q.top();
    //     cout<<t.mx<<" "<<t.pl<<" "<<t.l<<" "<<t.r<<" "<<t.x<<endl;
    //     q.pop();
    // }
    int ans=0;
    int cnt=0;
    while(!q.empty() && cnt<k)
    {
        node t=q.top();
        q.pop();
        ans+=t.mx;
        if(t.pl!=t.l) q.push({RMQ1(t.l,t.pl-1)-sum[t.x-1],RMQ2(t.l,t.pl-1),t.l,t.pl-1,t.x});
        if(t.pl!=t.r) q.push({RMQ1(t.pl+1,t.r)-sum[t.x-1],RMQ2(t.pl+1,t.r),t.pl+1,t.r,t.x});
        cnt++;
    }
    cout<<ans<<endl;
}

写于 2026/9/25 09:56

  1. 线性 DP 的优化

(i) 二分查找的优化

常见于一些和区间覆盖有关的题目。

例题:P1868 饥饿的奶牛

很容易能想到用线性 DP,但是直接打会听取 TLE 声一片。

所以我们可以对这些线段进行二分优化,先按照右端点排序,然后再找最大的 rr 使得与该区间不冲突的区间,最后转移 DP。这个操作可以很容易地用二分实现。

当然还有树状数组和线段树做法,但是太复杂了,二分够用了。

P1868 std
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
struct node{
    int l,r;  
}a[1000005];
int f[1000005];
bool cmp(node c,node d)
{
    return c.r<d.r;
}
int find(int x)
{
    int l=1,r=x-1;
    int ans=0;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(a[mid].r<a[x].l)
        {
            ans=mid;
            l=mid+1;
        }
        else
        {
            r=mid-1;
        }
    }
    return ans;
}
signed main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].l>>a[i].r;
    }
    sort(a+1,a+1+n,cmp);
    f[1]=a[1].r-a[1].l+1;

    for(int i=2;i<=n;i++)
    {
        f[i]=max(f[i-1],a[i].r-a[i].l+1);
        f[i]=max(f[i],f[find(i)]+a[i].r-a[i].l+1);
    }
    cout<<f[n]<<endl;
}

写于 2026/9/25 12:16

  1. 异或的一些性质。

参考题目:P9635 「yyOI R1」youyou 的异或

(i) 当且仅当 aa 为正整数,有:

a⊕a=0a \oplus a = 0

a⊕0=aa \oplus 0 = a

(ii) 当且仅当 aa 为正整数与奇数的时候,有:

a⊕1=a−1a \oplus 1 = a - 1

(iii) 当且仅当 aa 为正整数和偶数的时候,有:

a⊕(a+1)=1a \oplus (a+1) = 1

a⊕1=a+1a \oplus 1 = a+1

写于 2026/9/26 11:45

  1. 二维矩阵修改和求和

线段树可以,但是技巧太高级了,我不会。

可以考虑开个二维树状数组+二维差分

公式不推了,有点麻烦,但是得出这个答案之和四个和 Di,jD_{i,j} 有关的项有关。

参考:https://www.cnblogs.com/lindalee/p/11503503.html

P4514 std
C++
#include<bits/stdc++.h>
#define int long long
#define lowbit(x) x&(-x)
using namespace std;
const int N=(1<<11)+5;
int n,m;
int tr1[N][N];
int tr2[N][N];
int tr3[N][N];
int tr4[N][N];
void update(int x,int y,int d)
{
    for(int i=x;i<=n;i+=lowbit(i))
    {
        for(int j=y;j<=m;j+=lowbit(j))
        {
            tr1[i][j]+=d;
            tr2[i][j]+=x*d;
            tr3[i][j]+=y*d;
            tr4[i][j]+=x*y*d;
        }
    }
}
int query(int x,int y)
{
    int ans=0;
    for(int i=x;i>0;i-=lowbit(i))
    {
        for(int j=y;j>0;j-=lowbit(j))
        {
            ans+=(x+1)*(y+1)*tr1[i][j]-tr2[i][j]*(y+1)-tr3[i][j]*(x+1)+tr4[i][j];
        }
    }
    return ans;
}
signed main()
{
    char s;
    cin>>s;
    cin>>n>>m;
    while(cin>>s)
    {
        if(s=='L')
        {
            int a,b,c,d,e;
            cin>>a>>b>>c>>d>>e;
            update(a,b,e);
            update(c+1,d+1,e);
            update(a,d+1,-e);
            update(c+1,b,-e);
        }
        else
        {
            int a,b,c,d;
            cin>>a>>b>>c>>d;
            cout<<query(c,d)-query(c,b-1)-query(a-1,d)+query(a-1,b-1)<<endl;
        }
    }
}