【bzoj4864】神秘物质
Description
给出一个长度为n的序列,第i个数为ai,进行以下四种操作:
merge x e:将当前第x个数和第x+1个数合并,得到一个新的数e;
insert x e:在当前第x个数和第x+1个数之间插入一个新的数e;
max x y:求当前第x个数到第y个数之间任意子区间中区间极差的最大值;
min x y:求当前第x个数到第y个数之间任意子区间中区间极差的最小值
(区间极差:区间内最大值与最小值之差)
(子区间长度至少为2)
Solution
splay模板题。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int n,m,a[200010]; 6 namespace splay{ 7 #define lson(x) t[x].lc 8 #define rson(x) t[x].rc 9 #define fa(x) t[x].fa 10 struct tree{ 11 int lc,rc,fa; 12 int size,val,maxn,minn; 13 int delta,ans; 14 }t[200010]={0}; 15 int root=0,cnt=0; 16 void pushup(int x){ 17 t[x].size=t[lson(x)].size+t[rson(x)].size+1; 18 t[x].maxn=max(t[x].val,max(t[lson(x)].maxn,t[rson(x)].maxn)); 19 t[x].minn=t[x].val; 20 if(lson(x)) 21 t[x].minn=min(t[x].minn,t[lson(x)].minn); 22 if(rson(x)) 23 t[x].minn=min(t[x].minn,t[rson(x)].minn); 24 t[x].ans=t[x].delta; 25 if(lson(x)) 26 t[x].ans=min(t[x].ans,t[lson(x)].ans); 27 if(rson(x)) 28 t[x].ans=min(t[x].ans,t[rson(x)].ans); 29 return; 30 } 31 int build(int l,int r){ 32 int mid=(l+r)>>1,s=++cnt; 33 t[s].val=a[mid]; 34 t[s].delta=(mid)?abs(a[mid]-a[mid-1]):0x3f3f3f3f; 35 if(l<mid){ 36 lson(s)=build(l,mid-1); 37 fa(lson(s))=s; 38 } 39 if(mid<r){ 40 rson(s)=build(mid+1,r); 41 fa(rson(s))=s; 42 } 43 pushup(s); 44 return s; 45 } 46 int find(int k){ 47 int p=root; 48 while(true){ 49 if(t[lson(p)].size>=k) 50 p=lson(p); 51 else if(t[lson(p)].size+1==k) 52 return p; 53 else{ 54 k-=t[lson(p)].size+1; 55 p=rson(p); 56 } 57 } 58 } 59 void rotate(int x){ 60 int fa=fa(x); 61 if(fa==root) 62 root=x; 63 if(x==lson(fa)){ 64 lson(fa)=rson(x); 65 fa(rson(x))=fa; 66 rson(x)=fa; 67 fa(x)=fa(fa); 68 if(root!=x){ 69 if(fa==lson(fa(fa))) 70 lson(fa(fa))=x; 71 else 72 rson(fa(fa))=x; 73 } 74 fa(fa)=x; 75 } 76 else{ 77 rson(fa)=lson(x); 78 fa(lson(x))=fa; 79 lson(x)=fa; 80 fa(x)=fa(fa); 81 if(root!=x){ 82 if(fa==lson(fa(fa))) 83 lson(fa(fa))=x; 84 else 85 rson(fa(fa))=x; 86 } 87 fa(fa)=x; 88 } 89 pushup(fa); 90 pushup(x); 91 return; 92 } 93 void splay(int x,int y){ 94 while(fa(x)!=y){ 95 if(fa(fa(x))==y){ 96 rotate(x); 97 return; 98 } 99 int a=(x==lson(fa(x)))?1:-1; 100 int b=(fa(x)==lson(fa(fa(x))))?1:-1; 101 if(a*b==1){ 102 rotate(fa(x)); 103 rotate(x); 104 } 105 else{ 106 rotate(x); 107 rotate(x); 108 } 109 } 110 return; 111 } 112 void insert(int x,int y){ 113 splay(find(x),0); 114 splay(find(x+1),root); 115 lson(rson(root))=++cnt; 116 fa(cnt)=rson(root); 117 t[cnt].size=1; 118 t[cnt].val=t[cnt].maxn=t[cnt].minn=y; 119 t[cnt].delta=t[cnt].ans=abs(t[root].val-y); 120 t[rson(root)].delta=abs(t[rson(root)].val-y); 121 pushup(rson(root)); 122 pushup(root); 123 return; 124 } 125 void del(int x){ 126 splay(find(x-1),0); 127 splay(find(x+1),root); 128 lson(rson(root))=0; 129 t[rson(root)].delta=abs(t[root].val-t[rson(root)].val); 130 pushup(rson(root)); 131 pushup(root); 132 } 133 int querymaxn(int l,int r){ 134 splay(find(l-1),0); 135 splay(find(r+1),root); 136 return t[lson(rson(root))].maxn-t[lson(rson(root))].minn; 137 } 138 int queryminn(int l,int r){ 139 splay(find(l-1),0); 140 splay(find(r+1),root); 141 return t[lson(rson(root))].ans; 142 } 143 #undef lson 144 #undef rson 145 #undef fa 146 } 147 int main(){ 148 scanf("%d%d",&n,&m); 149 for(int i=1;i<=n;i++) 150 scanf("%d",a+i); 151 a[0]=0x3f3f3f3f; 152 a[n+1]=-0x3f3f3f3f; 153 splay::root=splay::build(0,n+1); 154 scanf("%d",&m); 155 while(m--){ 156 char op[6];int x,y; 157 scanf("%s%d%d",op,&x,&y); 158 if(op[0]=='i') 159 splay::insert(x+1,y); 160 else if(op[1]=='e'){ 161 splay::del(x+1); 162 splay::del(x+1); 163 splay::insert(x,y); 164 } 165 else if(op[1]=='a') 166 printf("%d\n",splay::querymaxn(x+1,y+1)); 167 else 168 printf("%d\n",splay::queryminn(x+2,y+1)); 169 } 170 return 0; 171 }