树套树可爱捏!
本文章感谢 GPT 的支持,一些优化是我和 GPT 讨论出来的,数据也是 GPT 造的。
本文章所有优化均未使用指令集、编译指令优化,编译环境为 Hydro OJ C++14 O2。
测试时使用的是 GPT 生成的 个强力测试数据,后记总结时使用的是之前的 个加上另外生成的 个。
前置知识
正文
第一步:不优化版线段树套线段树
线段树套线段树,时间复杂度 。
内层需要开 ,外层需要开 ,内层维护每个 的出现次数,外层维护每个 对应的内层线段树的根节点,内外都需要动态开点,加入一个点就将外层线段树的父节点和叶子节点对应的内层线段树加入 ,这里不再过多赘述。
代码:
#include<bits/stdc++.h>
#define maxn 1000000000
using namespace std;
namespace SegmentTree1{
struct node{
int l,r,cnt;
} tri[1800000000];
int tot;
int update(int p,int l,int r,int x,int y){
if(p == 0){
p = ++ tot;
}
tri[p].cnt += y;
if(l == r){
return p;
}
int mid = (l + r) >> 1;
(x <= mid) ? (tri[p].l = update(tri[p].l,l,mid,x,y)) : (tri[p].r = update(tri[p].r,mid + 1,r,x,y));
return p;
}
int query(int p,int l,int r,int ql,int qr){
if(p == 0){
return 0;
}
if(ql <= l and r <= qr){
return tri[p].cnt;
}
int mid = (l + r) >> 1,res = 0;
res += (ql <= mid) ? query(tri[p].l,l,mid,ql,qr) : 0;
res += (qr > mid) ? query(tri[p].r,mid + 1,r,ql,qr) : 0;
return res;
}
}
namespace SegmentTree2{
struct node{
int l,r,pos;
} tri[70000000];
int rt,tot;
int update(int p,int l,int r,int x,int y,int z){
if(p == 0){
p = ++ tot;
}
tri[p].pos = SegmentTree1::update(tri[p].pos,-maxn,maxn,y,z);
if(l == r){
return p;
}
int mid = (l + r) >> 1;
(x <= mid) ? (tri[p].l = update(tri[p].l,l,mid,x,y,z)) : (tri[p].r = update(tri[p].r,mid + 1,r,x,y,z));
return p;
}
int query(int p,int l,int r,int ql,int qr,int x,int y){
if(p == 0){
return 0;
}
if(ql <= l and r <= qr){
return SegmentTree1::query(tri[p].pos,-maxn,maxn,x,y);
}
int mid = (l + r) >> 1,res = 0;
res += (ql <= mid) ? query(tri[p].l,l,mid,ql,qr,x,y) : 0;
res += (qr > mid) ? query(tri[p].r,mid + 1,r,ql,qr,x,y) : 0;
return res;
}
};
int q,lst;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> q;
while(q --){
char opt;
int x,y,ax,ay,bx,by;
cin >> opt;
switch(opt){
case 'A':
cin >> x >> y;
x ^= lst,y ^= lst;
SegmentTree2::rt = SegmentTree2::update(SegmentTree2::rt,-maxn,maxn,x,y,1);
break;
case 'D':
cin >> x >> y;
x ^= lst,y ^= lst;
SegmentTree2::rt = SegmentTree2::update(SegmentTree2::rt,-maxn,maxn,x,y,-1);
break;
default:
cin >> ax >> ay >> bx >> by;
ax ^= lst,ay ^= lst,bx ^= lst,by ^= lst;
lst = SegmentTree2::query(SegmentTree2::rt,-maxn,maxn,ax,bx,ay,by);
cout << lst << "\n";
break;
}
}
return 0;
}我们交到题目上会发现只有 分,第一个点 MLE 了。
第二步:内存优化,改用线段树套平衡树
我们现在被空间限制了,所以我们采用时间换空间技巧,采用比线段树时间比较劣,但是空间较小的 fhq-treap,来代替内层的线段树。
fhq-treap 代码:
#include<bits/stdc++.h>
#define maxn 1000000000
#define pii pair<int,int>
#define uint unsigned int
using namespace std;
namespace Treap{
struct node{
int l,r,val,cnt,siz;
uint key;
} tri[64000000];
int tot;
uint seed;
inline uint rnd(){
uint z = (seed += 0x9e3779b9u);
z = (z ^ (z >> 16)) * 0x85ebca6bu;
z = (z ^ (z >> 13)) * 0xc2b2ae35u;
return z ^ (z >> 16);
}
inline int newnode(int val){
int p = ++ tot;
tri[p].val = val;
tri[p].cnt = tri[p].siz = 1;
tri[p].key = rnd();
return p;
}
inline void pushup(int p){
tri[p].siz = tri[tri[p].l].siz + tri[tri[p].r].siz + tri[p].cnt;
}
void split(int p,int k,int &x,int &y){
if(p == 0){
x = y = 0;
return;
}
if(tri[p].val > k){
y = p;
split(tri[p].l,k,x,tri[p].l);
pushup(y);
}else{
x = p;
split(tri[p].r,k,tri[p].r,y);
pushup(x);
}
}
int merge(int x,int y){
if(x == 0 or y == 0){
return x | y;
}
if(tri[x].key > tri[y].key){
tri[x].r = merge(tri[x].r,y);
pushup(x);
return x;
}else{
tri[y].l = merge(x,tri[y].l);
pushup(y);
return y;
}
}
inline int add(int rt,int x){
int a,b,c;
split(rt,x - 1,a,b);
split(b,x,b,c);
if(b == 0){
b = newnode(x);
}else{
tri[b].cnt ++;
tri[b].siz ++;
}
return merge(a,merge(b,c));
}
inline int del(int rt,int x){
int a,b,c;
split(rt,x - 1,a,b);
split(b,x,b,c);
tri[b].cnt --;
tri[b].siz --;
return merge(a,merge(b,c));
}
inline pii query(int rt,int l,int r){
int a,b,c;
split(rt,l - 1,a,b);
split(b,r,b,c);
int res = tri[b].siz;
rt = merge(a,merge(b,c));
return {rt,res};
}
}
namespace SegmentTree{
struct node{
int l,r,pos;
} tri[64000000];
int rt,tot;
int update(int p,int l,int r,int x,int y,int z){
p = (p == 0) ? (++ tot) : p;
(z == 1) ? (tri[p].pos = Treap::add(tri[p].pos,y)) : (tri[p].pos = Treap::del(tri[p].pos,y));
if(l == r){
return p;
}
int mid = (l + r) >> 1;
(x <= mid) ? (tri[p].l = update(tri[p].l,l,mid,x,y,z)) : (tri[p].r = update(tri[p].r,mid + 1,r,x,y,z));
return p;
}
int query(int p,int l,int r,int ql,int qr,int x,int y){
if(p == 0){
return 0;
}
if(ql <= l and r <= qr){
pii res = Treap::query(tri[p].pos,x,y);
tri[p].pos = res.first;
return res.second;
}
int mid = (l + r) >> 1,res = 0;
res += (ql <= mid) ? query(tri[p].l,l,mid,ql,qr,x,y) : 0;
res += (qr > mid) ? query(tri[p].r,mid + 1,r,ql,qr,x,y) : 0;
return res;
}
};
int q,lst;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> q;
while(q --){
char opt;
int x,y,ax,ay,bx,by;
cin >> opt;
switch(opt){
case 'A':
cin >> x >> y;
x ^= lst,y ^= lst;
SegmentTree::rt = SegmentTree::update(SegmentTree::rt,-maxn,maxn,x,y,1);
break;
case 'D':
cin >> x >> y;
x ^= lst,y ^= lst;
SegmentTree::rt = SegmentTree::update(SegmentTree::rt,-maxn,maxn,x,y,-1);
break;
default:
cin >> ax >> ay >> bx >> by;
ax ^= lst,ay ^= lst,bx ^= lst,by ^= lst;
lst = SegmentTree::query(SegmentTree::rt,-maxn,maxn,ax,bx,ay,by);
cout << lst << "\n";
break;
}
}
return 0;
}总用时 毫秒。
第三步:时间优化
我们发现平衡树中的 split 和 merge 需要频繁递归,拖慢了速度,我们尽量少用这两个函数,所以我们将平衡树的查询改为 while 循环版查询,修改后总用时 毫秒。
我们顺手加上快读,总用时 毫秒。
代码:
#include<bits/stdc++.h>
#define maxn 1000000000
#define uint unsigned int
using 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);
if(p1 == p2){
return EOF;
}
}
return *p1 ++;
}
inline int read(){
int x = 0,f = 1;
char ch = get_char();
while(ch < '0' or ch > '9'){
if(ch == '-'){
f = -1;
}
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';
buffer[tot ++] = '\n';
return;
}
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 Treap{
struct node{
int l,r,val,cnt,siz;
uint key;
} tri[64000000];
int tot;
uint seed;
inline uint rnd(){
uint z = (seed += 0x9e3779b9u);
z = (z ^ (z >> 16)) * 0x85ebca6bu;
z = (z ^ (z >> 13)) * 0xc2b2ae35u;
return z ^ (z >> 16);
}
inline int newnode(int val){
int p = ++ tot;
tri[p].val = val;
tri[p].cnt = tri[p].siz = 1;
tri[p].key = rnd();
return p;
}
inline void pushup(int p){
tri[p].siz = tri[tri[p].l].siz + tri[tri[p].r].siz + tri[p].cnt;
}
void split(int p,int k,int &x,int &y){
if(p == 0){
x = y = 0;
return;
}
if(tri[p].val > k){
y = p;
split(tri[p].l,k,x,tri[p].l);
pushup(y);
}else{
x = p;
split(tri[p].r,k,tri[p].r,y);
pushup(x);
}
}
int merge(int x,int y){
if(x == 0 or y == 0){
return x | y;
}
if(tri[x].key > tri[y].key){
tri[x].r = merge(tri[x].r,y);
pushup(x);
return x;
}else{
tri[y].l = merge(x,tri[y].l);
pushup(y);
return y;
}
}
inline int add(int rt,int x){
int a,b,c;
split(rt,x - 1,a,b);
split(b,x,b,c);
if(b == 0){
b = newnode(x);
}else{
tri[b].cnt ++;
tri[b].siz ++;
}
return merge(a,merge(b,c));
}
inline int del(int rt,int x){
int a,b,c;
split(rt,x - 1,a,b);
split(b,x,b,c);
tri[b].cnt --;
tri[b].siz --;
return merge(a,merge(b,c));
}
inline int query(int rt,int x){
int res = 0;
while(rt != 0){
if(x < tri[rt].val){
rt = tri[rt].l;
}else if(x == tri[rt].val){
res += tri[tri[rt].l].siz + tri[rt].cnt;
break;
}else{
res += tri[tri[rt].l].siz + tri[rt].cnt;
rt = tri[rt].r;
}
}
return res;
}
inline int query(int rt,int l,int r){
return query(rt,r) - query(rt,l - 1);
}
}
namespace SegmentTree{
struct node{
int l,r,pos;
} tri[25000005];
int rt,tot;
int update(int p,int l,int r,int x,int y,int z){
p = (p == 0) ? (++ tot) : p;
(z == 1) ? (tri[p].pos = Treap::add(tri[p].pos,y)) : (tri[p].pos = Treap::del(tri[p].pos,y));
if(l == r){
return p;
}
int mid = (l + r) >> 1;
(x <= mid) ? (tri[p].l = update(tri[p].l,l,mid,x,y,z)) : (tri[p].r = update(tri[p].r,mid + 1,r,x,y,z));
return p;
}
int query(int p,int l,int r,int ql,int qr,int x,int y){
if(p == 0){
return 0;
}
if(ql <= l and r <= qr){
return Treap::query(tri[p].pos,x,y);
}
int mid = (l + r) >> 1,res = 0;
res += (ql <= mid) ? query(tri[p].l,l,mid,ql,qr,x,y) : 0;
res += (qr > mid) ? query(tri[p].r,mid + 1,r,ql,qr,x,y) : 0;
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::rt = SegmentTree::update(SegmentTree::rt,-maxn,maxn,x,y,1);
break;
case 'D':
x = IO::read();
y = IO::read();
x ^= lst,y ^= lst;
SegmentTree::rt = SegmentTree::update(SegmentTree::rt,-maxn,maxn,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(SegmentTree::rt,-maxn,maxn,ax,bx,ay,by);
IO::write(lst);
break;
}
}
IO::flush();
return 0;
}第四步:时间优化
考虑平衡树中如果 和 都需要走相同的路径,我们就没有必要走两遍,可以先走公共部分,再处理不同部分,时间 毫秒。
#include<bits/stdc++.h>
#define maxn 1000000000
#define uint unsigned int
using 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);
if(p1 == p2){
return EOF;
}
}
return *p1 ++;
}
inline int read(){
int x = 0,f = 1;
char ch = get_char();
while(ch < '0' or ch > '9'){
if(ch == '-'){
f = -1;
}
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';
buffer[tot ++] = '\n';
return;
}
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 Treap{
struct node{
int l,r,val,cnt,siz;
uint key;
} tri[64000000];
int tot;
uint seed;
inline uint rnd(){
uint z = (seed += 0x9e3779b9u);
z = (z ^ (z >> 16)) * 0x85ebca6bu;
z = (z ^ (z >> 13)) * 0xc2b2ae35u;
return z ^ (z >> 16);
}
inline int newnode(int val){
int p = ++ tot;
tri[p].val = val;
tri[p].cnt = tri[p].siz = 1;
tri[p].key = rnd();
return p;
}
inline void pushup(int p){
tri[p].siz = tri[tri[p].l].siz + tri[tri[p].r].siz + tri[p].cnt;
}
void split(int p,int k,int &x,int &y){
if(p == 0){
x = y = 0;
return;
}
if(tri[p].val > k){
y = p;
split(tri[p].l,k,x,tri[p].l);
pushup(y);
}else{
x = p;
split(tri[p].r,k,tri[p].r,y);
pushup(x);
}
}
int merge(int x,int y){
if(x == 0 or y == 0){
return x | y;
}
if(tri[x].key > tri[y].key){
tri[x].r = merge(tri[x].r,y);
pushup(x);
return x;
}else{
tri[y].l = merge(x,tri[y].l);
pushup(y);
return y;
}
}
inline int add(int rt,int x){
int a,b,c;
split(rt,x - 1,a,b);
split(b,x,b,c);
if(b == 0){
b = newnode(x);
}else{
tri[b].cnt ++;
tri[b].siz ++;
}
return merge(a,merge(b,c));
}
inline int del(int rt,int x){
int a,b,c;
split(rt,x - 1,a,b);
split(b,x,b,c);
tri[b].cnt --;
tri[b].siz --;
return merge(a,merge(b,c));
}
inline int query(int rt,int x){
int res = 0;
while(rt != 0){
if(tri[rt].val <= x){
res += tri[tri[rt].l].siz + tri[rt].cnt;
rt = tri[rt].r;
}else{
rt = tri[rt].l;
}
}
return res;
}
inline int query(int rt,int l,int r){
while(rt != 0){
if(tri[rt].val < l){
rt = tri[rt].r;
}else if(tri[rt].val > r){
rt = tri[rt].l;
}else{
int res = tri[rt].cnt;
res += tri[tri[rt].l].siz - query(tri[rt].l,l - 1);
res += query(tri[rt].r,r);
return res;
}
}
return 0;
}
}
namespace SegmentTree{
struct node{
int l,r,pos;
} tri[25000005];
int rt,tot;
int update(int p,int l,int r,int x,int y,int z){
p = (p == 0) ? (++ tot) : p;
(z == 1) ? (tri[p].pos = Treap::add(tri[p].pos,y)) : (tri[p].pos = Treap::del(tri[p].pos,y));
if(l == r){
return p;
}
int mid = (l + r) >> 1;
(x <= mid) ? (tri[p].l = update(tri[p].l,l,mid,x,y,z)) : (tri[p].r = update(tri[p].r,mid + 1,r,x,y,z));
return p;
}
int query(int p,int l,int r,int ql,int qr,int x,int y){
if(p == 0){
return 0;
}
if(ql <= l and r <= qr){
return Treap::query(tri[p].pos,x,y);
}
int mid = (l + r) >> 1,res = 0;
res += (ql <= mid) ? query(tri[p].l,l,mid,ql,qr,x,y) : 0;
res += (qr > mid) ? query(tri[p].r,mid + 1,r,ql,qr,x,y) : 0;
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::rt = SegmentTree::update(SegmentTree::rt,-maxn,maxn,x,y,1);
break;
case 'D':
x = IO::read();
y = IO::read();
x ^= lst,y ^= lst;
SegmentTree::rt = SegmentTree::update(SegmentTree::rt,-maxn,maxn,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(SegmentTree::rt,-maxn,maxn,ax,bx,ay,by);
IO::write(lst);
break;
}
}
IO::flush();
return 0;
}第五步:时间优化,将 fhq-treap 替换为 AVL
我们发现,在现有体系上,线段树套 fhq-treap 已经难以继续优化了,所以我们考虑将 fhq-treap 替换为 AVL,AVL 相较于 fhq-treap 有较多的优势,fhq-treap 的时间复杂度是期望 ,而 AVL 则是最坏 ,这里主要就是树高的问题,AVL 的树高最坏为 。
总用时 毫秒,相较于上次提升巨大,因此我们也可以估算 fhq-treap 的树高期望大概为 ,代码:
#include<bits/stdc++.h>
#define maxn 1000000000
using 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);
if(p1 == p2){
return EOF;
}
}
return *p1 ++;
}
inline int read(){
int x = 0,f = 1;
char ch = get_char();
while(ch < '0' or ch > '9'){
if(ch == '-'){
f = -1;
}
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,h;
} tri[64000005];
int tot;
inline void pushup(int p){
tri[p].siz = tri[tri[p].l].siz + tri[tri[p].r].siz + tri[p].cnt;
tri[p].h = max(tri[tri[p].l].h,tri[tri[p].r].h) + 1;
}
inline int newnode(int val){
int p = ++ tot;
tri[p].val = val;
tri[p].cnt = tri[p].siz = tri[p].h = 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;
}
int add(int p,int x){
if(p == 0){
return newnode(x);
}
if(x == tri[p].val){
tri[p].cnt ++;
pushup(p);
return p;
}
if(x < tri[p].val){
tri[p].l = add(tri[p].l,x);
pushup(p);
if(tri[tri[p].l].h - tri[tri[p].r].h > 1){
if(tri[tri[tri[p].l].r].h > tri[tri[tri[p].l].l].h){
tri[p].l = rotate2(tri[p].l);
}
return rotate1(p);
}
}else{
tri[p].r = add(tri[p].r,x);
pushup(p);
if(tri[tri[p].r].h - tri[tri[p].l].h > 1){
if(tri[tri[tri[p].r].l].h > tri[tri[tri[p].r].r].h){
tri[p].r = rotate1(tri[p].r);
}
return rotate2(p);
}
}
return p;
}
int del(int p,int x){
if(x == tri[p].val){
tri[p].cnt --;
}else if(x < tri[p].val){
tri[p].l = del(tri[p].l,x);
}else{
tri[p].r = del(tri[p].r,x);
}
pushup(p);
return p;
}
inline int query(int p,int x){
int res = 0;
while(p != 0){
if(x < tri[p].val){
p = tri[p].l;
}else{
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 l,r,pos;
} tri[25000005];
int rt,tot;
int update(int p,int l,int r,int x,int y,int z){
p = (p == 0) ? (++ tot) : p;
(z == 1) ? (tri[p].pos = AVL::add(tri[p].pos,y)) : (tri[p].pos = AVL::del(tri[p].pos,y));
if(l == r){
return p;
}
int mid = (l + r) >> 1;
(x <= mid) ? (tri[p].l = update(tri[p].l,l,mid,x,y,z)) : (tri[p].r = update(tri[p].r,mid + 1,r,x,y,z));
return p;
}
int query(int p,int l,int r,int ql,int qr,int x,int y){
if(p == 0){
return 0;
}
if(ql <= l and r <= qr){
return AVL::query(tri[p].pos,x,y);
}
int mid = (l + r) >> 1,res = 0;
res += (ql <= mid) ? query(tri[p].l,l,mid,ql,qr,x,y) : 0;
res += (qr > mid) ? query(tri[p].r,mid + 1,r,ql,qr,x,y) : 0;
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::rt = SegmentTree::update(SegmentTree::rt,-maxn,maxn,x,y,1);
break;
case 'D':
x = IO::read();
y = IO::read();
x ^= lst,y ^= lst;
SegmentTree::rt = SegmentTree::update(SegmentTree::rt,-maxn,maxn,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(SegmentTree::rt,-maxn,maxn,ax,bx,ay,by);
IO::write(lst);
break;
}
}
IO::flush();
return 0;
}第六步:时间优化,特判不需要旋转的情况
AVL 依靠旋转保持时间复杂度,我们知道,有两种情况就不需要判断是否需要旋转了,分别是:
- 儿子节点已经做过旋转了,AVL 树保证只会做 次旋转,因为旋转过后,当前节点以下部分相当于没有变。
- 当前节点已经被创建过了,这种情况也不需要判断,因为加出现次数不改变树的结构。
我们再加一个优化,在删除、添加时动态维护节点大小和出现次数,只在旋转时进行 pushup,用于加快速度。
总时间 毫秒,代码:
#include<bits/stdc++.h>
#define maxn 1000000000
using 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);
if(p1 == p2){
return EOF;
}
}
return *p1 ++;
}
inline int read(){
int x = 0,f = 1;
char ch = get_char();
while(ch < '0' or ch > '9'){
if(ch == '-'){
f = -1;
}
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,h;
} tri[64000005];
int tot;
inline void pushup(int p){
tri[p].siz = tri[tri[p].l].siz + tri[tri[p].r].siz + tri[p].cnt;
tri[p].h = max(tri[tri[p].l].h,tri[tri[p].r].h) + 1;
}
inline int newnode(int val){
int p = ++ tot;
tri[p].val = val;
tri[p].cnt = tri[p].siz = tri[p].h = 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;
}
int add(int p,int x,bool &flag){
if(p == 0){
flag = true;
return newnode(x);
}
if(x == tri[p].val){
tri[p].cnt ++;
tri[p].siz ++;
flag = false;
return p;
}
int oldh = tri[p].h;
if(x < tri[p].val){
tri[p].l = add(tri[p].l,x,flag);
}else{
tri[p].r = add(tri[p].r,x,flag);
}
tri[p].siz ++;
if(flag == false){
return p;
}
int hl = tri[tri[p].l].h;
int hr = tri[tri[p].r].h;
int nh = max(hl,hr) + 1;
if(hl - hr > 1){
if(tri[tri[tri[p].l].r].h > tri[tri[tri[p].l].l].h){
tri[p].l = rotate2(tri[p].l);
}
flag = false;
return rotate1(p);
}
if(hr - hl > 1){
if(tri[tri[tri[p].r].l].h > tri[tri[tri[p].r].r].h){
tri[p].r = rotate1(tri[p].r);
}
flag = false;
return rotate2(p);
}
tri[p].h = nh;
flag = (nh > oldh);
return p;
}
int del(int p,int x){
if(x == tri[p].val){
tri[p].cnt --;
tri[p].siz --;
}else if(x < tri[p].val){
tri[p].l = del(tri[p].l,x);
tri[p].siz --;
}else{
tri[p].r = del(tri[p].r,x);
tri[p].siz --;
}
return p;
}
inline int query(int p,int x){
int res = 0;
while(p != 0){
if(x < tri[p].val){
p = tri[p].l;
}else{
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 l,r,pos;
} tri[25000005];
int rt,tot;
int update(int p,int l,int r,int x,int y,int z){
p = (p == 0) ? (++ tot) : p;
if(z == 1){
bool flag = false;
tri[p].pos = AVL::add(tri[p].pos,y,flag);
}else{
tri[p].pos = AVL::del(tri[p].pos,y);
}
if(l == r){
return p;
}
int mid = (l + r) >> 1;
(x <= mid) ? (tri[p].l = update(tri[p].l,l,mid,x,y,z)) : (tri[p].r = update(tri[p].r,mid + 1,r,x,y,z));
return p;
}
int query(int p,int l,int r,int ql,int qr,int x,int y){
if(p == 0){
return 0;
}
if(ql <= l and r <= qr){
return AVL::query(tri[p].pos,x,y);
}
int mid = (l + r) >> 1,res = 0;
res += (ql <= mid) ? query(tri[p].l,l,mid,ql,qr,x,y) : 0;
res += (qr > mid) ? query(tri[p].r,mid + 1,r,ql,qr,x,y) : 0;
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::rt = SegmentTree::update(SegmentTree::rt,-maxn,maxn,x,y,1);
break;
case 'D':
x = IO::read();
y = IO::read();
x ^= lst,y ^= lst;
SegmentTree::rt = SegmentTree::update(SegmentTree::rt,-maxn,maxn,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(SegmentTree::rt,-maxn,maxn,ax,bx,ay,by);
IO::write(lst);
break;
}
}
IO::flush();
return 0;
}第七步:时间优化,非递归线段树
我们可以将外层线段树改为非递归线段树,单点修改很好改,区间查询因为返回的值反正都要加入答案,所以我们不需要记录上一个点是什么,我们不管是哪个节点,都把答案加入总和中,用一个手写栈维护当前还未计算的节点。
总用时 毫秒,代码:
#include<bits/stdc++.h>
#define maxn 1000000000
using 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);
if(p1 == p2){
return EOF;
}
}
return *p1 ++;
}
inline int read(){
int x = 0,f = 1;
char ch = get_char();
while(ch < '0' or ch > '9'){
if(ch == '-'){
f = -1;
}
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,h;
} tri[64000005];
int tot;
inline void pushup(int p){
tri[p].siz = tri[tri[p].l].siz + tri[tri[p].r].siz + tri[p].cnt;
tri[p].h = max(tri[tri[p].l].h,tri[tri[p].r].h) + 1;
}
inline int newnode(int val){
int p = ++ tot;
tri[p].val = val;
tri[p].cnt = tri[p].siz = tri[p].h = 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;
}
int add(int p,int x,bool &flag){
if(p == 0){
flag = true;
return newnode(x);
}
if(x == tri[p].val){
tri[p].cnt ++;
tri[p].siz ++;
flag = false;
return p;
}
int oldh = tri[p].h;
if(x < tri[p].val){
tri[p].l = add(tri[p].l,x,flag);
}else{
tri[p].r = add(tri[p].r,x,flag);
}
tri[p].siz ++;
if(flag == false){
return p;
}
int hl = tri[tri[p].l].h;
int hr = tri[tri[p].r].h;
int nh = max(hl,hr) + 1;
if(hl - hr > 1){
if(tri[tri[tri[p].l].r].h > tri[tri[tri[p].l].l].h){
tri[p].l = rotate2(tri[p].l);
}
flag = false;
return rotate1(p);
}
if(hr - hl > 1){
if(tri[tri[tri[p].r].l].h > tri[tri[tri[p].r].r].h){
tri[p].r = rotate1(tri[p].r);
}
flag = false;
return rotate2(p);
}
tri[p].h = nh;
flag = (nh > oldh);
return p;
}
int del(int p,int x){
if(x == tri[p].val){
tri[p].cnt --;
tri[p].siz --;
}else if(x < tri[p].val){
tri[p].l = del(tri[p].l,x);
tri[p].siz --;
}else{
tri[p].r = del(tri[p].r,x);
tri[p].siz --;
}
return p;
}
inline int query(int p,int x){
int res = 0;
while(p != 0){
if(x < tri[p].val){
p = tri[p].l;
}else{
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 l,r,pos;
} tri[25000005];
int rt,tot,stp[128],stl[128],str[128];;
void update(int x,int y,int z){
rt = (rt == 0) ? (++ tot) : rt;
int p = rt,l = -maxn,r = maxn;
while(true){
if(z == 1){
bool flag = false;
tri[p].pos = AVL::add(tri[p].pos,y,flag);
}else{
tri[p].pos = AVL::del(tri[p].pos,y);
}
if(l == r){
break;
}
int mid = (l + r) >> 1;
if(x <= mid){
if(tri[p].l == 0){
tri[p].l = ++ tot;
}
p = tri[p].l;
r = mid;
}else{
if(tri[p].r == 0){
tri[p].r = ++ tot;
}
p = tri[p].r;
l = mid + 1;
}
}
}
int query(int ql,int qr,int x,int y){
if(rt == 0){
return 0;
}
int p = rt,top = 0,res = 0;
stp[++ top] = p;
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 mid = (l + r) >> 1;
if(ql <= mid and tri[u].l != 0){
stp[++ top] = tri[u].l;
stl[top] = l;
str[top] = mid;
}
if(qr > mid and tri[u].r != 0){
stp[++ top] = tri[u].r;
stl[top] = mid + 1;
str[top] = r;
}
}
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;
}第八步:时间优化,AVL 非递归版
AVL 也可以改为非递归版,但是有点麻烦,我们需要将沿途递归到的所有节点存储下来,然后我们倒着扫一遍,其它都差不多,但是重点注意,如果修改了这个节点,也就是如果做了旋转操作,需要将父亲节点的左儿子或者右儿子修改掉,直接将要插入的数和父亲节点的权值比大小就可以判断了,具体实现部分可以看代码,总时间 毫秒,代码:
#include<bits/stdc++.h>
#define maxn 1000000000
using 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);
if(p1 == p2){
return EOF;
}
}
return *p1 ++;
}
inline int read(){
int x = 0,f = 1;
char ch = get_char();
while(ch < '0' or ch > '9'){
if(ch == '-'){
f = -1;
}
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,h;
} tri[64000005];
int tot;
inline void pushup(int p){
tri[p].siz = tri[tri[p].l].siz + tri[tri[p].r].siz + tri[p].cnt;
tri[p].h = max(tri[tri[p].l].h,tri[tri[p].r].h) + 1;
}
inline int newnode(int val){
int p = ++ tot;
tri[p].val = val;
tri[p].cnt = tri[p].siz = tri[p].h = 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;
if(x < tri[p].val){
p = tri[p].l;
}else{
p = tri[p].r;
}
}
int np = newnode(x);
if(top == 0){
return np;
}
if(x < tri[path[top]].val){
tri[path[top]].l = np;
}else{
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 = tri[u].h;
int hl = tri[tri[u].l].h;
int hr = tri[tri[u].r].h;
int nh = max(hl,hr) + 1;
int nr = u;
if(hl - hr > 1){
if(tri[tri[tri[u].l].r].h > tri[tri[tri[u].l].l].h){
tri[u].l = rotate2(tri[u].l);
}
nr = rotate1(u);
flag = false;
}else if(hr - hl > 1){
if(tri[tri[tri[u].r].l].h > tri[tri[tri[u].r].r].h){
tri[u].r = rotate1(tri[u].r);
}
nr = rotate2(u);
flag = false;
}else{
tri[u].h = nh;
flag = (nh > oldh);
}
if(nr != u){
if(i == 1){
root = nr;
}else if(x < tri[path[i - 1]].val){
tri[path[i - 1]].l = nr;
}else{
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){
if(x < tri[p].val){
p = tri[p].l;
}else{
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 l,r,pos;
} tri[25000005];
int rt,tot;
void update(int x,int y,int z){
rt = (rt == 0) ? (++ tot) : rt;
int p = rt,l = -maxn,r = maxn;
while(true){
if(z == 1){
tri[p].pos = AVL::add(tri[p].pos,y);
}else{
tri[p].pos = AVL::del(tri[p].pos,y);
}
if(l == r){
break;
}
int mid = (l + r) >> 1;
if(x <= mid){
if(tri[p].l == 0){
tri[p].l = ++ tot;
}
p = tri[p].l;
r = mid;
}else{
if(tri[p].r == 0){
tri[p].r = ++ tot;
}
p = tri[p].r;
l = mid + 1;
}
}
}
int query(int ql,int qr,int x,int y){
if(rt == 0){
return 0;
}
int p = rt,top = 0,res = 0,stp[128],stl[128],str[128];
stp[++ top] = p;
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 mid = (l + r) >> 1;
if(ql <= mid and tri[u].l != 0){
stp[++ top] = tri[u].l;
stl[top] = l;
str[top] = mid;
}
if(qr > mid and tri[u].r != 0){
stp[++ top] = tri[u].r;
stl[top] = mid + 1;
str[top] = r;
}
}
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;
}第九步:时间优化,四叉树版
考虑将线段树变成四叉树,虽然从复杂度上看,这个是 ,二叉树是 。后者更优,但是因为平衡树的实现中,插入较慢,查询较快,这个线段树在修改时又是 的,所以综合一下,这个更快,四叉树和普通线段树其实没有什么区别,只不过需要将区间拆分成 个区间,需要注意有可能区间长度没有到 这时候需要判断,具体实现可以参考代码,总用时 毫秒,代码:
#include<bits/stdc++.h>
#define maxn 1000000000
#define uc unsigned char
using 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;
}第十步:时间优化,树状数组优化
这里外层的线段树我们考虑将所有 加上 ,这样区间就是 ,我们直接按照 维护,这样可以直接从二进制第 位扫描到第 位,每一位判断去左儿子还是右儿子也非常方便,直接将这一位取出来,判断他是奇数还是偶数即可,另外,我们可以仿照树状数组的优化方法,当且仅当 要去当前节点的左儿子时才加入当前节点的内层 AVL 树中,否则不加入,然后每次查询即查询前缀部分,然后相减即可,这里参考了第四步的路径压缩方法,比如 和 都在左儿子,那么就不用重复跑两遍,直接先跳到第一个不同的地方,然后再分别计算前缀和,最后相减即可,这里讲的是线段树二叉树,因为这里如果还用四叉树,非常难以维护,所以这里没有用。总时间 毫秒,代码:
#include<bits/stdc++.h>
#define maxn 1000000000
#define uc unsigned char
#define uint unsigned int
using 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 中的所有访问的点存入一个数组中,并且存储权值( 或 )表示加或减,而在都存入数组后,我们在内层 AVL 的 query 中将这个数组循环展开,每次往下跳,对于每个点,都有四个值,分别为 p0、p1、s0、s1,分别表示 到了哪里, 到了哪里, 往左(不含 )的和, 往左(含有 )的和。这样结束的时候 s1 - s0 就是答案,这样循环展开一下,同时处理 的点即可,时间总和 毫秒,代码:
#include<bits/stdc++.h>
#define maxn 1000000000
#define uc unsigned char
#define uint unsigned int
using 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 的努力下,最终,从第一版的 毫秒优化到了 毫秒,新数据中最慢的点 毫秒,并且在随机数据下表现优秀。
本文仅为节选,后续有可能继续进行优化,欢迎大家发明更优的解法,也欢迎进行 hack。
AI 使用说明
作者有部分优化是和 GPT 讨论后得出的,题目的数据为 GPT 生成的。