题目要求实现的操作都是线段树的,区间增加,旋转...自己重新打别人的代码的时候都快裂开了,这么多的函数,啊这,平板电视到底怎么用啊(((φ(◎ロ◎;)φ)))...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | #include<bits/stdc++.h> using namespace std; typedef long long LL; const double pi=4*atan(1.0); const int INF=0x3f3f3f3f; const double eps=1e-6; const int MAXN=100010; const int MAXM=2*MAXN; int a[MAXN],n; struct SplayTree { #define Key_value (nt[nt[root][1]][0]) int nt[MAXN][2],pre[MAXN],val[MAXN],Size[MAXN],add[MAXN],rev[MAXN],minv[MAXN]; int root,tot1; int s[MAXN],tot2; ///内存池 内存池容量 有删除操作要这样写 void NewNode( int &r, int fa, int k) { if (tot2)r=s[tot2--]; else r=++tot1; nt[r][0]=nt[r][1]=0; pre[r]=fa; Size[r]=1; add[r]=rev[r]=0; val[r]=minv[r]=k; } void Update_Rev( int r) ///添加反转标记 { if (!r) return ; swap(nt[r][0],nt[r][1]); rev[r]^=1; } void Update_Add( int r, int ADD) ///添加增量标记 { if (!r) return ; add[r]+=ADD; val[r]+=ADD; minv[r]+=ADD; } void PushUp( int r) { Size[r]=Size[nt[r][0]]+Size[nt[r][1]]+1; minv[r]=val[r]; if (nt[r][0])minv[r]=min(minv[r],minv[nt[r][0]]); if (nt[r][1])minv[r]=min(minv[r],minv[nt[r][1]]); } void PushDown( int r) { if (rev[r]) { Update_Rev(nt[r][0]); Update_Rev(nt[r][1]); rev[r]=0; } if (add[r]) { Update_Add(nt[r][0],add[r]); Update_Add(nt[r][1],add[r]); add[r]=0; } } void Build( int &x, int l, int r, int fa) ///对序列建平衡二叉树 { if (l>r) return ; int mid=(l+r)>>1; NewNode(x,fa,a[mid]); Build(nt[x][0],l,mid-1,x); Build(nt[x][1],mid+1,r,x); PushUp(x); } void Init() { root=tot1=tot2=0; nt[root][0]=nt[root][1]=Size[root]=add[root]=rev[root]=pre[root]=0; minv[root]=INF; ///这个不用也可以,如果在PushUp那判断了的话,否则需要 NewNode(root,0,INF); NewNode(nt[root][1],root,INF); Build(Key_value,1,n,nt[root][1]); PushUp(nt[root][1]); PushUp(root); } void Rotate( int x, int kind) ///旋转 { int y=pre[x]; PushDown(y); PushDown(x); nt[y][!kind]=nt[x][kind]; pre[nt[x][kind]]=y; if (pre[y]) nt[pre[y]][nt[pre[y]][1]==y]=x; pre[x]=pre[y]; nt[x][kind]=y; pre[y]=x; PushUp(y); } void Splay( int r, int goal) ///Splay调整 { PushDown(r); while (pre[r]!=goal) { if (pre[pre[r]]==goal) Rotate(r,nt[pre[r]][0]==r); else { int y=pre[r]; int kind=(nt[pre[y]][0]==y); if (nt[y][kind]==r) { Rotate(r,!kind); Rotate(r,kind); } else { Rotate(y,kind); Rotate(r,kind); } } } PushUp(r); if (goal==0)root=r; } int Get_Kth( int r, int k) ///返回以r为根的子树中第k个结点编号 { PushDown(r); int t=Size[nt[r][0]]+1; if (t==k) return r; if (t>k) return Get_Kth(nt[r][0],k); else return Get_Kth(nt[r][1],k-t); } int Get_Min( int r) ///返回以r为根的子树的最左边的端点编号 { PushDown(r); while (nt[r][0]) { r=nt[r][0]; PushDown(r); } return r; } int Get_Max( int r) ///返回以r为根的子树的最右边的端点编号 { PushDown(r); while (nt[r][1]) { r=nt[r][1]; PushDown(r); } return r; } ///下面是本题用到的操作 void ADD( int l, int r, int v) ///a[l]~a[r]+=v { Splay(Get_Kth(root,l),0); Splay(Get_Kth(root,r+2),root); Update_Add(Key_value,v); PushUp(nt[root][1]); PushUp(root); } void Reverse( int l, int r) ///将区间反转 { Splay(Get_Kth(root,l),0); Splay(Get_Kth(root,r+2),root); Update_Rev(Key_value); PushUp(nt[root][1]); PushUp(root); } void Revolve( int l, int r, int T) ///a[l]~a[r]循环右移T位 { int len=r-l+1; T%=len; if (T==0) return ; int c=r-T; ///将区间[l,c]与[c+1,r]调换位置 处理方法为将[c+1,r]这棵子树插入到l-1与l两结点中间 Splay(Get_Kth(root,c+1),0); Splay(Get_Kth(root,r+2),root); ///将[c+1,r]对应的子树旋转到关键位置 int temp=Key_value; ///保存这棵子树的根节点编号 Key_value=0; ///删除这棵子树 PushUp(nt[root][1]); PushUp(root); Splay(Get_Kth(root,l),0); Splay(Get_Kth(root,l+1),root); Key_value=temp; pre[Key_value]=nt[root][1]; ///添加子树时这句不能忘记 PushUp(nt[root][1]); PushUp(root); } void Insert( int x, int P) ///在第x个数后面插入P { Splay(Get_Kth(root,x+1),0); Splay(Get_Kth(root,x+2),root); NewNode(Key_value,nt[root][1],P); PushUp(nt[root][1]); PushUp(root); } void Erase( int r) ///回收内存 { if (r) { s[++tot2]=r; Erase(nt[r][0]); Erase(nt[r][1]); } } void Delete( int x) ///删除第x个数 { Splay(Get_Kth(root,x),0); Splay(Get_Kth(root,x+2),root); Erase(Key_value); pre[Key_value]=0; Key_value=0; PushUp(nt[root][1]); PushUp(root); } int Query_Min( int l, int r) ///查询区间最小值 { Splay(Get_Kth(root,l),0); Splay(Get_Kth(root,r+2),root); return minv[Key_value]; } /**以下为Debug部分 用于输出整个序列*/ void middle_order( int r) ///Splay的中序遍历即为维护的序列 { if (r) { PushDown(r); ///这句话不能少 middle_order(nt[r][0]); printf( "%d " ,val[r]); middle_order(nt[r][1]); } } void Test() { Splay(Get_Min(root),0); Splay(Get_Max(root),root); middle_order(Key_value); ///两个外加端点的中间即为维护序列 printf( "\n" ); } }st; int main() { char op[20]; int x,y,z,q; while (scanf( "%d" ,&n)!=EOF) { for ( int i=1;i<=n;i++) scanf( "%d" ,&a[i]); st.Init(); //st.Test(); scanf( "%d" ,&q); while (q--) { scanf( "%s" ,op); if (strcmp(op, "ADD" )==0) { scanf( "%d%d%d" ,&x,&y,&z); st.ADD(x,y,z); //st.Test(); } else if (strcmp(op, "REVERSE" )==0) { scanf( "%d%d" ,&x,&y); st.Reverse(x,y); //st.Test(); } else if (strcmp(op, "REVOLVE" )==0) { scanf( "%d%d%d" ,&x,&y,&z); st.Revolve(x,y,z); //st.Test(); } else if (strcmp(op, "INSERT" )==0) { scanf( "%d%d" ,&x,&y); st.Insert(x,y); //st.Test(); } else if (strcmp(op, "DELETE" )==0) { scanf( "%d" ,&x); st.Delete(x); //st.Test(); } else { scanf( "%d%d" ,&x,&y); printf( "%d\n" ,st.Query_Min(x,y)); } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!