码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 4288 Coder

时间:2015-06-10 19:01:16      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=4288

Coder

Description

In mathematics and computer science, an algorithm describes a set of procedures or instructions that define a procedure. The term has become increasing popular since the advent of cheap and reliable computers. Many companies now employ a single coder to write an algorithm that will replace many other employees. An added benefit to the employer is that the coder will also become redundant once their work is done. 1
  You are now the signle coder, and have been assigned a new task writing code, since your boss would like to replace many other employees (and you when you become redundant once your task is complete).
Your code should be able to complete a task to replace these employees who do nothing all day but eating: make the digest sum.
  By saying “digest sum” we study some properties of data. For the sake of simplicity, our data is a set of integers. Your code should give response to following operations:
  1. add x – add the element x to the set;
  2. del x – remove the element x from the set;
  3. sum – find the digest sum of the set. The digest sum should be understood by

\begin{align*}\large{\sum_{1 \leq i \leq k}{a_i}}\ \ \ \ \ \ \ {where  \ \ i\ \  mod \ \ 5\ = \ 3}\end{align*}

where the set S is written as $\large{a_1, a_2, ... , a_k}$ satisfying $\large{a_1 < a_2 < a_3 < ... < a_k}$

Input


  There’re several test cases.
  In each test case, the first line contains one integer $N ( 1 \leq N \leq 10^5 )$, the number of operations to process.
  Then following is n lines, each one containing one of three operations: “add x” or “del x” or “sum”.
  You may assume that $1 \leq x \leq 10^9.$
  Please see the sample for detailed format.
  For any “add x” it is guaranteed that x is not currently in the set just before this operation.
  For any “del x” it is guaranteed that x must currently be in the set just before this operation.
  Please process until EOF (End Of File).

Output

For each operation “sum” please print one line containing exactly one integer denoting the digest sum of the current set. Print 0 if the set is empty.

Sample Input

9
add 1
add 2
add 3
add 4
add 5
sum
add 6
del 3
sum
6
add 1
add 3
add 5
add 7
add 9
sum

Sample Output

3

4

5

技术分享
  1 #include<algorithm>
  2 #include<iostream>
  3 #include<cstdlib>
  4 #include<cstring>
  5 #include<cstdio>
  6 #include<vector>
  7 #include<map>
  8 using std::cin;
  9 using std::cout;
 10 using std::endl;
 11 using std::find;
 12 using std::map;
 13 using std::pair;
 14 using std::vector;
 15 #define all(c) (c).begin(), (c).end()
 16 #define iter(c) decltype((c).begin())
 17 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
 18 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
 19 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
 20 #define pb(e) push_back(e)
 21 #define mp(a, b) make_pair(a, b)
 22 const int Max_N = 110000;
 23 typedef int key_t;
 24 typedef unsigned long long ull;
 25 struct Node {
 26     key_t key;
 27     Node *ch[2];
 28     double fix;
 29     int size;
 30     ull sum[5];
 31     bool vis;
 32     void set(const key_t &k, double pri, Node *p) {
 33         key = k, fix = pri, size = 1;
 34         vis = false;
 35         ch[0] = ch[1] = p;
 36         rep(i, 5) sum[i] = 0;
 37     }
 38     inline int cmp(const key_t v) const {
 39         return v == key ? -1 : v > key;
 40     }
 41 };
 42 struct Treap {
 43     int top;
 44     Node *tail, *root, *null;
 45     Node stack[Max_N], *pool[Max_N];
 46     inline double get_fix() {
 47         return rand() / (double)RAND_MAX;
 48     }
 49     inline void init() {
 50         top = 0;
 51         tail = &stack[0];
 52         null = tail++;
 53         null->set(0, 1.0, NULL);
 54         null->size = 0;
 55         root = null;
 56     }
 57     inline Node *newNode(const key_t &k, double pri) {
 58         Node *p = null;
 59         if (!top) p = tail++;
 60         else p = pool[--top];
 61         p->set(k, pri, null);
 62         return p;
 63     }
 64     inline void rotate(Node *&x, int d) {
 65         Node *k = x->ch[!d];
 66         x->ch[!d] = k->ch[d];
 67         k->ch[d] = x;
 68         x->vis = k->vis = false;
 69         x = k;
 70     }
 71     inline void insert(Node *&x, const key_t &k, double p) {
 72         if (x == null) { x = newNode(k, p); return; }
 73         int d = x->cmp(k);
 74         if (-1 == d) { x->vis = false; return; }
 75         insert(x->ch[d], k, p);
 76         if (x->fix > x->ch[d]->fix) rotate(x, !d);
 77         x->vis = false;
 78         return;
 79     }
 80     inline void erase(Node *&x, const key_t &k) {
 81         if (!x->size) return;
 82         int d = x->cmp(k);
 83         if (-1 == d) {
 84             if (!x->ch[0]->size || !x->ch[1]->size) {
 85                 pool[top++] = x;
 86                 x = x->ch[0]->size ? x->ch[0] : x->ch[1];
 87             } else {
 88                 int c = x->ch[0]->fix < x->ch[1]->fix;
 89                 rotate(x, !c);
 90                 erase(x->ch[!c], k);
 91             }
 92         } else {
 93             erase(x->ch[d], k);
 94         }
 95         if (x->size) x->vis = false;
 96         return;
 97     }
 98     inline void insert(const key_t &k) {
 99         insert(root, k, get_fix());
100     }
101     inline void erase(const key_t &k) {
102         erase(root, k);
103     }
104     inline int count(Node *x) {
105         return !x->size ? 0 : x->size;
106     }
107     inline void dfs(Node *x) {
108         if (!x->size) return;
109         if (!x->vis) {
110             Node *lch = x->ch[0], *rch = x->ch[1];
111             dfs(lch); dfs(rch);
112             int lcnt = count(lch);
113             int rcnt = count(rch);
114             x->size = lcnt + 1 + rcnt;
115             rep(i, 5) x->sum[i] = 0;
116             if (lch->size) rep(i, 5) x->sum[i] = lch->sum[i];
117             x->sum[lcnt % 5] += x->key;
118             if (rch->size) rep(i, 5) x->sum[(lcnt + 1 + i) % 5] += rch->sum[i];
119             x->vis = true;
120         }
121     }
122     inline void go() {
123         int x;
124         char buf[256];
125         scanf("%s", buf);
126         if (buf[0] == a) {
127             scanf("%d", &x);
128             insert(x);
129         } else if (buf[0] == d) {
130             scanf("%d", &x);
131             erase(root, x);
132         } else {
133             dfs(root);
134             if (!root->size) puts("0");
135             else printf("%lld\n", root->sum[2]);
136         }
137     }
138 }trp;
139 int main() {
140 #ifdef LOCAL
141     freopen("in.txt", "r", stdin);
142     freopen("out.txt", "w+", stdout);
143 #endif
144     int Q;
145     while (~scanf("%d", &Q)) {
146         trp.init();
147         rep(q, Q) trp.go();
148     }
149     return 0;
150 }
View Code

 

hdu 4288 Coder

标签:

原文地址:http://www.cnblogs.com/GadyPu/p/4566545.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!