BZOJ 3223 文艺平衡树
3223: Tyvj 1729 文艺平衡树
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5402 Solved: 3196
[Submit][Status][Discuss]
Description
您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1
Input
第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2……n-1,n) m表示翻转操作次数
接下来m行每行两个数[l,r] 数据保证 1<=l<=r<=n
Output
输出一行n个数字,表示原始序列经过m次变换后的结果
Sample Input
5 3
1 3
1 3
1 4
1 3
1 3
1 4
Sample Output
4 3 2 1 5
HINT
N,M<=100000
Source
正如sourse所说这是一道平衡树的题目,treep对于区间维护好像并不太擅长
我们用splay,splay改变序列是靠中序遍历的不同,区间反转维护lazy标记即可
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 | #include <bits/stdc++.h> #define ll long long #define inf 10000100 using namespace std; inline int read(){ int x=0; int f=1; char ch= getchar (); while (! isdigit (ch)) { if (ch== '-' ) f=-1;ch= getchar ();} while ( isdigit (ch)) {x=x*10+ch- '0' ;ch= getchar ();} return x*f; } const int MAXN=1e6+10; namespace zhangenming{ int son[MAXN][2],n,m,siz[MAXN],rev[MAXN]={},fa[MAXN],a[MAXN],tot,key[MAXN],root=0; inline void push_up( int x){ siz[x]=siz[son[x][1]]+siz[son[x][0]]+1; } inline int get( int x){ return x==son[fa[x]][1]; } inline void push_down( int x){ if (!x||!rev[x]) return ; rev[son[x][1]]^=1;rev[son[x][0]]^=1; swap(son[x][1],son[x][0]); rev[x]=0; } inline void rote( int x){ push_down(x);push_down(fa[x]); int oldl=fa[x]; int wei=get(x); int oldf=fa[fa[x]]; son[oldl][wei]=son[x][wei^1];fa[son[oldl][wei]]=oldl; fa[x]=oldf;fa[oldl]=x;son[x][wei^1]=oldl; if (oldf){ son[oldf][son[oldf][1]==oldl]=x; } push_up(x);push_up(oldl); } inline int build( int leftt, int rightt, int root){ if (leftt>rightt) return 0; int mid=(rightt+leftt)>>1; int now=++tot; rev[now]=0;fa[now]=root;key[now]=a[mid]; int lefttchild=build(leftt,mid-1,now); int righttchild=build(mid+1,rightt,now); son[now][0]=lefttchild;son[now][1]=righttchild;push_up(now); return now; } inline void splay( int xx, int tal){ for ( int f;(f=fa[xx])!=tal;rote(xx)){ if (fa[f]!=tal){ rote(get(f)==get(xx)?f:xx); } } if (tal==0) root=xx; } inline int find( int xx){ int now=root; while (1){ push_down(now); if (xx==0) return now; if (xx<=siz[son[now][0]]){ now=son[now][0]; } else { xx-=siz[son[now][0]]+1; if (xx==0) return now; now=son[now][1]; } } } void print( int root){ push_down(root); if (son[root][0]) print(son[root][0]); if (key[root]!=inf&&key[root]!=-inf) printf ( "%d " ,key[root]); if (son[root][1]) print(son[root][1]); } void init(){ n=read();m=read(); a[1]=inf;a[n+2]=-inf; for ( int i=1;i<=n;i++){ a[i+1]=i; } root=build(1,n+2,0); } inline void printt(){ for ( int i=1;i<=n+2;i++){ cout<<son[i][0]<< ' ' <<son[i][1]<<endl; } } void solve(){ while (m--){ int xx=read(); int yy=read(); if (xx>=yy) continue ; xx=find(xx);yy=find(yy+2); splay(xx,0);splay(yy,xx); rev[son[son[root][1]][0]]^=1; } print(root); } } int main(){ //freopen("All.in","r",stdin); //freopen("a.out","w",stdout); using namespace zhangenming; init(); solve(); return 0; } |
分类:
高级数据结构-Splay
, OJ-BZOJ
标签:
BZOJ
, 高级数据结构-Splay
【推荐】国内首个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