[BZOJ3223]Tyvj 1729 文艺平衡树
[BZOJ3223]Tyvj 1729 文艺平衡树
试题描述
您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1
输入
第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2……n-1,n) m表示翻转操作次数
接下来m行每行两个数[l,r] 数据保证 1<=l<=r<=n
输出
输出一行n个数字,表示原始序列经过m次变换后的结果
输入示例
5 3 1 3 1 3 1 4
输出示例
4 3 2 1 5
数据规模及约定
N,M<=100000
题解
裸 splay,注意最后要多输出一个空格。
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 | #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <algorithm> using namespace std; int read() { int x = 0, f = 1; char c = getchar(); while (!isdigit(c)){ if (c == '-' ) f = -1; c = getchar(); } while (isdigit(c)){ x = x * 10 + c - '0' ; c = getchar(); } return x * f; } #define maxn 100010 struct Node { int v, siz; bool rev; Node() {} Node( int _): v(_), rev(0) {} } ns[maxn]; int rt, ToT, fa[maxn], ch[2][maxn]; void maintain( int o) { ns[o].siz = 1; for ( int i = 0; i < 2; i++) if (ch[i][o]) ns[o].siz += ns[ch[i][o]].siz; return ; } void build( int & o, int l, int r) { if (l > r) return ; int mid = l + r >> 1; ns[o = ++ToT] = Node(mid); if (l == r) return maintain(o); build(ch[0][o], l, mid - 1); build(ch[1][o], mid + 1, r); if (ch[0][o]) fa[ch[0][o]] = o; if (ch[1][o]) fa[ch[1][o]] = o; return maintain(o); } void pushdown( int o) { for ( int i = 0; i < 2; i++) if (ch[i][o]) ns[ch[i][o]].rev ^= ns[o].rev; if (ns[o].rev) swap(ch[0][o], ch[1][o]), ns[o].rev = 0; return ; } void rotate( int u) { int y = fa[u], z = fa[y], l = 0, r = 1; if (z) ch[ch[1][z]==y][z] = u; if (ch[1][y] == u) swap(l, r); fa[u] = z; fa[y] = u; fa[ch[r][u]] = y; ch[l][y] = ch[r][u]; ch[r][u] = y; maintain(y); maintain(u); return ; } int S[maxn], top; void splay( int u) { int t = u; S[++top] = t; while (fa[t]) t = fa[t], S[++top] = t; while (top) pushdown(S[top--]); while (fa[u]) { int y = fa[u], z = fa[y]; if (z) { if (ch[0][y] == u ^ ch[0][z] == y) rotate(u); else rotate(y); } rotate(u); } return ; } int split( int u) { if (!u) return 0; splay(u); int tmp = ch[1][u]; fa[tmp] = 0; ch[1][u] = 0; maintain(u); return tmp; } int merge( int a, int b) { if (!a) return maintain(b), b; if (!b) return maintain(a), a; pushdown(a); while (ch[1][a]) a = ch[1][a], pushdown(a); splay(a); ch[1][a] = b; fa[b] = a; return maintain(a), a; } int qkth( int o, int k) { if (!o) return 0; pushdown(o); int ls = ch[0][o] ? ns[ch[0][o]].siz : 0; if (k == ls + 1) return o; if (k > ls + 1) return qkth(ch[1][o], k - ls - 1); return qkth(ch[0][o], k); } int Find( int k) { rt = 1; while (fa[rt]) rt = fa[rt]; return qkth(rt, k); } void Rev( int ql, int qr) { int lrt = Find(ql - 1), mrt = Find(qr), rrt; split(lrt); rrt = split(mrt); ns[mrt].rev ^= 1; mrt = merge(lrt, mrt); merge(mrt, rrt); return ; } int main() { int n = read(), q = read(); build(rt, 1, n); while (q--) { int l = read(), r = read(); Rev(l, r); } for ( int i = 1; i <= n; i++) { int k = Find(i); printf( "%d " , ns[k].v); } return 0; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App