hdu 4288 Coder


Coder

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2799    Accepted Submission(s): 1117


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
需要5棵线段树,存对应的余数。
假设我们知道五棵线段树在(t<<1)和(t<<1|1)的值,怎么push_up呢?
如果余数是i,那么(t<<1)处的i肯定都属于t的i,至于(t<<1|1)的i,因为要加上(t<<1)的个数和,所以会发生变化,假设从j变到i,那么
(j+num[t<<1])%5 == i ,所以j=((i-num[t<<1]%5)+5)%5;
/*
 *=====================
 *File Name:a.cpp
 *Author: qqspeed
 *Date: 2014年 07月 17日 星期四 10:26:03 CST
 *=====================
 */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

typedef long long ll;
#define rep(i,s,t) for(int i=s;i<t;i++)
#define red(i,s,t) for(int i=s-1;i>=t;i--)
#define ree(i,now) for(int i=head[now];i!=-1;i=edge[i].next)
#define clr(a,v) memset(a,v,sizeof a)
#define L t<<1
#define R t<<1|1
#define MID int mid=(l+r)>>1
#define max(a,b) (a<b?b:a)
#define min(a,b) (a<b?a:b)
#define SQR(a) ((a)*(a))

inline int input(){
	int ret=0;bool isN=0;char c=getchar();
	while(c<'0' || c>'9'){
		if(c=='-') isN=1;
		c=getchar();
	}
	while(c>='0' && c<='9'){
		ret=ret*10+c-'0';
		c=getchar();
	}
	return isN?-ret:ret;
}

inline void output(ll x){    
    if(x<0){    
        putchar('-');x=-x;    
    }    
    int len=0,data[20];    
    while(x){    
        data[len++]=x%10;x/=10;    
    }    
    if(!len)    data[len++]=0;    
    while(len--)   
        putchar(data[len]+48);    
    putchar('\n');  
}  

const int MAXN=100005;
char str[5];
int n;
int x[MAXN],op[MAXN];
int to[MAXN],cnt;
int num[MAXN<<2];
long long sum[5][MAXN<<2];

inline void push_up(int t){
	rep(i,0,5){
		sum[i][t]=sum[i][L]+sum[(i-num[L]%5+5)%5][R];
	}
	num[t]=num[L]+num[R];
}

inline void add(int t,int l,int r,int x,int v,int a){
	if(l==r){
		num[t]+=v;
		sum[1][t]+=v*a;
	}
	else{
		MID;
		if(x<=mid) add(L,l,mid,x,v,a);
		else add(R,mid+1,r,x,v,a);
		push_up(t);
	}
}

int main(){
	while(~scanf("%d",&n)){
		clr(num,0),clr(sum,0);
		cnt=0;
		rep(i,0,n){
			scanf("%s",str);
			if(str[0]=='a') op[i]=1;
			else if(str[0]=='d') op[i]=-1;
			else op[i]=2;
			if(op[i]!=2){
				x[i]=input();
				to[++cnt]=x[i];
			}
		}
		sort(to+1,to+cnt+1);
		cnt=unique(to+1,to+cnt+1)-(to+1);
		rep(i,0,n){
			if(op[i]==2){
				output(sum[3][1]);
			}
			else{
				int v=lower_bound(to+1,to+cnt+1,x[i])-(to+1)+1;
				add(1,1,cnt,v,op[i],x[i]);
			}
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值