【HDOJ】5096 ACM Rank

Treap+set仿函数重定义。每当ac一道题目时,相当于对总时间减去一个大数。

  1 /* 5096 */
  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 
 42 const int maxn = 10005;
 43 const int BigInt = 1e7;
 44 int S[maxn], top, tot, root;
 45 int lst[maxn], lstac[maxn];
 46 bool mark[maxn][15];
 47 int pen[maxn][15];
 48 int T[maxn];
 49 char cmd[10], line[1005];
 50     
 51 typedef struct fcmp {
 52     bool operator() (const int& a, const int& b) {
 53         if (lstac[a]==-1 && lstac[b]==-1)
 54             return a<b;
 55         if (lstac[a]!=-1 && lstac[b]!=-1)
 56             return lstac[a]<lstac[b];
 57         else if (lst[a] == -1)
 58             return false;
 59         else
 60             return true;
 61     }
 62 } fcmp;
 63 
 64 typedef struct Node {
 65     int ch[2];
 66     int r, s, v, c;
 67     set<int, fcmp> st;
 68 
 69     Node() {}
 70 
 71     void setId(int v_, int id) {
 72         v = v_;
 73         st.clr();
 74         st.insert(id);
 75         ch[0] = ch[1] = 0;
 76         r = rand();
 77         s = c = 1;
 78     }
 79 
 80     int cmp(int x) const {
 81         if (x == v)    return -1;
 82         return x<v ? 0:1;
 83     }
 84 
 85     void insert(int id) {
 86         st.insert(id);
 87     }
 88 
 89     void erase(int id) {
 90         st.erase(id);
 91     }
 92 } Node;
 93 
 94 Node nd[maxn];
 95 
 96 void newNode(int& r, int id, int v) {
 97     if (top) {
 98         r = S[--top];
 99     } else {
100         r = ++tot;
101     }
102     nd[r].setId(v, id);
103 }
104 
105 void maintain(int r) {
106     nd[r].s = nd[nd[r].ch[0]].s + nd[nd[r].ch[1]].s + nd[r].c;
107 }
108 
109 void Rotate(int& r, int d) {
110     int k = nd[r].ch[d^1];
111     nd[r].ch[d^1] = nd[k].ch[d];
112     nd[k].ch[d] = r;
113     maintain(r);
114     maintain(k);
115     r = k;
116 }
117 
118 void Insert(int& r, int id, int v) {
119     if (r == 0) {
120         newNode(r, id, v);
121         return ;
122     } else {
123         int d = nd[r].cmp(v);
124         if (d == -1) {
125             ++nd[r].c;
126             ++nd[r].s;
127             nd[r].insert(id);
128             return ;
129         }
130         Insert(nd[r].ch[d], id, v);
131         if (nd[nd[r].ch[d]].r > nd[r].r)
132             Rotate(r, d^1);
133     }
134     maintain(r);
135 }
136 
137 void Remove(int& r, int id, int v) {
138     int d = nd[r].cmp(v);
139 
140     if (d == -1) {
141         if (nd[r].c > 1) {
142             --nd[r].c;
143             --nd[r].s;
144             nd[r].erase(id);
145             return ;
146         }
147 
148         if (nd[r].ch[0] && nd[r].ch[1]) {
149             int d2 = nd[nd[r].ch[0]].r > nd[nd[r].ch[1]].r ? 1:0;
150             Rotate(r, d2);
151             Remove(nd[r].ch[d2], id, v);
152         } else {
153             S[top++] = r;
154             r = nd[r].ch[1] + nd[r].ch[0];
155         }
156     } else {
157         Remove(nd[r].ch[d], id, v);
158     }
159 
160     if (r)
161         maintain(r);
162 }
163 
164 void init() {
165     nd[0].ch[0] = nd[0].ch[1] = nd[0].s = nd[0].v = nd[0].c = 0;
166     memset(pen, 0, sizeof(pen));
167     memset(mark, false, sizeof(mark));
168     memset(lst, -1, sizeof(lst));
169     memset(lstac, -1, sizeof(lstac));
170     memset(T, 0, sizeof(T));
171     top = tot = root = 0;
172 }
173 
174 int Rank(int r, int v) {
175     int d = nd[r].cmp(v);
176     int sz = nd[nd[r].ch[0]].s;
177 
178     if (d == -1)
179         return sz+1;
180     else if (d == 0)
181         return Rank(nd[r].ch[0], v);
182     else
183         return sz+nd[r].c+Rank(nd[r].ch[1], v);
184 }
185 
186 int kth(int r, int k) {
187     if (r==0 || k<=0 || k>nd[r].s)    return -1;
188     int sz = nd[nd[r].ch[0]].s;
189 
190     if (k <= sz) {
191         return kth(nd[r].ch[0], k);
192     } else if (k <= sz+nd[r].c) {
193         if (k == sz+1) {
194             return *nd[r].st.begin();
195         } else {
196             return -1;
197         }
198     } else {
199         return kth(nd[r].ch[1], k-sz-nd[r].c);
200     }
201 
202 }
203 
204 int main() {
205     ios::sync_with_stdio(false);
206     #ifndef ONLINE_JUDGE
207         freopen("data.in", "r", stdin);
208         freopen("data.out", "w", stdout);
209     #endif
210 
211     int n, m;
212     int ino;
213     int t, k, tno, res, pid;
214     int ans;
215 
216     while (scanf("%d %d", &n, &m)!=EOF) {
217         init();
218         ino = 0;
219         rep(i, 0, n) {
220             Insert(root, i, 0);
221         }
222         while (1) {
223             scanf("%s %s", cmd, line);
224             ++ino;
225             if (cmd[0] == 'C')
226                 break;
227             if (cmd[0] == 'T') {
228                 int len = strlen(line);
229                 int i = 0;
230 
231                 k = 0;
232                 while (i < len) {
233                     k = 10 * k + line[i]-'0';
234                     ++i;
235                 }
236                 ans = kth(root, k);
237                 printf("%d
", ans);
238             } else if (cmd[0] == 'R') {
239                 int len = strlen(line);
240                 int i = 0;
241 
242                 tno = 0;
243                 while (i < len) {
244                     tno = 10 * tno + line[i]-'0';
245                     ++i;
246                 }
247                 t = T[tno];
248                 ans = Rank(root, t);
249                 printf("%d
", ans);
250             } else if (cmd[0] == 'S') {
251                 int len = strlen(line);
252                 int i = 0;
253 
254                 t = 0;
255                 while (i<len && line[i]!=':') {
256                     t = 10 * t + line[i]-'0';
257                     ++i;
258                 }
259 
260                 tno = 0;
261                 ++i;
262                 while (i<len && line[i]!=':') {
263                     tno = 10 * tno + line[i]-'0';
264                     ++i;
265                 }
266 
267                 ++i;
268                 pid = line[i++] - 'A';
269 
270                 res = 0;
271                 ++i;
272                 while (i < len) {
273                     res = 10 * res + line[i]-'0';
274                     ++i;
275                 }
276 
277                 if (mark[tno][pid])
278                     continue;
279                 if (lst[tno]>=0 && t-lst[tno]<5)
280                     continue;
281                 if (res != 1) {
282                     pen[tno][pid] += 20;
283                     lst[tno] = t;
284                     continue;
285                 }
286                 printf("[%d][%c]
", tno, pid+'A');
287                 Remove(root, tno, T[tno]);
288                 T[tno] += pen[tno][pid];
289                 T[tno] += t;
290                 mark[tno][pid] = true;
291                 T[tno] -= BigInt;
292                 lst[tno] = t;
293                 lstac[tno] = ino;
294                 Insert(root, tno, T[tno]);
295             }
296         }
297         putchar('
');
298     }
299 
300     #ifndef ONLINE_JUDGE
301         printf("time = %d.
", (int)clock());
302     #endif
303 
304     return 0;
305 }
原文地址:https://www.cnblogs.com/bombe1013/p/4909379.html