hdu 4288 Coder

本文介绍了一种使用Treap数据结构解决特定数据操作问题的方法,包括添加元素、删除元素和计算数据集的特定函数。文章详细阐述了算法实现过程,包括数据结构设计、操作函数和输入输出格式,提供了具体的样例输入和输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目连接

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

 

转载于:https://www.cnblogs.com/GadyPu/p/4566545.html

资源下载链接为: https://pan.quark.cn/s/9e7ef05254f8 行列式是线性代数的核心概念,在求解线性方程组、分析矩阵特性以及几何计算中都极为关键。本教程将讲解如何用C++实现行列式的计算,重点在于如何输出分数形式的结果。 行列式定义如下:对于n阶方阵A=(a_ij),其行列式由主对角线元素的乘积,按行或列的奇偶性赋予正负号后求和得到,记作det(A)。例如,2×2矩阵的行列式为det(A)=a11×a22-a12×a21,而更高阶矩阵的行列式可通过Laplace展开或Sarrus规则递归计算。 在C++中实现行列式计算时,首先需定义矩阵类或结构体,用二维数组存储矩阵元素,并实现初始化、加法、乘法、转置等操作。为支持分数形式输出,需引入分数类,包含分子和分母两个整数,并提供与整数、浮点数的转换以及加、减、乘、除等运算。C++中可借助std::pair表示分数,或自定义结构体并重载运算符。 计算行列式的函数实现上,3×3及以下矩阵可直接按定义计算,更大矩阵可采用Laplace展开或高斯 - 约旦消元法。Laplace展开是沿某行或列展开,将矩阵分解为多个小矩阵的行列式乘积,再递归计算。在处理分数输出时,需注意避免无限循环和除零错误,如在分数运算前先约简,确保分子分母互质,且所有计算基于整数进行,最后再转为浮点数,以避免浮点数误差。 为提升代码可读性和可维护性,建议采用面向对象编程,将矩阵类和分数类封装,每个类有明确功能和接口,便于后续扩展如矩阵求逆、计算特征值等功能。 总结C++实现行列式计算的关键步骤:一是定义矩阵类和分数类;二是实现矩阵基本操作;三是设计行列式计算函数;四是用分数类处理精确计算;五是编写测试用例验证程序正确性。通过这些步骤,可构建一个高效准确的行列式计算程序,支持分数形式计算,为C++编程和线性代数应用奠定基础。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值