HDU4288 Coder(离线+线段树)

Coder

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


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 {a 1, a 2, ... , a k} satisfying a 1 < a 2 < a 3 < ... < a k
  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 <= 10 5 ), 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 <= 10 9.
  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
Hint
C++ maybe run faster than G++ in this problem.
 

Source
题意:求集合内下标mod5=3的值的和。
思路:先离线把要用到的数离散化,去重并且排序,这样做添加或删除操作的时候就可以用二分找出下表并在线段树内修改,由于是单点更新,就不用”向下传递“了。
其实操作和单点更新一样,只是要用一个数组sum[5]记录对应区间内mod5的5种情况的和。所以当添加或删除一个数时都是在sum[1]这里增删,可以想想区间[2,2]是不是只有一个数,那么这个数的下标是1,mod5=1,所以在sum[1]处处理,更新父节点的时候要注意,父亲的左区间的下标与左儿子的下标mod5的情况完全相同,而父亲的右区间和右儿子的下标在左儿子的个数前提上mod5,所以cnt代表区间内有效数的个数。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define maxn 100010
#define lson rt<<1
#define rson rt<<1|1
#define ll long long int
using namespace std;
struct node{
    int left, right, cnt;
    ll sum[5];
}tree[maxn<<2];
int a[maxn], b[maxn];
char q[maxn], c[5];
void pushUp(int rt){
    for(int i = 0;i < 5;i++)
        tree[rt].sum[i] = tree[lson].sum[i] + tree[rson].sum[((i-tree[lson].cnt)%5+5)%5];
}
void bulid(int rt, int l, int r){
    tree[rt].left = l;
    tree[rt].right = r;
    tree[rt].cnt = 0;
    memset(tree[rt].sum, 0, sizeof tree[rt].sum);
    if(l==r) return;
    int mid = (l+r)>>1;
    bulid(lson, l, mid);
    bulid(rson, mid+1, r);
}
void update(int rt, int pos, int add){
    tree[rt].cnt += add;
    if(tree[rt].left==tree[rt].right){
        tree[rt].sum[1] += add*b[pos-1];
        return;
    }
    int mid = (tree[rt].left+tree[rt].right)>>1;
    if(pos<=mid) update(lson, pos, add);
    else update(rson, pos, add);
    pushUp(rt);
}
int main()
{
    int N, i, j, k;
    while(~scanf("%d", &N)){
        k = 0;
        for(i = 0;i < N;i++){
            scanf("%s", c);
            q[i] = c[0];
            if(q[i] != 's')
                scanf("%d", &a[k++]);
        }
        memcpy(b, a, sizeof(int)*k);
        sort(b, b+k);
        int n = unique(b, b+k)-b;
        //printf("k:%d\n", k);
        bulid(1, 1, n);
        //printf("--\n");
        for(i = j = 0;i< N;i++){
            if(q[i] == 's'){
                printf("%I64d\n", tree[1].sum[3]);
                continue;
            }
            int ps = lower_bound(b, b+n, a[j++]) - b + 1;
            //printf("q:%c ps:%d\n", q[i], ps);
            if(q[i] == 'a') update(1, ps, 1);
            else  update(1, ps, -1);
        }
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值