hdu 4288 Coder (离线)

来源:互联网 发布:ghost系统备份软件 编辑:程序博客网 时间:2024/05/17 13:43

点击打开链接

Coder

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


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
9add 1add 2add 3add 4add 5sumadd 6del 3sum6add 1add 3add 5add 7add 9sum
 

Sample Output
345
题目大意,有一个集合,集合内部是排好序的(升序),有三种操作:1:add x,添加数 x ;(保证此操作前没有存在x而且1<=x<=1e9)2:del x,删除数 x ;(保证此操作之前存在x)3:sum,求当前集合里面,位置处于 i % 5==3 上面的那些数的和。位置分五种情况(分别是除以5余1 2 3 4 0)能不能在每个结点,保存该区间这五种位置的数的和呢?每次添加,删除一个数,就更新这五个位置的和。然后为了保证线段树的底部元素是有序的,把全部操作都读进来,把要添加的数进行一个排序,去一下重,这样就知道有多少个数要添加到线段树了,线段树的大小于是也确定了,就可以建起树来了。同时我们可以发现,我们是预先给每个要添加的数预留了一个位置的,所以添加数x表现为x在线段树中存在,删除x就表现为不存在x。其中用k来区分,add时,k=1;sum[0]=k*val,cnt+=2*k-1;             del时,k=0;sum[0]=k*val,cnt+=2*k-1;最后难点就在于怎样更新每个节点的五个位置的和。对于每一个节点,我们用一个数组sum[5]保存那5个和。那么对于底层节点,如果数val存在,sum[0]=val,否则,sum[0]=0。那么对于中间的节点,我们怎么根据左右子区间保存的和,更新自己的和?首先我们要弄清楚,节点保存的那个和,位置是以节点对应区间的左端点为起点的。所以对于左子区间,其起点跟当前节点的起点是一样的,所以就直接加到当前节点上,不用位置转移,也就是t[root<<1].sum[ i ] 直接加到 t[root].sum[ i ]。但是对于右子区间,它的起点是向右偏移了左子区间数目个数(t[root<<1].cnt)那么多的长度,所以右子区间要进行位置转移,故右区间转移后 t[root<<1|1].sum[(i+t[root<<1].num)%5];t[root].sum[i]=t[root<<1].sum[i]+t[root<<1|1].sum[(i+t[root<<1].cnt)%5];这样就更新了当前节点了。然后就可以轻松解决问题了。
#include<iostream>#include<stdio.h>#include<string>#include<math.h>#include<vector>#include<queue>#include<map>#include<string.h>#include<algorithm>#define N 100005#define mod 1005#define ll __int64//#pragma comment(linker, "/STACK:1024000000,1024000000")#define ex 2.7182818284590452354+#define pi 3.141592653589793239#define INFF 999999999using namespace std;char str[N][5];int a[N],b[N];int k;struct node{    int l;    int r;    int cnt;    ll sum[5];    int mid()    {        return (l+r)>>1;    }}t[N<<2];void push_up(int root){    int i;    for(i=0;i<5;i++)    {        int xx=t[root<<1].cnt;        t[root].sum[i]=t[root<<1].sum[i]+t[root<<1|1].sum[(i+xx)%5];    }}void build(int root,int l,int r){    int i;    t[root].l=l;    t[root].r=r;    t[root].cnt=0;    for(i=0;i<5;i++)        t[root].sum[i]=0;    if(l==r)        return;    int mid=t[root].mid();    build(root<<1,l,mid);    build(root<<1|1,mid+1,r);}void update(int root,int x,int v){    t[root].cnt+=2*k-1;    if(t[root].l==t[root].r)    {        t[root].sum[0]=(ll)k*v;        return;    }    int mid=t[root].mid();    if(x<=mid)        update(root<<1,x,v);    else        update(root<<1|1,x,v);    push_up(root);}int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        int i,tot=0;        for(i=0;i<n;i++)        {            scanf("%s",&str[i]);            if(str[i][0]!='s')                scanf("%d",&b[i]);            a[tot++]=b[i];        }        sort(a,a+tot);        tot=unique(a,a+tot)-a;        if(tot==0)            memset(t[1].sum,0,sizeof(t[1].sum));        else            build(1,1,tot);        for(i=0;i<n;i++)        {            int pos=lower_bound(a,a+tot,b[i])-a;            if(str[i][0]=='a')            {                k=1;                update(1,pos,b[i]);            }            if(str[i][0]=='d')            {                k=0;                update(1,pos,b[i]);            }            if(str[i][0]=='s')                printf("%I64d\n",t[1].sum[3]);        }    }    return 0;}



0 0
原创粉丝点击