跳到正文

题解

[Ynoi2014] 不归之人与望眼欲穿的人们 题解

TLEWA约 13 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 13 分钟
题目
P5065

版本

  1. v2当前
  2. v1

观前注意

本篇题解是乱搞题解,具有错误的复杂度。如果要学习正经的解题思路可以选择跳过本篇题解。

解题思路

Ynoi 就是应该乱搞过题嘛!顺带拿个最优解玩玩()

我们考虑一个朴素的 O(nqlog⁡V)O(nq \log V) 暴力做法:

我们 O(1)O(1) 进行 11 操作的暴力修改,对于 22 操作的询问,我们考虑一种的双指针做法:

枚举 ll 节点,记录所选区间每个位上出现的 11 的次数,符合条件的 rr 节点显然是单调递增的,收缩/扩展的复杂度是 O(log⁡2V)O(\log_2 V),单次复杂度 O(nlog⁡V)O(n \log V)。

但是,n,q≤50000n,q \le 50000,这个做法远远无法通过所有数据,考虑优化。

考虑优化扩展的过程,考虑压位。注意到无符号 short 类型的值域恰好大于 nn 的范围,并且值域为 2302^{30},我们用 16×30=48016 \times 30 = 480 个二进制位就可以存下双指针时每一位的桶,同时我们发现双指针的扩展和收缩操作都是简单的加减法,这启示我们使用指令集优化去改进代码的常数。

这个桶刚好可以装到一个 __m512i 中,于是我们应用 AVX512 指令集,将双指针所用的桶的操作进行优化:

C++
struct opt {
    __m512i t;
    long long p;
    opt():t(),p(0){};
    inline bool operator<(const opt& x)const{return p<x.p;}
    inline bool operator>(const opt& x)const{return p>x.p;}
    inline bool operator<=(const opt& x)const{return p<=x.p;}
    inline bool operator>=(const opt& x)const{return p>=x.p;}
    inline opt& operator+=(const opt x) {
        t=_mm512_add_epi16(t,x.t);
        p = _mm512_cmp_epi16_mask(t, _mm512_setzero_epi32(), _MM_CMPINT_GT);
        return *this;
    }
    inline opt& operator-=(const opt x) {
        t=_mm512_sub_epi16(t,x.t);
        p = _mm512_cmp_epi16_mask(t, _mm512_setzero_epi32(), _MM_CMPINT_GT);
        return *this;
    }
    inline opt& operator=(long long x) {
        p = x;
        t = _mm512_maskz_mov_epi16((__mmask32)(x & 0xFFFFFFFF), _mm512_set1_epi16(1));
        return *this;
    }
};

复杂度瞬间除以了一个二十多倍的常数,如果将使用 AVX512 指令集进行操作的复杂度近似看作 O(1)O(1),复杂度优化为 O(nq)O(nq)。

同时注意到这个 O(nq)O(nq) 常数还挺小的

这个时候 n,q=10000n,q=10000 的数据大概跑 300ms 左右?我们继续优化常数。

思考一件事情,如果出题人要卡掉和我们类似的一切暴力,会采取什么手段?

因为暴力进行 11 操作的复杂度非常低,同时 11 操作后询问依然足够强的操作序列不好寻找,所以出题人想要卡满我们,会尽量提升 22 操作的数量。

然后继续代入出题人视角,猜出题人数据大概怎么造的:

如果数据纯随机的话,那么有两种可能:

  1. 答案为 11 的询问特别多
  2. 答案为 −1-1 的询问特别多

我作为出题人,有没有什么办法尽量避免上面两种情况的出现呢?

一种容易想到的做法是,随机生成 11 操作之后暴力 dp 去找合法 22 操作的范围,然后在这个范围内随机询问。

注意到这样生成的数据,相同的答案应当特别多。同时我们发现这么生成数据,生成一次 11 操作的复杂度极高。已知 lxl 名下没有超算所以猜测 11 操作确实很少。又发现这个题目居然不强制在线所以我们将每个 11 操作之间的 22 操作离线后将答案记忆化然后特判掉 −1-1 的情况就可以极大减小常数。

然后呢?然后就过了!说明 lxl 确实没有偷偷拥有自己的大型计算集群的同时拿到了最优解第二名。

但是来都来了,抢个最优解再走吧!

考虑很难构造一个序列使得在这个序列的各部分选数都接近最优答案,这其中双指针就进行了大量无用的扫描,通过于是我们分块记录一下一个块内的所有元素或起来的桶,然后在双指针的时候如果发现一整块区间都无法更新最优答案那么就可以快速跳过一个块,从而大幅提升一次操作的时间效率,应用这个优化之后,轻松拿到了最优解(PS:其实这份提交的正确性有一定问题,请以总用时 730ms 那一份为准)。

AC 代码

C++
#include<bits/stdc++.h>

using namespace std;

const int N=50005,B=70;

namespace IO {

const int Read_buf=1<<20;
const int Write_buf=1<<20;

// using fread
#define INPUT_OPTIMIZE

// using fwrite
#define OUTPUT_OPTIMIZE

#ifdef INPUT_OPTIMIZE
	#ifdef __unix__
		#include<sys/mman.h>
		inline static const char *_read_ptr = (const char *)mmap(nullptr, 0x7fffffff, 1, 2, 0, 0);
		#define getchar() (*_read_ptr++)
	#else
	   	static char buf[Read_buf], *p1 = buf, *p2 = buf;
	    #define getchar() p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, Read_buf, stdin), p1 == p2) ? EOF : *p1++
	#endif
#endif

    inline bool read(char *t) {
        memset(t, 0, sizeof t);
        char *pd = t, c = getchar();
        while (isspace(c)) c = getchar();
        while (!isspace(c)) *pd++ = c, c = getchar();
        return c == EOF;
    }

    template <typename T>
    inline bool read(T &t) {
        t=0;T sgn=1;
        char c = getchar();
        while (!isdigit(c)) {if(c == '-')sgn *= -1;c = getchar();}
        while (isdigit(c)) t = (t << 3) + (t << 1) + (c ^ 48), c = getchar();
        t*=sgn;
        return c == EOF;
    }

    template <typename T, typename... Args>
    inline bool read(T &t, Args&... args) {
        return read(t) ? 1 : read(args...);
    }

#ifdef OUTPUT_OPTIMIZE
    static char outbuf[Write_buf], *out = outbuf;
    #define putchar(x) *out++ = x
    #define flush() fwrite(outbuf, 1, out - outbuf, stdout)
#else
   #define flush() 0
#endif

    template
    <typename T>
    inline
    void write(T x) {
        if (!x) return putchar('0'), void();
        static char t[9], p = 0;
        if(x<0){putchar('-');x=-x;}
        while (x) t[++p] = (x % 10) ^ 48, x /= 10;
        while (p) putchar(t[p--]);
    }
}
using namespace IO;

#pragma GCC target("avx","avx2","avx512f","avx512bw","sse")
#include <immintrin.h>
#include <emmintrin.h>

int n,q;
int arr[N];

struct opt {
    __m512i t;
    long long p;
    opt():t(),p(0){};
    inline bool operator<(const opt& x)const{return p<x.p;}
    inline bool operator>(const opt& x)const{return p>x.p;}
    inline bool operator<=(const opt& x)const{return p<=x.p;}
    inline bool operator>=(const opt& x)const{return p>=x.p;}
    inline opt& operator+=(const opt x) {
        t=_mm512_add_epi16(t,x.t);
        p = _mm512_cmp_epi16_mask(t, _mm512_setzero_epi32(), _MM_CMPINT_GT);
        return *this;
    }
    inline opt& operator-=(const opt x) {
        t=_mm512_sub_epi16(t,x.t);
        p = _mm512_cmp_epi16_mask(t, _mm512_setzero_epi32(), _MM_CMPINT_GT);
        return *this;
    }
    inline opt& operator=(long long x) {
        p = x;
        t = _mm512_maskz_mov_epi16((__mmask32)(x & 0xFFFFFFFF), _mm512_set1_epi16(1));
        return *this;
    }
}A[N],Blocks[N];

opt res,cnt,V;

long long fans=0,fsiz=0;
inline int calc(int& k) {
    if(fans>=k)
        return fsiz;
    if(cnt.p<k)
        return -1;

    long long l,r,ans;
    l=1,r=1,ans=n+1;
    res=opt({});
    while(l<=n) {
        while(r<=n&&res.p<k) {
            if(r%B==0 && (res.p|Blocks[r/B].p)<k) res+=Blocks[r/B],r+=B;
            else res+=A[r++];
        }
        if(r>n&&res.p<k) break;
        if(l%B==0 && l+B<=r) {
        	V=res;
        	while((V-=Blocks[l/B]).p==res.p) res=V,l+=B;
		}
        if(ans>r-l) {
            fsiz=ans=r-l;
            fans=res.p;
        }else if(ans==r-l) fans=max(fans,res.p);
		res-=A[l++];
    }

    return ans;
}

vector<pair<int,int>> qst;
inline void solve() {
    sort(qst.begin(),qst.end());
    for(auto& [val,id]:qst) {
        val=calc(val);
        swap(val,id);
    }
    sort(qst.begin(),qst.end());
    for(auto& [id,val]:qst) {
    	write(val);
    	putchar('\n');
	}
}

signed main() {
	read(n,q);
    for(int i=1;i<=n;++i) read(arr[i]);
    for(int i=1;i<=n;++i) A[i]=arr[i],cnt+=A[i],Blocks[i/B]+=A[i];

    long long op,p,x,k;
    for(int i=1;i<=q;++i) {
        read(op);
        if(op==1) {
            solve();qst.clear();
            read(p,x);
            cnt-=A[p],Blocks[p/B]-=A[p];
            arr[p]=x;
            A[p]=x;
            cnt+=A[p],Blocks[p/B]+=A[p];
            fans=0,fsiz=0;
        }else {
            read(k);
            qst.push_back({k,i});
        }
    }
    solve();
    flush();
}

理论时间复杂度 O(nqlog⁡V)O(nq \log V),实际效率超过目前一切其他做法,说明猜测出题人造数据的方法确实可以帮助乱搞过题。

原文