标题
增加 1 行删除 1 行
1我和我的兄弟共用一个 🚫 套 🚫 | 浅谈树套树的优化方法1我和我的兄弟共用一个树套树 | 浅谈树套树的优化方法
正文
增加 829 行删除 2 行
123456789101112131,773 行未改动全部展开17871788178917901791179217931794179517961797树套树可爱捏!本文章感谢 gpt 的支持,一些优化是我和 gpt 讨论出来的,数据也是 gpt 造的。## 前置知识- [线段树套线段树](https://oi-wiki.org/ds/seg-in-seg/)- [线段树套平衡树](https://oi.wiki/ds/balanced-in-seg/)- [AVL](https://oi.wiki/ds/avl/)## 正文[题目链接](https://hydro.ac/d/Bobi/p/P1000)1,773 行未改动全部展开```### 第九步:时间优化,四叉树版## 后文::::info[AI 使用说明]{open}作者有部分优化是和 gpt 讨论后得出的,题目的数据为 gpt 生成的。::::1234567891011121314151617181,773 行未改动全部展开17921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624树套树可爱捏!本文章感谢 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 生成的。::::