hdoj 4288 && Codeforces 85D Coder 【线段树】
2016-01-12 17:49
239 查看
Coder
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4792 Accepted Submission(s): 1833
Problem 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
where the set S is written as {a1, a2, ... , ak} satisfying a1 < a2 < a3 < ... < ak
Can you complete this task (and be then fired)?
------------------------------------------------------------------------------
1 See http://uncyclopedia.wikia.com/wiki/Algorithm
Input
There’re several test cases.
In each test case, the first line contains one integer N ( 1 <= N <= 105 ), 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 <= x <= 109.
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
HintC++ maybe run faster than G++ in this problem.
题意:有N个操作,add x 表示向集合里面加入x,delete x 表示将x从集合中删去,sum 求和所有位置i % 5 == 3的数。
建议去Codeforces上提交。。。
思路:设置cnt记录区间元素的个数,sum[i]表示区间所有位置pos % 5 == i的数之和(编号从区间左端点开始)。
设置lson区间有cnt1个数,rson区间有cnt2个数,在rson区间上第i个数 = father上第cnt1 + i个数。 那么有[father].sum[i] = [lson].sum[i] + [rson].sum[(i-[lson].cnt)%5]。
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <string> #define INF 0x3f3f3f3f #define eps 1e-8 #define MAXN (100000+10) #define MAXM (200000+10) #define Ri(a) scanf("%d", &a) #define Rl(a) scanf("%lld", &a) #define Rf(a) scanf("%lf", &a) #define Rs(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define Pf(a) printf("%.2lf\n", (a)) #define Pl(a) printf("%lld\n", (a)) #define Ps(a) printf("%s\n", (a)) #define W(a) while(a--) #define CLR(a, b) memset(a, (b), sizeof(a)) #define MOD 1000000007 #define LL long long #define lson o<<1, l, mid #define rson o<<1|1, mid+1, r #define ll o<<1 #define rr o<<1|1 #define PI acos(-1.0) #define first fi #define second se using namespace std; struct Tree{ int l, r, len; int cnt; LL sum[5]; }; Tree tree[MAXN<<2]; struct Node{ int op, val; }; Node num[MAXN]; int rec[MAXN]; void Build(int o, int l, int r) { tree[o].l = l; tree[o].r = r; tree[o].len = r - l + 1; tree[o].cnt = 0; for(int i = 0; i < 5; i++) tree[o].sum[i] = 0; if(l == r) return ; int mid = (l + r) >> 1; Build(lson); Build(rson); } void PushUp(int o){ for(int i = 0; i < 5; i++) tree[o].sum[i] = tree[ll].sum[i] + tree[rr].sum[((i-tree[ll].cnt)%5+5)%5]; tree[o].cnt = tree[ll].cnt + tree[rr].cnt; } void Update(int o, int pos, int c, LL v) { if(tree[o].l == tree[o].r) { tree[o].cnt += c; tree[o].sum[1] += v; return ; } int mid = (tree[o].l + tree[o].r) >> 1; if(pos <= mid) Update(ll, pos, c, v); else Update(rr, pos, c, v); PushUp(o); } int main() { int N; while(Ri(N) != EOF) { int k = 0; for(int i = 0; i < N; i++) { char str[10]; Rs(str); if(str[0] == 's') {num[i].op = 0; continue;} if(str[0] == 'a') num[i].op = 1; else if(str[0] == 'd') num[i].op = -1; Ri(num[i].val); rec[k++] = num[i].val; } sort(rec, rec+k); int R = unique(rec, rec+k) - rec; if(R == 0) R = 1; Build(1, 0, R-1); for(int i = 0; i < N; i++) { if(num[i].op == 0) Pl(tree[1].sum[3]); else { int op = num[i].op; int pos = lower_bound(rec, rec+R, num[i].val) - rec; Update(1, pos, op, num[i].val * op); } } } return 0; }
相关文章推荐
- 如何查看安卓手机应用程序创建的数据库
- [练习]在一个数组中存300条记录,用3个线程统计求和。
- Android图片加载库Picasso源码分析
- Android应用性能测试
- 搭建vagrant环境
- 用vim打开c程序会出现乱码
- ajax 省市联动 代码
- oracle 锁表简单解锁
- Codeforces 612E Square Root of Permutation 【置换群】
- android之壁纸机制
- Linux 系统应用编程——网络编程(socket编程)
- 正则表达式符号
- git配置远程访问
- Echarts小练习
- inotify + rsync 打造文件实时同步
- Certificate、Provisioning Profile、App ID的介绍及其之间的关系
- Codeforces 612D The Union of k-Segments 【思维】
- 微信连wifi portal验证
- Codeforces 612C Replace To Make Regular Bracket Sequence 【stack】
- 为nginx 绑定CPU