[Ynoi2014] 不归之人与望眼欲穿的人们 题解
观前注意
本篇题解是乱搞题解,具有错误的复杂度。如果要学习正经的解题思路可以选择跳过本篇题解。
解题思路
Ynoi 就是应该乱搞过题嘛!顺带拿个最优解玩玩()
我们考虑一个朴素的
我们
枚举
但是,
考虑优化扩展的过程,考虑压位。注意到无符号 short 类型的值域恰好大于
这个桶刚好可以装到一个 __m512i 中,于是我们应用 AVX512 指令集,将双指针所用的桶的操作进行优化:
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 指令集进行操作的复杂度近似看作
同时注意到这个
这个时候
思考一件事情,如果出题人要卡掉和我们类似的一切暴力,会采取什么手段?
因为暴力进行
然后继续代入出题人视角,猜出题人数据大概怎么造的:
如果数据纯随机的话,那么有两种可能:
- 答案为
1 的询问特别多 - 答案为
-1 的询问特别多
我作为出题人,有没有什么办法尽量避免上面两种情况的出现呢?
一种容易想到的做法是,随机生成
注意到这样生成的数据,相同的答案应当特别多。同时我们发现这么生成数据,生成一次
然后呢?然后就过了!说明 lxl 确实没有偷偷拥有自己的大型计算集群的同时拿到了最优解第二名。
但是来都来了,抢个最优解再走吧!
考虑很难构造一个序列使得在这个序列的各部分选数都接近最优答案,这其中双指针就进行了大量无用的扫描,通过于是我们分块记录一下一个块内的所有元素或起来的桶,然后在双指针的时候如果发现一整块区间都无法更新最优答案那么就可以快速跳过一个块,从而大幅提升一次操作的时间效率,应用这个优化之后,轻松拿到了最优解(PS:其实这份提交的正确性有一定问题,请以总用时 730ms 那一份为准)。
AC 代码
#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();
}
理论时间复杂度 说明猜测出题人造数据的方法确实可以帮助乱搞过题。