Hdu 4288 Coder(从小到大排列的集合中下标模5为3的数的和)
来源:互联网 发布:手机淘宝店铺怎么开 编辑:程序博客网 时间:2024/06/09 17:59
Coder
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
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).
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
9add 1add 2add 3add 4add 5sumadd 6del 3sum6add 1add 3add 5add 7add 9sum
Sample Output
345HintC++ maybe run faster than G++ in this problem.
Source
2012 ACM/ICPC Asia Regional Chengdu Online
题意:有三种类型的操作,(1)."add x",表示往集合里添加数x。(2).“del x”表示将集合中数x删除。(3).“sum”求出从小到大排列的集合中下标模5为3的数的和。集合中的数都是唯一的。the set S is written as {a1, a2, ... , ak} satisfying a1 < a2 < a3 < ... < ak
(1<=n<=1e5,1<=x<=1e9)
思路:因为插入的数不一定是有序的,所以我们要先对其离散化,然后每个位置维护一下余数分别为0,1,2,3,4的数的和 还有 这个区间总共出现了多少数,然后简单进行更新就可以了。
#include<bits/stdc++.h>using namespace std;#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1const int maxn=100100;typedef long long ll;ll sumv[maxn*4][5],cnt[maxn*4],num[maxn*4];int t[maxn],pos,v;struct q{ int op,num;}Q[maxn];void build(int l,int r,int rt){ for(int i=0;i<5;i++) sumv[rt][i]=0; cnt[rt]=0; if(l==r){ num[rt]=t[l]; return ; } int mid=(l+r)>>1; build(lson); build(rson);}void pushup(int rt){ cnt[rt]=cnt[rt<<1]+cnt[rt<<1|1]; cnt[rt]%=5; for(int i=0;i<5;i++) sumv[rt][i]=sumv[rt<<1][i]+sumv[rt<<1|1][(i+5-cnt[rt<<1])%5];}void update(int l,int r,int rt){ if(l==r){ sumv[rt][1]+=num[rt]*v; cnt[rt]+=v; return ; } int mid=(l+r)>>1; if(pos<=mid) update(lson); else update(rson); pushup(rt);}int main(){ int m; while(scanf("%d",&m)!=EOF){ char op[10]; int n=0; for(int i=1;i<=m;i++){ scanf("%s",op); if(op[0]=='a'){ Q[i].op=1; scanf("%d",&Q[i].num); t[++n]=Q[i].num; } else if(op[0]=='s') Q[i].op=3; else scanf("%d",&Q[i].num),Q[i].op=2; } sort(t+1,t+n+1); n=unique(t+1,t+n+1)-t-1; build(1,n,1); for(int i=1;i<=m;i++){ if(Q[i].op==1) v=1,pos=lower_bound(t+1,t+n+1,Q[i].num)-t,update(1,n,1); else if(Q[i].op==2) v=-1,pos=lower_bound(t+1,t+n+1,Q[i].num)-t,update(1,n,1); else printf("%lld\n",sumv[1][3]); } } return 0;}
0 0
- Hdu 4288 Coder(从小到大排列的集合中下标模5为3的数的和)
- .输出数组中下标为3的数。和值为3的倍数的元素
- 33.将字符串中下标位置为奇数的字符按照ascII从小到大排序输出。
- 第十三周项目1--(3)数组中元素为3倍数的和下标为3倍数的数
- HDU 4288 Coder 线段树维护区间%5的和
- 十四周-输出数组中下标和数值为三的倍数的数
- 一个数组中两个数的和为N,找出这两个数字的下标
- 第13周项目1数组中元素值为3的倍数的数和下标为3的倍数的数
- 算法---从一个数组(或者集合中)找出和为某个值的下标
- 前m大的数HDU 1280(以和为下标比大小)输出M个最大和
- <C语言>函数的值传递与地址传递--以输入三个数并从小到大排列为例
- (hdu step 3.2.5)Humble Numbers(从小到大输出因子只有2,3,,5,7的数)
- 17. 编写函数,将字符串中下标为奇数的字符按ASCII码升序排列
- 【查找】在按照绝对值排序的数组中,找出和为k的两个数的下标
- 给出集合{0,1,2,3,4,5,6,7,8,9}的全排列从小到大的第1000000个的值
- ASCII码值从小到大的排列顺序
- Java求从小到大排列的因子
- 算法.给定数为数组中2个元素的之和,求出数组的下标
- ESP8266 WIFI模块开发笔记一 NONRTOS开发
- 【机房重构】视图
- 排序算法java 二 --基数排序、归并排序
- Android测试之旅之JUnit(二)
- avi视频格式
- Hdu 4288 Coder(从小到大排列的集合中下标模5为3的数的和)
- 推荐系统的误区
- Fragment切换页面思路整理
- Linux编码问题
- 关于 MotionEvent 的笔记
- 有用的机器学习链接(持续更新)
- android自定义View之重写View来实现全新的控件
- ReactNative 学习第二节 环境搭建
- bzoj 3242: [Noi2013]快餐店 dfs&递推