Hdu 4288 Coder(从小到大排列的集合中下标模5为3的数的和)
2016-05-05 20:36
369 查看
Coder
Problem DescriptionIn 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.
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|1
const 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;
}
相关文章推荐
- 关于 MotionEvent 的笔记
- avi视频格式
- 拦截导弹
- 推荐系统的误区
- Linux编码问题
- Fragment切换页面思路整理
- HDU 1232 畅通工程
- android自定义View之重写View来实现全新的控件
- Java线程之CompletionService批处理任务
- “java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Timestamp”
- javaScript学习笔记——数组末尾元素的增加和删除
- ESP8266 WIFI模块开发笔记一 NONRTOS开发
- tableviewcell自适应高度当中遇到添加label作为contentView,在实际显示时右侧出现空白
- HTML5-postMessage实现跨域
- 数字比较的问题 short integer和数字比较
- 一次死锁追踪经历
- Myeclipse的使用
- 二维数组的查找
- 格式与布局
- 有n个人围成一圈,顺序排号。 从第1个人开始报数(从1到3报数), 凡报到3的人推出圈子, 问最后留下的是原来第几号的人。