【Splay】bzoj3223 Tyvj 1729 文艺平衡树
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 | #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<stack> using namespace std; #define maxn 100010 #define INF 2147483647 int fa[maxn],val[maxn],c[maxn][2],root,tot,siz[maxn],cnt[maxn],val2[maxn]; bool delta[maxn]; void Maintain( int x) { siz[x]=siz[c[x][0]]+siz[c[x][1]]+cnt[x]; } void Mark( int x){ if (x){ delta[x]^=1; } } void pushdown( int x){ if (delta[x]){ Mark(c[x][0]); Mark(c[x][1]); swap(c[x][0],c[x][1]); delta[x]=0; } } void NewNode( int &x, int Fa, int key, int key2) { x=++tot; fa[x]=Fa; c[x][0]=c[x][1]=0; val[x]=key; val2[x]=key2; siz[x]=cnt[x]=1; } void Rotate( int x, bool flag) { int y=fa[x]; c[y][!flag]=c[x][flag]; fa[c[x][flag]]=y; if (fa[y]){ c[fa[y]][c[fa[y]][1]==y]=x; } fa[x]=fa[y]; c[x][flag]=y; fa[y]=x; Maintain(y); Maintain(x); } stack< int >st; void Splay( int x, int goal) { pushdown(x); if (!x || x==goal){ return ; } int U=x; while (U!=root){ st.push(U); U=fa[U]; } st.push(U); while (!st.empty()){ pushdown(st.top()); st.pop(); } int y; while ((y=fa[x])!=goal){ if (fa[y]==goal){ Rotate(x,c[y][0]==x); } else { if ((c[y][0]==x)==(c[fa[y]][0]==y)){ Rotate(y,c[fa[y]][0]==y); } else { Rotate(x,c[y][0]==x); y=fa[x]; } Rotate(x,c[y][0]==x); } } Maintain(x); if (!goal){ root=x; } } int Find( int key, int x=root) { while (c[x][val[x]<key]){ if (val[x]==key){ return x; } x=c[x][val[x]<key]; } return x; } void Insert( int key, int key2) { if (!root){ NewNode(root,0,key,key2); return ; } int x=Find(key); if (val[x]==key){ ++cnt[x]; Splay(x,0); return ; } NewNode(c[x][val[x]<key],x,key,key2); Splay(c[x][val[x]<key],0); } int Kth( int K, int x=root) { while (1){ pushdown(x); int Siz0=siz[c[x][0]]; if (K<=Siz0){ x=c[x][0]; } else if (K<=Siz0+cnt[x]){ break ; } else { K-=(Siz0+cnt[x]); x=c[x][1]; } } return x; } void print( int x) { if (!x){ return ; } pushdown(x); print(c[x][0]); printf ( "%d " ,val2[x]); print(c[x][1]); Maintain(x); } int n,m; int main(){ scanf ( "%d%d" ,&n,&m); Insert(0,0); for ( int i=1;i<=n;++i){ Insert(i,i); } Insert(n+1,n+1); int x,y; for ( int i=1;i<=m;i++){ scanf ( "%d%d" ,&x,&y); Splay(Kth(x),0); Splay(Kth(y+2),root); Mark(c[c[root][1]][0]); } Splay(Kth(1),0); Splay(Kth(n+2),root); print(c[c[root][1]][0]); return 0; } |
——The Solution By AutSky_JadeK From UESTC
转载请注明出处:http://www.cnblogs.com/autsky-jadek/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步