QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#671086 | #21403. 文艺平衡树 | jager59 | TL | 0ms | 0kb | C++14 | 2.0kb | 2024-10-24 10:45:20 | 2024-10-24 10:45:20 |
answer
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
return x*f;
}
int n,m,rt;
struct tree{
int fa,siz,v,son[2],tag;
}t[N];
inline void up(int p){
t[p].siz=t[t[p].son[0]].siz+1+t[t[p].son[1]].siz;
}
inline int build(int fa,int l,int r){
if(l>r)return 0;
int mid=l+r>>1;
t[mid+1].fa=fa;t[mid+1].v=mid;
t[mid+1].son[0]=build(mid+1,l,mid-1),t[mid+1].son[1]=build(mid+1,mid+1,r);
up(mid+1);
return mid+1;
}
inline void cg(int p){
swap(t[p].son[0],t[p].son[1]),t[p].tag^=1;
}
inline void spread(int p){
if(t[p].tag){
if(t[p].son[0])cg(t[p].son[0]);
if(t[p].son[1])cg(t[p].son[1]);
t[p].tag=0;
}
}
inline void rotate(int x){
int y=t[x].fa,z=t[y].fa,k=(t[y].son[1]==x);
spread(z);spread(y);spread(x);
t[x].fa=z,t[z].son[y==t[z].son[1]]=x;
t[y].son[k]=t[x].son[k^1],t[t[x].son[k^1]].fa=y;
t[x].son[k^1]=y,t[y].fa=x;
up(y);up(x);
}
inline void splay(int x,int goal){
while(t[x].fa!=goal){
int y = t[x].fa,z = t[y].fa;
if(z!=goal)(x==t[y].son[1])^(y==t[z].son[1])?rotate(x):rotate(y);
rotate(x);
}
if(!goal)rt=x;
}
inline int kth(int k){
int p = rt;
while(1){
// cerr<<p<<' '<<k<<endl;
if(k<=t[t[p].son[0]].siz)p=t[p].son[0];
else{
k-=t[t[p].son[0]].siz+1;
if(!k)return p;
p=t[p].son[1];
}
}
return 1e9;
}
inline void dfs(int p){
if(!p)return;
spread(p);
dfs(t[p].son[0]);
if(1<=t[p].v&&t[p].v<=n)printf("%d ",t[p].v);
dfs(t[p].son[1]);
}
int main(){
n=read(),m=read();
rt=build(0,0,n+1);
for(int i = 1,l,r;i<=m;i++){
l=read(),r=read();
l=kth(l),r=kth(r+2);
splay(l,0),splay(r,l);
cg(t[r].son[0]);
}
dfs(rt);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
30 5 7 26 7 18 5 9 4 15 3 15