版本比较
我和我的兄弟共用一个树套树 | 浅谈树套树的优化方法
标题
增加 1 行删除 1 行
11我和我的兄弟共用一个 🚫 套 🚫 | 浅谈树套树的优化方法我和我的兄弟共用一个树套树 | 浅谈树套树的优化方法正文
增加 829 行删除 2 行
12334567891011121314151617181,773 行未改动全部展开179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622179626232624树套树可爱捏!
本文章感谢 gpt 的支持,一些优化是我和 gpt 讨论出来的,数据也是 gpt 造的。本文章感谢 GPT 的支持,一些优化是我和 GPT 讨论出来的,数据也是 GPT 造的。
本文章所有优化均未使用指令集、编译指令优化,编译环境为 Hydro OJ C++14 O2。
测试时使用的是 GPT 生成的 $3$ 个强力测试数据,后记总结时使用的是之前的 $3$ 个加上另外生成的 $3$ 个。
## 前置知识
- [线段树套线段树](https://oi-wiki.org/ds/seg-in-seg/)- [线段树套平衡树](https://oi.wiki/ds/balanced-in-seg/)- [AVL](https://oi.wiki/ds/avl/)- [手写编译优化(无需修改编译指令)(循环展开)](https://oi.wiki/lang/optimizations/)
## 正文
[题目链接](https://hydro.ac/d/Bobi/p/P1000)1,773 行未改动全部展开```
### 第九步:时间优化,四叉树版
考虑将线段树变成四叉树,虽然从复杂度上看,这个是 $O(4 \log_4 n)$,二叉树是 $O(2 \log_2 n)$。后者更优,但是因为平衡树的实现中,插入较慢,查询较快,这个线段树在修改时又是 $O(\log_4 n)$ 的,所以综合一下,这个更快,四叉树和普通线段树其实没有什么区别,只不过需要将区间拆分成 $4$ 个区间,需要注意有可能区间长度没有到 $4$ 这时候需要判断,具体实现可以参考代码,总用时 $7255$ 毫秒,代码:
```cpp#include<bits/stdc++.h>#define maxn 1000000000#define uc unsigned charusing namespace std;namespace IO{ int tot; char buffer[10000000],tmp[80]; inline char get_char(){ static char buf[10000000],*p1 = buf,*p2 = buf; if(p1 == p2){ p2 = (p1 = buf) + fread(buf,1,10000000,stdin); } return *p1 ++; } inline int read(){ int x = 0,f = 1; char ch = get_char(); while(ch < '0' or ch > '9'){ f = (ch == '-') ? -1 : f; ch = get_char(); } while(ch >= '0' and ch <= '9'){ x = (x << 3) + (x << 1) + (ch ^ 48); ch = get_char(); } return x * f; } inline char getc(){ char c; c = get_char(); while(c < 'A' or c > 'Z'){ c = get_char(); } return c; } inline void flush(){ fwrite(buffer,1,tot,stdout); tot = 0; } inline void write(int x){ if(x == 0){ buffer[tot ++] = '0'; }else{ int len = 0; while(x > 0){ tmp[len ++] = x % 10 + '0'; x /= 10; } for(int i = len - 1;i >= 0;i --){ buffer[tot ++] = tmp[i]; } } buffer[tot ++] = '\n'; if(tot > 9999900){ flush(); } }}namespace AVL{ struct node{ int l,r,val,cnt,siz; } tri[64000005]; int tot; uc h[64000005]; inline void pushup(int p){ tri[p].siz = tri[tri[p].l].siz + tri[tri[p].r].siz + tri[p].cnt; h[p] = max(h[tri[p].l],h[tri[p].r]) + 1; } inline int newnode(int val){ int p = ++ tot; tri[p].val = val; tri[p].cnt = tri[p].siz = h[p] = 1; return p; } inline int rotate1(int p){ int t = tri[p].l; tri[p].l = tri[t].r; tri[t].r = p; pushup(p); pushup(t); return t; } inline int rotate2(int p){ int t = tri[p].r; tri[p].r = tri[t].l; tri[t].l = p; pushup(p); pushup(t); return t; } inline int add(int root,int x){ int path[64],top = 0,p = root; while(p != 0){ if(x == tri[p].val){ tri[p].cnt ++; tri[p].siz ++; for(int i = top;i >= 1;i --){ tri[path[i]].siz ++; } return root; } path[++ top] = p; p = (x < tri[p].val) ? tri[p].l : tri[p].r; } int np = newnode(x); if(top == 0){ return np; } (x < tri[path[top]].val) ? (tri[path[top]].l = np) : (tri[path[top]].r = np); bool flag = true; for(int i = top;i >= 1;i --){ int u = path[i]; tri[u].siz ++; if(flag == false){ continue; } int oldh = h[u]; int hl = h[tri[u].l]; int hr = h[tri[u].r]; int nh = max(hl,hr) + 1; int nr = u; if(hl - hr > 1){ if(h[tri[tri[u].l].r] > h[tri[tri[u].l].l]){ tri[u].l = rotate2(tri[u].l); } nr = rotate1(u); flag = false; }else if(hr - hl > 1){ if(h[tri[tri[u].r].l] > h[tri[tri[u].r].r]){ tri[u].r = rotate1(tri[u].r); } nr = rotate2(u); flag = false; }else{ h[u] = nh; flag = (nh > oldh); } if(nr != u){ (i == 1) ? (root = nr) : ((x < tri[path[i - 1]].val) ? tri[path[i - 1]].l = nr : tri[path[i - 1]].r = nr); } } return root; } inline int del(int root,int x){ int path[64],top = 0,p = root; while(tri[p].val != x){ path[++ top] = p; p = ((x < tri[p].val) ? tri[p].l : tri[p].r); } tri[p].cnt --; tri[p].siz --; for(int i = top;i >= 1;i --){ tri[path[i]].siz --; } return root; } inline int query(int p,int x){ int res = 0; while(p != 0){ (x < tri[p].val) ? (p = tri[p].l) : (res += tri[tri[p].l].siz + tri[p].cnt,p = tri[p].r); } return res; } inline int query(int p,int l,int r){ return query(p,r) - query(p,l - 1); }}namespace SegmentTree{ struct node{ int ch[4],pos; } tri[32000005]; int rt,tot; int stp[256],stl[256],str[256]; inline int get(int x,int l,int r,int &nl,int &nr){ int len = r - l + 1; int base = len >> 2,tmp = len & 3; int b0 = l + base + (tmp > 0) - 1; int b1 = b0 + base + (tmp > 1); int b2 = b1 + base + (tmp > 2); return (x <= b0) ? (nl = l,nr = b0,0) : ((x <= b1) ? (nl = b0 + 1,nr = b1,1) : ((x <= b2) ? (nl = b1 + 1,nr = b2,2) : (nl = b2 + 1,nr = r,3))); } inline void update(int x,int y,int z){ rt = (rt == 0) ? (++ tot) : rt; int p = rt,l = -maxn,r = maxn; while(true){ tri[p].pos = ((z == 1) ? AVL::add(tri[p].pos,y) : AVL::del(tri[p].pos,y)); if(l == r){ break; } int nl,nr; int c = get(x,l,r,nl,nr); tri[p].ch[c] = (tri[p].ch[c] == 0) ? (++ tot) : tri[p].ch[c]; p = tri[p].ch[c]; l = nl,r = nr; } } inline int query(int ql,int qr,int x,int y){ if(rt == 0 or ql > qr){ return 0; } int top = 1,res = 0; stp[top] = rt; stl[top] = -maxn; str[top] = maxn; while(top != 0){ int u = stp[top],l = stl[top],r = str[top]; top --; if(ql <= l and r <= qr){ res += AVL::query(tri[u].pos,x,y); continue; } int len = r - l + 1,base = len >> 2,tmp = len & 3,cl = l; for(int i = 0;i < 4;i ++){ int siz = base + (i < tmp); if(siz == 0){ continue; } int cr = cl + siz - 1; if(ql <= cr and qr >= cl and tri[u].ch[i] != 0){ stp[++ top] = tri[u].ch[i]; stl[top] = cl; str[top] = cr; } cl = cr + 1; } } return res; }};int q,lst;int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); q = IO::read(); while(q --){ char opt; int x,y,ax,ay,bx,by; opt = IO::getc(); switch(opt){ case 'A': x = IO::read(); y = IO::read(); x ^= lst,y ^= lst; SegmentTree::update(x,y,1); break; case 'D': x = IO::read(); y = IO::read(); x ^= lst,y ^= lst; SegmentTree::update(x,y,-1); break; default: ax = IO::read(); ay = IO::read(); bx = IO::read(); by = IO::read(); ax ^= lst,ay ^= lst,bx ^= lst,by ^= lst; lst = SegmentTree::query(ax,bx,ay,by); IO::write(lst); break; } } IO::flush(); return 0;}```
### 第十步:时间优化,树状数组优化
这里外层的线段树我们考虑将所有 $x$ 加上 $10^9$,这样区间就是 $[0,2 \times 10^9]$,我们直接按照 $[0,2^{31} - 1]$ 维护,这样可以直接从二进制第 $30$ 位扫描到第 $0$ 位,每一位判断去左儿子还是右儿子也非常方便,直接将这一位取出来,判断他是奇数还是偶数即可,另外,我们可以仿照树状数组的优化方法,当且仅当 $x$ 要去当前节点的左儿子时才加入当前节点的内层 AVL 树中,否则不加入,然后每次查询即查询前缀部分,然后相减即可,这里参考了第四步的路径压缩方法,比如 $l$ 和 $r$ 都在左儿子,那么就不用重复跑两遍,直接先跳到第一个不同的地方,然后再分别计算前缀和,最后相减即可,这里讲的是线段树二叉树,因为这里如果还用四叉树,非常难以维护,所以这里没有用。总时间 $5928$ 毫秒,代码:
```cpp#include<bits/stdc++.h>#define maxn 1000000000#define uc unsigned char#define uint unsigned intusing namespace std;namespace IO{ int tot; char buffer[10000000],tmp[80]; inline char get_char(){ static char buf[10000000],*p1 = buf,*p2 = buf; if(p1 == p2){ p2 = (p1 = buf) + fread(buf,1,10000000,stdin); } return *p1 ++; } inline int read(){ int x = 0,f = 1; char ch = get_char(); while(ch < '0' or ch > '9'){ f = (ch == '-') ? -1 : f; ch = get_char(); } while(ch >= '0' and ch <= '9'){ x = (x << 3) + (x << 1) + (ch ^ 48); ch = get_char(); } return x * f; } inline char getc(){ char c; c = get_char(); while(c < 'A' or c > 'Z'){ c = get_char(); } return c; } inline void flush(){ fwrite(buffer,1,tot,stdout); tot = 0; } inline void write(int x){ if(x == 0){ buffer[tot ++] = '0'; }else{ int len = 0; while(x > 0){ tmp[len ++] = x % 10 + '0'; x /= 10; } for(int i = len - 1;i >= 0;i --){ buffer[tot ++] = tmp[i]; } } buffer[tot ++] = '\n'; if(tot > 9999900){ flush(); } }}namespace AVL{ struct node{ int l,r,val,cnt,siz; } tri[62000005]; int tot; uc h[62000005]; inline void pushup(int p){ tri[p].siz = tri[tri[p].l].siz + tri[tri[p].r].siz + tri[p].cnt; h[p] = max(h[tri[p].l],h[tri[p].r]) + 1; } inline int newnode(int val){ int p = ++ tot; tri[p].val = val; tri[p].cnt = tri[p].siz = h[p] = 1; return p; } inline int rotate1(int p){ int t = tri[p].l; tri[p].l = tri[t].r; tri[t].r = p; pushup(p); pushup(t); return t; } inline int rotate2(int p){ int t = tri[p].r; tri[p].r = tri[t].l; tri[t].l = p; pushup(p); pushup(t); return t; } inline int add(int root,int x){ int path[64],top = 0,p = root; while(p != 0){ if(x == tri[p].val){ tri[p].cnt ++; tri[p].siz ++; for(int i = top;i >= 1;i --){ tri[path[i]].siz ++; } return root; } path[++ top] = p; p = (x < tri[p].val) ? tri[p].l : tri[p].r; } int np = newnode(x); if(top == 0){ return np; } (x < tri[path[top]].val) ? (tri[path[top]].l = np) : (tri[path[top]].r = np); bool flag = true; for(int i = top;i >= 1;i --){ int u = path[i]; tri[u].siz ++; if(flag == false){ continue; } int oldh = h[u]; int hl = h[tri[u].l]; int hr = h[tri[u].r]; int nh = max(hl,hr) + 1; int nr = u; if(hl - hr > 1){ if(h[tri[tri[u].l].r] > h[tri[tri[u].l].l]){ tri[u].l = rotate2(tri[u].l); } nr = rotate1(u); flag = false; }else if(hr - hl > 1){ if(h[tri[tri[u].r].l] > h[tri[tri[u].r].r]){ tri[u].r = rotate1(tri[u].r); } nr = rotate2(u); flag = false; }else{ h[u] = nh; flag = (nh > oldh); } if(nr != u){ (i == 1) ? (root = nr) : ((x < tri[path[i - 1]].val) ? tri[path[i - 1]].l = nr : tri[path[i - 1]].r = nr); } } return root; } inline int del(int root,int x){ int path[64],top = 0,p = root; while(tri[p].val != x){ path[++ top] = p; p = ((x < tri[p].val) ? tri[p].l : tri[p].r); } tri[p].cnt --; tri[p].siz --; for(int i = top;i >= 1;i --){ tri[path[i]].siz --; } return root; } inline int query(int p,int x){ int res = 0; while(p != 0){ (x < tri[p].val) ? (p = tri[p].l) : (res += tri[tri[p].l].siz + tri[p].cnt,p = tri[p].r); } return res; } inline int query(int p,int l,int r){ return query(p,r) - query(p,l - 1); }}namespace SegmentTree{ struct node{ int ch[2],pos; } tri[22100005]; int rt,tot; inline void update(int x,int y,int z){ uint v = x + maxn; rt = (rt == 0) ? (++ tot) : rt; int p = rt; for(int bit = 30;bit >= 0;bit --){ int c = (v >> bit) & 1; if(c == 0){ tri[p].pos = (z == 1) ? AVL::add(tri[p].pos,y) : AVL::del(tri[p].pos,y); } if(bit != 0){ tri[p].ch[c] = (tri[p].ch[c] == 0) ? (++ tot) : tri[p].ch[c]; p = tri[p].ch[c]; } } } inline int get_sum(int p,uint x,int bit,int yl,int yr){ int res = 0; for(;bit >= 0 and p != 0;bit --){ int c = (x >> bit) & 1; if(c == 1){ res += AVL::query(tri[p].pos,yl,yr); } p = tri[p].ch[c]; } return res; } inline int query(int ql,int qr,int yl,int yr){ uint l = ql + maxn,r = qr + maxn + 1; int p = rt,bit = 30; while(p != 0 and bit >= 0 and ((l >> bit) & 1) == ((r >> bit) & 1)){ p = tri[p].ch[(l >> bit) & 1]; bit --; } if(p == 0 or bit < 0){ return 0; } return AVL::query(tri[p].pos,yl,yr) + get_sum(tri[p].ch[1],r,bit - 1,yl,yr) - get_sum(tri[p].ch[0],l,bit - 1,yl,yr); }};int q,lst;int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); q = IO::read(); while(q --){ char opt; int x,y,ax,ay,bx,by; opt = IO::getc(); switch(opt){ case 'A': x = IO::read(); y = IO::read(); x ^= lst,y ^= lst; SegmentTree::update(x,y,1); break; case 'D': x = IO::read(); y = IO::read(); x ^= lst,y ^= lst; SegmentTree::update(x,y,-1); break; default: ax = IO::read(); ay = IO::read(); bx = IO::read(); by = IO::read(); ax ^= lst,ay ^= lst,bx ^= lst,by ^= lst; lst = SegmentTree::query(ax,bx,ay,by); IO::write(lst); break; } } IO::flush(); return 0;}```
### 第十一步:时间优化,循环展开
我们发现在外层线段树中,需要频繁调用 AVL 的 `query` 函数,而 `query` 函数可以并行运行,我们将 `get_sum` 中的所有访问的点存入一个数组中,并且存储权值($1$ 或 $-1$)表示加或减,而在都存入数组后,我们在内层 AVL 的 `query` 中将这个数组循环展开,每次往下跳,对于每个点,都有四个值,分别为 `p0`、`p1`、`s0`、`s1`,分别表示 $l$ 到了哪里,$r$ 到了哪里,$l$ 往左(不含 $l$)的和,$r$ 往左(含有 $r$)的和。这样结束的时候 `s1 - s0` 就是答案,这样循环展开一下,同时处理 $8$ 的点即可,时间总和 $3631$ 毫秒,代码:
```cpp#include<bits/stdc++.h>#define maxn 1000000000#define uc unsigned char#define uint unsigned intusing namespace std;namespace IO{ int tot; char buffer[10000000],tmp[80]; inline char get_char(){ static char buf[10000000],*p1 = buf,*p2 = buf; if(p1 == p2){ p2 = (p1 = buf) + fread(buf,1,10000000,stdin); } return *p1 ++; } inline int read(){ int x = 0,f = 1; char ch = get_char(); while(ch < '0' or ch > '9'){ f = (ch == '-') ? -1 : f; ch = get_char(); } while(ch >= '0' and ch <= '9'){ x = (x << 3) + (x << 1) + (ch ^ 48); ch = get_char(); } return x * f; } inline char getc(){ char c; c = get_char(); while(c < 'A' or c > 'Z'){ c = get_char(); } return c; } inline void flush(){ fwrite(buffer,1,tot,stdout); tot = 0; } inline void write(int x){ if(x == 0){ buffer[tot ++] = '0'; }else{ int len = 0; while(x > 0){ tmp[len ++] = x % 10 + '0'; x /= 10; } for(int i = len - 1;i >= 0;i --){ buffer[tot ++] = tmp[i]; } } buffer[tot ++] = '\n'; if(tot > 9999900){ flush(); } }}namespace AVL{ struct node{ int l,r,val,cnt,siz; } tri[62000005]; int tot; uc h[62000005]; inline void pushup(int p){ tri[p].siz = tri[tri[p].l].siz + tri[tri[p].r].siz + tri[p].cnt; h[p] = max(h[tri[p].l],h[tri[p].r]) + 1; } inline int newnode(int val){ int p = ++ tot; tri[p].val = val; tri[p].cnt = tri[p].siz = h[p] = 1; return p; } inline int rotate1(int p){ int t = tri[p].l; tri[p].l = tri[t].r; tri[t].r = p; pushup(p); pushup(t); return t; } inline int rotate2(int p){ int t = tri[p].r; tri[p].r = tri[t].l; tri[t].l = p; pushup(p); pushup(t); return t; } inline int add(int root,int x){ int path[64],top = 0,p = root; while(p != 0){ if(x == tri[p].val){ tri[p].cnt ++; tri[p].siz ++; for(int i = top;i >= 1;i --){ tri[path[i]].siz ++; } return root; } path[++ top] = p; p = (x < tri[p].val) ? tri[p].l : tri[p].r; } int np = newnode(x); if(top == 0){ return np; } (x < tri[path[top]].val) ? (tri[path[top]].l = np) : (tri[path[top]].r = np); bool flag = true; for(int i = top;i >= 1;i --){ int u = path[i]; tri[u].siz ++; if(flag == false){ continue; } int oldh = h[u]; int hl = h[tri[u].l]; int hr = h[tri[u].r]; int nh = max(hl,hr) + 1; int nr = u; if(hl - hr > 1){ if(h[tri[tri[u].l].r] > h[tri[tri[u].l].l]){ tri[u].l = rotate2(tri[u].l); } nr = rotate1(u); flag = false; }else if(hr - hl > 1){ if(h[tri[tri[u].r].l] > h[tri[tri[u].r].r]){ tri[u].r = rotate1(tri[u].r); } nr = rotate2(u); flag = false; }else{ h[u] = nh; flag = (nh > oldh); } if(nr != u){ (i == 1) ? (root = nr) : ((x < tri[path[i - 1]].val) ? tri[path[i - 1]].l = nr : tri[path[i - 1]].r = nr); } } return root; } inline int del(int root,int x){ int path[64],top = 0,p = root; while(tri[p].val != x){ path[++ top] = p; p = ((x < tri[p].val) ? tri[p].l : tri[p].r); } tri[p].cnt --; tri[p].siz --; for(int i = top;i >= 1;i --){ tri[path[i]].siz --; } return root; } inline int query(int *rt,int *sg,int n,int l,int r){ int res = 0; for(int i = 0;i < n;i += 8){ int p0 = (i + 0 < n) ? rt[i + 0] : 0,p1 = p0,s0 = 0,s1 = 0; int p2 = (i + 1 < n) ? rt[i + 1] : 0,p3 = p2,s2 = 0,s3 = 0; int p4 = (i + 2 < n) ? rt[i + 2] : 0,p5 = p4,s4 = 0,s5 = 0; int p6 = (i + 3 < n) ? rt[i + 3] : 0,p7 = p6,s6 = 0,s7 = 0; int p8 = (i + 4 < n) ? rt[i + 4] : 0,p9 = p8,s8 = 0,s9 = 0; int p10 = (i + 5 < n) ? rt[i + 5] : 0,p11 = p10,s10 = 0,s11 = 0; int p12 = (i + 6 < n) ? rt[i + 6] : 0,p13 = p12,s12 = 0,s13 = 0; int p14 = (i + 7 < n) ? rt[i + 7] : 0,p15 = p14,s14 = 0,s15 = 0; while(p0 != 0 or p1 != 0 or p2 != 0 or p3 != 0 or p4 != 0 or p5 != 0 or p6 != 0 or p7 != 0 or p8 != 0 or p9 != 0 or p10 != 0 or p11 != 0 or p12 != 0 or p13 != 0 or p14 != 0 or p15 != 0){ (r < tri[p0].val) ? (p0 = tri[p0].l) : (s0 += tri[tri[p0].l].siz + tri[p0].cnt,p0 = tri[p0].r); (l - 1 < tri[p1].val) ? (p1 = tri[p1].l) : (s1 += tri[tri[p1].l].siz + tri[p1].cnt,p1 = tri[p1].r); (r < tri[p2].val) ? (p2 = tri[p2].l) : (s2 += tri[tri[p2].l].siz + tri[p2].cnt,p2 = tri[p2].r); (l - 1 < tri[p3].val) ? (p3 = tri[p3].l) : (s3 += tri[tri[p3].l].siz + tri[p3].cnt,p3 = tri[p3].r); (r < tri[p4].val) ? (p4 = tri[p4].l) : (s4 += tri[tri[p4].l].siz + tri[p4].cnt,p4 = tri[p4].r); (l - 1 < tri[p5].val) ? (p5 = tri[p5].l) : (s5 += tri[tri[p5].l].siz + tri[p5].cnt,p5 = tri[p5].r); (r < tri[p6].val) ? (p6 = tri[p6].l) : (s6 += tri[tri[p6].l].siz + tri[p6].cnt,p6 = tri[p6].r); (l - 1 < tri[p7].val) ? (p7 = tri[p7].l) : (s7 += tri[tri[p7].l].siz + tri[p7].cnt,p7 = tri[p7].r); (r < tri[p8].val) ? (p8 = tri[p8].l) : (s8 += tri[tri[p8].l].siz + tri[p8].cnt,p8 = tri[p8].r); (l - 1 < tri[p9].val) ? (p9 = tri[p9].l) : (s9 += tri[tri[p9].l].siz + tri[p9].cnt,p9 = tri[p9].r); (r < tri[p10].val) ? (p10 = tri[p10].l) : (s10 += tri[tri[p10].l].siz + tri[p10].cnt,p10 = tri[p10].r); (l - 1 < tri[p11].val) ? (p11 = tri[p11].l) : (s11 += tri[tri[p11].l].siz + tri[p11].cnt,p11 = tri[p11].r); (r < tri[p12].val) ? (p12 = tri[p12].l) : (s12 += tri[tri[p12].l].siz + tri[p12].cnt,p12 = tri[p12].r); (l - 1 < tri[p13].val) ? (p13 = tri[p13].l) : (s13 += tri[tri[p13].l].siz + tri[p13].cnt,p13 = tri[p13].r); (r < tri[p14].val) ? (p14 = tri[p14].l) : (s14 += tri[tri[p14].l].siz + tri[p14].cnt,p14 = tri[p14].r); (l - 1 < tri[p15].val) ? (p15 = tri[p15].l) : (s15 += tri[tri[p15].l].siz + tri[p15].cnt,p15 = tri[p15].r); } res += sg[i + 0] * (s0 - s1) * (int)(i < n); res += sg[i + 1] * (s2 - s3) * (int)(i + 1 < n); res += sg[i + 2] * (s4 - s5) * (int)(i + 2 < n); res += sg[i + 3] * (s6 - s7) * (int)(i + 3 < n); res += sg[i + 4] * (s8 - s9) * (int)(i + 4 < n); res += sg[i + 5] * (s10 - s11) * (int)(i + 5 < n); res += sg[i + 6] * (s12 - s13) * (int)(i + 6 < n); res += sg[i + 7] * (s14 - s15) * (int)(i + 7 < n); } return res; }}namespace SegmentTree{ struct node{ int ch[2],pos; } tri[22100005]; int rt,tot; inline void update(int x,int y,int z){ uint v = x + maxn; rt = (rt == 0) ? (++ tot) : rt; int p = rt; for(int bit = 30;bit >= 0;bit --){ int c = (v >> bit) & 1; if(c == 0){ tri[p].pos = (z == 1) ? AVL::add(tri[p].pos,y) : AVL::del(tri[p].pos,y); } if(bit != 0){ tri[p].ch[c] = (tri[p].ch[c] == 0) ? (++ tot) : tri[p].ch[c]; p = tri[p].ch[c]; } } } inline int query(int ql,int qr,int yl,int yr){ uint l = ql + maxn,r = qr + maxn + 1; int p = rt,bit = 30; while(p != 0 and bit >= 0 and ((l >> bit) & 1) == ((r >> bit) & 1)){ p = tri[p].ch[(l >> bit) & 1]; bit --; } if(p == 0 or bit < 0){ return 0; } int st[64],sg[64],top = 0; st[top] = tri[p].pos,sg[top ++] = 1; int u = tri[p].ch[1],v = tri[p].ch[0]; for(int k = bit - 1;k >= 0;k --){ int a = (r >> k) & 1,b = (l >> k) & 1; if(u != 0){ if(a == 1 and tri[u].pos != 0){ st[top] = tri[u].pos,sg[top ++] = 1; } u = tri[u].ch[a]; } if(v != 0){ if(b == 1 and tri[v].pos != 0){ st[top] = tri[v].pos,sg[top ++] = -1; } v = tri[v].ch[b]; } } return AVL::query(st,sg,top,yl,yr); }};int q,lst;int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); q = IO::read(); while(q --){ char opt; int x,y,ax,ay,bx,by; opt = IO::getc(); switch(opt){ case 'A': x = IO::read(); y = IO::read(); x ^= lst,y ^= lst; SegmentTree::update(x,y,1); break; case 'D': x = IO::read(); y = IO::read(); x ^= lst,y ^= lst; SegmentTree::update(x,y,-1); break; default: ax = IO::read(); ay = IO::read(); bx = IO::read(); by = IO::read(); ax ^= lst,ay ^= lst,bx ^= lst,by ^= lst; lst = SegmentTree::query(ax,bx,ay,by); IO::write(lst); break; } } IO::flush(); return 0;}```
## 后文
在我和 GPT 的努力下,最终,从第一版的 $23170$ 毫秒优化到了 $3631$ 毫秒,新数据中最慢的点 $3860$ 毫秒,并且在随机数据下表现优秀。
本文仅为节选,后续有可能继续进行优化,欢迎大家发明更优的解法,也欢迎进行 hack。
::::info[AI 使用说明]{open}作者有部分优化是和 gpt 讨论后得出的,题目的数据为 gpt 生成的。作者有部分优化是和 GPT 讨论后得出的,题目的数据为 GPT 生成的。::::