HDU - 4288 Coder(5棵 线段树)

Coder

 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.

题意:有 n 个操作,每一次可向从小到大有序的集合 S 中插入或删除一个数(插入后也有序),或是询问 S 中下标(从 0 开始,题目是从 1 开始的) % 5 == 2 的位置的数的和。

思路:5棵线段树,第 i 棵线段树表示 S 中位置 % 5 = i 的数的和。。。

建树是所有数一起建,然后用 1 或 0 代表数有没有在集合里面。所以需要记录当前区间内数的个数。

对于左子树的第几位是可以直接算的,但是对于右子树是第几位是需要知道左子树有多少个数。所以得到push_up有

sum[i][rt] = sum[i][rt << 1] + sum[((i - num[rt << 1]) % 5 + 5) % 5][rt << 1 | 1];

还有一点是update中是直接使用的sum[0] , 因为只有一个数时,他的下标就是0

代码:

#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define INT(t) int t; scanf("%d",&t)

using namespace std;

const int maxn = 1e5 + 10;
struct xx{
    char op[4];
    int x;
}Q[maxn];
int cnt,en;
int Ls[maxn];
LL sum[5][maxn << 2];
int coun[maxn << 2];

int getId(int x){
    return lower_bound(Ls + 1,Ls + 1 + en,x) - Ls;  
}

void update(int l,int r,int pos,int i,int vals,int valc){
    if(l == r){
        sum[0][i] = vals;
        coun[i] += valc;
        return ;
    }
    int mid = (l + r) >> 1;
    if(pos <= mid) update(l,mid,pos,ls,vals,valc);
    if(pos > mid) update(mid + 1,r,pos,rs,vals,valc);
    for(int j = 0;j < 5;++ j)
        sum[j][i] = sum[j][ls] + sum[(((j - coun[ls]) % 5 + 5) % 5)][rs];
    coun[i] = coun[ls] + coun[rs];
}

int main() {
    int n;
    while(~scanf("%d",&n)){
        cnt = 0;
        for(int i = 1;i <= n;++ i){
            scanf("%s",Q[i].op);
            if(Q[i].op[0] != 's'){
                scanf("%d",&Q[i].x);
                Ls[++ cnt] = Q[i].x;
            }
        }
        sort(Ls + 1,Ls + 1 + cnt);
        en = unique(Ls + 1,Ls + 1 + cnt) - Ls - 1;
        clr(sum,0); clr(coun,0);
        for(int i = 1;i <= n;++ i){
            if(Q[i].op[0] != 's'){
                if(Q[i].op[0] == 'a')
                    update(1,en,getId(Q[i].x),1,Q[i].x,1);
                else
                    update(1,en,getId(Q[i].x),1,0,-1);
            }
            else printf("%lld\n",sum[2][1]);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值