标签:oid while rate 位置 n+1 wap title lists 一段
1 #include<stdio.h>
2 #include<algorithm>
3 #include<string.h>
4 using namespace std;
5 #define N 500006
6 #define lc (tr[id].c[0])
7 #define rc (tr[id].c[1])
8 #define key (tr[tr[root].c[1]].c[0])
9 struct tree
10 {
11 int fa,sum,c[2],lz,v;
12 }tr[N];
13 struct point
14 {
15 int v,id;
16 bool operator<(const point a)const
17 {
18 if(a.v==v)return id<a.id;
19 else return v<a.v;
20 }
21 }so[N/5];
22 int tot,root,n;
23 int xia[N];
24 int newpoint(int d,int f)
25 {
26 tr[tot].sum=1;
27 tr[tot].v=d;
28 tr[tot].c[0]=tr[tot].c[1]=-1;
29 tr[tot].lz=0;
30 tr[tot].fa=f;
31 return tot++;
32 }
33 void push(int id)
34 {
35 int lsum,rsum;
36 if(lc==-1)lsum=0;
37 else lsum=tr[lc].sum;
38 if(rc==-1)rsum=0;
39 else rsum=tr[rc].sum;
40 tr[id].sum=lsum+rsum+1;
41 }
42 int build(int l,int r,int v)
43 {
44 if(r<l)return -1;
45 int mid=(r+l)>>1;
46 int ro=newpoint(mid,v);
47 xia[mid]=ro;
48 tr[ro].c[0]=build(l,mid-1,ro);
49 tr[ro].c[1]=build(mid+1,r,ro);
50 push(ro);
51 return ro;
52 }
53 void lazy(int id)
54 {
55 if(tr[id].lz)
56 {
57 swap(lc,rc);
58 tr[lc].lz^=1;
59 tr[rc].lz^=1;
60 tr[id].lz=0;
61 }
62 }
63
64 void xuanzhuan(int x,int k)
65 {
66 if(tr[x].fa==-1)return ;
67 int fa=tr[x].fa;
68 int w;
69 lazy(fa);
70 lazy(x);
71 tr[fa].c[!k]=tr[x].c[k];
72 if(tr[x].c[k]!=-1)tr[tr[x].c[k]].fa=fa;
73 tr[x].fa=tr[fa].fa;
74 tr[x].c[k]=fa;
75 if(tr[fa].fa!=-1)
76 {
77 w=tr[tr[fa].fa].c[1]==fa;
78 tr[tr[fa].fa].c[w]=x;
79 }
80 tr[fa].fa=x;
81 push(fa);
82 push(x);
83 }
84
85 void splay(int x,int goal)
86 {
87 if(x==-1)return ;
88 lazy(x);
89 while(tr[x].fa!=goal)
90 {
91 int y=tr[x].fa;
92 lazy(tr[y].fa);
93 lazy(y);
94 lazy(x);
95 bool w=(x==tr[y].c[1]);
96 if(tr[y].fa!=goal&&w==(y==tr[tr[y].fa].c[1]))xuanzhuan(y,!w);
97 xuanzhuan(x,!w);
98 }
99 if(goal==-1)root=x;
100 push(x);
101 }
102 int next(int id)
103 {
104 lazy(id);
105 int p=tr[id].c[1];
106 if(p==-1)return id;
107 lazy(p);
108 while(tr[p].c[0]!=-1)
109 {
110 p=tr[p].c[0];
111 lazy(p);
112 }
113 return p;
114 }
115 int main()
116 {
117 while(scanf("%d",&n),n)
118 {
119 for(int i=1;i<=n;i++)
120 {
121 scanf("%d",&so[i].v);
122 so[i].id=i;
123 }
124 sort(so+1,so+n+1);
125 so[0].id=0;
126 tot=0;
127 int d,l;
128 root=build(0,n+1,-1);
129 for(int i=1;i<=n;i++)
130 {
131 int ro=xia[so[i].id];
132 int ne;
133 splay(ro,-1);
134 d=tr[tr[root].c[0]].sum;
135 l=xia[so[i-1].id];
136 ne=next(ro);
137 splay(l,-1);
138 splay(ne,root);
139 lazy(root);
140 lazy(tr[root].c[1]);
141 tr[key].lz^=1;
142 if(i!=1)printf(" ");
143 printf("%d",d);
144 }
145 printf("\n");
146 }
147 return 0;
148 }
题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1890
标签:oid while rate 位置 n+1 wap title lists 一段
原文地址:http://www.cnblogs.com/xxjnoi/p/7172220.html