【HDOJ】1890 Robotic Sort
伸展树伤不起啊,很容易wa,很容易T,很容易M。
1 /* 1890 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #include <vector> 9 #include <deque> 10 #include <algorithm> 11 #include <cstdio> 12 #include <cmath> 13 #include <ctime> 14 #include <cstring> 15 #include <climits> 16 #include <cctype> 17 #include <cassert> 18 #include <functional> 19 #include <iterator> 20 #include <iomanip> 21 using namespace std; 22 //#pragma comment(linker,"/STACK:102400000,1024000") 23 24 #define sti set<int> 25 #define stpii set<pair<int, int> > 26 #define mpii map<int,int> 27 #define vi vector<int> 28 #define pii pair<int,int> 29 #define vpii vector<pair<int,int> > 30 #define rep(i, a, n) for (int i=a;i<n;++i) 31 #define per(i, a, n) for (int i=n-1;i>=a;--i) 32 #define clr clear 33 #define pb push_back 34 #define mp make_pair 35 #define fir first 36 #define sec second 37 #define all(x) (x).begin(),(x).end() 38 #define SZ(x) ((int)(x).size()) 39 #define lson l, mid, rt<<1 40 #define rson mid+1, r, rt<<1|1 41 #define grandlson ch[ch[root][1]][0] 42 43 typedef struct node_t { 44 int id, val; 45 node_t() {} 46 friend bool operator<(const node_t& a, const node_t& b) { 47 if (a.val != b.val) 48 return a.val < b.val; 49 return a.id < b.id; 50 } 51 } node_t; 52 53 const int maxn = 1e5+10; 54 int pre[maxn], ch[maxn][2], size[maxn], rev[maxn]; 55 node_t nd[maxn]; 56 int root, tot, n; 57 int ans[maxn]; 58 59 void newNode(int& r, int k, int fa) { 60 r = k; 61 pre[r] = fa; 62 ch[r][0] = ch[r][1] = 0; 63 size[r] = 1; 64 rev[r] = 0; 65 } 66 67 void PushUp(int r) { 68 size[r] = size[ch[r][0]] + size[ch[r][1]] + 1; 69 } 70 71 void UpdateRev(int r) { 72 if (r == 0) return ; 73 74 swap(ch[r][0], ch[r][1]); 75 rev[r] ^= 1; 76 } 77 78 void PushDown(int r) { 79 if (rev[r]) { 80 UpdateRev(ch[r][0]); 81 UpdateRev(ch[r][1]); 82 rev[r] = 0; 83 } 84 } 85 86 void Build(int& rt, int l, int r, int fa) { 87 if (l > r) return ; 88 89 int mid = (l + r) >> 1; 90 newNode(rt, mid, fa); 91 Build(ch[rt][0], l, mid-1, rt); 92 Build(ch[rt][1], mid+1, r, rt); 93 PushUp(rt); 94 } 95 96 void Rotate(int x, int d) { 97 int y = pre[x]; 98 PushDown(y); 99 PushDown(x); 100 ch[y][d^1] = ch[x][d]; 101 pre[ch[x][d]] = y; 102 if (pre[y]) 103 ch[pre[y]][ch[pre[y]][1]==y] = x; 104 pre[x] = pre[y]; 105 ch[x][d] = y; 106 pre[y] = x; 107 PushUp(y); 108 } 109 110 void Splay(int r, int goal) { 111 112 PushDown(r); 113 while (pre[r] != goal) { 114 if (pre[pre[r]] == goal) { 115 PushDown(pre[r]); 116 PushDown(r); 117 Rotate(r, ch[pre[r]][0]==r); 118 } else { 119 PushDown(pre[pre[r]]); 120 PushDown(pre[r]); 121 PushDown(r); 122 int y = pre[r]; 123 int d = ch[pre[y]][0]==y; 124 if (ch[y][d] == r) { 125 Rotate(r, d^1); 126 Rotate(r, d); 127 } else { 128 Rotate(y, d); 129 Rotate(r, d); 130 } 131 } 132 } 133 134 PushUp(r); 135 if (goal == 0) 136 root = r; 137 } 138 139 int kth(int r, int k) { 140 int sz = size[ch[r][0]] + 1; 141 142 PushDown(r); 143 if (k == sz) 144 return r; 145 if (k < sz) 146 return kth(ch[r][0], k); 147 else 148 return kth(ch[r][1], k-sz); 149 } 150 151 int getNext(int rt) { 152 PushDown(rt); 153 if (ch[rt][1] == 0) return -1; 154 rt = ch[rt][1]; 155 while (ch[rt][0]) { 156 rt = ch[rt][0]; 157 PushDown(rt); 158 } 159 return rt; 160 } 161 162 void init() { 163 ch[root][0] = ch[root][1] = size[root] = rev[root] = 0; 164 newNode(root, n+1, 0); 165 newNode(ch[root][1], n+2, root); 166 Build(grandlson, 1, n, ch[root][1]); 167 PushUp(ch[root][1]); 168 PushUp(root); 169 } 170 171 void inorder(int rt) { 172 if (rt == 0) return ; 173 174 inorder(ch[rt][0]); 175 printf("rt = %d, lson = %d, rson = %d, fa = %d, sz = %d, rev = %d\n", 176 rt, ch[rt][0], ch[rt][1], pre[rt], size[rt], rev[rt]); 177 inorder(ch[rt][1]); 178 } 179 180 int main() { 181 ios::sync_with_stdio(false); 182 #ifndef ONLINE_JUDGE 183 freopen("data.in", "r", stdin); 184 freopen("data.out", "w", stdout); 185 #endif 186 187 int r; 188 189 while (scanf("%d", &n)!=EOF && n) { 190 rep(i, 1, n+1) { 191 scanf("%d", &nd[i].val); 192 nd[i].id = i; 193 } 194 init(); 195 sort(nd+1, nd+1+n); 196 rep(i, 1, n+1) { 197 r = nd[i].id; 198 Splay(r, 0); 199 ans[i] = size[ch[root][0]]; 200 Splay(kth(root, i), 0); 201 Splay(getNext(r), root); 202 UpdateRev(grandlson); 203 } 204 rep(i, 1, n+1) { 205 printf("%d", ans[i]); 206 if (i == n) 207 putchar('\n'); 208 else 209 putchar(' '); 210 } 211 } 212 213 #ifndef ONLINE_JUDGE 214 printf("time = %d.\n", (int)clock()); 215 #endif 216 217 return 0; 218 }