题解【P9069 [Ynoi Easy Round 2022] 堕天作战】

· · 题解

为啥倍增值域分块题解都没说怎么卡常啊,怎么只有我一个人人傻常数大啊。

lxl 讲的,记录一下。

Link

题目大意

给定一个长为 n 的序列 a_i,m 次操作,修改操作是将一个区间内所有 \neq x 的 a_i \leftarrow a_i-x,查询操作区间求和并对 2^{64} 取模,强制在线。

## 题目分析 首先仔细观察数据范围,发现 $x\geq 0$,这启发我们每次修改一定会将负数减掉,所以要将负数分开处理。 如何产生负数呢?假设当前要减去一个 $x$,那么当前区间内 $<x$ 的位置都会被减成负数,后续不需要单独考虑,不难发现每个数只会变成一次负数,维护区间和动态插入,我们维护一个动态开点线段树即可,维护一下当前区间的最小值,每次直接递归找到所有 $<x$ 的数暴力删除,并扔到负数的线段树上即可,这部分每次删除需要 $O(\log n)$ 的复杂度,所以是 $O(n\log n)$ 的。 所以不难发现问题转换成了将一个区间 $>x$ 的数减去 $x$,这里就要引出一个东西叫做倍增值域分块,其实就是一个二进制分组,具体的,将值域分成 $[0,0],[1,1],[2,3),[4,7)\dots$ 也就是按 $2^i$ 分成若干段,考虑对这每一段分别维护一个数据结构,在本题就是线段树。 这东西有什么用呢?你考虑 $>x$ 的数分布在 $x$ 所在块以及更大的块,我们先考虑更大的。 若一个数 $a_i$ 减掉 $x$ 后,二进制位减少了,那么我们就要修改其所在的块,这部分怎么处理呢?我们和维护负数一样,直接维护当前区间的最小值,若最小值减去 $x$ 后不在当前块,则递归找到并删去重构,这为什么是对的呢?你考虑一个数最多减少 $\log V$ 次二进制位,其中 $V$ 是值域,每次修改需要 $O(\log n)$ 的复杂度删除并插入,所以这部分复杂度是 $O(n\log n \log V)$ 的,所以正确。 而对于不满足上述条件的区间,直接做一个区间减得修改标记即可,因为最小值被修改后都没变,整个区间一定都不会变。 然后现在来考虑如何修改 $x$ 所在的块,不难发现这一块可以分成三部分,$<x$ 的直接暴力找扔到负数上,$=x$ 的区间直接特判掉不管,$>x$ 的区间只要减去 $x$ 就一定会减少二进制位,所以直接按最小值递归即可。 所以这两种情况是可以用同一个修改操作去写的。 查询直接枚举块求和即可。 ## Code 这部分其实才是最重要的。 你会发现这道题卡常,怎么办呢?首先加上快读是必要的,你得有个速度快的快读。 其次是一个比较抽象的优化,你考虑修改倍增值域分块的底数,建议找 $2$ 的正整数次幂,因为这样方便求最高位,然后按照这个底数去做,这里我使用的底数是 $16$,实测效果不错,然后对原序列按这个底数分块,线段树维护的时候,若当前区间长度 $\leq 16$,则不再递归直接暴力枚举,按照 lxl 的说法这样有利于缓存,其他题解虽然用了,但都没明说。 最后一定要注意处理好当前区间 $=x$ 的特判,维护最小值最大值即可,不然会超时调好久。 我的线段树因为写法特殊写的很长,凑合看吧。 ```cpp #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <cmath> #include <queue> #define unll unsigned long long using namespace std; namespace io { #define SIZE (1 << 20) char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr; inline void flush(void) { return fwrite(obuf, 1, oS - obuf, stdout), oS = obuf, void(); } inline char getch(void) { return (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); } inline void putch(char x) { *oS++ = x; if (oS == oT) flush(); return; } string getstr(void) { string s = ""; char c = getch(); while (c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == EOF) c = getch(); while (!(c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == EOF)) s.push_back(c), c = getch(); return s; } void putstr(string str, int begin = 0, int end = -1) { if (end == -1) end = str.size(); for (register int i = begin; i < end; i++) putch(str[i]); return; } template <typename T> inline T read() { register T x = 0; for (f = 1, c = getch(); c < '0' || c > '9'; c = getch()) if (c == '-') f = -1; for (x = 0; c <= '9' && c >= '0'; c = getch()) x = x * 10 + (c & 15); return x * f; } template <typename T> inline void write(const T& t) { register T x = t; if (!x) putch('0'); if (x < 0) putch('-'), x = -x; while (x) qu[++qr] = x % 10 + '0', x /= 10; while (qr) putch(qu[qr--]); return; } struct Flusher_ { ~Flusher_() { flush(); } } io_flusher_; } // namespace io using io::getch; using io::getstr; using io::putch; using io::putstr; using io::read; using io::write; const int N = 5e5+5; int n,m; int rt[50];long long L[N],R[N],now; long long a[N]; int getbit(long long x){ if(x<=0){ return 0; }else{ return __lg(x)/4+1; } } struct aa{ int lc,rc,siz; long long mi,mx,tag; unll val; void clear(){ lc = rc = siz = 0; mi = mx = tag = 0; val = 0; } }node[N*30]; int sta[N*10],top,tot,B=16; struct seg{ void pushup(int u){ if(!node[u].lc){ node[u].mi = node[node[u].rc].mi;node[u].mx = node[node[u].rc].mx; node[u].val = node[node[u].rc].val;node[u].siz = node[node[u].rc].siz; }else if(!node[u].rc){ node[u].mi = node[node[u].lc].mi;node[u].mx = node[node[u].lc].mx; node[u].val = node[node[u].lc].val;node[u].siz = node[node[u].lc].siz; }else{ node[u].mi = min(node[node[u].lc].mi,node[node[u].rc].mi);node[u].mx = max(node[node[u].lc].mx,node[node[u].rc].mx); node[u].val = node[node[u].lc].val+node[node[u].rc].val;node[u].siz = node[node[u].lc].siz+node[node[u].rc].siz; } } int newnode(){ if(!top){ return ++tot; }else{ node[sta[top]].clear(); return sta[top--]; } } void lazy_tag(int u,long long x){ if(!u){ return; } node[u].tag+=x; node[u].mi+=x;node[u].mx+=x; node[u].val+=(1ull*node[u].siz*x); } void pushdown(int u){ if(!node[u].tag){ return; } if(node[u].lc){ lazy_tag(node[u].lc,node[u].tag); } if(node[u].rc){ lazy_tag(node[u].rc,node[u].tag); } node[u].tag = 0; } void gx(int u,int l,int r,int z){ node[u].mi = 2e9;node[u].mx=-2e9; node[u].val = node[u].siz = node[u].tag = 0; for(int i=l;i<=r;i++){ if(getbit(a[i])==z){ node[u].val=(node[u].val+a[i]);node[u].siz++; node[u].mi = min(node[u].mi,a[i]); node[u].mx = max(node[u].mx,a[i]); } } } void upd(int &u,int l,int r,int x,int y,int z){ if(!u){ u = newnode(); } if(r-l+1<=B){ for(int i=l;i<=r;i++){ if(getbit(a[i])==z){ a[i]+=node[u].tag; } } a[x]-=node[u].tag; node[u].tag = 0; gx(u,l,r,z); return; } pushdown(u); int mid = (l+r)/2; if(x<=mid){ upd(node[u].lc,l,mid,x,y,z); }else{ upd(node[u].rc,mid+1,r,x,y,z); } pushup(u); } void modf(int u,int l,int r,int ll,int rr,int x){ if(!u){ return; } if(r-l+1<=B){ for(int i=l;i<=r;i++){ if(getbit(a[i])==0){ a[i]+=node[u].tag; } } node[u].tag = 0; int xx = max(l,ll),yy = min(r,rr); for(int i=xx;i<=yy;i++){ if(getbit(a[i])==0){ a[i]+=x; } } gx(u,l,r,0); return; } if(l==ll&&r==rr){ lazy_tag(u,x); return; } pushdown(u); int mid = (l+r)/2; if(rr<=mid){ modf(node[u].lc,l,mid,ll,rr,x); }else if(ll>mid){ modf(node[u].rc,mid+1,r,ll,rr,x); }else{ modf(node[u].lc,l,mid,ll,mid,x); modf(node[u].rc,mid+1,r,mid+1,rr,x); } pushup(u); } void add(int x,int y){ if(y<0){ upd(rt[0],1,n,x,y,0); }else{ upd(rt[getbit(y)],1,n,x,y,getbit(y)); } } void del2(int &u,int l,int r,int ll,int rr,int x){ if(!u||(node[u].mi==x&&node[u].mx==x)){ return; } if(r-l+1<=B){ for(int i=l;i<=r;i++){ if(getbit(a[i])==now){ a[i]+=node[u].tag; } } node[u].tag = 0; int xx = max(l,ll),yy = min(r,rr); for(int i=xx;i<=yy;i++){ if(getbit(a[i])==now){ if(a[i]==x){ continue; } a[i]-=x; if(a[i]<L[now]){ add(i,a[i]); } } } gx(u,l,r,now); return; } int mid = (l+r)/2; pushdown(u); if(l==ll&&r==rr){ if(node[u].mi-x>=L[now]){ lazy_tag(u,-x); return; } if(node[u].lc){ if(node[node[u].lc].mi-x>=L[now]){ lazy_tag(node[u].lc,-x); }else{ del2(node[u].lc,l,mid,ll,mid,x); } } if(node[u].rc){ if(node[node[u].rc].mi-x>=L[now]){ lazy_tag(node[u].rc,-x); }else{ del2(node[u].rc,mid+1,r,mid+1,rr,x); } } pushup(u); if(!node[u].siz){ sta[++top] = u; u = 0; } return; } if(rr<=mid){ if(node[u].lc){ del2(node[u].lc,l,mid,ll,rr,x); } }else if(ll>mid){ if(node[u].rc){ del2(node[u].rc,mid+1,r,ll,rr,x); } }else{ if(node[u].lc){ del2(node[u].lc,l,mid,ll,mid,x); } if(node[u].rc){ del2(node[u].rc,mid+1,r,mid+1,rr,x); } } pushup(u); if(!node[u].siz){ sta[++top] = u; u = 0; } } unll query(int u,int l,int r,int ll,int rr){ if(!u){ return 0; } if(r-l+1<=B){ for(int i=l;i<=r;i++){ if(getbit(a[i])==now){ a[i]+=node[u].tag; } } node[u].tag = 0; int x = max(l,ll),y = min(r,rr); unll res = 0; for(int i=x;i<=y;i++){ if(getbit(a[i])==now){ res+=a[i]; } } return res; } if(l==ll&&r==rr){ return node[u].val; } pushdown(u); int mid = (l+r)/2; if(rr<=mid){ if(!node[u].lc){ return 0; } return query(node[u].lc,l,mid,ll,rr); }else if(ll>mid){ if(!node[u].rc){ return 0; } return query(node[u].rc,mid+1,r,ll,rr); }else{ unll res; if(node[u].lc){ res = query(node[u].lc,l,mid,ll,mid); }else{ res = 0; } if(node[u].rc){ res += query(node[u].rc,mid+1,r,mid+1,rr); } return res; } } }T; int Mod = (1<<20)-1; int ksm(int x,int y){ int res = 1; while(y){ if(y&1){ res = res*x; } y>>=1; x = x*x; } return res; } int main(){ ios::sync_with_stdio(false); n = read<int>();m = read<int>(); int x; for(int i=1;i<=n;i++){ x = read<int>(); a[i] = x; } for(int i=1;i<=8;i++){ L[i] = ksm(16,i-1); R[i] = ksm(16,i)-1; } for(int i=1;i<=n;i++){ T.upd(rt[getbit(a[i])],1,n,i,a[i],getbit(a[i])); } int op,l,r;unll ans = 0; while(m--){ op = read<int>();l = read<int>();r = read<int>(); l^=ans;r^=ans; if(op==1){ x = read<int>(); x^=ans; if(x==0){ continue; } T.modf(rt[0],1,n,l,r,-x); for(int i=1;i<=8;i++){ if(!rt[i]){ continue; } now = i; T.del2(rt[i],1,n,l,r,x); } }else{ ans = 0; for(int i=0;i<=8;i++){ if(!rt[i]){ continue; } now = i; ans = (ans+T.query(rt[i],1,n,l,r)); } cout<<ans<<'\n'; ans=ans&Mod; } } return 0; } ```